Article submitted by Michael Williamson. We are running out of articles! Please help DPOTD and submit good articles about software you like!
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 and testing the latest release of a distribution, running older operating systems, or just testing.
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 you run this command, QEMU should let you enter commands. To change CDs, simply type in the commands:
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.
Installing kqemu from the repositories is reasonably straightforward. First of, grab the kqemu-source package —if you apt-get is your package manager 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 prepare
m-a auto-install kqemu
That should be it! 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.
There is one final option: -kernel-kqemu. This, in theory, speeds up the emulation 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 (if it worked!), but Windows 98 just crashes.
QEMU is available from Debian Sarge and Ubuntu Warty.