QEMU - open source processor emulator

mail

QEMU

Flags

Flag Usage
-boot d Specify the boot device :
  • a, b : floppy 1 or 2
  • c : first HDD (default)
  • d : first CD-ROM
  • n-p : Etherboot from network adapter 1-4 (?)
-cdrom file Use file as CD-ROM image. You can use the host CD-ROM by using /dev/cdrom as filename.
-hdc and -cdrom are mutually exclusive.
-cpu cpuModel Select CPU model. qemu -cpu ? to list available models. More about x86_64 processors.
-curses Normally, QEMU uses SDL to display the VGA output. With this option, QEMU can display the VGA output when in text mode using a curses/ncurses interface. Nothing is displayed in graphical mode.
Using this option without -k fr completely f*cks up my keyboard.
-hda file Use file as hard disk 0 image. And respectively hdb, hdc, hdd for disks 1, 2 and 3 images.
-k language Use keyboard layout language (fr for French). Defaults to en-us.
-m n Set virtual Memory (RAM) size to n MiB. You can specify an optional M or G for MiB or GiB. Defaults to 128 MiB.
-smp n Simulate an SMP system with n CPUs.
Additional parameters are available for sockets, cores and threads : -smp sockets=8,cores=4,threads=4
-snapshot Any changes made to the virtual machine while it is running are written to temporary files and thrown away when the virtual machine is turned off. No changes are saved to the original .img file.

Setup

This has changed a lot since the initial version of this article, and this setup method is now obsolete : qemu has become a dummy package.
apt install qemu

About x86_64 processors :

  • It looks like only x86 (i386) processors can be emulated by qemu -cpu (even when selecting the qemu64 or other *64* models). To emulate an AMD64 processor, use qemu-system-x86_64 instead of qemu.
  • If a machine was created with an x86_64 processor, it won't boot if it's given a x86 processor.

How to create a new virtual machine ?

  1. Create a new 2G disk image : qemu-img create /path/to/myVirtualMachine.img 2G
  2. Boot an ISO image to install an operating system :
    qemu -hda /path/to/myVirtualMachine.img -cdrom /path/to/image.iso -boot d -m 256
    When booting a Debian image, it is recommended to start it in Expert mode. This is more verbose if something goes wrong.
  3. When it's installed, boot the system with : qemu -hda /path/to/myVirtualMachine.img -m 256

Convert / compress a virtual machine image :

qemu-img convert -c myVirtualMachine.img -O qcow myVirtualMachine.img.compressed

Enable networking (source, bridge) :

Run these actions on the host system :

  1. aptitude install bridge-utils
  2. In /etc/network/interfaces, change the configuration method for the physical interface from auto to manual so that Network Manager (or similar) leaves it quiet :
    #auto eth0
    iface eth0 inet manual
  3. Append to /etc/network/interfaces :
    # auto load a bridge
    auto br0
    
    # configure the bridge using DHCP
    iface br0 inet dhcp
    
    	# create a tap device owned by bob (who is a host system user), and turn it up
    	pre-up ip tuntap add dev tap0 mode tap user bob
    	pre-up ip link set tap0 up
    
    	# add all physical interfaces to the bridge
    	bridge_ports all tap0
    
    	# speed up the activating of the bridge (details)
    	bridge_stp		off
    	bridge_maxwait		0
    	bridge_fd		0
    
    	post-down ip link set tap0 down
    	post-down ip tuntap del dev tap0 mode tap
  4. Load changes : service networking restart
  5. Boot the virtual machine with the network management options (details) :
    qemu -hda imagefile.img -net nic -net tap,ifname=tap0,script=no,downscript=no
    The network adapter of the guest system will be configured by the guest system itself with /etc/network/interfaces and DHCP.
  6. To run extra guests simultaneously, create some tap1, tap2, ..., tap devices by duplicating the lines containing tap0 in /etc/network/interfaces.
    • Don't forget to update the ifname=tap... argument of the command above when starting the guest systems.
    • If guests were made by cloning a "master" virtual machine, there are chances the MAC addresses are all the same. Read about how to fix this.

Using snapshots (source)

Create a snapshot :

qemu-img create -f qcow2 -b file.img file.img.snapshot
Thanks to the "Copy On Write" principle, this is both very fast and highly space efficient but you MUST NOT EVER make any change to file.img - not even boot it - or all snapshots depending on it will be corrupted.

Temporary snapshots

Update the base image

Delete all existing snapshots first, then boot, update (and re-snapshot !).

Commit changes made in a snapshot into the base image :

qemu-img commit file.img.snapshot

The QEMU monitor (source) :

Enter the monitor (on a running machine)
CTRL-ALT-SHIFT-2
Leave the monitor
CTRL-ALT-SHIFT-1
Get info
info cpus

Release mouse grab :

This can be done with CTRL-ALT

Creating a new virtual machine :

  1. Define some shell variables :
    rootDir='/home/bob/'; isoImageFile="${rootDir}path/to/debian-7.3.0-amd64-netinst.iso"; vmDir="${rootDir}Qemu/"; vmFile="${vmDir}debian73x64.img"; vmDiskSizeGb=2; ramMb=1024; nbCpu=2
  2. Create and install the new virtual machine :qemu-img create "$vmFile" "${vmDiskSizeGb}G"; qemu-system-x86_64 -smp $nbCpu -m $ramMb -boot d -hda "$vmFile" -cdrom "$isoImageFile"
  3. Convert and compress the virtual machine image : compressedVmFile="${vmFile}.compressed"; qemu-img convert -c "$vmFile" -O qcow "$compressedVmFile"
  4. Configure networking on the host system
  5. Boot the virtual machine with network support to make sure it's ok :
    qemu-system-x86_64 -smp $nbCpu -m $ramMb -hda "$compressedVmFile" -net nic -net tap,ifname=tap0,script=no,downscript=no
    Once there :
    1. try some pings (to the virtual machine itself, the host system, any external host, ...)
    2. make sure you have all your CPUs : top 1
    3. while in top, check the RAM quantity
  6. If several virtual machines were "snapshoted" from the same initial install :
    1. Assign different MAC addresses manually
    2. Assign static IP addresses manually
  7. When ok, leave top with q and halt the virtual machine.
  8. Start the virtual machine in temporary snapshot mode : qemu-system-x86_64 -smp $nbCpu -m $ramMb -hda "$compressedVmFile" -net nic -net tap,ifname=tap0,script=no,downscript=no -snapshot
  9. Enjoy !