I have a Linux box running the excellent Mythbuntu (Ubuntu-based) distribution, headless (that is, without a monitor). Quite a lot of the time it’s sat around doing nothing (and even during recording or playback the CPU is idle).
For some side-projects I wanted a clean Linux installation to mess about with. It seemed a good idea to run virtual machines and make the most of existing hardware; what surprised me was just how easy this turned out to be
The Ubuntu documentation for KVM is excellent, I must say, but I fancied distilling things further and blogging here, as I typically do to record most of my technical adventures. I’m not going to bother with any of the GUI VM builder tools or even the Q&A install script, but simply specify the VM config fully, up front.
Optionally, check whether your CPU has virtualization extensions – any fairly recent desktop chip should do. On Ubuntu there’s a command called
kvm-ok, or you can poke
INFO: /dev/kvm exists
KVM acceleration can be used
# egrep -q '(vmx|svm)' /proc/cpuinfo && echo 'good to go!'
good to go!
First up install the KVM software:
# apt-get install qemu-kvm virtinst
This will pull in all the necessary packages. On other platforms it should be similar, but the
virtinst package is often renamed (e.g.
Before getting stuck in to KVM we need to reconfigure the system’s network adapter to be a bridge. I prefer to set a static IP for servers on my home LAN and use the
/etc/network/interfaces file for configuration:
# cat > /etc/network/interfaces
auto lo eth0 br0
iface lo inet loopback
iface eth0 inet manual
iface br0 inet static
post-up ip link set br0 address <MAC-ADDRESS>
Obviously, fill in the blanks for your own system’s IP and MAC address details. Next we can blow away Ubuntu’s network mangler daemon and poke the KVM service into life:
# apt-get --purge remove network-manager
# /etc/init.d/networking restart
# service libvirt-bin start
Now find somewhere on your disk for the VMs and a little script to live, and create a directory. I named mine
/opt/vm. In there, try starting with this little shell script:
virt-install --name=sandbox --ram=512 --vcpus=2 --os-type=linux \
--autostart --disk=path=/opt/vm/sandbox.img,size=50 \
--graphics=vnc,listen=0.0.0.0,port=5900 --noautoconsole \
Walking through the above, it should be clear we’re creating a new VM called
sandbox (this is the name KVM knows it by, not a hostname), with 512MB RAM, two virtual CPUs, a Linux-friendly boot environment, and 50GB (sparse) disk. The VM will be automatically booted by the KVM service when its host system boots. The last line specifies an installation CD image from which the new VM will boot.
For the graphics configuration I’ve asked for a headless system with the console being offered up via a VNC port on the host server. Note that the
listen=0.0.0.0 is essential to connect remotely (e.g. over your home LAN) to the console because otherwise the VNC port is simply bound to the loopback interface.
Running the above will bring the new VM into life:
Creating storage file sandbox.img | 50 GB 00:00
Creating domain... | 0 B 00:01
Domain installation still in progress. You can reconnect to
the console to complete the installation process.
What KVM means by “installation still in progress” is that it knows this system is installing from the boot CD, so you should go right ahead and fire up VNC and connect to the console (port 5900 on the host server) to complete the process.
You’ll find that KVM saved the
sandbox VM configuration in XML format in the
/etc/libvirt/qemu directory, so that’s where to go to tweak the settings. Good documentation is available at the KVM website.
Be aware, however, that because KVM assumed the attached CD ISO was only needed for initial install, it’s not featured in the saved config as a permanent connection. You can, of course, remedy this (check out the
virt-install man page for starters).
To finish off, here’s how to manage the lifecycle (start, restart, blow away, etc) of the VM. Use the
virsh utility which can either be run with a single instruction or with no parameters for an interactive CLI:
Welcome to virsh, the virtualization interactive terminal.
virsh # list
Id Name State
10 sandbox running
virsh # destroy
error: command 'destroy' requires <domain> option
virsh # destroy sandbox
Domain sandbox destroyed
virsh # create sandbox
error: Failed to open file 'sandbox': No such file or directory
virsh # create sandbox.xml
Domain sandbox created from sandbox.xml
virsh # list
Id Name State
11 sandbox running
help command, and note that the VM’s XML settings file may need updating if you change things (see
I hope this is a useful and quick tutorial for KVM on Ubuntu… Good Luck!