HK1247677B - Virtual machine cloning method and device under kvm virtualization - Google Patents
Virtual machine cloning method and device under kvm virtualization Download PDFInfo
- Publication number
- HK1247677B HK1247677B HK18106882.8A HK18106882A HK1247677B HK 1247677 B HK1247677 B HK 1247677B HK 18106882 A HK18106882 A HK 18106882A HK 1247677 B HK1247677 B HK 1247677B
- Authority
- HK
- Hong Kong
- Prior art keywords
- virtual machine
- memory
- data
- executable program
- module
- Prior art date
Links
Description
技术领域Technical Field
本申请属云计算领域,具体地说,涉及一种KVM虚拟化下虚拟机克隆的方法及装置。The present application relates to the field of cloud computing, and more specifically, to a method and apparatus for cloning virtual machines under KVM virtualization.
背景技术Background Art
虚拟机是指运行在Windows或Linux计算机上的一个应用程序,这个应用程序“模拟”了一个基于x86的标准PC的环境。这个环境和普通的计算机一样,都有芯片组、CPU、内存、显卡、声卡、网卡、软驱、硬盘、光驱、串口、并口、USB控制器、SCSI控制器等设备,提供这个应用程序的“窗口”就是虚拟机的显示器。在一台电脑上将硬盘和内存的一部分拿出来虚拟出若干台机器,每台机器可以运行单独的操作系统而互不干扰,这些“新”机器各自拥有自己独立的CMOS、硬盘和操作系统,用户可以像使用普通机器一样对它们进行分区、格式化、安装系统和应用软件等操作,还可以将这几个操作系统联成一个网络。在虚拟系统崩溃之后可直接删除不影响本机系统,同样本机系统崩溃后也不影响虚拟系统,可以下次重装后再加入以前做的虚拟系统。同时它也是唯一的能在Windows和Linux主机平台上运行的虚拟计算机软件。虚拟机软件不需要重开机,就能在同一台电脑使用好几个操作系统,不但方便,而且安全。A virtual machine is an application running on a Windows or Linux computer that "simulates" the environment of a standard x86-based PC. This environment, like a regular computer, includes a chipset, CPU, memory, graphics card, sound card, network card, floppy drive, hard drive, optical drive, serial and parallel ports, USB controllers, and SCSI controllers. The "window" that displays this application is the virtual machine's display. A portion of the hard drive and memory is allocated to create multiple virtual machines within a single computer, each capable of running a separate operating system without interfering with each other. These "new" machines each have their own independent CMOS, hard drive, and operating system. Users can partition, format, install operating systems, and other software on them just like regular machines, and even connect these operating systems to form a network. A virtual system crash can be directly deleted without affecting the local system, and similarly, a local system crash will not affect the virtual system; the previously created virtual system can be reinstalled and added later. It is also the only virtualization software that runs on both Windows and Linux host platforms. Virtual machine software allows you to use multiple operating systems on the same computer without having to reboot, making it both convenient and secure.
在公有云环境下,有时需要虚拟机快速完成创建和部署,如进行虚拟机级别的流量均衡,而且通常这些虚拟机运行完全一样的系统和应用,运行时相当一部分内存是完全一样的虚拟机之间可以共享以节省内存使用。虚拟机快速克隆技术可以对一个虚拟机进行秒级克隆,并且虚拟机之间以COW方式共享内存现有技术中,VMWare公司提供的虚拟机克隆技术只能在VMWare的虚拟环境下使用,然而在KVM(Kernel-based Virtual Machine的简称,即基于内核的虚拟机)虚拟化环境下,还未有可行的快速虚拟机克隆技术方案,因此,一种KVM虚拟化下虚拟机快速克隆的方法亟待提出。In public cloud environments, virtual machines (VMs) sometimes need to be created and deployed quickly, such as for VM-level traffic balancing. These VMs often run identical systems and applications, and a significant portion of their memory is shared between them to conserve memory usage. Rapid VM cloning technology can clone a VM in seconds, and VMs share memory in a Co-Work (COW) fashion. Existing VM cloning technology provided by VMWare only works within VMWare's virtual environments. However, in the KVM (Kernel-based Virtual Machine) virtualization environment, there is no viable fast VM cloning solution. Therefore, a method for fast VM cloning in KVM virtualization is urgently needed.
发明内容Summary of the Invention
有鉴于此,本申请所要解决的技术问题是提供了一种KVM虚拟化下虚拟机克隆的方法及装置。In view of this, the technical problem to be solved by the present application is to provide a method and device for cloning virtual machines under KVM virtualization.
为了解决上述技术问题,本申请开了一种KVM虚拟化下虚拟机克隆的方法,包括:In order to solve the above technical problems, the present application provides a method for cloning a virtual machine under KVM virtualization, including:
暂停第一虚拟机并保存所述第一虚拟机的数据至指定内存中;其中,所述数据在所述指定内存中不被释放;Pausing a first virtual machine and saving data of the first virtual machine in a designated memory; wherein the data in the designated memory is not released;
从所述指定内存中获取虚拟机可执行程序的启动参数;Obtaining startup parameters of the virtual machine executable program from the designated memory;
启动所述虚拟机可执行程序实现所述第二虚拟机的克隆。The virtual machine executable program is started to clone the second virtual machine.
其中,启动所述虚拟机可执行程序进行所述第二虚拟机的克隆,包括:The step of starting the virtual machine executable program to clone the second virtual machine includes:
创建子进程,子进程调用虚拟机可执行程序;Create a child process, which calls the virtual machine executable program;
启动所述虚拟机可执行程序并进行初始化,并在所述初始化完成后得到所述第二虚拟机。The virtual machine executable program is started and initialized, and the second virtual machine is obtained after the initialization is completed.
其中,所述创建子进程包括:所述第一虚拟机对应的父进程将所述第一虚拟机的内存地址以及所述第一虚拟机的显存地址继承给所述子进程以使所述第二虚拟机与所述第一虚拟机共享内存与显存。The creating of the child process includes: the parent process corresponding to the first virtual machine inheriting the memory address and the video memory address of the first virtual machine to the child process so that the second virtual machine and the first virtual machine share memory and video memory.
其中,所述第一虚拟机的数据至少包括:所述第一虚拟机的虚拟机状态信息、以及所述第一虚拟机的内存地址、显存地址;The data of the first virtual machine includes at least: virtual machine state information of the first virtual machine, and memory address and video memory address of the first virtual machine;
其中,所述从所述指定内存中获取虚拟机可执行程序的启动参数,包括:The step of obtaining startup parameters of the virtual machine executable program from the designated memory includes:
从所述指定内存中获取所述第一虚拟机的状态信息,并更新所述第一虚拟机的状态信息得到所述启动参数。The state information of the first virtual machine is obtained from the designated memory, and the state information of the first virtual machine is updated to obtain the startup parameters.
进一步地,所述方法还包括:获取所述指定内存的地址;将所述指定内存的地址作为新增参数传入所述虚拟机可执行程序的启动参数。Furthermore, the method further includes: obtaining the address of the designated memory; and passing the address of the designated memory as a new parameter into the startup parameters of the virtual machine executable program.
进一步地,所述方法还包括:所述子进程和所述第一虚拟机对应的父进程以写时复制方式共享页表。Furthermore, the method further includes: the child process and a parent process corresponding to the first virtual machine share a page table in a copy-on-write manner.
进一步地,所述方法还包括:将所述第一虚拟机暂停时未完成的IO输出至存储介质。Furthermore, the method further includes: outputting the unfinished IO when the first virtual machine is suspended to a storage medium.
进一步地,所述方法还包括:从所述指定内存中获取所述第一虚拟机的数据,恢复所述第一虚拟机。Furthermore, the method further includes: acquiring data of the first virtual machine from the designated memory, and restoring the first virtual machine.
本申请还公开一种KVM虚拟化下虚拟机克隆的方法,包括:The present application also discloses a method for cloning a virtual machine under KVM virtualization, comprising:
暂停第一虚拟机并保存所述第一虚拟机的数据至指定内存中;其中,所述数据在所述指定内存中不被释放;Pausing a first virtual machine and saving data of the first virtual machine in a designated memory; wherein the data in the designated memory is not released;
启动虚拟机可执行程序实现所述第二虚拟机的克隆;Starting a virtual machine executable program to clone the second virtual machine;
从所述指定内存中将所述数据恢复至所述第一虚拟机。The data is restored from the designated memory to the first virtual machine.
其中,启动所述虚拟机可执行程序进行所述第二虚拟机的克隆,包括:The step of starting the virtual machine executable program to clone the second virtual machine includes:
创建子进程,子进程调用虚拟机可执行程序;启动所述虚拟机可执行程序并进行初始化,并在所述初始化完成后得到所述第二虚拟机。A child process is created, and the child process calls a virtual machine executable program; the virtual machine executable program is started and initialized, and the second virtual machine is obtained after the initialization is completed.
其中,所述创建子进程包括:所述第一虚拟机对应的父进程将所述第一虚拟机的内存地址以及所述第一虚拟机的显存地址继承给所述子进程以使所述第二虚拟机与所述第一虚拟机共享内存与显存。The creating of the child process includes: the parent process corresponding to the first virtual machine inheriting the memory address and the video memory address of the first virtual machine to the child process so that the second virtual machine and the first virtual machine share memory and video memory.
其中,所述第一虚拟机的数据至少包括:所述第一虚拟机的虚拟机状态信息、以及所述第一虚拟机的内存地址、显存地址;The data of the first virtual machine includes at least: virtual machine state information of the first virtual machine, and memory address and video memory address of the first virtual machine;
进一步地,所述方法还包括:所述子进程和所述第一虚拟机对应的父进程以写时复制方式共享页表。Furthermore, the method further includes: the child process and a parent process corresponding to the first virtual machine share a page table in a copy-on-write manner.
进一步地,所述方法还包括:将所述第一虚拟机暂停时未完成的IO输出至存储介质。Furthermore, the method further includes: outputting the unfinished IO when the first virtual machine is suspended to a storage medium.
本申请还公开一种KVM虚拟化下虚拟机克隆的装置,包括:The present application also discloses a device for cloning a virtual machine under KVM virtualization, comprising:
第一预处理模块,用于暂停第一虚拟机并保存所述第一虚拟机的数据至指定内存中;其中,所述数据在所述指定内存中不被释放;A first pre-processing module is configured to pause the first virtual machine and save data of the first virtual machine to a designated memory; wherein the data is not released in the designated memory;
第一参数获取模块,用于从所述指定内存中获取虚拟机可执行程序的启动参数;A first parameter acquisition module, configured to acquire startup parameters of the virtual machine executable program from the designated memory;
第一克隆模块,用于启动所述虚拟机可执行程序实现所述第二虚拟机的克隆。The first cloning module is used to start the virtual machine executable program to clone the second virtual machine.
其中,所述第一克隆模块用于:创建子进程,子进程调用虚拟机可执行程序;The first cloning module is used to: create a child process, and the child process calls the virtual machine executable program;
启动所述虚拟机可执行程序并进行初始化,并在所述初始化完成后得到所述第二虚拟机。The virtual machine executable program is started and initialized, and the second virtual machine is obtained after the initialization is completed.
其中,所述第一克隆模块用于:所述第一虚拟机对应的父进程将所述第一虚拟机的内存地址以及所述第一虚拟机的显存地址继承给所述子进程以使所述第二虚拟机与所述第一虚拟机共享内存与显存。The first cloning module is used to: the parent process corresponding to the first virtual machine inherits the memory address and the video memory address of the first virtual machine to the child process so that the second virtual machine and the first virtual machine share memory and video memory.
其中,所述第一虚拟机的数据至少包括:所述第一虚拟机的虚拟机状态信息、以及所述第一虚拟机的内存地址、显存地址;The data of the first virtual machine includes at least: virtual machine state information of the first virtual machine, and memory address and video memory address of the first virtual machine;
其中,所述第一参数获取模块用于:从所述指定内存中获取所述第一虚拟机的状态信息,并更新所述第一虚拟机的状态信息得到所述启动参数。The first parameter acquisition module is configured to acquire the state information of the first virtual machine from the designated memory, and update the state information of the first virtual machine to obtain the startup parameters.
其中,所述第一参数获取模块还用于:获取所述指定内存的地址;将所述指定内存的地址作为新增参数传入所述虚拟机可执行程序的启动参数。The first parameter acquisition module is further configured to: acquire the address of the designated memory; and pass the address of the designated memory as a new parameter into the startup parameters of the virtual machine executable program.
进一步地,所述装置还包括第一共享模块,所述第一共享模块用于:所述子进程和所述第一虚拟机对应的父进程以写时复制方式共享页表。Furthermore, the device further includes a first sharing module, and the first sharing module is configured to enable the child process and a parent process corresponding to the first virtual machine to share a page table in a copy-on-write manner.
进一步地,所述装置还包括第一IO输出模块,所述第一IO输出模块用于:将所述第一虚拟机暂停时未完成的IO输出至存储介质。Furthermore, the apparatus further includes a first IO output module, and the first IO output module is configured to output unfinished IO operations when the first virtual machine is suspended to a storage medium.
进一步地,所述装置还包括第一数据恢复模块,所述第一数据恢复模块用于:从所述指定内存中获取所述第一虚拟机的数据,恢复所述第一虚拟机。Furthermore, the device further includes a first data recovery module, and the first data recovery module is used to: obtain the data of the first virtual machine from the designated memory and restore the first virtual machine.
本申请还公开一种KVM虚拟化下虚拟机克隆的装置,包括:The present application also discloses a device for cloning a virtual machine under KVM virtualization, comprising:
第二预处理模块,用于暂停第一虚拟机并保存所述第一虚拟机的数据至指定内存中;其中,所述数据在所述指定内存中不被释放;A second pre-processing module is configured to pause the first virtual machine and save data of the first virtual machine to a designated memory; wherein the data is not released in the designated memory;
第二克隆模块,用于启动虚拟机可执行程序实现所述第二虚拟机的克隆;A second cloning module, configured to start a virtual machine executable program to clone the second virtual machine;
第二数据恢复模块,用于从所述指定内存中将所述数据恢复至所述第一虚拟机。The second data recovery module is configured to recover the data from the designated memory to the first virtual machine.
其中,所述第二克隆模块用于:创建子进程,子进程调用虚拟机可执行程序;启动所述虚拟机可执行程序并进行初始化,并在所述初始化完成后得到所述第二虚拟机。The second cloning module is used to: create a child process, which calls a virtual machine executable program; start the virtual machine executable program and initialize it, and obtain the second virtual machine after the initialization is completed.
其中,所述第二克隆模块用于:所述第一虚拟机对应的父进程将所述第一虚拟机的内存地址以及所述第一虚拟机的显存地址继承给所述子进程以使所述第二虚拟机与所述第一虚拟机共享内存与显存。The second cloning module is configured to: cause the parent process corresponding to the first virtual machine to inherit the memory address and the video memory address of the first virtual machine to the child process so that the second virtual machine and the first virtual machine share memory and video memory.
其中,所述第一虚拟机的数据至少包括:所述第一虚拟机的虚拟机状态信息、以及所述第一虚拟机的内存地址、显存地址;The data of the first virtual machine includes at least: virtual machine state information of the first virtual machine, and memory address and video memory address of the first virtual machine;
进一步地,所述装置还包括第二共享模块,所述第二共享模块用于:所述子进程和所述第一虚拟机对应的父进程以写时复制方式共享页表。Furthermore, the device further includes a second sharing module, and the second sharing module is used for: the child process and the parent process corresponding to the first virtual machine share the page table in a copy-on-write manner.
进一步地,所述装置还包括第二IO输出模块,所述第二IO输出模块用于:将所述第一虚拟机暂停时未完成的IO输出至存储介质。与现有技术相比,本申请可以获得包括以下技术效果:Furthermore, the device further includes a second IO output module, which is used to output the unfinished IO when the first virtual machine is suspended to a storage medium. Compared with the prior art, the present application can achieve the following technical effects:
1)将第一虚拟机的数据保存至不释放带标记的指定内存,从而实现子进程虚拟机可执行程序的启动参数的快速修改更新;1) Saving the data of the first virtual machine to a designated memory with a mark that is not released, thereby enabling rapid modification and update of startup parameters of the child process virtual machine executable program;
2)第二虚拟机对应的子进程和所述第一虚拟机对应的父进程以写时复制方式共享页表。2) The child process corresponding to the second virtual machine and the parent process corresponding to the first virtual machine share a page table in a copy-on-write manner.
3)通过在子进程中执行虚拟机可执行程序来实现虚拟机的快速克隆,其天然地与原虚拟机基于COW(写时复制)共享内存,快速克隆虚拟机时间在100ms-200ms左右,做到用户无感知。3) Rapid cloning of virtual machines is achieved by executing the virtual machine executable program in a child process. It naturally shares memory with the original virtual machine based on COW (Copy on Write). The time for rapid cloning of virtual machines is about 100ms-200ms, making it imperceptible to users.
4)在实现第二虚拟机克隆的同时,保障了第一虚拟机的工作能力。4) While cloning the second virtual machine, the working capability of the first virtual machine is guaranteed.
当然,实施本申请的任一产品必不一定需要同时达到以上所述的所有技术效果。Of course, any product implementing this application does not necessarily need to achieve all of the technical effects described above at the same time.
附图说明BRIEF DESCRIPTION OF THE DRAWINGS
此处所说明的附图用来提供对本申请的进一步理解,构成本申请的一部分,本申请的示意性实施例及其说明用于解释本申请,并不构成对本申请的不当限定。在附图中:The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:
图1是本申请实施例一的技术流程图;FIG1 is a technical flow chart of Example 1 of the present application;
图2是本申请实施例一的装置结构示意图;FIG2 is a schematic diagram of the structure of the device according to the first embodiment of the present application;
图3是本申请实施例二的技术流程图;FIG3 is a technical flow chart of Example 2 of the present application;
图4是本申请实施例二的装置结构示意图;FIG4 is a schematic diagram of the structure of the device according to the second embodiment of the present application;
图5是本申请应用场景实例的技术流程图。FIG5 is a technical flow chart of an example application scenario of the present application.
具体实施方式DETAILED DESCRIPTION
以下将配合附图及实施例来详细说明本申请的实施方式,藉此对本申请如何应用技术手段来解决技术问题并达成技术功效的实现过程能充分理解并据以实施。The following will describe the implementation methods of the present application in detail with reference to the accompanying drawings and examples, so that the implementation process of how the present application applies technical means to solve technical problems and achieve technical effects can be fully understood and implemented accordingly.
在一个典型的配置中,计算设备包括一个或多个处理器(CPU)、输入/输出接口、网络接口和内存。In a typical configuration, a computing device includes one or more processors (CPUs), input/output interfaces, network interfaces, and memory.
内存可能包括计算机可读介质中的非永久性存储器,随机存取存储器(RAM)和/或非易失性内存等形式,如只读存储器(ROM)或闪存(flash RAM)。内存是计算机可读介质的示例。Memory may include non-permanent storage in a computer-readable medium, random access memory (RAM) and/or non-volatile memory in the form of read-only memory (ROM) or flash RAM. Memory is an example of a computer-readable medium.
计算机可读介质包括永久性和非永久性、可移动和非可移动媒体可以由任何方法或技术来实现信息存储。信息可以是计算机可读指令、数据结构、程序的模块或其他数据。计算机的存储介质的例子包括,但不限于相变内存(PRAM)、静态随机存取存储器(SRAM)、动态随机存取存储器(DRAM)、其他类型的随机存取存储器(RAM)、只读存储器(ROM)、电可擦除可编程只读存储器(EEPROM)、快闪记忆体或其他内存技术、只读光盘只读存储器(CD-ROM)、数字多功能光盘(DVD)或其他光学存储、磁盒式磁带,磁带磁磁盘存储或其他磁性存储设备或任何其他非传输介质,可用于存储可以被计算设备访问的信息。按照本文中的界定,计算机可读介质不包括非暂存电脑可读媒体(transitory media),如调制的数据信号和载波。Computer-readable media include permanent and non-permanent, removable and non-removable media that can be implemented by any method or technology to store information. The information can be computer-readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, magnetic cassettes, magnetic tape, magnetic disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer-readable media does not include non-transitory media such as modulated data signals and carrier waves.
如在说明书及权利要求当中使用了某些词汇来指称特定组件。本领域技术人员应可理解,硬件制造商可能会用不同名词来称呼同一个组件。本说明书及权利要求并不以名称的差异来作为区分组件的方式,而是以组件在功能上的差异来作为区分的准则。如在通篇说明书及权利要求当中所提及的“包含”为一开放式用语,故应解释成“包含但不限定于”。“大致”是指在可接收的误差范围内,本领域技术人员能够在一定误差范围内解决所述技术问题,基本达到所述技术效果。此外,“耦接”一词在此包含任何直接及间接的电性耦接手段。因此,若文中描述一第一装置耦接于一第二装置,则代表所述第一装置可直接电性耦接于所述第二装置,或通过其他装置或耦接手段间接地电性耦接至所述第二装置。说明书后续描述为实施本申请的较佳实施方式,然所述描述乃以说明本申请的一般原则为目的,并非用以限定本申请的范围。本申请的保护范围当视所附权利要求所界定者为准。For example, certain terms are used in the specification and claims to refer to specific components. Those skilled in the art will understand that hardware manufacturers may use different terms to refer to the same component. This specification and claims do not distinguish components based on differences in name, but rather on differences in their functionality. Throughout the specification and claims, the term "including" is an open-ended term and should be interpreted as meaning "including, but not limited to." "Substantially" means that within an acceptable range of error, a person skilled in the art can solve the technical problem and substantially achieve the technical effect. Furthermore, the term "coupled" encompasses any direct and indirect electrical coupling means. Therefore, if a first device is described as being coupled to a second device, this means that the first device can be directly electrically coupled to the second device or indirectly electrically coupled to the second device via other devices or coupling means. The subsequent description of the specification describes preferred embodiments of the present application. However, this description is intended to illustrate the general principles of the present application and is not intended to limit the scope of the present application. The scope of protection of the present application shall be determined by the appended claims.
还需要说明的是,术语“包括”、“包含”或者其任何其他变体意在涵盖非排他性的包含,从而使得包括一系列要素的商品或者系统不仅包括那些要素,而且还包括没有明确列出的其他要素,或者是还包括为这种商品或者系统所固有的要素。在没有更多限制的情况下,由语句“包括一个……”限定的要素,并不排除在包括所述要素的商品或者系统中还存在另外的相同要素。It should also be noted that the terms "include," "comprises," or any other variations thereof are intended to encompass non-exclusive inclusion, such that a product or system comprising a series of elements includes not only those elements but also other elements not explicitly listed, or elements inherent to such product or system. In the absence of further limitations, an element defined by the phrase "comprises a..." does not exclude the presence of other identical elements in the product or system comprising the element.
KVM即Kernel-based Virtual Machine的简称,即基于内核的虚拟机,是一种用于Linux内核中的虚拟化基础设施。KVM的主要功能是实现CPU的虚拟化、内存虚拟化、中断虚拟化等。Linux加载了KVM模块后,因用户无法直接控制内核模块的操作,需进一步利用其它工具创建虚拟机。KVM, short for Kernel-based Virtual Machine, is a virtualization infrastructure used within the Linux kernel. KVM's main functions include CPU virtualization, memory virtualization, and interrupt virtualization. After loading the KVM module in Linux, users cannot directly control the kernel module's operations and must use other tools to create virtual machines.
QEMU,即Quick Emulator(快速仿真器),是一个广泛使用的开源计算机仿真器和虚拟机。当作为一个虚拟机时,QEMU可以通过直接使用真机的系统资源,让虚拟系统能够获得接近于物理机的性能表现。然而,QEMU独立运行时性能很差,利用KVM通过一些硬件虚拟化功能提供的接口,可以实现运行时的高性能。QEMU, or Quick Emulator, is a widely used open-source computer emulator and virtual machine. When used as a virtual machine, QEMU can directly utilize the system resources of a real machine, enabling the virtual system to achieve performance close to that of a physical machine. However, QEMU's performance is poor when running standalone. By leveraging KVM through interfaces provided by hardware virtualization features, high performance can be achieved.
在KVM虚拟化中,QEMU是运行在Host(Linux宿主机)上的进程,是虚拟机的容器。虚拟机跑在QEMU进程中,虚拟机每一个VCPU(Virtual CPU)对应QEMU进程的一个线程,虚拟机使用的内存是QEMU进程在宿主机上申请的,除此之外,QEMU进程还负责虚拟机设备(例如网卡、键盘、鼠标)的模拟。以下部分将结合附图对本申请的优选实施例进行详细的阐述。In KVM virtualization, QEMU is a process running on the host (Linux host) and serves as a container for the virtual machine. The virtual machine runs within the QEMU process, and each virtual CPU (VCPU) of the virtual machine corresponds to a thread of the QEMU process. The memory used by the virtual machine is allocated by the QEMU process on the host. In addition, the QEMU process is responsible for simulating virtual machine devices (such as the network card, keyboard, and mouse). The following section will describe the preferred embodiments of this application in detail with reference to the accompanying drawings.
图1是本申请实施例一的技术流程图,结合图1,本申请一种KVM虚拟化下虚拟机克隆的方法,可以由以下步骤实现:FIG1 is a technical flow chart of the first embodiment of the present application. In conjunction with FIG1 , the present application provides a method for cloning a virtual machine under KVM virtualization, which can be implemented by the following steps:
步骤S110:暂停第一虚拟机并保存所述第一虚拟机的数据至指定内存中;其中,所述数据在所述指定内存中不被释放Step S110: Pause the first virtual machine and save the data of the first virtual machine to a designated memory; wherein the data in the designated memory is not released
步骤S120:从所述指定内存中获取虚拟机可执行程序的启动参数;Step S120: obtaining startup parameters of the virtual machine executable program from the designated memory;
步骤S130:启动所述虚拟机可执行程序实现所述第二虚拟机的克隆。Step S130: starting the virtual machine executable program to clone the second virtual machine.
具体的,在步骤S110中,预先修改Linux内核从而在虚拟机克隆之前进行一些预处理以使得在虚拟机克隆调用exec函数时,不释放预先指定的内存。所述指定的内存是QEMU在宿主机host上申请的,并且带有特殊标记,用以作为执行exec调用时不被释放的识别标志。Specifically, in step S110, the Linux kernel is pre-modified to perform some pre-processing before the virtual machine is cloned so that when the virtual machine clone calls the exec function, the pre-specified memory is not released. The specified memory is requested by QEMU on the host machine and has a special mark to serve as an identification mark that it will not be released when the exec call is executed.
宿主机分配所述指定内存时,为指定的不释放内存添加识别标志。与此同时,通过修改QEMU的设置,不指定DONTFORK标记从而使得fork()生成的子进程和所述第一虚拟机对应的父进程以写时复制方式共享页表。DONTFORK标记在QEMU中是默认自动添加的,若是内存被添加了这个标记,在创建子进程时,内存不会继承给子进程。本申请中,需要将父进程的内存继承给子进程,从而通过采用这种方式使得新克隆的第二虚拟机和原第一虚拟机可以共享内存。When the host machine allocates the specified memory, an identification flag is added to the specified non-released memory. At the same time, by modifying the settings of QEMU, the DONTFORK flag is not specified so that the child process generated by fork() and the parent process corresponding to the first virtual machine share the page table in a copy-on-write manner. The DONTFORK flag is automatically added by default in QEMU. If this flag is added to the memory, the memory will not be inherited to the child process when the child process is created. In this application, the memory of the parent process needs to be inherited to the child process, so that the newly cloned second virtual machine and the original first virtual machine can share memory by adopting this method.
暂停第一虚拟机,即被克隆的虚拟机,在暂停之后,预先保留暂停时刻第一虚拟机的数据。保留所述数据,目的之一在于从所述数据中获取第一虚拟的状态信息,从而快速更新所述状态信息获得第二虚拟机克隆时,虚拟机可执行程序所需的启动参数;目的之二在于,保留所述第一虚拟机的数据以避免后续执行exec系统调用时丢失这些数据,同时,第一虚拟机恢复运行后根据这些保留的数据还可以恢复到暂停时的状态,在成功克隆出第二虚拟机的同时保证所述第一虚拟机可以正常运行。其中,所述当前数据包括所述第一虚拟机的虚拟机状态信息、以及所述第一虚拟机使用的内存地址、显存地址。所述虚拟机状态为所述第一虚拟机所需要保存状态的所有设备的当前状态信息,例如虚拟机的机名、通用唯一识别码,mac地址、CPU运行状态,鼠标键盘等设备状态。具体的,在步骤S120中,从所述指定内存中获取虚拟机可执行程序的启动参数。本申请实施例中的所述虚拟机可执行程序可以是QEMU可执行程序。The first virtual machine, i.e., the cloned virtual machine, is paused. After the pause, the data of the first virtual machine at the time of the pause is retained in advance. The data is retained for one purpose to obtain the state information of the first virtual machine from the data, thereby quickly updating the state information to obtain the startup parameters required by the virtual machine executable program when cloning the second virtual machine; the second purpose is to retain the data of the first virtual machine to avoid losing this data when the exec system call is subsequently executed. At the same time, after the first virtual machine resumes operation, it can also be restored to the state at the time of suspension based on the retained data, ensuring that the first virtual machine can operate normally while successfully cloning the second virtual machine. The current data includes the virtual machine state information of the first virtual machine, as well as the memory addresses and video memory addresses used by the first virtual machine. The virtual machine state is the current state information of all devices that the first virtual machine needs to save the state, such as the virtual machine name, universal unique identifier, MAC address, CPU operating status, mouse and keyboard device status, etc. Specifically, in step S120, the startup parameters of the virtual machine executable program are obtained from the designated memory. The virtual machine executable program in the embodiment of the present application can be a QEMU executable program.
所述QEMU可执行程序在启动之前需要配置一些启动参数,例如VNC(VirtualNetwork Computer,即虚拟网络计算机)地址、虚拟机名、UUID(Universally UniqueIdentifier,即通用唯一识别码)等。The QEMU executable program needs to be configured with some startup parameters before starting, such as VNC (Virtual Network Computer) address, virtual machine name, UUID (Universally Unique Identifier), etc.
本申请实施例中,QEMU可执行程序的启动参数是依据第一虚拟机的状态信息进行修改更新的。换言之,克隆出的第二虚拟机与第一虚拟机的状态信息类似,第二虚拟机的状态信息只需要通过第一虚拟机的状态进行修改即可快速得到。In the embodiment of the present application, the startup parameters of the QEMU executable program are modified and updated based on the state information of the first virtual machine. In other words, the state information of the cloned second virtual machine is similar to that of the first virtual machine, and the state information of the second virtual machine can be quickly obtained by simply modifying the state of the first virtual machine.
例如,第一虚拟机的网卡mac地址为:For example, the MAC address of the network card of the first virtual machine is:
mac=52:54:00:ce:67:a9,第二虚拟机的网卡mac地址就可以改为mac=52:54:00:ce:67:aa,这样可以避免产生mac冲突。mac=52:54:00:ce:67:a9, the MAC address of the network card of the second virtual machine can be changed to mac=52:54:00:ce:67:aa, which can avoid MAC conflicts.
需要说明的是,本申请实施例中,还需将带有执行exec时不释放标记的所述指定的内存的地址作为新增参数传入所述QEMU可执行程序的启动参数,其目的在于,子进程执行QEMU可执行程序时,直接根据QEMU可执行程序中的新增参数从所述指定的内存地址中获取所述第一虚拟机的内存地址以及显存地址供第二虚拟机共享。It should be noted that, in the embodiment of the present application, the address of the specified memory with the non-release mark when executing exec needs to be passed as a new parameter into the startup parameters of the QEMU executable program. The purpose is that when the child process executes the QEMU executable program, the memory address and video memory address of the first virtual machine are directly obtained from the specified memory address according to the new parameters in the QEMU executable program for sharing by the second virtual machine.
具体的,在步骤S130中,通过fork()创建代表新虚拟机即第二虚拟机的子进程。Specifically, in step S130, a child process representing the new virtual machine, ie, the second virtual machine, is created through fork().
一个进程包括代码、数据和分配给进程的资源,fork()函数通过系统调用创建一个与原来进程几乎完全相同的进程,也就是两个进程可以做完全相同的事,但如果初始参数或者传入的变量不同,两个进程也可以做不同的事。一个进程调用fork()函数后,系统先给新的进程分配资源,例如存储数据和代码的空间。然后把原来的进程的所有值都复制到新的新进程中,只有少数值与原来的进程的值不同。A process consists of code, data, and allocated resources. The fork() function creates a process that is nearly identical to the original process through a system call. This means the two processes can do exactly the same thing, but they can also do different things if the initial parameters or variables passed in are different. When a process calls fork(), the system first allocates resources to the new process, such as space for storing data and code. It then copies all values from the original process to the new process, with only a few values differing from those in the original process.
fork函数的一次调用,会返回两个值。它在调用进程(称为父进程)中返回一次,返回值是新派生进程(称为子进程)的进程ID号;在子进程中又返回一次,返回值为0.因此,返回值本身可以告知当前进程是子进程还是父进程。fork在子进程返回0而不是父进程的进程ID的原因在于:任何子进程只有一个父进程,而子进程总是可以通过getppid取得父进程的进程ID。相反,父进程可以有许多子进程而且无法获取各个子进程的进程ID。如果父进程想要跟踪所有子进程的进程ID,那么它必须记录每次调用fork的返回值。A single call to the fork function returns two values. It returns once in the calling process (called the parent process), with the process ID of the newly forked process (called the child process); and again in the child process, with a return value of 0. Therefore, the return value itself tells you whether the current process is the child or the parent. The reason fork returns 0 in the child process, rather than the parent's process ID, is that any child process has only one parent, and the child process can always obtain the parent's process ID using getppid. In contrast, a parent process can have many children and cannot obtain the process ID of each child. If the parent process wants to keep track of the process IDs of all its child processes, it must record the return value of each fork call.
需要说明的是,fork()函数创建的子进程可与父进程之间共享内存、天然地具有COW(copy on write,即写时复制)特性,同时,父进程中的执行exec系统调用时内存不释放的标识也被继承给子进程。It should be noted that the child process created by the fork() function can share memory with the parent process and naturally has the COW (copy on write) feature. At the same time, the flag that the memory is not released when the exec system call is executed in the parent process is also inherited by the child process.
本步骤中,所述子进程通过调用exec()函数启动所述虚拟机可执行程序。exec族函数,其功能在于根据指定的文件名找到可执行文件。exec系统调用经常与fork()联合使用,先用fork建立一个子进程,然后在子进程中使用exec,这样就实现了父进程运行一个与其不同的子进程,并且父进程不会被覆盖。执行exec系统调用,一般是先用fork()函数新建立一个进程,然后让新建的进程去执行exec调用。在fork()建立新进程之后,父进程与子进程共享代码段,但数据空间是分开的,但父进程会把自己数据空间的内容拷贝到子进程中去,还有上下文也会拷贝到子进程中去。为了提高效率,采用一种写时复制(copy onwrite)的策略,即创建子进程的时候,并不拷贝父进程的地址空间,父子进程拥有共同的地址空间,只有当子进程需要写入数据时(如向缓冲区写入数据,这时候会复制地址空间,复制缓冲区到子进程中去。从而父子进程拥有独立的地址空间。而对于fork()之后执行exec后,这种策略能够很好的提高效率,如果一开始就拷贝,那么exec之后,子进程的数据会被放弃,被新的进程所代替。In this step, the child process starts the virtual machine executable program by calling the exec() function. The exec family of functions is used to find an executable file according to a specified file name. The exec system call is often used in conjunction with fork(). First, fork is used to create a child process, and then exec is used in the child process. In this way, the parent process runs a different child process from it, and the parent process will not be overwritten. To execute the exec system call, generally, a new process is first created using the fork() function, and then the newly created process is allowed to execute the exec call. After fork() creates a new process, the parent process and the child process share the code segment, but the data space is separate. However, the parent process will copy the contents of its own data space to the child process, and the context will also be copied to the child process. In order to improve efficiency, a copy-on-write strategy is adopted, that is, when creating a child process, the address space of the parent process is not copied. The parent and child processes have a common address space. Only when the child process needs to write data (such as writing data to the buffer), the address space will be copied and the buffer will be copied to the child process. Thus, the parent and child processes have independent address spaces. After executing exec after fork(), this strategy can greatly improve efficiency. If the copy is made at the beginning, then after exec, the data of the child process will be abandoned and replaced by the new process.
上一步骤中,获取到所述虚拟机可执行程序的启动参数,在配置好启动参数之后,所述虚拟机可执行程序即可启动工作并进行初始化。In the previous step, the startup parameters of the virtual machine executable program are obtained. After the startup parameters are configured, the virtual machine executable program can start working and be initialized.
其中,所述初始化可以包括各种设备的初始化。KVM虚拟化下大多设备都是QEMU软件模拟的。例如,在软件模块,QEUM需要申请各种数据结构用来存放这些设备的状态,申请各种定时器来模拟各种时钟(RTC、ACPI PM Timer等)。The initialization can include initializing various devices. Under KVM virtualization, most devices are emulated by the QEMU software. For example, in the software module, QEMU needs to apply for various data structures to store the status of these devices and apply for various timers to simulate various clocks (RTC, ACPI PM Timer, etc.).
比如VCPU的初始化,在KVM虚拟化下,虚拟机的一个VCPU跑在一个QEMU线程中,QEMU就需要创建线程,申请各种数据结构存放VCPU的状态。由于所述启动参数中包含所述指定的内存地址,因此在给第二虚拟机申请内存时直接使用所述指定的内存中保存的第一虚拟机的内存地址,而不再重新申请,因为内存不释放标志在fork()创建时,由父进程继承给了子进程,因此子进程调用exec执行QEMU可执行程序时,被标记的所述指定内存并没有被释放,可以继续使用,从而实现了第一虚拟机和第二虚拟机的内存共享。For example, when initializing a VCPU, under KVM virtualization, a virtual machine's VCPU runs in a QEMU thread. QEMU needs to create a thread and request various data structures to store the VCPU's state. Because the startup parameters include the specified memory address, when requesting memory for the second virtual machine, the memory address of the first virtual machine stored in the specified memory is directly used without re-requesting. Because the memory non-release flag is inherited by the parent process to the child process during fork() creation, when the child process calls exec to execute the QEMU executable program, the marked specified memory is not released and can continue to be used, thus achieving memory sharing between the first and second virtual machines.
优选的,本申请实施例中,虚拟机可执行程序在初始化完成之后恢复所述预先指定的内存中的所述第一虚拟机当前数据。从而,在克隆出新虚拟机的情况下,完好地保留了原虚拟机的工作能力。Preferably, in an embodiment of the present application, the virtual machine executable program restores the current data of the first virtual machine in the pre-designated memory after initialization is completed. Thus, when a new virtual machine is cloned, the working capability of the original virtual machine is fully preserved.
优选的,本申请实施例中,恢复运行的第一虚拟机以及fork-exec后开始运行的第二虚拟机都会以写时复制形式写入到设定的不同路径。假设第一虚拟机原本的数据存储路径为vm1.qcow2,在第一虚拟机暂停之后,写入第一虚拟机和第二虚拟机的数据都将写入新的路径,例如第一虚拟机的数据存储路径可以为vm11.qcow2,第二虚拟机的数据存储路径可以为vm12.qcow2,新路径vm11.cow2以及vm12.cow2都以vm1.qcow2为backing file(后备镜像),采用后备镜像差量生成虚拟机的方式,在多台虚拟机上公用一个后备镜像,节省大量磁盘空间。Preferably, in an embodiment of the present application, the first virtual machine that resumes operation and the second virtual machine that starts running after fork-exec are both written to different set paths in the form of copy-on-write. Assuming that the original data storage path of the first virtual machine is vm1.qcow2, after the first virtual machine is suspended, the data written to the first virtual machine and the second virtual machine will be written to the new path. For example, the data storage path of the first virtual machine can be vm11.qcow2, and the data storage path of the second virtual machine can be vm12.qcow2. The new paths vm11.cow2 and vm12.cow2 both use vm1.qcow2 as the backing file (backup image), and use the backup image difference to generate virtual machines. A backup image is shared by multiple virtual machines, saving a lot of disk space.
优选的,本申请实施例中,进一步还包括,在执行第二虚拟机克隆之前,将第一虚拟机后端未完成的IO强制输出至存储介质,避免所述子进程执行exec系统调用时导致这些未完成的IO丢失。其中,所述存储介质可以是虚拟机的存储介质,可以是本地HOST上的磁盘,也可以是网络的分布式网盘,本申请实施例并不限制。Preferably, in an embodiment of the present application, before executing the second virtual machine clone, the unfinished IO of the first virtual machine backend is forcibly output to a storage medium to avoid the loss of these unfinished IO when the child process executes the exec system call. The storage medium can be the storage medium of the virtual machine, a disk on the local host, or a distributed network disk, which is not limited in the embodiment of the present application.
本实施例中,通过修改Linux内核使得执行exec系统调用时不释放带标记的指定内存,从而实现子进程QEMU可执行程序的启动参数的快速修改更新;通过修改QEMU,不指定DONTFORK标记以使fork()生成的子进程和所述第一虚拟机对应的父进程以写时复制方式共享页表;与此同时,通过在fork()生成的子进程中调用exec()执行QEMU可执行程序来实现虚拟机的快速克隆,其天然地与原虚拟机基于写时拷贝COW共享内存,快速克隆虚拟机vmfork时间在100ms-200ms左右,做到用户无感知。In this embodiment, the Linux kernel is modified so that the designated memory with a mark is not released when the exec system call is executed, thereby realizing the rapid modification and update of the startup parameters of the child process QEMU executable program; by modifying QEMU, the DONTFORK mark is not specified so that the child process generated by fork() and the parent process corresponding to the first virtual machine share the page table in a copy-on-write manner; at the same time, by calling exec() in the child process generated by fork() to execute the QEMU executable program, the rapid cloning of the virtual machine is realized, which naturally shares memory with the original virtual machine based on copy-on-write COW, and the rapid cloning of the virtual machine vmfork time is about 100ms-200ms, so that the user is not aware of it.
图2是本申请实施例一对应的装置结构示意图,结合图2,所述装置包括:第一预处理模块210、第一参数获取模块220、第一克隆模块230、第一共享模块240、第一IO输出模块250、数据复原模块260。Figure 2 is a schematic diagram of the device structure corresponding to Example 1 of the present application. In conjunction with Figure 2, the device includes: a first preprocessing module 210, a first parameter acquisition module 220, a first cloning module 230, a first sharing module 240, a first IO output module 250, and a data recovery module 260.
所述第一预处理模块210,用于暂停第一虚拟机并保存所述第一虚拟机的数据至指定内存中;其中,所述数据在所述指定内存中不被释放;The first pre-processing module 210 is configured to pause the first virtual machine and save data of the first virtual machine to a designated memory; wherein the data is not released in the designated memory;
所述第一参数获取模块220,用于从所述指定内存中获取虚拟机可执行程序的启动参数;The first parameter acquisition module 220 is used to obtain startup parameters of the virtual machine executable program from the designated memory;
所述第一克隆模块230,用于启动所述虚拟机可执行程序实现所述第二虚拟机的克隆。The first cloning module 230 is configured to start the virtual machine executable program to clone the second virtual machine.
其中,所述第一克隆模块230用于:创建子进程,子进程调用虚拟机可执行程序;The first cloning module 230 is used to: create a child process, and the child process calls the virtual machine executable program;
启动所述虚拟机可执行程序并进行初始化,并在所述初始化完成后得到所述第二虚拟机。The virtual machine executable program is started and initialized, and the second virtual machine is obtained after the initialization is completed.
其中,所述第一克隆模块230用于:所述第一虚拟机对应的父进程将所述第一虚拟机的内存地址以及所述第一虚拟机的显存地址继承给所述子进程以使所述第二虚拟机与所述第一虚拟机共享内存与显存。The first cloning module 230 is configured to: enable the parent process corresponding to the first virtual machine to inherit the memory address and the video memory address of the first virtual machine to the child process so that the second virtual machine and the first virtual machine share memory and video memory.
其中,所述第一虚拟机的数据至少包括:所述第一虚拟机的虚拟机状态信息、以及所述第一虚拟机的内存地址、显存地址;The data of the first virtual machine includes at least: virtual machine state information of the first virtual machine, and memory address and video memory address of the first virtual machine;
其中,所述第一参数获取模块210用于:从所述指定内存中获取所述第一虚拟机的状态信息,并更新所述第一虚拟机的状态信息得到所述启动参数。The first parameter acquisition module 210 is configured to acquire the state information of the first virtual machine from the designated memory, and update the state information of the first virtual machine to obtain the startup parameters.
其中,所述第一参数获取模块210还用于:获取所述指定内存的地址;将所述指定内存的地址作为新增参数传入所述虚拟机可执行程序的启动参数。The first parameter acquisition module 210 is further configured to: acquire the address of the designated memory; and pass the address of the designated memory as a new parameter into the startup parameters of the virtual machine executable program.
进一步地,所述装置还包括第一共享模块240,所述第一共享模块240用于:所述子进程和所述第一虚拟机对应的父进程以写时复制方式共享页表。Furthermore, the apparatus further includes a first sharing module 240, and the first sharing module 240 is configured to: enable the child process and the parent process corresponding to the first virtual machine to share a page table in a copy-on-write manner.
进一步地,所述装置还包括第一IO输出模块250,所述第一IO输出模块250用于:将所述第一虚拟机暂停时未完成的IO输出至存储介质。Furthermore, the apparatus further includes a first IO output module 250, and the first IO output module 250 is configured to output unfinished IOs when the first virtual machine is suspended to a storage medium.
进一步地,所述装置还包括第一数据恢复模块260,所述第一数据恢复模块260用于:从所述指定内存中获取所述第一虚拟机的数据,恢复所述第一虚拟机。Furthermore, the apparatus further includes a first data recovery module 260, and the first data recovery module 260 is configured to obtain the data of the first virtual machine from the designated memory and recover the first virtual machine.
图2所示装置可以执行图1所示实施例所述方法,其实现原理和技术效果不再赘述。The device shown in FIG2 can execute the method described in the embodiment shown in FIG1 , and its implementation principle and technical effects are not described in detail any more.
图3是本申请实施例二的技术流程图,结合图3,本申请实施例一种KVM虚拟化下虚拟机克隆装置的另一实施方式包括:FIG3 is a technical flow chart of the second embodiment of the present application. In conjunction with FIG3 , another implementation of a virtual machine cloning device under KVM virtualization in the embodiment of the present application includes:
步骤S310:暂停第一虚拟机并保存所述第一虚拟机的数据至指定内存中;其中,所述数据在所述指定内存中不被释放;Step S310: Pause the first virtual machine and save the data of the first virtual machine to a designated memory; wherein the data is not released in the designated memory;
步骤S320:启动虚拟机可执行程序实现所述第二虚拟机的克隆;Step S320: starting the virtual machine executable program to clone the second virtual machine;
步骤S330:从所述指定内存中将所述数据恢复至所述第一虚拟机。Step S330: Restore the data from the designated memory to the first virtual machine.
上述步骤的执行过程参见图1所示的实施例,此处不再赘述。The execution process of the above steps is shown in the embodiment shown in FIG1 , and will not be repeated here.
需要说明的是,本申请实施例中,虚拟机可执行程序在初始化完成之后得到克隆出的所述第二虚拟机,此时即可恢复所述预先指定的内存中的所述第一虚拟机当前数据。从而,在克隆出新虚拟机的情况下,完好地保留了原虚拟机的工作能力。It should be noted that in the embodiment of the present application, after the virtual machine executable program is initialized and the second virtual machine is cloned, the current data of the first virtual machine in the pre-designated memory can be restored. Thus, when the new virtual machine is cloned, the working capabilities of the original virtual machine are fully preserved.
图4是本申请实施例二的装置结构示意图,结合图4,所述装置包括:FIG4 is a schematic diagram of the structure of a device according to the second embodiment of the present application. In conjunction with FIG4 , the device includes:
第二预处理模块410,用于暂停第一虚拟机并保存所述第一虚拟机的数据至指定内存中;其中,所述数据在所述指定内存中不被释放;A second pre-processing module 410 is configured to pause the first virtual machine and save data of the first virtual machine to a designated memory; wherein the data is not released in the designated memory;
第二克隆模块420,用于启动虚拟机可执行程序实现所述第二虚拟机的克隆;A second cloning module 420 is configured to start a virtual machine executable program to clone the second virtual machine;
第二数据恢复模块430,用于从所述指定内存中将所述数据恢复至所述第一虚拟机。The second data recovery module 430 is configured to recover the data from the designated memory to the first virtual machine.
其中,所述第二克隆模块420用于:创建子进程,子进程调用虚拟机可执行程序;启动所述虚拟机可执行程序并进行初始化,并在所述初始化完成后得到所述第二虚拟机。The second cloning module 420 is used to: create a child process, the child process calls the virtual machine executable program; start the virtual machine executable program and initialize it, and obtain the second virtual machine after the initialization is completed.
其中,所述第二克隆模块420用于:所述第一虚拟机对应的父进程将所述第一虚拟机的内存地址以及所述第一虚拟机的显存地址继承给所述子进程以使所述第二虚拟机与所述第一虚拟机共享内存与显存。The second cloning module 420 is configured to: cause the parent process corresponding to the first virtual machine to inherit the memory address and the video memory address of the first virtual machine to the child process so that the second virtual machine and the first virtual machine share memory and video memory.
其中,所述第一虚拟机的数据至少包括:所述第一虚拟机的虚拟机状态信息、以及所述第一虚拟机的内存地址、显存地址;The data of the first virtual machine includes at least: virtual machine state information of the first virtual machine, and memory address and video memory address of the first virtual machine;
进一步地,所述装置还包括第二共享模块440,所述第二共享模块440用于:所述子进程和所述第一虚拟机对应的父进程以写时复制方式共享页表。Furthermore, the apparatus further includes a second sharing module 440, and the second sharing module 440 is configured to: enable the child process and the parent process corresponding to the first virtual machine to share a page table in a copy-on-write manner.
进一步地,所述装置还包括第二IO输出模块450,所述第二IO输出模块450用于:将所述第一虚拟机暂停时未完成的IO输出至存储介质。Furthermore, the apparatus further includes a second IO output module 450, and the second IO output module 450 is configured to output the unfinished IO when the first virtual machine is suspended to a storage medium.
图4所示装置可以执行图3所示实施例所述方法,其实现原理和技术效果不再赘述。The device shown in FIG4 can execute the method described in the embodiment shown in FIG3 , and its implementation principle and technical effects are not described in detail any more.
应用实例Application Examples
以下部分将结合图3,阐述在一个实际的应用场景中,本申请技术方案的实现的完整过程。The following section will combine Figure 3 to explain the complete process of implementing the technical solution of this application in an actual application scenario.
根据本申请的技术方案,在进行虚拟机克隆之前,预先修改Linux系统内核,以使系统在调用exec时不释放带指定标记的内存。According to the technical solution of the present application, before cloning a virtual machine, the Linux system kernel is modified in advance so that the system does not release memory with a specified tag when calling exec.
1、向宿主机申请一块带指定标记的内存,记为内存M。这块内存在系统执行exec调用时不被释放。此时宿主机在分配内存时,给这块内存添加exec时不释放的标记,同时去除默认的DONTFORK标记,从而这块带标记的内存在fork创建子进程时,由父进程继承给子进程。1. Request a block of memory with a specified flag from the host, denoted as memory M. This memory is not released when the system executes an exec call. When allocating memory, the host adds a flag indicating that it will not be released during exec, and removes the default DONTFORK flag. This flagged memory is then inherited by the parent process when the child process is forked.
2、暂停第一虚拟机的运行,此处所述第一虚拟机是被克隆的虚拟机。2. Pause the operation of the first virtual machine, where the first virtual machine is the cloned virtual machine.
3、将第一虚拟机后端没完成的I/O刷到任意存储介质,系统执行exec调用后除了指定保留的内存,其他的都被释放掉了,包括在后端没完成的I/O的内存中数据结构也没了,这样会导致IO丢失。3. The unfinished I/O of the first virtual machine backend is flushed to any storage medium. After the system executes the exec call, all memory except the specified reserved memory is released, including the data structure in the memory of the unfinished I/O of the backend, which will cause IO loss.
4、保存第一虚拟机的状态以及第一虚拟机使用的内存地址核显存地址到预先申请的指定内存M中。系统执行exec调用后除了指定保留的内存,其他的都被释放掉了,因此为了保证第一虚拟机在恢复运行之后的工作状态,需要预先保存第一虚拟机的设备如CPU、磁盘、鼠标等的状态。4. Save the state of the first virtual machine and the memory addresses and graphics memory addresses used by the first virtual machine to the pre-requested designated memory M. After the system executes the exec call, all memory except the reserved memory is released. Therefore, to ensure the working state of the first virtual machine after resumption, it is necessary to save the state of the first virtual machine's devices, such as the CPU, disk, and mouse.
5、从第一虚拟机暂停的时间节点开始,所有写入第一虚拟机以及第二虚拟机的数据都将以COW的形式,存放到别的路径,路径的命名可以是以原镜像名—虚拟机命名。5. Starting from the time point when the first virtual machine is paused, all data written to the first virtual machine and the second virtual machine will be stored in a COW format in another path. The path can be named after the original image name - the virtual machine.
6、设置QEMU申请的用于虚拟机的内存和显存继承给子进程。通过fork创建代表新虚拟机即第二虚拟机的子进程。由fork函数的特性决定,被创建的子进程和父进程之间共享内存,天然地具有COW特性。如图3所示,执行pid=fork(),创建子进程,pid将返回两个值。若pid的返回值为0,则说明当前进程为新创建的子进程,执行步骤7;若pid的返回值非零,则判断当前进程是父进程,直接执行步骤10即可。6. Set the memory and video memory requested by QEMU for the virtual machine to be inherited by the child process. Create a child process representing the new virtual machine, i.e., the second virtual machine, through fork. Due to the characteristics of the fork function, the created child process and the parent process share memory and naturally have the COW characteristic. As shown in Figure 3, execute pid = fork() to create a child process, and pid will return two values. If the return value of pid is 0, it means that the current process is a newly created child process, and execute step 7; if the return value of pid is non-zero, it is determined that the current process is the parent process, and directly execute step 10.
7、被创建的子进程调用exec执行QEMU可执行程序并更新QEMU可执行程序的启动参数。具体更新方法是,使用父进程调用QEMU可执行程序时类似的启动参数,并按照需要替换其中的参数,比如vnc地址、虚拟机名,uuid等,除此之外还需将预先申请的指定内存M的地址作为新增参数传入QEMU可执行程序的启动参数中,从而实现对QEMU可执行程序启动参数的更新。7. The created child process calls exec to execute the QEMU executable program and update the startup parameters of the QEMU executable program. The specific update method is to use similar startup parameters when the parent process calls the QEMU executable program and replace the parameters as needed, such as the vnc address, virtual machine name, uuid, etc. In addition, the address of the pre-applied specified memory M must be passed as a new parameter to the startup parameters of the QEMU executable program, thereby updating the startup parameters of the QEMU executable program.
8、QEMU可执行程序的启动参数被更新后,QEMU可执行程序从main处开始运行,进行一系列的初始化。因更新后的启动参数中有指定内存M的地址传入,所以在给第二虚拟机申请内存时,直接使用M中保存的第一虚拟机的内存地址,而不再重新申请,由此实现了新旧虚拟机的内存共享,节省了内存空间。8. After the startup parameters of the QEMU executable program are updated, the QEMU executable program starts running from main and performs a series of initializations. Because the address of the specified memory M is passed in the updated startup parameters, when applying for memory for the second virtual machine, the memory address of the first virtual machine saved in M is directly used instead of applying for it again. This enables memory sharing between the old and new virtual machines, saving memory space.
9、QEMU可执行程序初始化完成后,恢复指定内存M中保存的虚拟机状态。9. After the QEMU executable program is initialized, the virtual machine state saved in the specified memory M is restored.
10、释放预先申请的指定内存M并恢复第一虚拟机和第二虚拟机的运行。刚恢复运行的第一虚拟机和新被克隆出来的第二虚拟机在初始时,具有一样的运行状态。这样一个克隆过程,从第一虚拟机暂停至第一虚拟机恢复运行的时间只需要100ms-200ms,几乎做到了用户无感知,快速克隆的同时实现了良好的用户体验。10. Release the previously requested memory M and resume the operation of the first and second virtual machines. The newly resumed first virtual machine and the newly cloned second virtual machine initially have the same operating state. This cloning process takes only 100ms-200ms from pausing the first virtual machine to resuming operation, making it virtually invisible to the user. This fast cloning process provides a good user experience.
上述说明示出并描述了本发明的若干优选实施例,但如前所述,应当理解本发明并非局限于本文所披露的形式,不应看作是对其他实施例的排除,而可用于各种其他组合、修改和环境,并能够在本文所述发明构想范围内,通过上述教导或相关领域的技术或知识进行改动。而本领域人员所进行的改动和变化不脱离本发明的精神和范围,则都应在本发明所附权利要求的保护范围内。The foregoing description shows and describes several preferred embodiments of the present invention. However, as previously stated, it should be understood that the present invention is not limited to the forms disclosed herein and should not be construed as excluding other embodiments. Rather, the present invention can be used in various other combinations, modifications, and environments and can be modified within the scope of the inventive concept described herein by the teachings above or by techniques or knowledge in the relevant art. Modifications and variations made by those skilled in the art that do not depart from the spirit and scope of the present invention are intended to be within the scope of the appended claims.
Claims (26)
Publications (3)
Publication Number | Publication Date |
---|---|
HK1247677A1 HK1247677A1 (en) | 2018-09-28 |
HK1247677A HK1247677A (en) | 2018-09-28 |
HK1247677B true HK1247677B (en) | 2021-08-13 |
Family
ID=
Similar Documents
Publication | Publication Date | Title |
---|---|---|
EP3183650B1 (en) | Rapid suspend/resume for virtual machines via resource sharing | |
US11487523B2 (en) | Updating machine emulator | |
US9996383B2 (en) | Handling disk state inheritance for forked virtual machines | |
US9998467B2 (en) | Application publishing using memory state sharing | |
US10379967B2 (en) | Live rollback for a computing environment | |
US10564996B2 (en) | Parentless virtual machine forking | |
US9477507B2 (en) | State customization of forked virtual machines | |
US8990806B2 (en) | Customized virtual machine creation | |
CN107463426B (en) | Method and device for cloning virtual machine under KVM virtualization | |
US20180101452A1 (en) | Memory first live snapshot | |
US10977063B2 (en) | Elastic compute fabric using virtual machine templates | |
US20120144391A1 (en) | Provisioning a virtual machine | |
US20150178108A1 (en) | Fast Instantiation of Virtual Machines | |
WO2015138704A1 (en) | Method and system for implementing virtual machine images | |
CN107533441B (en) | Creating operating system volumes | |
US12333321B2 (en) | Application-assisted live migration | |
US11693689B2 (en) | Online disk encryption using mirror driver | |
US11513825B2 (en) | System and method for implementing trusted execution environment on PCI device | |
HK1247677B (en) | Virtual machine cloning method and device under kvm virtualization | |
HK1247677A (en) | Virtual machine cloning method and device under kvm virtualization | |
HK1247677A1 (en) | Virtual machine cloning method and device under kvm virtualization |