2009-05-26

Create and startup a virtual machine with KVM under (K)Ubuntu Linux 9.04

This text describes how you can create a virtual machine with Ubuntu 8.04 server edition running under (K)Ubuntu Linux 9.04 desktop edition using kvm. There are a lot of tools to manage virtual machines under Ubuntu. I tried some of these, but in the end some simple shell scripts given to me by a colleague of mine were the best source of information.

I used commands from the scripts and a lot of information on the Internet to create a virtual machine running Ubuntu 8.04 server edition, with its own ip-address and with ssh access. The virtual machine is owned by user vosf (me) and run by user vosf. The name of the machine will be 'mugamma'. I'll explain the name Mugamma below.

Mugamma will be used for system management, running Puppet and Subversion.
It will store its data files on a NAS, so there's no need to give the machine lots if Gigabytes for storage.


Why Mugamma


Host 'mugamma' is named after an enormous building in Cairo, near Tahrir Square. It's the biggest public administrative building on the African continent. As a tourist you go there for visa extensions. You need a visa extension if you stay in Egypt for more than one month plus two weeks. Going to Mugamma is an interesting experience. Last time I went to Egypt I was there for 6 1/2 weeks.

I didn't know about the 2 extra weeks, so I thought I needed the visa extension. I experienced the chaos and mostly unfriendly staff over there. Thanks to a helpful lady (some of them are okay) I learned that I was going to leave Egypt just before I needed a visa extension; one day longer in Egypt and I'ld really need it. They had already provided my with two sets of forms and I had already filled in the forms. I had my photos ready and visited three or four front offices, fighting myself through crowds of shouting Egyptians and foreigners, until I learned I didn't need the visa extension. I can say I've had my share of the Mugamma experience anyway :)

The Mugamma building is a present from the Soviet Union to Egypt in the eary 1950's. In the Al Ahram newspaper it was once called "Egyptian bureaucracy's answer to Kafka's Castle". Most people think it's an ugly building. Not me. I think most people find it ugly because it is a Soviet style building and people have learned that everything from the Soviet Union is bad. Many people just do not want to admit they like it, or find it interesting at least.

Notes


I will write my steps as instructions to create the machine.

Most output I present is a little bit different from the actual output of the commands.

Please read the Ubuntu documentation on virtualization to test if your machine supports virtualization.

I will not use the Virtual Machine Manager (libvirt and tools).

I'm not providing a list of packages you need to install.
You'll find out yourself which packages are missing.

I created the machine and a startup script in directory ~/VirtualMachines. In the instructions I tell you to this too, but you must make your own choices here of course.

Steps


Network bridge


To make it possible for the virtual machine to access the network, we need a network bridge. This doesn't seem to work with Wifi interfaces. Change file /etc/network/interfaces from:


                                                                                                             
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp

into:



auto lo
iface lo inet loopback

auto eth0
iface eth0 inet manual

auto br0
iface br0 inet dhcp
bridge_ports eth0
bridge_fd 9
bridge_hello 2
bridge_maxage 12
bridge_stp off

After a restart of the network (sudo /etc/init.d/networking restart) there's a network interface called 'br0' with the ip-address the 'eth0' interface had before the change. Interface 'eth0' has no more ip-address attached.


Make yourself member of group kvm


If there's no group 'kvm', you probably need to install package 'kvm' first. Add yourself to the group 'kvm', either on the command line or using a GUI user management tool in (k)ubuntu. Then, in an open terminal, you can use 'newgrp kvm' to add 'kvm' to the list of groups you're in for the current terminal session. In a new session the membership will be the case automatically.


$ groups                                                                                                                                
vosf [...] admin [...]
$ newgrp kvm
$ groups
vosf [...] admin kvm [...]

Create location to store the virtual machine


In a terminal:


                                                                                                             
$ mkdir ~/VirtualMachines
$ cd ~/VirtualMachines

Create image file


                                                                                                             
$ qemu-img create -f qcow2 mugamma 2048M
$ ls -l

-rw-r--r-- 1 vosf vosf 20480 2009-05-21 13:02 mugamma

2 GB is enough for the basic OS, some extra packages, configuration and log-files. Program data is stored on a NAS. The current size of the image file is much smaller than its maximum size of 2 GB.


Install Ubuntu


First download Ubuntu Server Edition 8.04 for 32 bits Intel processors to $HOME/path/to/ubuntu-8.04.2-server-i386.iso

Then:
                                                                                               
$ kvm -boot d -cdrom $HOME/path/to/ubuntu-8.04.2-server-i386.iso -hda mugamma -m 128M
This starts up the guided install.

I chose:
* enter, enter, enter
* Country = 'other', then 'Netherlands'
* 'No' for keyboard layout
* enter, enter
* 'mugamma' (without quotes) for machine name
* 'Guided - use entire disk' for partitioning
* enter
* 'Yes' for writing to disk (this starts the install process and takes some time)
* 'Fred Vos' for full name of new user
* 'vosf' for account
* 'secret' for password and again for verification (or maybe something else)
* Blank for HTTP proxy question
* Selected 'OpenSSH server' as software to install
* Enter to continue to boot the OS


This is an example. Change at least the machine name, username and account for your situation.
After this it looks asif things went wrong. Message: 'FATAL: No bootable device'. Just close the Qemu window here.


Prepare for startup by user


Later I will move this virtual machine to a new physical machine and on that new machine it will be owned by and be started by root. While setting it up and experimenting, it will be owned by me and be started by me.


Create a script file called 'qemu-ifup' in the current directory ($HOME/VirtualMachines):


                                                                                                          
#!/bin/sh
set -x

switch=br0

if [ -n "$1" ];then
/usr/bin/sudo /usr/sbin/tunctl -u `whoami` -t $1
/usr/bin/sudo /sbin/ip link set $1 up
sleep 0.5s
/usr/bin/sudo /usr/sbin/brctl addif $switch $1
exit 0
else
echo "Error: no interface specified"
exit 1
fi

Then make it executable:


                           
$ chmod +x qemu-ifup

Create a random MAC address for the machines' network interface. I want to reuse that MAC-address, so my DNS will always generate the same IP-address. The MAC-address I will present here is 00:11:22:33:44:55, but it's something else.


Create a small executable script 'mugamma-startup.sh' to startup mugamma:


                                                    
#!/bin/sh

iface=$(sudo tunctl -b -u vosf)
sleep 1
kvm -hda mugamma -m 128M -daemonize -net nic,macaddr=00:11:22:33:44:55 -net tap,ifname=$iface,vlan=0,script=qemu-ifup

Change 'vosf' with your username or something more generic and make it executable:


                                                                                        
$ chmod +x mugamma-startup.sh

The system has grown now. In my case after this step it was 541 MB:


                                                                                        
$ ls -l

-rw-r--r-- 1 vosf vosf 567226368 2009-05-24 21:42 mugamma
-rwxr-xr-x 1 vosf vosf 161 2009-05-24 22:04 mugamma-startup.sh
-rwxr-xr-x 1 vosf vosf 310 2009-05-20 19:51 qemu-ifup

Time to start up the system for the first time


You can now startup the virtual machine:



$ ./mugamma-startup.sh

and login to the system as user in the VNC session. After retreiving the ip-address of the machine in that session, you can login to the machine with ssh from a normal terminal window.


Final steps


When you assign a fixed ip-address to the machine in the DHCP server of your router, based on the MAC-address of the virtual machine, you do not need the VNC window anymore. Adding '-vnc none' to the kvm command in the startup-script starts up the machine without a VNC window.


Now the system is ready for installing the necessary stuff.