Virtualization with KVM
Virtualization has made a lot of progress during the last decade, primarily due to the development of myriad open-source virtual machine hypervisors. This progress has almost eliminated the barriers between operating systems and dramatically increased utilization of powerful servers, bringing immediate benefit to companies. Up until recently, the focus always has been on software-emulated virtualization. Two of the most common approaches to software-emulated virtualization are full virtualization and paravirtualization. In full virtualization, a layer, commonly called the hypervisor or the virtual machine monitor, exists between the virtualized operating systems and the hardware. This layer multiplexes the system resources between competing operating system instances. Paravirtualization is different in that the hypervisor operates in a more cooperative fashion, because each guest operating system is aware that it is running in a virtualized environment, so each cooperates with the hypervisor to virtualize the underlying hardware.
Both approaches have advantages and disadvantages. The primary advantage of the paravirtualization approach is that it allows the fastest possible software-based virtualization, at the cost of not supporting proprietary operating systems. Full virtualization approaches, of course, do not have this limitation; however, full virtualization hypervisors are very complex pieces of software. VMware, the commercial virtualization solution, is an example of full virtualization. Paravirtualization is provided by Xen, User-Mode Linux (UML) and others.
With the introduction of hardware-based virtualization, these lines have blurred. With the advent of Intel's VT and AMD's SVM, writing a hypervisor has become significantly easier, and it now is possible to enjoy the benefits of full virtualization while keeping the hypervisor's complexity at a minimum.
Xen, the classic paravirtualization engine, now supports fully virtualized MS Windows, with the help of hardware-based virtualization. KVM is a relatively new and simple, yet powerful, virtualization engine, which has found its way into the Linux kernel, giving the Linux kernel native virtualization capabilities. Because KVM uses hardware-based virtualization, it does not require modified guest operating systems, and thus, it can support any platform from within Linux, given that it is deployed on a supported processor.
KVM is a unique hypervisor. The KVM developers, instead of creating major portions of an operating system kernel themselves, as other hypervisors have done, devised a method that turned the Linux kernel itself into a hypervisor. This was achieved through a minimally intrusive method by developing KVM as kernel module. Integrating the hypervisor capabilities into a host Linux kernel as a loadable module can simplify management and improve performance in virtualized environments. This probably was the main reason for developers to add KVM to the Linux kernel.
This approach has numerous advantages. By adding virtualization capabilities to a standard Linux kernel, the virtualized environment can benefit from all the ongoing work on the Linux kernel itself. Under this model, every virtual machine is a regular Linux process, scheduled by the standard Linux scheduler. Traditionally, a normal Linux process has two modes of execution: kernel and user. The user mode is the default mode for applications, and an application goes into kernel mode when it requires some service from the kernel, such as writing to the hard disk. KVM adds a third mode, the guest mode. Guest mode processes are processes that are run from within the virtual machine. The guest mode, just like the normal mode (non-virtualized instance), has its own kernel and user-space variations. Normal kill and ps commands work on guest modes. From the non-virtualized instance, a KVM virtual machine is shown as a normal process, and it can be killed just like any other process. KVM makes use of hardware virtualization to virtualize processor states, and memory management for the virtual machine is handled from within the kernel. I/O in the current version is handled in user space, primarily through QEMU.
A typical KVM installation consists of the following components:
A device driver for managing the virtualization hardware; this driver exposes its capabilities via a character device /dev/kvm.
A user-space component for emulating PC hardware; currently, this is handled in the user space and is a lightly modified QEMU process.
The I/O model is directly derived from QEMU's, with support for copy-on-write disk images and other QEMU features.
How do you find out whether your system will run KVM? First, you need a processor that supports virtualization. For a more detailed list, have a look at wiki.xensource.com/xenwiki/HVM_Compatible_Processors. Additionally, you can check /proc/cpuinfo, and if you see vmx or smx in the cpu flags field, your system supports KVM.
KVM is a fairly recent project compared with its competitors. In an interview with Avi Kivity, the main developer, he compared KVM with alternative solutions:
In many ways, VMware is a ground-breaking technology. VMware manages to fully virtualize the notoriously complex x86 architecture using software techniques only, and to achieve very good performance and stability. As a result, VMware is a very large and complex piece of software. KVM, on the other hand, relies on the new hardware virtualization technologies that have appeared recently. As such, it is very small (about 10,000 lines) and relatively simple. Another big difference is that VMware is proprietary, while KVM is open source.
Xen is a fairly large project, providing both paravirtualization and full virtualization. It is designed as a standalone kernel, which only requires Linux to perform I/O. This makes it rather large, as it has its own scheduler, memory manager, timer handling and machine initialization.
KVM, in contrast, uses the standard Linux scheduler, memory management and other services. This allows the KVM developers to concentrate on virtualization, building on the core kernel instead of replacing it.
QEMU is a user-space emulator. It is a fairly amazing project, emulating a variety of guest processors on several host processors, with fairly decent performance. However, the user-space architecture does not allow it to approach native speeds without a kernel accelerator. KVM recognizes the utility of QEMU by using it for I/O hardware emulation. Although KVM is not tied to any particular user space, the QEMU code was too good not to use—so we used it.
KVM, however, is not perfect due to its newness; it has some limitations including the following:
At the time of this writing, KVM supports only Intel and AMD virtualization, whereas Xen supports IBM PowerPC and Itanium as well.
SMP support for hosts is lacking in the current release.
However, the project is continuing at a rapid pace, and according to Avi Kivity, KVM already is further ahead than Xen in some areas and surely will catch up in other areas in the future.
How Virtualization Works
Platform virtualization is an old technology; however, in recent years, the hardware and operating systems have matured to the point of making the promise of virtualization a reality. The most fundamental part of virtualization is the hypervisor. The hypervisor acts as a layer between the virtualized guest operating system and the real hardware. In some cases, the hypervisor is an operating system, such as with Xen; in other cases, it's user-level software, such as VMware. The virtualized guest operating system, or the virtualized instance, is an isolated operating system that views the underlying hardware platform as belonging to it. But, in reality, the hypervisor provides it with this illusion.
Processor Support for Virtualization
Due to the resurgence of interest in virtualization technology, microprocessor manufacturers have updated their processors to have native support for virtualization. Doing so allows the processor to support a hypervisor directly and simplifies the task of writing hypervisors, as is the case with KVM. The processor manages the processor states for the host and guest operating systems, and it also manages the I/O and interrupts on behalf of the virtualized operating system.
KVM has been added to many distribution-specific repositories, including OpenSUSE/SUSE, Fedora 7 (which comes with KVM built-in), Debian and Ubuntu (Feisty).
For other distributions, you need to download a kernel of version 2.6.20 and above. When compiling a custom kernel, select Device Drivers→Virtualization when configuring the kernel, and enable support for hardware-based virtualization. You also can get the KVM module along with the required user-space utilities from sourceforge.net/project/showfiles.php?group_id=180599.
I have installed the OpenSUSE packages; hence, filenames used in the examples in this article may be different from those in your release.
Using the compiled kernel with virtualization support enabled, the next step is to create a disk image for the guest operating system. You do so with qemu-img, as shown below. Note that the size of the image is 6GB, but using QEMU's copy-on-write format (qcow), the file will grow as needed, instead of occupying the full 6GB:
# qemu-img create -f qcow image.img 6G
Instantiation of a new guest operating system is provided by a utility called qemu-kvm. This utility works with the kvm module, using /dev/kvm to load a guest, associate it with the virtual disk (a regular QEMU qcow file in the host operating system), and then boot it. In some distributions this utility may be called kvm.
With your virtual disk created, load the guest operating system into it. The following example assumes that the guest operating system is on a CD-ROM. In addition to populating the virtual disk with the CD-ROM ISO image, you must boot the image when it's done:
# qemu-kvm -m 384 -cdrom guestos.iso -hda image.img -boot d
The I/O in the current release of KVM is handled by QEMU, so let's look at some important QEMU switches:
-m: memory in terms of megabytes.
-cdrom: the file, ideally an ISO image, acts as a CD-ROM drive to the VM. If no cdrom switch is specified, the ide1 master acts as the CD-ROM.
-hda: points to a QEMU copy-on-write image file. For more hard disks we could specify:
#qemu-kvm -m 384 -hda vmdisk1.img -hdb vmdisk2.img -hdc vmdisk3.img
-boot: allows us to customize the boot options; the -d switch boots from the CD-ROM.
The default command starts the guest OS in a subwindow, but you can start in full-screen mode, by passing the following switch:
Additionally, KVM allows low-level control over the hardware of the virtualized environment. You can redirect serial, parallel and USB ports to specific devices by specifying the appropriate switches. Sound in the VM is supported as well, and you can pass your sound card to the VM via the -soundhw switch to enable sound.
The following are some keyboard shortcuts:
Ctrl-Alt-F: toggle full screen.
Ctrl-Alt-N: switch to virtual console N.
Ctrl-Alt: toggle mouse and keyboard.
With the introduction of KVM into the Linux kernel, future Linux distributions will have built-in support for virtualization, giving them an edge over other operating systems. There will be no need for any dual-boot installation in the future, because all the applications you require could be run directly from the Linux desktop. KVM is just one more of the many existing open-source hypervisors, reaffirming that open source has been instrumental to the progress of virtualization technology.
Irfan Habib is student of software engineering at the National University of Sciences and Technology, Pakistan. He loves to code in Python, which he finds to be one of the most productive languages ever developed.