How to create a new virtual machine ? (see the "impatients" version)
- Download an ISO image
- Set up a few variables :
vmName='debian'; diskSize='10G'; ramSize='1G'; qemuWorkDir="$HOME/QEMU/"; mkdir -p "$qemuWorkDir"
- Create a new disk image :
- Boot an ISO image to install an operating system :
When installing
Debian :
- proceed in Expert mode : this is more verbose if something goes wrong
- at the Installing the base system stage, when offered 2 kernel versions to install :
- don't pick the linux-image-amd64 version
- select the linux-image-x.y.z version
- Once installed, boot the system :
Convert / compress a virtual machine image :
qemu-img convert -c myVirtualMachine.img -O qcow myVirtualMachine.img.compressed
- qcow is qemu's Copy On Write image format.
- myVirtualMachine.img.compressed is compressed and still runnable.
Looks like this is not necessary anymore as of 2024-08
Run these actions on the host system !
- apt install bridge-utils
- In /etc/network/interfaces, change the configuration method for the physical interface from auto to manual so that Network Manager (or equivalent) 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
- reload changes :
- service networking restart
- systemctl restart NetworkManager
- 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 !