QEMU/KVM Virtualization
QEMU/KVM is currently the most popular virtualization technology. It is based on the kvm module provided by the Linux kernel, with a streamlined structure, low performance loss, and is open source and free (compared to the paid vmware), so it has become the preferred virtualization solution for most enterprises.
The current virtualization solutions of major cloud vendors are basically using KVM technology for new server instances. Even AWS, which started first and has been using Xen heavily, has switched to Nitro virtualization technology based on KVM customization since EC2 C5.
However, KVM, as an enterprise-class underlying virtualization technology, is not deeply optimized for desktop use, so if you want to use it as desktop virtualization software and replace VirtualBox/VMware, it is somewhat difficult.
This article is a summary document of my personal learning about KVM, with the goal of using KVM as desktop virtualization software.
I. Installing QUEU/KVM
The QEMU/KVM environment requires the installation of a number of components, each of which has its own role to play.
- qemu: emulates various types of input and output devices (network cards, disks, USB ports, etc.)
- qemu uses kvm at the bottom to emulate CPU and RAM, which is much faster than software emulation.
- libvirt: provides simple and unified tools and APIs for managing virtual machines, shielding the underlying complex structure. (supports qemu-kvm/virtualbox/vmware)
- ovmf: Enables UEFI support for virtual machines
- virt-manager: GUI interface for managing virtual machines (can manage remote kvm hosts).
- virt-viewer: Interact directly with the virtual machine through the GUI interface (can manage remote kvm hosts).
- dnsmasq vde2 bridge-utils openbsd-netcat: Network-related component that provides virtual networking features such as Ethernet virtualization, network bridging, and NAT networking.
- dnsmasq provides DHCP and DNS resolution for NAT virtual networks.
- vde2: Ethernet virtualization
- bridge-utils: As the name implies, provides network bridging related tools.
- openbsd-netcat: The Swiss Army knife of TCP/IP.
Installation commands.
|
|
Once the installation is complete, you can’t use it directly yet, you need to do some extra work. Please continue to the next step.
1. libguestfs - Virtual Machine Disk Image Processing Tool
libguestfs is a virtual machine disk image processing tool that can be used to directly modify/view/virtual machine images, convert image formats, etc.
It provides the following list of commands.
virt-df centos.img
: View hard disk usagevirt-ls centos.img /
: List directory filesvirt-copy-out -d domain /etc/passwd /tmp
: Perform file copy in virtual imagevirt-list-filesystems /file/xx.img
: View file system informationvirt-list-partitions /file/xx.img
: View partition informationguestmount -a /file/xx.qcow2(raw/qcow2 are supported) -m /dev/VolGroup/lv_root --rw /mnt
: Mount the partition directly to the hostguestfish
: Interactive shell that runs all the above commands.virt-v2v
: Convert virtual machines of other formats (e.g. ova) into kvm virtual machines.virt-p2v
: Converts a physical machine into a virtual machine.
The above commands may be used during the learning process, it never hurts to install them in advance. The installation commands are as follows.
2. Start QEMU/KVM
Start the libvirtd backend service via systemd.
3. Enable non-root users to use kvm properly
Once qumu/kvm is installed, by default you need root privileges to use it properly. To make it easier to use, first edit the file /etc/libvirt/libvirtd.conf
:
unix_sock_group = "libvirt"
to uncomment this line so that thelibvirt
user group can use unix sockets.unix_sock_rw_perms = "0770"
, uncomment this line so that users can read and write unix sockets.
Then create a new libvirt user group and add the current user to it:
Finally, restart the libvirtd service and it should work fine:
|
|
3. Enabling nested virtualization
If you need to run a virtual machine within a virtual machine (e.g. to test secure container technologies such as katacontainers within a virtual machine), then you need to enable the kernel module kvm_intel to implement nested virtualization.
Verify that nested virtualization is enabled.
Now you should be able to find the virt-manager icon on your system and go inside to use it. The usage of virt-manager is similar to that of virtualbox/vmware workstation, so I won’t go into details here, you should be able to figure it out yourself.
The following is an advanced section that introduces how to manage virtual machine disks and KVM through the command line. If you are still new to kvm, we recommend that you first familiarize yourself with virt-manager through the graphical interface before reading on.
II. Virtual machine disk image management
This requires the use of two tools.
- libguestfs: virtual machine disk image management tool, which has been introduced earlier
- qemu-img: qemu’s disk image management tool, used to create disks, expand and shrink disks, generate disk snapshots, view disk information, convert disk formats, etc.
|
|
1. Importing vmware images
Import the kvm directly from the vmware ova file, this way the image should work directly (the NIC needs to be reconfigured).
|
|
You can also extract the vmdk disk image from ova, convert the vmware vmdk file to qcow2 format, and then import it into kvm (the NIC needs to be reconfigured):
The qcow2 image obtained by directly converting the vmdk file will report errors such as “disk cannot be mounted”. According to the Importing Virtual Machines and disk images - ProxmoxVE Docs, you need to download and install the MergeIDE.zip component from the Internet, and you need to change the hard disk type to IDE before starting the virtual machine in order to solve this problem.
2. import img image
The img image file, the so-called raw format image, also known as bare image, has faster IO speed than qcow2, but is large and does not support advanced features such as snapshots. If you do not pursue IO performance, it is recommended to convert it to qcow2 and then use it.
|
|
III. Virtual Machine Management
Virtual machine management can be done using the command line tool virsh
/ virt-install
or the GUI tool virt-manager
.
The GUI is very foolproof, so I won’t introduce it here, but mainly the command line tool virsh
/ virt-install
.
First, let’s introduce a few concepts in libvirt.
- Domain: Refers to an instance of the operating system running on the virtual machine - a virtual machine, or the configuration used to start the virtual machine.
- Guest OS: The virtual operating system running in the domain.
In most cases, you can interpret the domain
involved in the following commands as a virtual machine.
0. Setting the default URI
virsh
/ virt-install
/ virt-viewer
and a series of libvirt commands, sudo virsh net-list -all will use qemu:///session
as URI to connect to QEMU/KVM by default, only only the root account will use qemu:///system
by default .
On the other hand, the GUI tool virt-manager
will also use qemu:///system
to connect to QEMU/KVM by default (same as the root account)
qemu:///system
is the global qemu environment for the system, while the qemu:///session
environment is isolated by user. Also qemu:///session
does not have a default network
, which can cause problems when creating virtual machines.
Therefore, you need to change the default URI to qemu:///system
or you will definitely get screwed:
|
|
1. Virtual Machine Network
After the installation of qemu-kvm is complete, a default
network is created by default in the qemu:///system
environment, while qemu:///session
does not provide a default network and needs to be created manually.
We usually use the qemu:///system
environment just fine, you can use the following method to view and start the default network so that it will be available when you create the virtual machine later.
|
|
You can also create a new VM network, which requires writing the xml configuration for the network manually, and then creating it with virsh net-define --file my-network.xml
, which I won’t go into detail about here because I won’t use it for a while…
2. Create the virtual machine - virt-intall
|
|
The -os-variant
is used to set the OS-related optimization configuration, which is set by the official documentation highly recommended, and its optional parameters can be viewed via osinfo-query os
.
3. virtual machine management - virsh
Once the virtual machine has been created, you can use virsh to manage the virtual machine.
To view the list of virtual machines.
Use virt-viewer
to log in to the virtual machine terminal using the vnc protocol.
Starting, shutting down, suspending (hibernating), restarting the virtual machine.
Virtual machine snapshot management.
Deleting a virtual machine.
|
|
Migration of virtual machines.
cpu/memory modifications.
Virtual machine monitoring.
|
|
Modify disks, networks and other devices.