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.
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 ?
- Create a new 2G disk image : qemu-img create /path/to/myVirtualMachine.img 2G
- 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.
- 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
Run these actions on the host system :
- aptitude install bridge-utils
- 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
- 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
- Load changes : service networking restart
- 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.
- 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.
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.
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 :
- 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
- 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"
- Convert and compress the virtual machine image :
compressedVmFile="${vmFile}.compressed"; qemu-img convert -c "$vmFile" -O qcow "$compressedVmFile"
- Configure networking on the host system
- 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 :
- try some pings (to the virtual machine itself, the host system, any external host, ...)
- make sure you have all your CPUs : top 1
- while in top, check the RAM quantity
- If several virtual machines were "snapshoted" from the same initial install :
- Assign different MAC addresses manually
- Assign static IP addresses manually
- When ok, leave top with q and halt the virtual machine.
- 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
- Enjoy !