This guide is based on Ubuntu 12.04 and assumes that you already have a working virtualization environment based on KVM / libvirt, e.g. by following this tutorial.
To automate the installation of virtual machines with vmbuilder first install python-vm-builder
sudo apt-get install python-vm-builder
Vmbuilder by default uses virtio for the network interface but not for the virtual hard disk which makes the virtual machines very slow. To fix this edit /etc/vmbuilder/libvirt/libvirtxml.tmpl and change the line
Since my virtual machines require a pointopoint entry in /etc/network/interfaces I also edit
/etc/vmbuilder/ubuntu/interfaces.tmpl and add an additional line pointopoint underneath gateway. This will set pointopoint to the gateway address that I specify with vmbuilder.
As a last ugly rather hack I edit /usr/share/pyshared/VMBuilder/plugins/kvm/vm.py and change the default file image type from qcow2 to raw to improve the disk throughput.
filetype = 'raw'
Instead of editing vm.py you could also use the qemu-img command after running vmbuilder to convert the qcow2 image to raw (or just keep qcow2 if you are fine with it).
Now run vmbuilder to install a virtual machine with Ubuntu 12.04 (adjust the parameter settings as needed of course):
sudo vmbuilder kvm ubuntu --suite precise --flavour virtual --arch amd64 --libvirt qemu:///system --hostname gonzo --ip 18.104.22.168 --net 22.214.171.124 --mask 255.255.255.255 --bcast 126.96.36.199 --gw 188.8.131.52 --domain example.com --user john --name john --pass 12345 --verbose --rootsize=20480 --swapsize=2048 --mem 1024 --cpus 1 --addpkg=openssh-server --addpkg=vim --addpkg=acpid --dest=/var/lib/libvirt/images/gonzo/
For any new VM repeat the command with the right hostname and IP’s.
You have to be very careful with not overwritting existing images with vmbuilder (happens when you use the -o parameter), all files in the destination folder will be deleted. That’s why i am using the hostname for the destination directory to have a unique subfolder. The other parameters are very well explained in the Ubuntu Server guide at https://help.ubuntu.com/12.04/serverguide/jeos-and-vmbuilder.html.
One bug remains though, occasionally I receive grub error messages and the virtual machine does not start, there is a bug report about the issue at https://bugs.launchpad.net/ubuntu/+source/vm-builder/+bug/610652. In this case it helps to just delete the VM and try it again using the exactly same command.