1

First, let's take a look at several concepts and tools related to 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: API interface for virtualization services
  • virsh: a command line tool based on libvirt
  • qemu-manager: Graphical management tool

new KVM virtual machine, you can specify another disk file as 16195ec09c005b BackingFile . BackingFile is a read-only virtual disk base image, which can be shared among multiple virtual machines. When creating and running a virtual machine based on BackingFile, it will only incrementally write files to its own disk files, thereby improving efficiency and saving disk and maintenance costs.

virtual machine snapshot saves the state of the virtual machine at a certain point in time. When we encounter problems or errors in the automated testing process, we can use the snapshot to save and restore to a certain point in time during execution. With the BackingFile mechanism, the virtual machine supports a multi-layer dependent snapshot chain like the following.

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 an independent disk image file, which does not depend on other images.

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

Suppose we have created the following directories in the user's working directory.

kvm        根目录
  iso       存放光盘镜像
  base      存放BackingFile
  share     存放共享磁盘镜像,用户存储测试工具、驱动等
  image     存放测试机的磁盘镜像
  xml       存放导出的虚拟机XML配置文件

Let's use an example to show you how to quickly create a test virtual machine.

  1. Follow the previous article to create a Win10 virtual machine;
  2. In the virtual machine, install the test software used in the work;
  3. Use the following command to create a new shared tool disk;

    qemu-img create -f qcow2 -o cluster_size=2M kvm/share/tools.qcow2 10G
  4. Mount the shared disk to the virtual machine, copy tools and files to the disk;
  5. To remove the virtual machine, in the confirmation dialog box, please choose not to delete the related disk files;
  6. Move the original virtual machine main disk file to the base mirror directory, such as kvm/base/windows/win10/x64-pro-zh_cn.qcow2.
  7. Execute the following command to create a new virtual machine disk with the above basic 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 newly created virtual machine and shared disk.

In addition to the qemu-manager software using the graphical interface, a command line method is also provided here, which can be used in the code of the test platform.

  1. Export 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
    • memory and currentMemory
    • mac address
    • The source file of the first disk
  2. In the Element of the first disk, add the following BackingFile related content:
<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 the page VNC to access the virtual machine desktop, find the graphics element of XML and modify it to the following content.
<graphics type="vnc" port="-1" autoport="yes" listen="0.0.0.0" passwd="P2ssw0rd">
  <listen type="address" address="0.0.0.0"/>
</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, and 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 GO language to implement related functions of virtual machine management based on the libvirt interface. This open source project aims to provide you with an on-demand test environment management platform based on KVM virtual machines and Docker containers. For details, please refer to the website https: //github.com/easysoft/zagent .

Commonly used commands:

# 查看虚拟机信息
qemu-img info --backing-chain kvm/image/test-win10-x64-pro-zh_cn-01.qcow2

# 修改虚拟机磁盘大小
qemu-img resize x64-pro-zh_cn.qcow2 +10G

# 查看虚拟机里列表
virsh list --all

# 查看虚拟机VNC端口
virsh vncdisplay win10-test

# 导出虚拟机XML配置文件
virsh dumpxml win10-test > win10-test.xml

# 创建虚拟机磁盘镜像
qemu-img create -f qcow2 -o cluster_size=2M,backing_file=base.qcow2 win10-test.qcow2 40G

# 转换虚拟机镜像
qemu-img convert -O qcow2 vm02.qcow2 vm-templ.img

# 定义、取消定义,启动、停止虚拟机
virsh define win10-test.xml
virsh start win10-test
virsh destroy win10-test
virsh undefine win10-test

thematic catalog


陈哥聊测试
158 声望3.3k 粉丝

资深敏捷测试顾问,国内知名项目管理软件禅道团队成员。