QEMU is a fast processor emulator using dynamic translation to achieve good emulation speed. It is a free open-source alternative to VMware.
As QEMU requires no host kernel patches to run, it is very safe and easy to use.
Contents
QEMU Modes
QEMU has two operating modes:
- Full system emulation. In this mode, QEMU emulates a full system (for example a PC), including a processor and various peripherals. It can be used to launch different Operating Systems without rebooting the PC or to debug system code.
User mode emulation (Linux host only). In this mode, QEMU can launch Linux processes compiled for one CPU on another CPU. For example, it can be used to launch Wine or to ease cross-compilation and cross-debugging.
Furthermore, there are three options for running qemu:
Using Qemu
Setting up a testing/unstable system
QEMU is especially handy to set up an emulated testing/unstable system when working on the Debian installer itself or on the boot system, or when trying some experimental features without impact on the productive system. A sid system can be set up with the following steps:
- Create the hard disk image with
$ qemu-img create debian.img 2G
or with the qcow2 disk image format if you want to use QEMU's own "Copy On Write" image format$ qemu-img create -f qcow2 win.qcow 2G
Download a current boot image, e.g. the businesscard image at http://cdimage.debian.org/cdimage/daily-builds/daily/arch-latest/i386/iso-cd/
- Boot the image with
$ qemu -hda debian.img -cdrom debian-testing-i386-businesscard.iso -boot d -m 256
- When the usual debian boot screen appears, boot into "expert" mode
- Install the system as usual; to set up a sid system choose "unstable" when being asked by the installer.
After the installation is done, the system can be booted with
$ qemu -hda debian.img -m 256
Backing up the disk image
The disk image "debian.img" is a sparse file. After installing a Debian base system, it fits on a CD-ROM even without compression:
$ tar c --sparse -f backup.tar debian.img
creates a tar file of about 320M (supposed that the image contains a 1.9GB ext3 root filesystem and a 250MB swap partition). After unpacking with tar xf, the sparse file is restored and can be booted immediately.
Better still, convert from a sparse file into the qemu's own "Copy On Write" image. This conversion will save the same space and still be runnable.
$ qemu-image convert -c debian.img -O qcow debian_recompressed.img
If the guest system's image is still larger than reasonable, then open up the Guest system and run "dd if=/dev/zero of=/tmp/junk ; sync ; rm /tmp/junk". That will push out deleted file scraps, recompression should work then.
Networking
Guests on NATed internal network
By default, QEMU invokes the -nic and -user options to add a single network adapter to the guest and provide NATed external Internet access. The host and guest will not see each other.
Host and guests on same network
To create a bridge between host and guests, do the following (working on Debian Testing as of Aug 08):
1) Install these packages: bridge-utils and uml-utilities.
2) Modprobe 'tun' or add to /etc/modules
3) Edit /etc/network/interfaces:
a) Remove the 'auto' line and change the 'method' of your physical, wired network adapters from to 'manual':
#auto eth0 iface eth0 inet manual
b) add a stanza for the bridge.
auto br0 iface br0 inet dhcp pre-up /usr/sbin/tunctl -u <username> -t tap0 pre-up ifconfig tap0 up bridge_ports all tap0 post-down ifconfig tap0 down post-down tunctl -d tap0
Explanation: this stanza auto loads a bridge and configures it using DHCP. A tap device is created owned by <username> and is brought up. On the 'bridge_ports' line, 'all' adds all physical interfaces to the bridge; virtual interfaces have to be listed explicitly. See man bridge-utils-interfaces - note that the man page warns against adding wireless adapters to the bridge.
4) Launch QEMU:
qemu -k en-us -hda imagefile.img -net nic -net tap,ifname=tap0,script=no
'-k en-us' prevents loss of keyboard control due to a bug as of Aug 08.
'ifname=tap0' - the tap name here corresponds with the name in the bridge stanza above.
'script=no' - disables the script at /etc/qemu-up as this is not needed.
The guest virtual network adapters will be configured by the /etc/network/interfaces file in the guest file system. DHCP will work so this is simplest way to go.
5) To run additional guests, duplicate the lines in the bridge stanza for tap1, tap2 as needed and change the ifname argument in the command line. If you run more guests from the same image file, udev will rename the interface to avoid duplication (e.g. eth0 => eth1), so add extra interface stanzas in the guest interfaces file to configure these.
Qemu networking with VDE
Virtual Distributed Ethernet (VDE) provides is a virtual switch that can connect multiple virtual machines together, both local and remote. With VDE it is possible to create a virtual network of QEMU machines running on one or more real computers.
1. Install vde2 and uml-utilities (for tunctl).
2. Add users which will be running VM's to the vde2-net group.
3. Add to /etc/network/interfaces:
auto mytap iface mytap inet static address 10.0.3.1 netmask 255.255.255.0 vde2-switch -t mytap
4. If automatic configuration of guest network interfaces is desired, install a DHCP server such as dnsmasq as well. Configure it to assign addresses only on the TAP interface (e.g. 'mytap' in the above example).
5. Either reboot or run the following:
# modprobe tun # ifup mytap # /etc/init.d/dnsmasq restart #if used % newgrp vde2-net #run as user starting Qemu VM's
6. Finally, start the VM:
% vdeqemu -net nic -net vde,sock=/var/run/vde2/mytap.ctl debianimage.img
If running more than one VM, you need to provide a unique MAC address:
vdeqemu -net nic,macaddr=52:54:00:12:01:00 -net vde,sock=/var/run/vde2/mytap.ctl debianimage.img
If copying a MAC address from an existing interface, make it unique by altering any of the last 6 digits at the end. The first 6 designate the card manufacturer and so should not be made up.
If running kvm instead of qemu, the command name is 'vdekvm'.
7. Qemu VM's networked under VDE are not automatically NATed as they are under Qemu's default User Mode networking. For guest external network access you must enable NAT/Masquerading via iptables. A iptables rules-building program such as shorewall makes this easier.
More info on VDE: VDE Forum VDE wiki
Setup
Installing the Kqemu accelerator kernel module
The kqemu accelerator kernel module is available to speed up execution when emulating an x86 CPU on a native x86 CPU. As of version 1.3.0pre10, kqemu is distributed under a GPL licence (see KQEMU changelog). If you're running a standard Debian kernel, you can install the pre-built modules:
aptitude update && aptitude install kqemu-modules-$(uname -r)
Alternatively, compile and install KQEMU with Module Assistant:
aptitude update && aptitude install module-assistant module-assistant prepare module-assistant auto-install kqemu
Before using, modprobe kqemu or add it to /etc/modules and reboot.
If running Qemu under kvm (virtualization support in the Linux kernel), kqemu is not needed.
Compiling qemu from source
QEMU can not yet be compiled with gcc-4.0. Use something like
$ ./configure --cc=gcc-3.4 --host-cc=gcc-3.4
when compiling QEMU yourself and your gcc is already gcc-4.0.
See Also
http://www.nongnu.org/qemu/ - QEMU homepage
QEMU related tools :
qemubuilder - creates Debian packages sandboxed with QEMU
- QEMU frontends :
Alternative or Similar tools
SystemVirtualization tools
Emulator tools
Other Guides
Networking KVM (applies to Qemu) 05/2008 Note: the command 'qemu-system-x86_64' in this guide on Debian Etch is 'kvm'