It’s very easy to configure a machine as a KVM host these days.
Modern releases of Ubuntu and Centos both offer simple installation
methods that provide a useful system with minimal configuration.
Creating guests is a different story. A number of good tools are
available, supporting a huge variety of installation scenarios. While
it’s reassuring to know you won’t be limited by your tools, it can be
a frustrating experience when you’re just getting started.
This post shows how ubuntu-vm-builder can be used to go from zero to
fully-installed Chef client in under 2 minutes.
Installing KVM
Configuring the server as a KVM host is pretty straightforward.
Ubuntu’s
KVM documentation
is well-written, and a good place to start.
By default, libvirt will create a host-only NAT network, with DHCP
service, for guests to use. All my guests will be using a network
I’ve created manually, so I’ve removed the default one.
12345
$ virsh net-destroy default
Network default destroyed
$ virsh net-undefine default
Network default has been undefined
(Optional) Install apt-cacher-ng
Using apt-cacher-ng can speed up guest VM creation by keeping required
packages on the host.
This forum post
provides a good walkthrough.
123
$ sudo apt-get install apt-cacher-ng
$ echo 'Acquire::http::Proxy "http://localhost:3142";' | sudo tee /etc/apt/apt.conf.d/01proxy
$ sudo ufw allow proto tcp from 192.168.42.0/24 to 192.168.42.1 port 3142
Configuring ubuntu-vm-builder
The easiest way of creating Ubuntu guests appears to be the
ubuntu-vm-builder tool. The
wiki documentation
is decent, and the command-line help is comprehensive – see
ubuntu-vm-builder kvm --help.
My ~/.vmbuilder.cfg sets the options that will be used for most (or
all) of my Ubuntu guests.
The initial build process happens in a RAM disk, which means that
package installation is much faster than inside the guest.
As my guests will all be running Chef, I want to install the required
packages during the initial build. This requires enabling an
additional package repository, which can be achieved by customising
the APT sources template used by vmbuilder.
ubuntu/sources.list.tmpl
123456789101112131415161718
deb $mirror$suite#slurp#echo' '.join($components)deb $updates_mirror$suite-updates #slurp#echo' '.join($components)deb $security_mirror$suite-security #slurp#echo' '.join($components)#if$ppa#for$pin$ppadeb http://ppa.launchpad.net/$p/ubuntu $suite main#end for#end if# The Opscode repository, for Chefdeb http://apt.opscode.com/ precise-0.10 main
For the new VM to register as a Chef client, it needs my validation
key. The copy option in .vmbuilder.cfg lists files on the host to
install in the guest.