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

  • KVM: A kernel-based virtual machine (engine)
  • Qemu: used to simulate VM I/O devices
  • Qemu-img: virtual machine disk management tool
  • Libvirt: an API for virtualization services
  • Virsh: a command line tool based on libvirt
  • Qemu-manager: graphical management tool

When creating a KVM VM, you can specify another disk file as the BackingFile. BackingFile is a read-only virtual disk base image that can be shared across multiple VMS. When creating and running VMS based on BackingFile, files are incrementally written to their own disk files, which improves efficiency and saves disk and maintenance costs.

A VIRTUAL machine snapshot saves the state of a virtual machine at a specified point in time, allowing us to save and restore to a point in time in execution when we encounter a problem or error during automated testing. With the BackingFile mechanism, a VIRTUAL machine supports a snapshot chain with multiple dependencies such as the following.

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

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

qemu-img convert -O qcow2 vm02.qcow2 vm-templ.img
Copy the code

Suppose we create the following directory in the user’s working directory.

  • The root directory of the KVM

  • Iso Stores the CD-ROM image

  • Deposit base BackingFile

  • Share Stores the shared disk image and allows users to store test tools and drivers

  • Image Stores the disk image of the test machine. XML Stores the exported VM XML configuration file

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

  • Follow the steps in the previous article to create a Win10 virtual machine;
  • In the virtual machine, install the test software used in the work.
  • Run the following command to create a shared tool disk:

qemu-img create -f qcow2 -o cluster_size=2M kvm/share/tools.qcow2 10G
Copy the code
  • Mount a shared disk to the VM and copy tools and files to the disk.
  • In the dialog box that is displayed, do not delete the disk files.
  • 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
  • Run the following command to create a new VM disk using the basic image as the 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
Copy the code
  • On the GUI, create a test VM and attach it to the new VM and shared disk.

In addition to using the graphical interface qemu-Manager software, there is also a command-line method that you can use to test the platform code.

  • The XML configuration file of the VM is exported

virsh dumpxml test-win10-x64-pro-zh > kvm/xml/test-win10-x64-pro-zh.xml
Copy the code
  • Modify the following fields in the XML configuration file:

Name UUID VCPU Memory and currentMemory MAC Address Source file of the first disk

  • In the Elemnt of the first disk block, add the following information about BackingFile.

<backingStore type="file" index="2">
  <format type="qcow2"/>
  <source file="/home/aaron/kvm/base/windows/win10/x64-pro-zh_cn.qcow2"/>
<backingStore/>
Copy the code
  • If you need to use the page VNC to access the virtual machine desktop, find the XML graphics element and modify it as follows:

<graphics type=" VNC "port="-1" autoport="yes" Listen ="0.0.0.0" passwd="P2ssw0rd"> < Graphics type="address" 0.0.0.0 address = "" / > < / graphics >Copy the code
  • Use the following command to define the VIRTUAL machine.

virsh define kvm/xml/test-win10-x64-pro-zh.xml
Copy the code
  • Use the following command to start the VM.

virsh start test-win10-x64-pro-zh
Copy the code
  • Run the following command to obtain the VNC port number of the VM, and use port 5900+ this number in the VNC software to access the remote desktop of the VM.

virsh vncdisplay test-win10-x64-pro-zh
Copy the code

In addition, I use the GO language to achieve virtual machine management functions based on libvirt interface. This open source project aims to provide you with an on-demand test environment management platform based on KVM virtual machine and Docker container. For details, please refer to the website github.com/easysoft/za… .

Common commands:

Qemu-img info --backing-chain KVM /image/ test-win10-x64-pro-zh_CN-01. Qcow2 # Backing -img resize vm disk X64-pro-zh_cn. Qcow2 +10G # Check the VM list virsh list --all # Check the VM VNC port virsh vncdisplay win10-test # Export the VM XML configuration file virsh Dumpxml win10-test > win10-test. XML # Create a VM disk image qemu-img create -f qcow2 -o cluster_size=2M,backing_file=base.qcow2 Win10-test.qcow2 40G # Start and stop a VM virsh define win10-test. XML virsh start win10-test virsh destroy win10-test virsh undefine win10-testCopy the code