KVM Hypervisor Integrated in Linux Kernel 2.6.20

Kernel-based Virtual Machine (KVM) has been around for sometime and now is integrated with the linux kernel as of version 2.6.20. This new marriage makes it possible to run other operating systems, such as Windows XP and others within your Linux system. KVM takes advantages of the hardware support provided by the Intel-VT and the AMD-V processor.

Under KVM’s model, every virtual machine is a regular Linux process scheduled by the standard Linux scheduler. A normal Linux process has two modes of execution: kernel and user. KVM adds a “guest” mode which has its own kernel and user modes.

Running Windows XP over KVM/Linux compares favorably to the native execution of Windows XP. In comparison to its predecessor QEMU, KVM clearly out performs it in most cases more than 10:1 according to Linux Insight. However QEMU and KQEMU coupled together will still give KVM a run for the money without the need for virtualization support built into the processor.


The KVM Hypervisor

The KVM code simply turns a Linux kernel into a hypervisor simply by loading a kernel module. The kernel module exports a device called /dev/kvm, which enables a guest mode of the kernel (in addition to the traditional kernel and user modes). With /dev/kvm, a VM has its own address space separate from that of the kernel or any other VM that’s running. Devices in the device tree (/dev) are common to all user-space processes. But /dev/kvm is different in that each process sees a different device map (to support isolation of the VMs). With the kernel acting as a hypervisor, you can then start other operating systems, such as another Linux kernel, Windows, or another operating system.


Using KVM

Using KVM is really quite simple assuming that you have the hardware to support it, and all of the necessary KVM packages installed on your system. Enter this command at a shell prompt If you need to check to see if your processor supports hardware virtualization (works with bash, but should also work with other shells):

vlab-host01:~ $ egrep ‘(vmx|svm)’ /proc/cpuinfo
flags : fpu vme de pse tsc msr pae mce cx8 … vmx
flags : fpu vme de pse tsc msr pae mce cx8 … vmx
(some of the output is truncated)

There are two sets of flags because the system has two processors (which isn’t uncommon on newer systems). You would be looking for the “vmx” flag for an Intel-VT and the “svm” flag for an AMD-V processor.

With the KVM module installed, you can now start your guest operating system in user-space. For starters you could test KVM by initiating a KVM session using a bootable CDROM or DVD (ISO or physical media). If you need a permanent virtual server then first you would have to create a disk image and then start a KVM session with your installable media attached to the session. Here is the quickest way to do that:

vlab-host01:/vm/winxp $ qemu-img create -f qcow virtual-disk.img 5G
Formatting ‘virtual-disk.img’, fmt=qcow, size=5242880 kB

vlab-host01:/vm/winxp $ ls -l virtual-disk.img
-rw-r–r– 1 root root 20528 Dec 10 22:19 virtual-disk.img


This creates a 5GB disk image in qcow format. This dynamic disk image is a little bit less that 20KB initially when created, but will expand as-needed up to 5GB. You could make an image that starts out at 5GB as well, but this way saves space.

Next you can either create an ISO image of your installation media (generally faster) or you can just use the physical device link to install your guest operating system from.

In linux you can use a command like dd to create an ISO image from a CDROM or DVD:

vlab-host01:/vm/winxp $ dd=if=/dev/cdrom of=winxp-sp2.iso bs=1024K


Make sure that you are in a windowing environment like GNOME or KDE (others work too). Now you can initiate a KVM session like this:

vlab-host01:~ $ kvm -no-acpi -m 512 -smp 2 -cdrom winxp-sp2.iso -hda virtual-disk.img -boot d


In this case, the kvm command initializes a session that has 512MB of memory, attaches our installation image “winxp-sp2.iso” to the cdrom device (/dev/hdc), and the qcow image “virtual-disk.img” to the first hard disk (/dev/hda), boots from the CDROM, emulates 2 processors, and turns off ACPI (still a few bugs).

Viola! Hit enter and your KVM guest will boot up!

No comments: