Xen Virtualization

Installing Xen on Ubuntu Server 8.04.1 LTS

This tutorial provides step-by-step instructions on how to install Xen on an Ubuntu Hardy Heron (Ubuntu 8.04.1) server system (i386). You can find all the software used here in the Ubuntu repositories, so no external files or compilation are needed.

Xen lets you create guest operating systems (*nix operating systems like Linux and FreeBSD), so called "virtual machines" or domUs, under a host operating system (dom0). Using Xen you can separate your applications into different virtual machines that are totally independent from each other (e.g. a virtual machine for a mail server, a virtual machine for a high-traffic web site, another virtual machine that serves your customers' web sites, a virtual machine for DNS, etc.), but still use the same hardware.

Make sure that you are logged in as root

sudo su -

Install vim and make sure to turn on the "syntax on" in /etc/vim/vimrc

aptitude install vim

Disable Apparmor:

/etc/init.d/apparmor stop

update-rc.d -f apparmor remove

To install Xen and all needed dependencies, all we have to do is run the following command:

apt-get install ubuntu-xen-server

This will also install the xen-tools package which we use later on to create virtual machines.

Next do this:

mv /lib/tls /lib/tls.disabled

We also need to add the loop module to the kernel every time we boot our system, so we edit /etc/modules. If you already have a loop line in there, make it look like this, otherwise add it:

[...]

loop max_loop=64

[...]

Now take a look at the /boot directory to see which kernels and ramdisks are installed:

ls -l /boot/

You can now also take a look at the /usr/lib/xen-tools directory because it reveals which distributions can be installed in a virtual machine with xen-tools:

ls -l /usr/lib/xen-tools

I want to store my virtual machines in the /home/xen directory, therefore I create it now:

mkdir -p /home/xen

We will use xen-tools to create virtual machines. xen-tools make it very easy to create virtual machines - please read this tutorial to learn more: http://www.howtoforge.com/xen_tools_xen_shell_argo. As mentioned before, the xen-tools package got installed together with the ubuntu-xen-server package.

Now we edit /etc/xen-tools/xen-tools.conf. This file contains the default values that are used by the xen-create-image script unless you specify other values on the command line. I changed the following values and left the rest untouched:

[...]

dir = /home/xen

[...]

dist = hardy # Default distribution to install.

[...]

gateway = xxx.xxx.xxx.xxx

netmask = 255.255.255.0

broadcast = xxx.xxx.xxx.255

[...]

passwd = 1

[...]

mirror = http://archive.ubuntu.com/ubuntu/

[...]

The dist line holds the default distribution that you want to install in a virtual machine. The contents of the /usr/lib/xen-tools directory reveals which distributions are available (see above).

The kernel line must contain our Xen kernel, and the initrd line its ramdisk. The default /etc/xen-tools/xen-tools.conf file has the values kernel = /boot/vmlinuz-uname -r and initrd = /boot/initrd.img-uname -r which automatically translate to the correct kernel and ramdisk, so we don't have to modify these lines.

The passwd = 1 line makes that you can specify a root password when you create a new guest domain. In the mirror line specify an Ubuntu mirror close to you.

Make sure you specify a gateway and netmask. If you don't, and you don't specify a gateway and netmask on the command line when using xen-create-image, your guest domains won't have networking even if you specified an IP address!

Now reboot the system:

reboot

If your system reboots without problems, then everything is fine! Run:

uname -r

and your new Xen kernel should show up

2.6.24-19-xen

Now let's create our first guest domain, rtplxap01, running Hardy Heron (hardy) with the IP address 172.16.110.230:

xen-create-image --hostname=rtplxap01 --size=10Gb --swap=1024Mb --ide \

--ip=172.16.110.230 --netmask=255.255.255.0 --gateway=172.16.110.1 --force \

--dir=/home/xen --memory=512Mb --arch=i386 --install-method=debootstrap \

--dist=hardy --mirror=http://archive.ubuntu.com/ubuntu/ --passwd

A lot of switches are unnecessary here because we specified the same details in /etc/xen-tools/xen-tools.conf but it shows that you can specify the desired settings either on the command line or in /etc/xen-tools/xen-tools.conf. Please make sure that you specify --ide, otherwise your virtual machine might not boot!

To learn more about the available options, take a look at the xen-create-image man page:

man xen-create-image

There should now be a rtplxap01 configuration file - /etc/xen/rtplxap01.cfg. Take a look at it to become familiar with virtual machine configuration files:

The file: parameter is now deprecated in Xen 3.2, therefore we must edit /etc/xen/rtplxap01.cfg now and replace file: with tap:aio: (otherwise you'll get an error similar to this one:

Error: Device 769 (vbd) could not be connected. losetup /dev/loop0 /home/xen/domains/xen1.example.com/swap.img failed

[...]

disk = [

'tap:aio:/home/xen/domains/rtplxap01/swap.img,hda1,w',

'tap:aio:/home/xen/domains/rtplxap01/disk.img,hda2,w',

]

[...]

Please note: if you have a dual-core CPU and want the virtual machine to use both CPU cores, please add the line vcpus = '2' to the configuration file, like this:

# Configuration file for the Xen instance rtplxap01, created

# by xen-tools 3.8 on Mon May 5 16:49:32 2008.

#

#

# Kernel + memory size

#

kernel = '/boot/vmlinuz-2.6.24-19-xen'

ramdisk = '/boot/initrd.img-2.6.24-19-xen'

memory = '512'

vcpus = '2'

[...]

To start the virtual machine, run:

xm create /etc/xen/rtplxap01.cfg

To log in on that virtual machine, run:

xm console rtplxap01

Type CTRL+] if you are at the console, or CTRL+5 if you're using PuTTY to go back to dom0

In the virtual machine (when you use it for the first time), run:

mv /lib/tls /lib/tls.disabled

Now back to the host system (dom0). To get a list of running virtual machines, type:

xm list

To shut down rtplxap01, run:

xm shutdown rtplxap01

If you want rtplxap01 to start automatically at the next boot of the system, run:

ln -s /etc/xen/rtplxap01.cfg /etc/xen/auto

Here are the most important Xen commands:

    • xm create -c /path/to/config - Start a virtual machine.

    • xm shutdown <name> - Stop a virtual machine.

    • xm destroy <name> - Stop a virtual machine immediately without shutting it down. It's as if you switch off the power button.

    • xm list - List all running systems.

    • xm console <name> - Log in on a virtual machine.

    • xm help - List of all commands.