Virtualization in Debian Etch (Part 2 - KVM)


In Part 1, we have learned to setup virtual server running under UML on Debian Etch. In this Part 2 of virtualization articles, we will explore KVM. KVM stands for kernel-based virtual machine, which is now available in 2.6.20 mainline kernel. But KVM is known to be backward compatible to 2.6.18 of Debian Etch. So, it is still possible to run kvm on Debian Etch without upgrading the kernel to 2.6.20.

Follow the steps in this article, you will know:

  1. the hardware requirement for KVM
  2. how to build KVM kernel module and install userspace component on Debian Etch host OS.
  3. how to install Debian Etch on OS image
  4. how to start and run guest OS under the KVM
  5. the tips for running KVM guest OS on a host with single NIC interface and bridge network
  6. the performance of KVM - we compare the build time of Voyage Linux kernel image on native machine, UML and KVM

1. Prerequisite: Virtualization Hardware

For the best performance the processor must support hardware virtualization, provided by AMD's SVM capability and Intel's VT (Pacifica). To find out if your processor has the native hardware virtual machine support (HVM), run this command in a shell:
egrep '^flags.*(vmx|svm)' /proc/cpuinfo 
If it prints anything, the processor provides hardware virtualization support and is suitable for use with KVM. XenSource also lists all compatible processors that supports hardware virtual machine. Without hardware support, KVM falls back to the considerably slower QEMU-based emulation. My new barebone server has a Athlon 64 X2 4400+ processor, which is Rev. G and fabricated in 65nm, gives adequate capability for HVM.

2. Building KVM kernel module and userspace program

In my previous blog post, I have shown the steps for building kvm module and installing kvm userspace component on Debian Etch. Please don't mind if I repeat again here:

With Debian package search function, you can find the kvm (userspace component) and kvm-source (module source) package. Unfortunately, it is in unstable repository, but you can still download both the module source and userspace component manually from the repository directly:

# wget
# wget
And install it with:
# dpkg -i kvm*.deb
You may encounter dependency complaints from dpkg. Install the required packages manually to satisfy the dependency. In my case, it requires bridge-utils (your mileage may vary):
# apt-get install bridge-utils
# dpkg -i kvm*.deb
Now, both kvm and kvm-source are installed.

Next, using module assistant to build kvm kernel module, that is:

# m-a a-i kvm
Right now, you have kvm module for your kernel. Check here:
# modinfo kvm-intel kvm-amd
filename:       /lib/modules/2.6.18-4-k7/misc/kvm-intel.ko
author:         Qumranet
license:        GPL
vermagic:       2.6.18-4-k7 SMP mod_unload K7 REGPARM gcc-4.1
depends:        kvm
filename:       /lib/modules/2.6.18-4-k7/misc/kvm-amd.ko
author:         Qumranet
license:        GPL
vermagic:       2.6.18-4-k7 SMP mod_unload K7 REGPARM gcc-4.1
depends:        kvm
Since, we have k8 cpu, so load kvm-amd module:
# modprobe kvm-amd
And then verify with lsmod:
# lsmod| grep kvm
kvm_amd                15112  0
kvm                    57572  1 kvm_amd
The kernel module part is done. Next, we will make virtual machines run under KVM. I downloaded Voyage Linux 0.3.1 Live CD and test KVM:
# kvm \
  -cdrom voyage-0.3.1.iso \
  -boot d \
  -m 128
This mounts voyage-0.3.1.iso to a CD-ROM ("-cdrom" option), starts KVM with 128MB Ram ("-m 128" option) and boots from cdrom ("-boot d" option). Once after you start kvm command in a terminal on a Gnome/X-Window Desktop, another terminal window pops up and shows the usual boot message. If it boots successfully, you should see the voyage login prompt.

3. Preparing Debian Etch to OS image

As you may know the above command only boot cdrom and the kvm guest OS only has a CD-ROM device. There is no disk device or disk image attached to it. To install Debian Etch as a KVM guest OS, you will need a disk image. Since kvm is dervied from qemu, you will use qemu-img command to create the disk image. If you didn't install qemu before then you should install it first.
# apt-get install qemu
# qemu-img create punknix-kvm.qcow 4G
Formating 'punknix-kvm.qcow', fmt=raw, size=4194304 kB
So we create a 4GB sparse disk image to be used for Debian Etch installation. Increase the disk image size when appropriate. 4GB should be large enough for base installation without Gnome Desktop.

4. Preparing network script for KVM guest OS

In Debian, kvm uses /etc/kvm/kvm-ifup script to initialize the network for the virtual machine on the host side when starting a virtual machine. /etc/kvm/kvm-ifup looks like this:

switch=$(/sbin/ip route list | awk '/^default / { print $NF }')
/sbin/ifconfig $1 up
/usr/sbin/brctl addif ${switch} $1
Of course, this assumes the host system uses a bridge for default gateway. You may refer to /etc/qemu-if for another example:
sudo -p "Password for $0:" /sbin/ifconfig $1
To learn more about setting up the network, please refer to QEMU Documentation. In this article, we will use the bridge script.

Now, download business card CD and install Debian:

# kvm -hda ./punknix-kvm.qcow \
  -cdrom ./debian-40r0-i386-businesscard.iso \
  -m 128 \
  -boot d \
  -net nic -net tap,script=/etc/kvm/kvm-ifup
Using the auto-layout, we install Debian on a ~3.8G root partition and allocate 240MB for swap. The installation should work the same as you run on real machine except that it is slower.

4. Start and run KVM OS image

After installing Debian successfully, you should shutdown virtual machine instead of reboot as you will need to tell KVM to boot from disk image now:
# kvm -hda ./punknix-kvm.qcow \
  -m 128 \
  -boot c \
  -net nic -net tap,script=/etc/kvm/kvm-ifup 
If you want to run virtual machine as a daemon, you can use "-nographic" and "-daemonize" in kvm argument:
# kvm -hda ./punknix-kvm.qcow \
  -m 128 \
  -boot c \
  -net nic -net tap,script=/etc/kvm/kvm-ifup \
  -nographic -daemonize

6. Performance

Do you think KVM is faster than UML? Unfortunately, kernel build time does not show that KVM is faster:

Host OSAthlon 64 X2 4400+
Memory: 2GB Ram
UMLMemory: 128MB (use tmpfs)
Swap: 256MB
Disk Image: 4GB
KVMMemory: 128MB
Swap: 240MB
QEMU Disk Image: 4GB

If you compare UML and KVM, you will not be happy about the KVM performance. But if you compare KVM with QEMU, you can convinced yourself KVM is MUCH faster. Since KVM and QEMU is inter-changable, you can use same command line arguments to run the guest OS on QEMU and "feel" the difference.

Although KVM is in the mainline 2.6.20, it does not mean that it reaches a finished state, and it may see significant changes in the near future. The version in Debian unstable repository is KVM-18, while the most recent one is KVM-23. Referring to KernelTrap news post, there is a announcement in LKML that KVM-22 is significant in performance improvement. In the coming future, we may re-examine KVM again to see how it improves.

7. References

The following addition links are the reading reference for writing up this article:
[1] -
[2] -
[3] -