Installing Xen on CentOS

To install Xen, we simply run:

[root@host]# yum install kernel-xen xen

This installs Xen and a Xen kernel on our CentOS system. Afterwards, we can find our new Xen kernel (vmlinuz-2.6.18-8.1.4.el5xen) and its ramdisk (initrd-2.6.18-8.1.4.el5xen.img) in the /boot directory:

[root@host]# ls -l /boot/

Before we can boot the system with the Xen kernel, we must tell the bootloader GRUB about it. We open /boot/grub/menu.lst:

[root@host]# vi /boot/grub/menu.lst

and add the following stanza above all other kernel stanzas:

[...]
title CentOS (2.6.18-8.1.4.el5xen)
root (hd0,0)
kernel /xen.gz-2.6.18-8.1.4.el5
module
/vmlinuz-2.6.18-8.1.4.el5xen ro
root=/dev/VolGroup00/LogVol00
module
/initrd-2.6.18-8.1.4.el5xen.img
[...]

Then change the value of default to 0:

[...]
default=0
[...] 

The complete /boot/grub/menu.lst should look something like this:

# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE: You have a /boot partition. This means that
# all kernel and initrd paths are relative to /boot/, eg.
# root (hd0,0)
# kernel /vmlinuz-version ro root=/dev/VolGroup00/LogVol00
# initrd /initrd-version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.18-8.1.4.el5xen)
root (hd0,0)
kernel /xen.gz-2.6.18-8.1.4.el5
module
/vmlinuz-2.6.18-8.1.4.el5xen ro root=/dev/VolGroup00/LogVol00
module
/initrd-2.6.18-8.1.4.el5xen.img
title CentOS (2.6.18-8.1.1.el5)
root (hd0,0)
kernel /vmlinuz-2.6.18-8.1.1.el5
ro root=/dev/VolGroup00/LogVol00
initrd
/initrd-2.6.18-8.1.1.el5.img
title CentOS (2.6.18-8.el5)
root (hd0,0)
kernel /vmlinuz-2.6.18-8.el5 ro
root=/dev/VolGroup00/LogVol00
initrd /initrd-2.6.18-8.el5.img 

Afterwards, we reboot the system:

[root@host]# shutdown -r now 

The system should now automatically boot the new Xen kernel. After the system has booted, we can check that by running

[root@host]# uname -r
2.6.18-8.1.4.el5xen 

So it's really using the new Xen kernel!

We can now run:

[root@host]# xm list

to check if Xen has started. It should list Domain-0 (dom0):

[root@host]# xm list
Name ID Mem(MiB) VCPUs State Time(s)
Domain-0 0 350 1 r----- 94.4

CentOS comes with a nice tool called virt-install with which we can create virtual machines for Xen. To start it, we simply run

[root@host]# virt-install

The tools asks a few questions before it creates a virtual machine. I want to call my first virtual machine vm01, with 256MB RAM and a disk size of 4GB. I want to store it in the file /vm/vm01.img:

What is the name of your virtual machine? <-- vm01 
How much RAM should be allocated (in megabytes)? <-- 256 
What would you like to use as the disk (path)? <-- /vm/vm01.img 
How large would you like the disk (/vm/vm01.img) to be (in gigabytes)? <-- 4 
Would you like to enable graphics support? (yes or no) <-- no 
What is the install location? <-- http://wftp.tu-chemnitz.de/pub/linux/centos/5.0/os/i386  

The question about the graphics support refers to the installer, not the virtual machine itself! It is possible to start a graphical installer, but you'd have to connect to it via VNC. It's easier to use the text installer - it offers the same options, so I choose the text installer.

As install location, you should specify a mirror close to you where the installer can download all files needed for the installation of CentOS 5.0 in our virtual machine. You can find a list of CentOS mirrors here: http://www.centos.org/modules/tinycontent/index.php?id=13

After we have answered all questions, virt-install starts the normal CentOS 5.0 installer (in text mode) in our vm01 virtual machine. You already know the CentOS installer, so it should be no problem for you to finish the CentOS installation in vm01.

After the installation, we stay at the vm01 console. To leave it, type CTRL+] if you are at the console, or CTRL+5 if you're using PuTTY. You will then be back at the dom0 console.

virt-install has created the vm01 configuration file /etc/xen/vm01 for us (in dom0). It should look like this:

[root@host]# cat /etc/xen/vm01
# Automatically generated xen config file
name = "vm01"
memory = "256"
disk = [ 'tap:aio:/vm/vm01.img,xvda,w', ]
vif = [ 'mac=00:16:3e:13:e4:81, bridge=xenbr0', ]
uuid = "5aafecf1-dd66-401d-69cc-151c1cb8ac9e"
bootloader="/usr/bin/pygrub"
vcpus=1
on_reboot = 'restart'
on_crash = 'restart' 

Run

[root@host]# xm console vm01

to log in on that virtual machine again (type CTRL+] if you are at the console, or CTRL+5 if you're using PuTTY to go back to dom0), or use an SSH client to connect to it.

To get a list of running virtual machines, type

[root@host]# xm list 

The output should look like this:

[root@host]# xm list
Name ID Mem(MiB) VCPUs State Time(s)
Domain-0 0 259 1 r----- 1906.6
vm01 3 255 1 ------ 137.9

To shut down vm01, do this:

[root@host]# xm shutdown vm01 

To start vm01 again, run

[root@host]# xm create /etc/xen/vm01

If you want vm01 to start automatically at the next boot of the system, then do this:

[root@host]# ln -s /etc/xen/vm01 /etc/xen/auto 

Here are the most important Xen commands:

xm create -c /path/to/config - Start a virtual machine.
xm shutdown - Stop a virtual machine.
xm destroy - 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 - Log in on a virtual machine.
xm help - List of all commands.

If you would like to use kickstart you can use virt-install on the command line like this:

[root@host]# virt-install --paravirt --name cybervirt1 --ram 512 --file /etc/xen/cybervirt1.img --file-size 20 --nographics --location http://konstantin.planetdiscover.com/CentOS5.2/ --extra-args="ks=http://konstantin.planetdiscover.com/ks.cfg"

If the server has more than one network interface make sure you add them all in the /etc/xen/vm01 file:

name = "pub1-53"
uuid = "d78d5d81-131a-6ec6-fbc3-ac2184a7cba7"
maxmem = 3968
memory = 3968
vcpus = 2
bootloader = "/usr/bin/pygrub"
on_poweroff = "destroy"
on_reboot = "restart"
on_crash = "restart"
vfb = [ ]
disk = [ "tap:aio:/domu/pub1-53,xvda,w" ]
vif = [ "mac=00:16:3e:4c:cb:5c,bridge=xenbr0",mac=00:16:3e:4c:cb:5d,bridge=xenbr1" ] 

If you need to resize the file system on an instance shut down the XenU and run:

[root@host]# dd if=/dev/zero bs=1M count=1024 >> filesystem.image
[root@host]# e2fsck -f filesystem.image
[root@host]# resize2fs filesystem.image
[root@host]# e2fsck -f filesystem.image