First, let’s look at some of the concepts and tools associated with the KVM virtual machine.

  • KVM: Kernel-based virtual machine (engine)
  • QEMU: Used to simulate virtual machine IO devices
  • Qemu-img: Virtual machine disk management tool
  • Libvirt: The API interface for the virtualization service
  • Virsh: Command line tool based on libvirt implementation
  • QEMU-Manager: Graphical management tool

When you create a new KVM virtual machine, you can specify another disk file as a BackingFile. BackingFile is a read-only virtual disk base image that can be shared between multiple virtual machines. When you create and run a virtual machine based on BackingFile, you only write files incrementally to your own disk file, thereby increasing efficiency and saving disk and maintenance costs.

A virtual machine snapshot saves the state of the virtual machine at a specified point in time and can be used to save and recover to a point in time in execution when we encounter problems or errors during automated testing. With the backingFile mechanism, the virtual machine supports snapshot chains in the form of the following multi-level dependencies.

base image <-- vm01 <-- snap 1 <-- snap 2 <-- vm02(active)

You can use the following command to export a virtual machine in the snapshot chain to form a separate disk image file that does not depend on other images.

qemu-img convert -O qcow2 vm02.qcow2 vm-templ.img

Suppose we set up the following directory in the user’s working directory.

KVM root directory ISO storage BackingFile share storage BackingFile share storage test tools, drivers, etc. Image storage test machine disk image XML storage exported virtual machine XML configuration file

Here is an example to show you how to quickly create a test virtual machine.

  1. Follow the steps in the previous article to create a Win10 virtual machine;
  2. In the virtual machine, install the test software used in the work;
  3. Create a new shared utility disk using the following command.
qemu-img create -f qcow2 -o cluster_size=2M kvm/share/tools.qcow2 10G
  1. Mount the shared disk to the virtual machine, copy tools and files to the disk;
  2. To remove the virtual machine, select not to delete relevant disk files in the Confirm dialog box.
  3. Move the original virtual machine Lord disk file to the base image directory, such as the KVM/base/Windows/win10 / x64 – pro – zh_cn. Qcow2.
  4. Execute the following command to create a new virtual machine disk with the above base image as BackingFile;
qemu-img create -f qcow2 -o cluster_size=2M,backing_file=kvm/base/windows/win10/x64-pro-zh_cn.qcow2 kvm/image/test-win10-x64-pro-zh_cn-01.qcow2 40G
  1. In the graphical interface, create a new test virtual machine, hang on the new virtual machine and the shared disk.

In addition to the QEMU-Manager software, which uses a graphical interface, there is also a command-line method that you can use to test the platform’s code.

  1. Export the virtual machine XML configuration file
virsh dumpxml test-win10-x64-pro-zh > kvm/xml/test-win10-x64-pro-zh.xml
  1. Modify the following fields in the XML configuration file:

    • name
    • uuid
    • vcpu
    • The memory and currentMemory
    • mac address
    • Source file on disk 1
  2. To Elemnt on disk 1, add the following BackingFile contents:
<backingStore type="file" index="2">
  <format type="qcow2"/>
  <source file="/home/aaron/kvm/base/windows/win10/x64-pro-zh_cn.qcow2"/>
<backingStore/>
  1. If you need to use page VNC to access the virtual machine desktop, find the Graphics element of the XML and modify it to the following.
<graphics type=" VNC "port="-1" autoport="yes" listen="0.0.0.0" passwd="P2ssw0rd"> <listen type="address" 0.0.0.0 address = "" / > < / graphics >
  1. Use the following command to define the virtual machine.
virsh define kvm/xml/test-win10-x64-pro-zh.xml
  1. Use the following command to start the virtual machine.
virsh start test-win10-x64-pro-zh
  1. Use the following command to get the VNC port number of the virtual machine. Use the port “5900+ this number” in the VNC software to access the remote desktop of the virtual machine.
virsh vncdisplay test-win10-x64-pro-zh

In addition, I use the language to realize the virtual machine management related functions based on libvirt interface, the open source project aims to provide a KVM based virtual machine and Docker container, on-demand test environment management platform, for details please refer to https://github.com/easysoft/z… .

Common commands:

Qemu-img resize --backing-chain KVM /image/ test-win10-x64-pro-zh_CN-01. Qcow2 # set the size of the virtual machine's disk qemu-img resize while backing-chain KVM /image/ test-win10-x64-pro-zh_CN-01 X64-pro-zh_cn. qcow2 +10G # Virsh list --all # Virsh vncdisplay win10-test # Virsh vncdisplay win10-test # Virsh vncdisplay Qemu-img create -f qcow2-o cluster_size=2M,backing_file=base.qcow2 Qcow2 vm02.qcow2 vm-templ. Img # convert qemu-img vm02.qcow2 vm-templ. Img # convert qemu-img vm02.qcow2 vm-templ. Img # Virsh define win10-test. XML virsh start win10-test virsh destroy win10-test virsh undefine win10-test. Virsh destroy win10-test virsh undefine win10-test