Xen Templates

From ConShell
Jump to: navigation, search

One way to maximize the efficiencies of a Xen-based system deployment model is to utilize templates. VMware talks about this in their whitepaper for ESX2 best practices.

Now Vmware does a good job of making template building easy, using virtual center. With Xen, you have to roll your own to some extent. Here is how I do it.

Bootstrap a DomU named <distname>-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-"
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"

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

Un-customize files

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
  • /etc/sysconfig/network-scripts/ifcfg-eth0 - mine looks like this

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

  • Other configuration files to consider tweaking include /etc/dhclient.conf & /etc/hosts
  • Files to remove
    • SSH Host key files (auto-created at boot time)
rm -f /etc/ssh/*host*

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


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