vmclone: Quickly spin up temporary VMs
I wanted a way to easily spin up graphical VMs for experimentation without affecting my main system. I ended up with a small command that clones a prepared VM, starts it, and opens its console:
vmclone my-clone
I'm using QEMU/KVM for virtualization. One can of course make clones in virt-manager by right clicking the VM and selecting "Clone", but this makes a full copy of the disk, wasting space. To avoid this, my script uses qemu-img directly to make a thin clone:
#!/bin/bash
if [[ $# != 1 || "$1" == "-h" || "$1" == "--help" ]]; then
echo "Usage: $0 vmclone CLONE_NAME" >&2
exit 1
fi
set -eux
conn=qemu:///system
dir=/var/lib/libvirt/images
base=base-desktop
name="$1"
qemu-img create -f qcow2 -b "$dir/$base.qcow2" -B qcow2 "$dir/$name.qcow2"
virt-clone --connect "$conn" --original "$base" --name "$name" --file "$dir/$name.qcow2" --preserve-data
virsh -c "$conn" start "$name"
virt-manager -c "$conn" --show-domain-console "$name" &
The base image, base-desktop, is an Arch Linux VM that was installed with archinstall with the Xfce profile. After initial install, I added the appropriate guest agents and customized it to my liking:
sudo pacman -Sy spice-vdagent qemu-guest-agent bash-completion firefox xorg-xrandr less
One gotcha is that screen resolution isn't automatically adjusted in the guest when resizing the console window. As a workaround, first enable resizing in the console with View → Scale Display → Auto resize VM with window. Then, after you resize the window, run xrandr -s 0 in the guest. For convenience, I added a launcher to the Xfce panel to do this.
Future work
This script works for now, but a better solution would be to add support for thin clones of qcow2 images in virt-manager. I was surprised it didn't already exist!