Thursday, February 08, 2007

Debian HOW-TO : QEMU virtual machine

Last revision : April 28th, 2007

Virtual machines software are rapidly gaining in popularity. Xen, VMware and VirtualBox are all interesting solutions. However there is one that often gets overlooked : QEMU.

Mandriva 2007 and Knoppix 5.1.1 both running in Debian Etch

While QEMU is an impressive piece of software, it can still benefit from the use of tools like KVM or Kqemu, which will further enhance its performances. As much as the above mentioned tools are considered optional components of the QEMU virtual machine, they do enhance QEMU emulation speed to a near native level.


  • Preparation


KVM, as it relies on true hardware virtualization, is certainly the one that provides better added value. The only downside is that it requires a CPU with virtual capabilities.

To verify if you possess such a processor, check out the output of :

cat /proc/cpuinfo | grep flag


And look for either vmx or svm. If they show, then you can use KVM. Otherwise, Kqemu is your best option.


  • Starting with QEMU


For starter you will need QEMU :

sudo apt-get install qemu


And a few tools :

sudo apt-get install module-assistant build-essential


Plus the proverbial kernel-headers (or linux-headers starting with Debian Etch) :

sudo apt-get install linux-headers-$(uname -r)


Next, we take on the installation of KVM, so those who will rely on Kqemu can skip to the Kqemu accelerator section directly.


  • Hardware virtualization with KVM


For those about to use KVM, you must first install the userspace component (part of Debian's Sid repository). Download the require package :

wget http://snapshot.debian.net/archive/2007/02/08/debian/pool/main/k/kvm/kvm_12-1_i386.deb


Then simply install it like that :

sudo dpkg -i kvm_12-1_i386.deb


Linux Kernel 2.6.20


The module component of KVM is also required. You can either compile it as an external module, or use the latest Linux kernel, 2.6.20, which includes KVM. If you wish to compile a kernel, simply take a look at this tutorial.


Linux Kernel 2.6.19.x and below


Those using a Linux kernel version 2.6.19.x (or below) will have to build the KVM as an external module. The procedure is rather straight forward.

kvm-source, which is part of the Debian Sid official repository, is the required package.

Download it :

wget http://snapshot.debian.net/archive/2007/02/08/debian/pool/main/k/kvm/kvm-source_12-1_all.deb


Then install it :

sudo dpkg -i kvm-source_12-1_all.deb


At this point we can effectively compile the module (with module-assistant) :

cd /usr/src
sudo m-a prepare
sudo m-a a-i kvm


We now have everything needed to use KVM, just make sure you have the necessary rights to use it :

sudo adduser your_username kvm


You must of course use your own username instead of your_username. Then you'll need to log out and log back in so the (permission) changes take effects.

Now that KVM is appropriately set up, you can proceed with installing an OS in QEMU.

First you must load the right module (either kvm-intel or kvm-amd depending on your CPU). For example, installing Debian Etch is done like that :

sudo modprobe kvm-intel


Then create an image for Etch to install (sort of a virtual hard drive) :

qemu-img create etch_qemu.qcow 4G


Now that we have a 4G "disk", we can proceed with the actual installation. First download Etch netinstaller :

wget http://cdimage.debian.org/cdimage/daily-builds/daily/arch-latest/i386/iso-cd/debian-testing-i386-netinst.iso


Then launch it :

kvm -hda etch_qemu.qcow -cdrom debian-testing-i386-netinst.iso -boot d -m 256


Once this virtual Etch is installed, you can run it like that :

kvm -hda etch_qemu.qcow -m 256



  • Kqemu accelerator


While Kqemu's speed is not on par with KVM's one, it certainly constitutes a worthwhile alternative. Fortunately, installing Kqemu is rather simple with the help of module-assistant :

sudo apt-get install kqemu-source
cd /usr/src
sudo m-a prepare
sudo m-a a-i kqemu


Kqemu is now installed. Before using QEMU, Kqemu needs to be set up (this process must be redone after a reboot) :

sudo modprobe kqemu
sudo mknod /dev/kqemu c 250 0
sudo chmod 666 /dev/kqemu


The rest works exactly like when using KVM, except using the qemu command instead. Again let's take Debian Etch as an example on how to install and run an OS :

qemu-img create etch_qemu.qcow 4G
qemu -hda etch_qemu.qcow -cdrom debian-testing-i386-netinst.iso -boot d -m 256



  • Tips about Virtual Computing


Unfortunately QEMU doesn't take care of loading the required modules associated with either Kqemu or KVM. So make sure you do load the appropriate module (kqemu, kvm-intel or kvm-amd) before starting QEMU.

Instead of launching QEMU from the shell, you can also use qemu-launcher, as it will take care of everything :

sudo apt-get install qemu-launcher



Additionnaly, QEMU can run pre-built VMWARE images, of which you can find a great selection on VMWARE community site.

Some Linux distributions won't work very well with KVM. For example, Ubuntu Edgy installer doesn't support KVM, so you simply launch the installer with QEMU (without KVM) :

qemu -hda ubuntu_qemu.qcow -cdrom ubuntu-6.10-desktop.iso -boot d -m 256


Once you have it installed, Ubuntu can be run with KVM :

kvm -hda ubuntu_qemu.qcow -m 256


Further Reading


Debian - Kernel Based Virtual Machine
Windows XP under QEMU
How to create Screencasts with QEMU

6 comments:

Anonymous said...

The existence of vmx or svm processor flags does not mean, that you are ready to use hardware virtualization! There must be a BIOS, which activates these capabilities. I am using an IBM/Lenovo Z61m notebook with an Intel T7200 processor, but it does not work.

Hugues said...

When either the VMX (for Intel) or SVM (for AMD) are present, it does mean that your CPU support hardware virtualization.

However, you are right that it does have to be enabled in the BIOS. I should have mentioned it, as I believe it is disabled by default on most machine (in the BIOS) -- at least it was on mine. Additionally, some BIOS won't even offer the possibility to enable VMX/SVM on CPU that supports it (namely some HP laptops).

As for your Lenovo Z61m, it should support it, as specified by Intel. Simply enable VMX in the BIOS.

I hope that clarifies it. Thanks for your comment,

Hugues

Anonymous said...

It might be worth noting that the kqemu module is now available as a binary in Etch, so there's no need to compile the source.

Hugues said...

You are right about that : kqemu is available as a binary module. So anyone using a stock Debian kernel can simply use the pre-built module.

So :
sudo apt-get install kqemu-modules-$(uname -r)
should do it for most people.

I just happen to use a custom compiled kernel, that is why I compile the module using module-assistant.

Thank you for your comment,

Hugues

Anonymous said...

So far so good, however when I use qemu launcher it seems to be launching the machines using qemu rather than using kvm. Where do you set it to prefer kvm as the emulator tech? I don't see the option in the interface.

docbill said...

Actually, the only benchmarks I have seen that compare kvm and kqemu indicate that kqemu is faster. Still I would recommend that if you have a platform that supports both, use kvm. I think kqemu is fully optimized, while there is still some room for future improvement of kvm.