If you were to ask someone “what is the most popular open source hypervisor” chances are that the answer will be KVM. Indeed KVM (or Kernel-based Virtual Machine) has played a key role in the open source Linux based virtualization environment. However is it really a hypervisor? Moreover, can KVM by itself run virtual machines? We will delve more into such questions in this blog. We will also understand the relationship between KVM and QEMU (Quick EMUlator).
KVM and QEMU - the process ID check
You can use libvirt and the associated Virtual Machine Manager GUI to start a virtual machine. In the GUI, you can choose the ‘Virt Type’ as either KVM or QEMU. I started a VM, once with QEMU as the Virt Type and once with KVM. In both cases, I did a grep of the process ID to see if there was any difference. Note: I used Ubuntu 13.10 for my experiments.
When I started the VM with KVM as the Virt Type the process ID details showed an interesting attribute “accel=kvm” as highlighted below.
When I started the same Virtual Machine with QEMU as the Virt Type, the process ID showed “accel=tcg”.
Note that in both the cases the same binary is executed to start the VM, namely, qemu-system-x86_64. The main difference is the acceleration type.
KVM and QEMU - understanding hardware acceleration
To understand hardware acceleration, we must understand how Virtual Machine CPU works. In real hardware, the Operating System (OS) translates programs into instructions that are executed by the physical CPU. In a virtual machine, the same thing happens. However, the key difference is that the Virtual CPU is actually emulated (or virtualized) by the hypervisor. Therefore, the hypervisor software has to translate the instructions meant for the Virtual CPU and convert it into instructions for the physical CPU. This translation has a big performance overhead.
To minimize this performance overhead, modern processors support virtualization extensions. Intel support a technology called VT-x and the AMD equivalent is AMD-V. Using these technologies, a slice of physical CPU can be directly mapped to the Virtual CPU. **Hence the instructions meant for the Virtual CPU can be directly executed the physical CPU slice. **
KVM is the Linux kernel module that enables this mapping of physical CPU to Virtual CPU. This mapping provides the hardware acceleration for Virtual Machine and boosts its performance. Moreover, QEMU uses this acceleration when Virt Type is chosen as KVM.
Then what is TCG? If your server CPU does not support virtualization extension, then it is the job of the emulator (or hypervisor) to execute the Virtual CPU instruction using translation. QEMU uses TCG or Tiny Code Generator to optimally _translate and execute _the Virtual CPU instructions on the physical CPU.
KVM and QEMU - Type 1 or Type 2 hypervisor
QEMU by itself is a Type-2 hypervisor. It intercepts the instructions meant for Virtual CPU and uses the host operating system to get those instructions executed on the physical CPU. When QEMU uses KVM for hardware acceleration, the combination becomes a Type-1 hypervisor. This difference is quite clear from the description on the QEMU website.
KVM and QEMU - the x86 dependency
Since KVM is really a driver for the physical CPU capabilities, it is very tightly associated with the CPU architecture (the x86 architecture). This means that the benefits of hardware acceleration will be available only if the Virtual Machine CPU also uses the same architecture (x86).
If a VM needs to run Power PC CPU but the hypervisor server has an Intel CPU, then KVM will not work. You must use QEMU as the Virt Type and live with the performance overhead.
KVM and QEMU - the conclusion
Based on the discussion above, it is quite clear that QEMU plays a very critical role in Linux based Open Source virtualization solutions. For all practical applications, QEMU needs KVM’s performance boost. However, it is clear that **KVM by itself cannot provide the complete virtualization solution. It needs QEMU.