KVM ‘Quick’ (n dirty) howto.
Introduction (off-topic)
Recently, I found myself a new job with http://inuits.be. A company that ONLY deals with open source and related stuff. Yes, I’m quite ecstatic about it myself! The first meeting went very well and after a small ‘test’ of my capabilities, I was asked to find out how KVM works and write a howto about it. So that is exactly what I have done… Below, you will find the results of my endeavors.
Introduction
What is KVM?
KVM (for Kernel-based Virtual Machine) is a full virtualization solution for Linux on x86 hardware containing virtualization extensions (Intel VT or AMD-V). It consists of a loadable kernel module, kvm.ko, that provides the core virtualization infrastructure and a processor specific module, kvm-intel.ko or kvm-amd.ko. KVM also requires a modified QEMU although work is underway to get the required changes upstream.
Using KVM, one can run multiple virtual machines running unmodified Linux or Windows images. Each virtual machine has private virtualized hardware: a network card, disk, graphics adapter, etc.
The kernel component of KVM is included in mainline Linux, as of 2.6.20.
KVM is open source software.
Source: http://linux-kvm.org.
So what does this mean? We don’t have to software emulate the system to run our virtual machines in and speed up virtualization dramatically. As you can read, we need a kernel module to get things working so we will start off with inspecting and adjusting our kernel.
Installation
Preparing the kernel
Before modifying our kernel, it might be a good idea to check if we don’t have all we need ‘as it is’. You can do this quickly by running:
$ gunzip -c /proc/config.gz | grep KVM
which should result in the following output:
CONFIG_KVM=m CONFIG_KVM_INTEL=m # CONFIG_KVM_AMD is not set
Note: I’ve cut out the unimportant parts for now + mind the INTEL / AMD difference… but I’m sure you’ll figure that out.
You need at least CONFIG_KVM=y (or m if configured as module) and either CONFIG_KVM_INTEL or CONFIG_KVM_AMD. If you have kvm already enabled: make sure you have loaded the modules (if compiled as module) and skip to the next section.
If it is not enabled, configure your kernel to do so:
$ cd /usr/src/linux/
$ make menuconfig
[*] Virtualization --->
<M> Kernel-based Virtual Machine (KVM) support
&t;M> KVM for Intel processors support
< > KVM for AMD processors support
...
I picked modules to make sure I don’t have to reboot before getting started. And here is a one liner for the lazy:
$ sed -i -e 's/^# CONFIG_KVM is not set$/CONFIG_KVM=m/' \
-e 's/^# CONFIG_KVM_INTEL is not set$/CONFIG_KVM_INTEL=m/' /usr/src/linux/.config
While we are at it, don’t forget to add networking support if you plan on using it later. If you are installing a windows guest OS, remember that the safest windows is one without networking ☺.
Enable Universal TUN/TAP device driver support
Device Drivers --->
[*] Network device support --->
<M> Universal TUN/TAP device driver support
or short (in .config)
CONFIG_TUN=m
Loading required modules
$ modprobe -v kvm $ modprobe -v kvm-intel $ modprobe -v tun // networking support
At this point, you should check dmesg for any error messages. On my laptop, I found out the hard way that virtualization is disabled in the bios (kvm: disabled by bios)
Installing qemu
I installed qemu-kvm-0.12.2-r1 on my Gentoo box. I did had to unkey this package since it is still masked for now. Depending on your distro, you might have to find binary packages somewhere. Also: you should also be able to use the regular qemu with kvm from version 0.10.0 and up.
If you are not sure what version of qemu to pick, read this article: http://stateless.geek.nz/2009/10/16/kvm-vs-qemu-kvm-vs-kvm-kmod/
Gentoo specific installation
Before actually merging qemu, make sure to configure that targets qemu must be compiled for. Failing to do so will select them all and this takes a _lot_ longer.
$ echo 'QEMU_SOFTMMU_TARGETS="i386 x86_64"' >> /etc/make.conf $ echo 'QEMU_USER_TARGETS="i386 x86_64"' >> /etc/make.conf $ emerge -va qemu-kvm
If you enabled a lot (all?) architectures, go grab a bite, it’s gonna take a while!
Giving users permission to use KVM
On gentoo, this mechanism is provided by the kernel. A new group is created (called kvm) and a udev file is added to the rules database to make sure permissions are properly set up. You should also add the user to the kvm group and re-login if you want to start using kvm right away.
$ groupadd kvm $ gpasswd -a <username> kvm $ cat /etc/udev/rules.d/65-kvm.rules KERNEL=="kvm", GROUP="kvm", MODE="0660"
Running qemu
Creating the Virtual Disk
Although qemu has plenty of options that might prove useful, only a minimal subset of them is really needed to get going. Before starting the installation of our guest os however, we need to create a virtual disk to use.
$ qemu-img create -f qcow2 disk.img 10G
If you want to reuse this image on windows too, use raw as format (-f), see man qemu-img to know why.
Other qemu-img commands that might be useful (to find out the actual space used on the disk)
$ qemu-img info disk.img
Installing the guest OS
Now you are ready to install your Guest OS on your virtual disk. I have chosen to install PC-BSD. I picked up a dvd on FOSDEM2010 just last week and I certainly wanted to give it a try. Now is the perfect opportunity to both test bsd and complete this howto. Put the disk in your drive and start qemu with the following options:
qemu -cdrom /dev/cdrom disk.img -m 512
I found out the hard way why I needed to include the -m 512 switch (to set max ram to 512mb) after the install disk crashed running out of memory.
Some other useful qemu options might be (run qemu –help to get the complete list)
- -cpu pentium (-cpu ?)
- -fd<a|b> <file> (floppy)
- -hda<a|b|c|d> <file> (disks)
- -cdrom <file>
- -m 128 (virtual ram size)
- -k be (keyboard layout)
Screenies
More configuration
TBD.


February 13th, 2010 at 03:14
[...] the rest here: vStone Blog » Blog Archive » KVM 'Quick' (n dirty) howto. Plurk This Post Delicious Digg This Post MySpace Ping This Post Reddit This Post [...]
February 14th, 2010 at 01:59
[...] KVM ‘Quick’ (n dirty) howto. [...]