Create XEN image without xen-create-image

#!/bin/bash

if [ $# -eq 0 ]; then
echo "USAGE $0 XEN_NAME DISK_SIZE RAM_SIZE DIST_NAME"
echo "Example: $0 srv1 4096 256 edgy"
exit
fi

XEN_NAME=$1
DISK_SIZE=$2
RAM_SIZE=$3
DIST_NAME=$4

# prepare directories
if [ ! -d /home/xen ]; then
mkdir /home/xen
fi

if [ ! -d /home/xen/domains ]; then
mkdir /home/xen/domains
fi

if [ ! -d /home/xen/domains/$XEN_NAME ]; then
mkdir /home/xen/domains/$XEN_NAME
fi

# create images
dd if=/dev/zero of=/home/xen/domains/$XEN_NAME/disk.img bs=1M count=$DISK_SIZE
dd if=/dev/zero of=/home/xen/domains/$XEN_NAME/swap.img bs=1M count=$RAM_SIZE
mke2fs -F -j /home/xen/domains/$XEN_NAME/disk.img
mkswap /home/xen/domains/$XEN_NAME/swap.img
mount -o loop /home/xen/domains/$XEN_NAME/disk.img /mnt

# install
debootstrap $DIST_NAME /mnt
cp -ax /lib/modules/$(uname -r) /mnt/lib/modules

# create fstab
cat << EOF > /mnt/etc/fstab
/dev/sda1 / ext3 errors=remount-ro 0 1
proc /proc proc rw,nodev,nosuid,noexec 0 0
/dev/sda2 none swap sw 0 0
EOF

# create network interfaces
cat << EOF > /mnt/etc/network/interfaces
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
EOF

# set hostname
echo $XEN_NAME > /mnt/etc/hostname

umount /mnt/

# create xen config file
cat << EOF > /etc/xen/$XEN_NAME
kernel = '/boot/vmlinuz-$(uname -r)'
ramdisk = '/boot/initrd.img-$(uname -r)'
memory = '$RAM_SIZE'
root = '/dev/sda1 ro'
disk = [ 'file:/home/xen/domains/$XEN_NAME/disk.img,sda1,w', 'file:/home/xen/domains/$XEN_NAME/swap.img,sda2,w' ]
name = '$XEN_NAME'
dhcp = 'dhcp'
vif = [ '' ]
on_poweroff = 'destroy'
on_reboot = 'restart'
on_crash = 'restart'