Search

QEMU: easy and fast processor emulator

July 8th, 2007 edited by ana

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.

Posted in Debian, Ubuntu |

9 Responses

  1. james Says:

    I tried qemu and while it worked it was like swimming in treacle. Plus, to get Win32 running above 640×480 kqemu needed to be installed which caused qemu to fall over.

  2. Toni Says:

    I use Debian/Lenny. If i use the quemu option “-m 256″ it fails with error

    ————————–
    You do not have enough space in ‘/dev/shm’.
    —————————

    recommending to umount /dev/shm and mount it again with option “-o size=272m”. But this was deleting my DNS resolving (resolv.conf). It worked for me with mount options “-o remount,size=272m” with no umount before.

  3. undefined Says:

    qemu tweak prompted by Toni’s post:

    if using the “-snapshot” option, then the TMPDIR environment variable is used to determine the location of the “snapshot”. if TMPDIR points to 1 GB /tmp partition, but you write over 1 GB to snapshot before commiting (ie “dd if=/dev/zero of=/tmp/zero” within qemu to achieve maximum compression of free space in image once you commit), then you might want to set TMPDIR elsewhere.

  4. Nick Craig-Wood Says:

    If you have a recent processor - ie VT capable Intel processor, or an SVM capable AMD processor - you can run KVM. This runs part of the virtualisation directly on the processor using these extensions and thus is at least as fast as kqemu but without the compatibility problems (in my experience).

    For this you’ll need to use module-assistant to install kvm-source and install the kvm package (all from unstable).

    The kvm binary is a fork of qemu and has exactly the same parameters. One day kvm support will be merged back into qemu I think.

  5. Guillem Jover Says:

    Instead of using ‘-monitor stdio’, one can use Ctrl-Alt-2 to switch to the monitor in the second virtual console (’man qemu’ has more info on the other keystrokes).

  6. John Says:

    I am a newbie and from what you have posted it is very helpful and informative for installing QEMU, but…. how does one install say, Windows software inside QEMU ?

  7. John Says:

    I must add, as I don’t think I’ve made it clear, when I say “Installing Windows software” I mean after an image of a Windows system has been set up within QEMU.

  8. yungchin Says:

    Thanks for a great guide.

    I just installed qemu on Debian Etch, and found that there’s even a compiled kqemu module available as kqemu-modules-2.6-686 (you may want to replace 686 by something else), saving some work if you use a standard Debian kernel.

    A question: what does the major=0 option do in the modprobe command? I found mention of it in the QEMU documentation at http://fabrice.bellard.free.fr/qemu/kqemu-doc.html#SEC4 but I can’t say that means anything to me… Thanks!

  9. PSU Calculator Says:

    Thanks for the great guide!
    I also installed QEMU on Debian and it works like a charm!