Posted by Steve on Tue 6 Dec 2005 at 11:27
Xen 3.0 is the latest release of the popular virtualisation platform. It allows you to install "virtual servers" upon your base operating system, allowing you to create and run several instances of GNU/Linux upon one host. Here we'll take a simple overview at using Xen to install virtual servers.Xen Overview
Xen is a virtualisation technology, which allows you to setup a host which may run an arbitrary number of virtual operating systems. Unlike Qemu (which we covered a long time ago) or VMWare Xen expects the operating systems it hosts to be modified. (This limitation will eventually be removed when used with hardware which supports virtualisation.)
What does this mean? Well to be honest it means very little, in practise you're restricted in the operation systems you can install and host - GNU/Linux is fine, Microsoft Windows will not run.
Xen will only work upon x86 systems, sadly it is unlikely to ever be available for other platforms.
Getting started with Xen can be a little confusing, partly because there are several unfamiliar terms used in the documentation, and partly because of the complexity of actually installing it.
To help ease this introduction this is what we're going to do:
- Taking a single real computer called "lappy.my.flat" and installing Xen 3.0 upon it.
- Once Xen has been installed this will be the host. In Xen documentation this is referred to as "dom0".
- After the host is running we'll create and install a new virtual Debian installation:
- vm01.my.flat - A minimal installation of Debian Sarge.
Xen is a fairly non-standard package to install upon a system. Rather than being a binary or two and some support files, it actually contains its own Linux kernel.
This means that in order to install it you will need to either rebuild a complete Linux kernel yourself, or install the binaries available for download, then reboot into it.
Clearly this is a more involved installation than other packages, however the process is fairly simple so long as you are aware of the potential problems and side effects:
- If your host system uses some proprietary kernel modules they will need to be rebuilt, or they will not work.
- If you make any mistakes and aren't comfortable upgrading a kernel you should probably avoid this software.
- Installation on a remote host is not for the faint of heart.
That said I found the installation fairly painless. YMMV
To get started you'll need to download the software.
Currently there do not appear appear to be any Debian packages available, so we're going to install the binaries from the Xen homepage:root@lappy:~# mkdir /home/xen/bin root@lappy:~# cd /home/xen/bin root@lappy:/home/xen/bin# wget \ http://bits.xensource.com/Xen/latest/xen-3.0.0-install-x86_32.tgz root@lappy:/home/xen/bin# tar -pzxf xen-3.0.0-install-x86_32.tgz root@lappy:/home/xen/bin# cd xen-unstable-install root@lappy:/home/xen/bin/xen-unstable-install# sh ./install.sh
After running the install.sh script we'll have the kernel, modules, and tools installed upon the host system. But before trying out our new kernel we need to complete two more steps. Firstly we need to ensure that the Xen init scripts run when we reboot by running the following two commands:update-rc.d xend defaults update-rc.d xendomains defaults
The next step is to disable the TLS libraries upon the host system. This can be achived by running:mv /lib/tls /lib/tls.disabled
After performing this step we're ready to reboot into our new kernel - but to do that we'll first need to make it available to our boot loader.
For systems which are using the Grub boot loader this means adding the kernel to the startup menu by editing the file /boot/grub/menu.lst. To do this add the following lines to the end of that file:title Xen 3.0 / XenLinux 2.6 kernel /boot/xen-3.0.gz dom0_mem=262144 module /boot/vmlinuz-2.6-xen0 root=/dev/hda1 ro console=tty0
NOTE #1: If your root is not /dev/hda1 you will need to adjust that line - you should be able to see what your root is from examining the entries already present in your menu.lst file.
NOTE #2: For users of other boot loaders you will need to add your kernel by following your documentation.
Creating Virtual Installations
Once your system has booted with the new Xen kernel running you should test that it works as expected. By this I mean test that your kernel modules for networking, hardware access, etc, all work correctly. If something is not working you'll most likely need to install Xen from source instead and that is very much beyond the scope of this introduction.
Assuming that everything is working correctly we're now ready to start working with Xen and creating the virtual installations.
Booting The Xen Images
As the operating systems we're going to install are virtual they will not have their own hardware. This means that we need to create disk images for them to use as their own "hard drives". (ie. The guest installations will believe they have /dev/hda - but in actual fact this disk is merely an ordinary file upon the host system.)
Once we've created these virtual files we'll install Debian Sarge upon them using the debootstrap package. The process looks like this:
- Create two large files, one for the hard drive, one for swap.
- Format/initialise them appropriately.
- Install Debian Sarge upon the disk image.
- Configure the installations with hostnames, networking addresses, etc.
- Ensure they have an installation of OpenSSH upon them.
- Configure Xen to boot them.
- Actually start them up.
Each of these steps is fairly simple, but there are a lot of things to remember - so I wrote a small script to automate them all.
If you'd like to do this manually I found the following notes very useful. (Indeed my script was based upon performing these steps several times!)
The script I came up with is called create-xen-image.pl, and will perform all the basic setup we'll need. Whilst it isn't a great piece of code it will do the job, and any suggestions or improvements will be welcomed.
- Download create-xen-image.pl 8.0k
To ensure you have all the required software run:apt-get install bridge-utils iproute debootstrap
Once you have the software you may create a new image, vm01, with the following command:./create-xen-image.pl --hostname='vm01' \ --ip=192.168.1.91 --gateway=192.168.1.1 \ --network=192.168.1.0 --mask=255.255.255.0 \ --dir=/home/xen
This creates two files:
- /home/xen/domains/vm01/disk.img - The disk image.
- /home/xen/domains/vm01/swap.img - The swap image.
The disk.img file will be formatted as an ext3 filesystem and will have static networking details setup along with a hostname, APT sources.list, etc.
Once the command has finished you should follow the instructions displayed at the end of the process to setup a root password, and make any additional tweaks which you might desire. The virtual instance should be setup to start OpenSSH upon boot - so you can connect to it as soon as it boots successfully.
Further Information & Notes
The virtual installations which you'll run are each described in a configuration file inside /etc/xen - commonly these are named after the hostname of the servers.
If you used the create-xen-image.pl script presented above you will have a file created named after the hostname, eg /etc/xen/vm01.cfg. The simplest configuration file looks like this:kernel = "/boot/vmlinuz-2.6.12-xenU" memory = 96 name = "vm01" #dhcp = "dhcp" disk = [ 'file:/home/xen/domains/vm01/diskimage,sda1,w','file:/home/xen/domains/vm01/swapimage,sda2,w' ] root = "/dev/sda1 ro"
Here dhcp is commented out because the creation scrip setup a static IP address for the new host. If you wish to change to using DHCP you will need to edit the creation script and adjust the information it writes to /etc/network/interfaces in the new disk image.
(When you install Xen several sample configuration files are installed into /etc/xen. Examing these will show most of the available configuration options.)
To start the virtual installation run:xm create vm01.cfg -c
Here the "create" keyword is used to create a running instance, rather than create a fresh installation. You needn't worry that you're wiping anything if you wish to repeat the command!
Running this command will connect you to the console of the booting kernel (omit the -c flag to avoid this) once it has finished booting you should be able to ping your host, and connect to it via SSH.
To dismiss the console displaying the bootup messages press ^]. You may reconnect to the later by running "xm console vm01".
To shutdown the virtual server you may login as root and run /sbin/shutdown as normal or from the host run:xm shutdown vm01
If you were wishing to run Xen on a production basis you should examine using LVM as a means of speeding things up. Personally I found running using plain disk images acceptable upon my hardware.
In terms of speed Xen is faster than both Qemu and VMWare, and I can comfortably run four or five virtual installations upon a PIII@1200Mhz with 1Gb of RAM. The only drawback of using Xen I've noticed so far is the lack of X11.