#!/bin/bash
######################################################################################
# Derived from the following pust by Alvin Cura
# https://bugs.launchpad.net/ubuntu/+source/xen-3.1/+bug/144631/comments/25
# Largely modified by Austin Godber (godber@uberhip.com)
######################################################################################
usage()
{
printf "USAGE:\t ${0} <xendom> [root part size] [swap part size]\n\n"
printf "\t ${0} ldapvm 5G 1G\n"
printf "\t Partition sizes must be acceptible dd sizes: 1G, 512M\n"
exit
}
if [ -z ${1} ]; then usage; else xendomu=${1}; fi
if [ -z ${2} ]; then rootsize="4G"; else rootsize=${2}; fi
if [ -z ${3} ]; then swapsize="512M"; else swapsize=${3}; fi
######################################
# Set up some variables
######################################
# Add an optional mirror
mirror="http://10.6.6.2/apt-cacher/us.archive.ubuntu.com/ubuntu/"
basedir="/vms/generated"
if [ ! -d "${basedir}" ]; then
printf "Creating ${basedir}\n"
mkdir -p ${basedir} || {echo "Unable to create base VM directory: ${basedir}"; exit}
fi
xendir="${basedir}/${xendomu}"
printf "Creating directory ${xendir}: "
mkdir -p -m 0777 ${xendir} || {echo "Unable to create VM directory: ${xendir}"; exit}
printf "done.\n"
######################################
# Prepare Block Devices
######################################
printf "Creating sparse root volume ${xendir}/root.img with size ${rootsize}: "
dd if=/dev/zero of=${xendir}/root.img bs=1 count=0 seek=${rootsize}
printf "done.\n"
printf "Creating swap volume ${xendir}/swap.img with size ${swapsize}: "
dd if=/dev/zero of=${xendir}/swap.img bs=${swapsize} count=1
printf "done.\n"
printf "Creating ext3 filesystem on ${xendir}/root.img: "
mkfs.ext3 -F ${xendir}/root.img
printf "done.\n"
printf "Creating swap partition on ${xendir}/swap.img: "
mkswap ${xendir}/swap.img
printf "done.\n"
printf "Mounting ${xendir}/root.img on /tmp/${xendomu}: "
mkdir /tmp/${xendomu}
mount ${xendir}/root.img /tmp/${xendomu} -o loop
printf "done.\n"
######################################
# Fill it up with Linuxy Goodness
######################################
printf "Debootstrapping ${xendomu}: "
debootstrap gutsy /tmp/${xendomu} ${mirror}
printf "done.\n"
printf "Copying modules to ${xendomu}: "
cp -r /lib/modules/`uname -r` /tmp/${xendomu}/lib/modules/
printf "done.\n"
printf "Setting up fstab: "
cat > /tmp/${xendomu}/etc/fstab <<EOF
/dev/sda1 / ext3 rw,errors=remount-ro 0 1
/dev/sda2 none swap defaults 0 0
none /proc proc defaults 0 0
EOF
printf "done.\n"
printf "Setting up hostname: "
echo "${xendomu}" > /tmp/${xendomu}/etc/hostname
printf "done.\n"
printf "Setting up hosts: "
printf "127.0.0.1\tlocalhost\n127.0.1.1\t${xendomu}\n" > /tmp/${xendomu}/etc/hosts
printf "done.\n"
printf "Setting up network interfaces: "
cat > /tmp/${xendomu}/etc/network/interfaces <<EOF
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
EOF
printf "done.\n"
printf "Setting up apt sources: "
echo "deb http://archive.ubuntu.com/ubuntu gutsy main universe" > /tmp/${xendomu}/etc/apt/sources.list
printf "done.\n"
printf "Updating apt: "
chroot /tmp/${xendomu} apt-get update
printf "done.\n"
# This is a Xen compatability thing, Xen Paravirt doesn't work with
# TLS (Thread Local Storage) Libraries, moving them is the common work
# around.
printf "Disabling thread local storage libraries: "
mv /tmp/${xendomu}/lib/tls /tmp/${xendomu}/lib/tls.disabled
printf "done.\n"
# These are workarounds for Ubuntu 7.10 bugs, the bugs require
# slight guest (DomU) modification
printf "Doing tweaks required for Ubuntu 7.10 Dom0 in DomU: "
chroot /tmp/${xendomu} update-rc.d -f hwclock.sh remove
chroot /tmp/${xendomu} update-rc.d -f hwclockfirst.sh remove
chroot /tmp/${xendomu} rm /etc/udev/rules.d/85-hwclock.rules
printf "done.\n"
printf "Unmounting /tmp/${xendomu} and removing mount point: "
umount /tmp/${xendomu}
rmdir /tmp/${xendomu}
printf "done.\n"
######################################
# Generate the Xen Configuration File
######################################
printf "Generating Xen config file: "
cat > ${xendir}/${xendomu}.cfg <<EOCONF
kernel = "/boot/vmlinuz-`uname -r`"
ramdisk = "/boot/initrd.img-`uname -r`"
builder='linux'
memory = 256
name = "${xendomu}"
vcpus = 1
#vif = [ 'mac=00:16:3e:61:a6:b4' ]
vif = [ 'bridge=xenbr0' ]
disk = [ 'file:${xendir}/root.img,sda1,w', 'file:${xendir}/swap.img,sda2,w' ]
root = "/dev/sda1 ro"
extra='xencons=tty1' #Ubuntu 7.10 bug workaround
EOCONF
printf "done.\n"
printf "Your Xen Virtual Machine has been created, it can be found here: \n"
printf "\t${xendir}\n"