Create an Ubuntu 10.04 Xen Guest Image via Debootstrap

Comments

This is a lot easier than everyone makes it out to be. I’m doing this post because I was looking around at other tutorials and saw crap like having to hack scripts from Karmic and stuff like that. What the hell. Anyway, here’s a step-by-step guide to create a Lucid Lynx Xen-ready image (pvgrub-ready) using debootstrap. Debootstrap is a Debian/Ubuntu program but I believe that there is a way to install it on Fedora/Centos systems as well (at the time of writing I haven’t really looked, though).

Before we start, a quick disclaimer: while these steps will create a basic, functioning Lucid image, you’ll need to test it thoroughly for your Xen environment before deploying, especially in a production environment. Now, lets begin:

Create a blank .img file and format it

This will be used as a container for the image. Run the following commands:

dd if=/dev/zero of=Ubuntu10-04.i386.img bs=1024k seek=1024 count=0
mkfs.ext3 Ubuntu10-04.i386.img

While I am formatting this as ext3, you can use ext4 if you want, it doesn’t really matter.

Mount the image

mount -o loop Ubuntu10-04.i386.img /mnt/guest-image

Debootstrap it

debootstrap --arch=i386 lucid /mnt/guest-image

Replace ‘i386’ with ‘amd64’ if you want a 64bit image. This step can take some time depending on the speed of your connection. You can add the URL of a nearby mirror if you want, as I am in the UK I used http://uk.archive.ubuntu.com/ubuntu/. My debootstrap command looked like the following:

debootstrap --arch=i386 lucid /mnt/guest-image http://uk.archive.ubuntu.com/ubuntu/

Chroot to the mount

chroot /mnt/guest-image

Update the apt sources list

The exact contents will depend on your nearest mirror. I’m not going to paste them here, but if you are using Lucid on your desktop/laptop (like I am) then you can just copy/paste the contents. Otherwise, just take an Ubuntu list and change the name eg replace all the “hardy”s with “lucid”. Run the following afterwards:

aptitude update

This will grab a list of all the available packages.

Install the kernel and grub

Bootstrapping leaves you with a very basic image. It doesn’t even install a kernel or grub. Lets solve that:

aptitude install linux-image-virtual grub

Create the menu.lst file

This is so that the VPS uses the correct kernel (ie, you can have multiple kernels and swap between them if you really wanted to). Create /boot/grub/menu.lst and put the following in it:

default=1
 	timeout=5
 	hiddenmenu
 	title Lucid Xen Kernel (2.6.32-xx-generic-pae)
 	root (hd0)
 	kernel /boot/vmlinuz-2.6.32-xx-generic-pae ro root=/dev/xvda1 console=hvc0
 	initrd /boot/initrd.img-2.6.32-xx-generic-pae

Note that you’ll need to replace “xx” with the version of the kernel that was installed (at time of writing it was 24).

Also, if you are creating a 64-bit image, it doesn’t install a ‘generic-pae’ kernel, you’ll need to use the following lines instead:

kernel /boot/vmlinuz-2.6.32-xx-server ro root=/dev/xvda1 console=hvc0
initrd /boot/initrd.img-2.6.32-xx-server

Don’t forget to replace ‘xx’ with the latest version number!

Set up the console

The nice thing about Xen images that you can console onto them if you can’t access them via SSH. However, we need to set that up to work correctly. Create the /etc/init/hvc0.conf file and put the following in it:

# hvc0 - getty
#
# This service maintains a getty on hvc0 from the point the system is
# started until it is shut down again.
start on stopped rc RUNLEVEL=[2345]
stop on runlevel [!2345]
respawn exec /sbin/getty -L hvc0 9600 linux

Configure /etc/fstab

This is so that you can mount your partitions. The following is enough to get the VPS to work, so you can add to it as you see fit.

/dev/xvda1 / ext3 defaults 0 1
/dev/xvda2 none swap defaults 0 0
proc /proc proc defaults 0 0

If you formatted the image as ext4, change the first line to reflect this.

Set up the host information

Edit /etc/hosts and put in the following:

127.0.0.1 localhost 127.0.1.1 <hosthame>

Obviously, <hostname> needs to be replaced with a hostname. This hostname should also go into /etc/hostname. By default the bootstrap process puts the hostname of the machine it was run on into /etc/hostname.

Set up networking

Edit /etc/network/interfaces and add the following:

auto eth0
iface eth0 inet static
address 192.168.0.100
netmask 255.255.255.0
broadcast 192.168.0.255
gateway 192.168.0.1

Obviously, you’ll need to change the above for your own network. Next, add your nameserver(s) to /etc/resolv.conf. Bootstrap should have added the one on your current machine.

You should add a root password now, and maybe install some more software (I normally install ssh and wget) however that’s it for this guide. Exit the chroot and unmount the image, and now you have a functioning Lucid image :)

Next Post
Install Denyhosts on Centos

Previous Post
Check if Your CPU Supports Hardware Virtualization