Creating a Xen template

One way to increase the efficiencies of Xen based systems is to utilize templates. VMware talks about this in their whitepaper for ESX2 best practices.

With Xen, you have to create your own. Here is a straight forward guide for how to do it.

1. Bootstrap a DomU named -tpl (e.g. centos4-tpl).

I recommend using a file-backed VBD, but partition or LVM volume will work fine as well. Here is an example /etc/xen/centos4-tpl

kernel = "/boot/vmlinuz-2.6.12.6-xenU"
memory = 256
name = "centos4-tpl" 
disk = [  'file:/opt/xen/domains/centos4-tpl/diskimage,sdb1,w','file:/opt/xen/domains/centos4-tpl/swapimage,sdb2,w'  ]
root = "/dev/sdb1 ro"
dhcp="dhcp

This is just a normal system (DomU) install – see Centos-4 on Xen for an example. Un-customize files

2.Inside the VM, edit the following files

/etc/hosts
remove any address lines other than localhost

/etc/sysconfig/network
use a generic hostname which will be unique to each deployment

NETWORKING=yes
HOSTNAME=centos4-tpl-changeme.example.com

/etc/sysconfig/network-scripts/ifcfg-eth0
should look like this:

DEVICE=eth0
ONBOOT=yes
BOOTPROTO=dhcp

also important – remove any line starting with HWADDR, e.g.:

HWADDR=00:10:5A:XX:YY:ZZ

Other configuration files to consider tweaking include /etc/dhclient.conf & /etc/hosts

3. Files to remove:

– SSH Host key files (auto-created at boot time)

rm -f /etc/ssh/*host*

4. Shutdown the template VM

xm shutdown centos4-tpl

You might normally link your VMs into /etc/xen/auto. I recommend against this as the template VM can be left shutdown until/unless you want to update it, saving valuable RAM and CPU cycles.

Clone the virtual disk Now we can deploy from the template by cloning the data into a clean diskimage (or partition or LVM volume). Create the diskimage using an appropriate size (must be larger than the template). Oh -the nice thing here is that there is flexibility. For instance, you can have a file-based diskimage and clone the data onto LVM volumes. As long as you can mount the (virtual) disks, you can clone templatized systems.

Here we use /mnt/disk to mount the new system disk, and /mnt/image to mount the template disk.

First, mount the template disk.

mount -o loop /opt/xen/domains/centos4-tpl/diskimage /mnt/image

Next, create and mount the new system (DomU) disk space & swap space.

mkdir -p /opt/xen/domains/cloned
cd /opt/xen/domains/cloned
dd if=/dev/zero of=diskimage bs=1024k count=2048
dd if=/dev/zero of=swapimage bs=1024k count=256
mkfs.ext3 diskimage
mkswap swapimage
mkdir -p /mnt/disk
mount -o loop /opt/xen/domains/cloned/diskimage /mnt/disk

Create the exclude file in /tmp/XenCloneExclude

proc/*
users/*
tmp/*
lost+found/
etc/mtab

Copy the data across

rsync -av -SHWD --exclude-from="/tmp/XenCloneExclude" /mnt/image/ /mnt/disk

Chroot into the newly copied template and fixup certain files

chroot /mnt/disk /bin/bash

Fix the hostname, etc in the files we “un-customized” in the template.

Exit, unmount both the template image and volume

umount /mnt/disk
umount /mnt/image

Setup your Xen config and be on your way!

cd /etc/xen
cp centos4-tpl cloned
(edit cloned to change name and paths to disk and swap)
xm create -c cloned