分类: LINUX
2011-08-07 12:45:36
is a fast processor emulator using dynamic translation to achieve good emulation speed. It is a free open-source alternative to .
As QEMU requires no host kernel patches to run, it is very safe and easy to use.
Operation
QEMU has two operating modes:
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 or to ease cross-compilation and cross-debugging.
Furthermore, there are three options for running QEMU:
Pros |
Cons |
|
QEMU (plain) |
Doesn't require a kernel module |
Not as fast as the others |
QEMU + |
Faster |
Requires the KQEMU module |
QEMU / |
Fastest |
Requires the kvm module |
Installation
The QEMU emulator is packaged as , which can be installed using your preferred tool.
KQEMU (QEMU accelerator)
KQEMU is an optional kernel module 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 ).
QEMU as of version 0.12.0 no longer supports KQEMU. For newer x86/amd64 CPUs with the hardware virtualization support, use as QEMU accelerator.
Lenny
This is packaged for as in main, which can be compiled and installed using .
Install and build a kqemu-modules-* package for your system:
# aptitude update && aptitude install module-assistantAdd kqemu to and load the module:
# echo kqemu >> /etc/modulesUsage
Setting up a stable system
Debian developer Aurelien Jarno maintains a list of ready-to-use Debian QEMU images at
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 system can be set up with the following steps:
Create the hard disk image with:
$ qemu-img create debian.img 2Gor with the qcow2 disk image format if you want to use QEMU's own "Copy On Write" image format:
$ qemu-img create -f qcow2 debian.qcow 2GDownload 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 256After the installation is done, the system can be booted with:
$ qemu -hda debian.img -m 256Backing up the disk image
The disk image "debian.img" is a file. After installing a Debian base system, it fits on a CD-ROM even without compression:
$ tar c --sparse -f backup.tar debian.imgThis creates a 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-img convert -c debian.img -O qcow debian_recompressed.imgIf 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):
Install these packages: and .
modprobe tun or add 'tun' to /etc/modules
Edit /etc/network/interfaces:
Remove the 'auto' line and change the 'method' of your physical, wired network adapters from to 'manual':
#auto eth0Add a stanza for the bridge:
auto br0Explanation:
this stanza auto loads a bridge and configures it using DHCP. A tap
device is created owned by
Launch QEMU:
$ qemu -k en-us -hda imagefile.img -net nic -net tap,ifname=tap0,script=no,downscript=no-k en-us prevent the 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,downscript=no disable the scripts /etc/qemu-ifup and /etc/qemu-ifdown as they are 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.
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.
Install and (for tunctl).
Add to /etc/network/interfaces:
auto mytapIf automatic configuration of guest network interfaces is desired, install a DHCP server such as as well. Configure it to assign addresses only on the TAP interface (e.g. 'mytap' in the above example). If you use dhcpd, make sure mytap is added to /etc/default/dhcp3-server and the INTERFACES directive. anm
Either reboot or run the following:
# modprobe tunFinally, start the VM:
$ qemu -net nic -net vde,sock=/var/run/vde2/mytap.ctl debianimage.imgIf running more than one VM, you need to provide a unique MAC address:
$ qemu -net nic,macaddr=52:54:00:12:01:00 -net vde,sock=/var/run/vde2/mytap.ctl debianimage.img (Previously, vdeqemu was needed for running qemu with VDE, but recent versions of qemu has vde enabled already.) 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'.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. An iptables rules-building program such as makes this easier. You may also use NAT/Masquerade script from
More info on VDE:
Other
Mounting QEMU images
Never mount a QEMU image while QEMU is using it, or you are likely to corrupt the filesystem(s) within.
raw
Linux and other Unix-like hosts can mount images created with the raw format type using a loopback device. From a root login (or using sudo), mount a loopback with an offset of 32,256:
# mount -o loop,offset=32256 /path/to/image.img /mnt/mountpointFor example, to copy some files across to a FreeDOS hard drive image:
# mkdir -p /mnt/freedosNote: if you have an image without partitions you should omit the ,offset=32256 part. This is for instance the case if you want to mount linux-0.2.img (which can be found at the QEMU web site at the time of writing).
qcow2
You will need a 2.6.26 kernel or newer, so Lenny will do. Load the ndb module:
# modprobe nbd max_part=8If you leave off the max_part attribute, partitions are not supported and you’ll be able to access the disk, but not have device nodes for any of the partitions.
Assuming your qcow2 image filename is imagename.qcow, run:
# kvm-nbd --connect=/dev/nbd0 imagename.qcowNow you can check the partition table with:
# fdisk -l /dev/nbd0and mount, e.g., the first partition on /mnt with:
# mount /dev/nbd0p1 /mntSee Also
QEMU related tools:
- creates Debian packages sandboxed with QEMU
Alternative or similar tools:
tools
tools
External Links
- QEMU homepage
- QEMU FAQ
- channel
Note: the command 'qemu-system-x86_64' in this guide on Debian Etch is 'kvm'