Learn the fundamentals of kvm/qemu
I am starting this series of articles in the field of different virtualization technique and cloud computing, starting with QEMU/KVM. We will go deeper and deeper as topic demands.
Let’s start with what’s difference between this Hypervisor and VMM.
Hypervisor or VMM ???
Hypervisor is a piece of software or firmware that job is to handle creation and management of virtual machine. It’s of 2 types, Type1 and Type2 Hypervisor. Type1 hypervisor(QEMU/KVM) , which runs directly on host hardware to support virtualization whereas Type2(QEMU can also act as Type2) hypervisor runs on Host Operating System.
VMM is basically in a much broader set of virtualization in general, its not only specific to virtual machine but any way which provide isolation in a system like linux containers or firecracker.
So we can say VMM is kind of superset where Hypervisor is a part or subset of it. Generally these terms are used interchangeably.
How then KVM and QEMU acts as a hypervisor ??
KVM and QEMU comes under full virtualization technique, where guest operating systems run without modifications, believing they are running on real hardware and allows efficient and seamless execution of virtual machines on the host system.
KVM is a kernel module that provides the host hardware virtualization feature to be exposed to VMs. It allows kernel to act as hypervisor, enable it to create and manage virtual machine.
QEMU is a versatile open-source emulator that provides hardware emulation and virtualization capabilities. It emulates system hardware components like network interfaces, disks and more.
Host OS sees QEMU as regular multi threaded process and QEMU then talks to KVM via open/ioctl system calls and ioctl on FDs (file descriptor) are used to talk to VMs.
What’s this CPU in VMX mode ?
CPU with VMX mode, when invoked, cpu needs to switch context from host os to guest os. when this switch happens, context is stored in VMCB data structure (Virtual Machine Control Block) also called as virtual machine control structure. It’s a common areas hared between KVM and guest OS.
How this I/O devices are then handled ?
For handling any I/O devices, VM generally raises an interrupt and exit back to KVM for handling as they are in special ring 0 privilege level. All these that when to interrupt are available and configured by KVM in VMCS.
we will talk about all these in detail once we go ahead in the series.
QEMU and KVM is used together in for virtualization in the industry.
QEMU can leverage KVM in executing guest code directly on host CPU rather than emulating full hardware, so increasing efficiency and performance. It also serves as the front-end interface for managing VMs by providing command-line options and management interfaces for creating, configuring and controlling VMs.
Okay so How do we interact with this now ?
We can either directly interact it with command line tools or by using a higher level tool like Libvirt.
Libvirt ??
You might have heared about it, its an VMM which provides a higher-level management interface for KVM/QEMU and other virtualization technologies. It abstracts the details of the underlying hypervisor, making it easier to manage virtual machines and host resources.
With libvirt, you can perform tasks such as creating, configuring, starting, stopping, and monitoring virtual machines using a unified set of commands and APIs.
Conclusion
QEMU/KVM when used together can be referred to be as hypervisor, although they serve different roles in virtualization stack, together they act as a complete virtualization solution.
KVM acting as core hypervisor component by proving hardware assistance and QEMU compliments him by emulating hardware devices.
Libvirt as higher management software makes us to work with any kind of hypervisor under-hood.