Show navigation

Getting Started With QEMU

Saturday 30th June 2007

QEMU lets you emulate a machine - in other words, you can run a virtual computer on top of your real computer. This makes it perfect for trying the latest release of a distribution, running older operating systems, or just testing.

Installation

Getting QEMU depends on your operating system - if you're on Debian, for instance, you can type apt-get install qemu. However, in order to get the latest version, as well as to avoid a bug in the version in the Debian repositories, you might want to download QEMU from upstream. If you're using an x86 machine, such as the AMD Athlon, Intel Pentium, or Intel Core, in 32-bit mode, then you can safely choose "Binary distribution for linux-i386". Simply extract the contents, as root, to /, making sure to maintain the directory structure.

Playing with QEMU

So, let's say you wanted to run a LiveCD, which you have stored on your hard drive. Easy enough! We just type:

qemu -cdrom path/to/livecd.iso -boot d

The -cdrom option tells QEMU the path to the CD you want to use, while -boot dictates what device we're booting from - in this case, we want to boot from the CD, which is always device d.

You might get a message about kqemu - you can safely ignore this for now.

Alternatively, you may want to boot from a real CD in your computer - so, you simply use the path to your CD drive in /dev. For instance, if your CD drive is /dev/hdc, then we would use:

qemu -cdrom /dev/hdc -boot d

Of course, we don't just want to use CDs all the time - we might want to actually install something! So, we've got to make a hard drive image first. This is achieved by using qemu-img, like so:

qemu-img create virtualharddrive.qcow 5G -f qcow

This creates an image called virtualharddrive.qcow, which is 5 gigabytes big. If you wanted it to be 5 megabytes, you could type 5M instead, for ten gigabytes, type 10G, and so on. Finally, the -f option tells qemu-img what format you want to use. While there are a few to choose from, qcow is the norm - it works well enough, and only takes up the space on the hard drive that it needs. If the virtual hard drive had a capacity of 5 gigabytes, but only contained 2 gigabytes of data, then it would only take up about 2 gigabytes on your real hard disk.

So, now we want to get installing. Simply type:

qemu virtualharddrive.qcow -cdrom path/to/installcd.iso -boot d

Then, you can simply follow the instructions just as with an ordinary installation.

Now, not all distributions come on one CD - in some cases, you'll need to swap CDs. To achieve this, we need to do two things. First of all, we make the QEMU monitor appear on the command line by adding the option -monitor stdio, so we end up with something like:

qemu virtualharddrive.qcow -cdrom path/to/installcd.iso -boot d -monitor stdio

When using this option, QEMU should let you enter commands on the command line. To change CDs, simply type:

eject cdrom
change cdrom path/to/newcd.iso

Voila! The virtual machine should now have changed CDs so you can continue the installation. Naturally, you can change CDs at any time, not just during installation.

Once the installation has finished, you'll want to boot from the hard drive. Since QEMU does this by default, simply remove the -boot d part of the command:

qemu virtualharddrive.qcow -cdrom path/to/installcd.iso -monitor stdio

If you have no intention of using the CD after the installation, then you can cut that out as well:

qemu virtualharddrive.qcow -monitor stdio

This should let you play around with your newly installed system at your heart's content without endangering your own PC. If you want to fiddle with something, but don't want the changes written to the image, then add the option -snapshot. If, after using this option, you decide that you actually want to save the changes made to the hard drive, then simply type commit into the QEMU monitor, and the changes will be written.

While these commands work, unless you're using lightweight distributions, you might find things going a little slowly. This is due to QEMU, by default, only taking up 128MB of RAM. You can increase the amount available by using the -m option, followed by the amount of RAM in megabytes. For instance, if I wanted to allocate 256MB to QEMU for running a LiveCD, then I would type:

qemu -cdrom path/to/livecd.iso -boot d -m 256

That should speed things up nicely! But don't give QEMU too much memory - you want some left for your other applications. Unfortunately, things are probably still fairly slow - to speed things up even more, you'll probably want to use kqemu, otherwise known as the QEMU accelerator.

kqemu

Installing kqemu from Apt repositories is reasonably straightforward. First of, grab the kqemu-source package - if apt-get is your package mangager of choice, then the command used is:

apt-get install kqemu-source

If you don't have module assistant already, you'll need that installed as well:

apt-get install module-assistant

Then, type in the following commands (as root):

m-a update
m-a prepare
m-a auto-install kqemu

That should be it! More generally, if you want to use kqemu, you can download the latest version, extract the contents, and run the commands:

./configure
make

Then, as root:

make install

Now, every time you want to use the kqemu module, you first need to become root, and then type:

modprobe kqemu major=0

Then, as an ordinary user, QEMU will automatically use kqemu, which should help speed things up. If QEMU complains that it still cannot use kqemu, then you might not have the necessary permissions - try typing the following as root:

chmod 666 /dev/kqemu

Hopefully, kqemu should now be usable by QEMU. If you do not want to use kqemu, such as in case it doesn't work with a guest operating system, then use the option -no-kqemu.

There is one final option: -kernel-kqemu. This, in theory, speeds up the virtualisation even further. Unfortunately, it isn't as simple as that. Firstly, the version of QEMU in the Debian repositories cannot use -kernel-kqemu. Secondly, even if it could, not all guest operating systems would work with this enabled - for instance, a recent distribution of GNU/Linux would probably work faster with this option, but Windows 98 just crashes.

However, if you install QEMU from upstream you shouldn't have a problem - -kernel-kqemu should speed things up on GNU/Linux, as well as Windows 2000 and XP once they are installed. The option should work on most recent distributions, so it's probably worth trying on *BSD and other operating systems.

Useful Links