Concept Introduce

Qemu

Qemu is an emulator that simulates the CPU and other hardware to the Guest OS, which thinks it is dealing directly with the hardware, but is actually dealing with the hardware simulated by Qemu, which translates the instructions to the real hardware.

Since all instructions have to pass through Qemu, performance is poor.

Figure 1:Qemu Architecture
From:KVM-Qemu-Libvirt三者之间的关系

KVM

KVM is a module for the linux kernel, which requires CPU support. Using hardware-assisted virtualization technologies Intel-VT, AMD-V, memory-related such as Intel’s EPT and AMD’s RVI technologies, Guest OS CPU instructions do not have to be translated by Qemu and run directly, greatly improving speed. KVM exposes the interface through /dev/kvm, and user-state programs can access this interface through the ioctl function. See the following pseudo-code:

  1. open("/dev/kvm")
  2. ioctl(KVM_CREATE_VM)
  3. ioctl(KVM_CREATE_VCPU)
  4. for (;;) {
  5. ioctl(KVM_RUN)
  6. switch (exit_reason) {
  7. case KVM_EXIT_IO:
  8. case KVM_EXIT_HLT:
  9. }
  10. }

The KVM kernel module itself can only provide CPU and memory virtualization, so it must be combined with QEMU to form a completed virtualization technology, which is called qemu-kvm.

qemu-kvm

Qemu integrates KVM, calls the /dev/kvm interface via ioctl, and leaves the CPU instructions to the kernel module. kvm is responsible for cpu virtualization + memory virtualization, which virtualizes cpu and memory, but kvm cannot emulate other devices. qemu emulates IO devices (NICs, disks, etc.), and kvm, together with qemu, enables server virtualization in the true sense. It is called qemu-kvm because it uses both of these things.

Qemu emulates other hardware, such as Network, Disk, which also affects the performance of these devices, so the pass through semi-virtualized devices virtio_blk, virtio_net are created to improve the performance of the devices.

Figure 2:Qemu-KVM Architecture
From:UCSB CS290B

Libvirt

Why Libvirt?

What does Libvirt provide?

Currently, libvirt has become the most widely used tool and API for managing various virtual machines, and some common virtual machine management tools (e.g. virsh, virt-install, virt-manager, etc.) and cloud computing framework platforms (e.g. OpenStack, OpenNebula, Eucalyptus, etc.) are available. Eucalyptus, etc.) all use libvirt’s APIs at the bottom.

Figure 3:Relation between libvirt and KVM
From: Libvirt Wiki

Operations

Install and config in Arch Linux

  1. [root@liqiang.io]# yay -Sy archlinux-keyring
  2. [root@liqiang.io]# yay -Sy qemu virt-manager virt-viewer dnsmasq vde2 bridge-utils openbsd-netcat
  3. [root@liqiang.io]# yay -Sy ebtables iptables
  4. [root@liqiang.io]# yay -Sy libguestfs
  5. [root@liqiang.io]# sudo systemctl enable libvirtd.service
  6. [root@liqiang.io]# sudo systemctl start libvirtd.service

This will install all the software needed, the next step is to configure it:

  1. [root@liqiang.io]# cat /etc/libvirt/libvirtd.conf
  2. ... ...
  3. unix_sock_group = "libvirt"
  4. unix_sock_rw_perms = "0770"
  5. [root@liqiang.io]# sudo usermod -a -G libvirt $(whoami)
  6. [root@liqiang.io]# sudo systemctl restart libvirtd.service

virsh Operation

Configure Network

  1. [root@liqiang.io]# sudo virsh net-define /etc/libvirt/qemu/networks/default.xml
  2. [root@liqiang.io]# sudo virsh net-start default
  3. [root@liqiang.io]# sudo virsh net-autostart default # run at system start
  4. [root@liqiang.io]#

Configure console connection

  1. [root@liqiang.io]# sudo systemctl enable serial-getty@ttyS0.service
  2. [root@liqiang.io]# sudo systemctl start serial-getty@ttyS0.service
  3. [root@liqiang.io]#

Create VM

  1. [root@liqiang.io]# sudo virt-install --name=testvm-00 \
  2. --os-type=Linux \
  3. --os-variant=centos7.0 \
  4. --vcpu=4 \
  5. --ram=4096 \
  6. --disk path=/home/liuliqiang/data/kvm/images/testvm00.img,size=30 \
  7. --graphics spice \
  8. --location=/home/liuliqiang/data/kvm/isos/CentOS-7-x86_64-DVD-2009.iso \
  9. --network bridge:virbr0

Enter VM

  1. [root@liqiang.io]# virsh console zhangsan

Shutdown VM

  1. [root@liqiang.io]# virsh shutdown VM_NAME
  2. [root@liqiang.io]# virsh shutdown --domain VM_NAME
  3. [root@liqiang.io]# virsh destroy VM_NAME # force stop
  4. [root@liqiang.io]# virsh destroy --domain VM_NAME # force stop
  5. [root@liqiang.io]# virsh undefine --domain VM_NAME # remove vm

View VM info

  1. [root@liqiang.io]# virsh list --all
  2. Id Name State
  3. ----------------------------
  4. 1 200 running
  5. 2 envoy180 running
  6. ... ...
  7. - base-f-vm shut off
  8. [root@liqiang.io]#

Ref