#!/bin/bash
################################################################################
# mkxen - Thu Jul  5 14:28:42 PDT 2007 - Dale Bewley <dale / bewley.net>
#-------------------------------------------------------------------------------
# Simple script to install Xen DomU on a Fedora 7 box
#
# Notes:
#  o Installs happen on the bench LAN br101. Production requires move to br6.
#	# virsh shutdown <imgname>
#	# virsh dumpxml <imgname> | sed s/br101/br6/ > <imgname>.xml
#	# virsh define <imgname>.xml
#	# virsh startup <imgname>
# Bugs:
#  o This was missing from inittab on F7 so serial console was inaccessible.
#    I don't *think* it was the fault of my kickstart...
#	co:2345:respawn:/sbin/agetty xvc0 9600 vt100-nav
#  o Install of F7 was very slow with no output. Progress could be seen in
#    the http log of the RPM mirror. Be patient.
################################################################################

IMG_ROOT=/var/lib/xen/images
MAC_PREFIX='AA:00'
FILE_SUFFIX=.xen
FILE_SIZE=10
BRIDGE=br101

NAME=$1
OS=$2

if [ -z "$NAME" ]; then
    echo "Usage: $0 <imgname> <os>"
    echo "Default OS: f7"
    exit 1
fi

if [ -z "$OS" ]; then
	OS=f7
fi

KICKSTART=http://ks/ks-${OS}-xen.cfg

case "$OS" in
	fc6)
		MIRROR=http://ks/fedora/linux/core/6/x86_64
		;;
	f7)
		MIRROR=http://ks/fedora/linux/releases/7/Fedora/x86_64/os
		;;
	*)
		echo "OS Choices: fc6, f7"
		exit 1
		;;
esac


generate_mac () {
    # create a MAC based on the time
    date=$(printf "%02X\n", `date +%s`)
    MAC=$MAC_PREFIX
    i=1;
    while [ $i -lt 8 ]; do
        pair=`echo $date | cut -c${i}-$(($i + 1))`
        MAC=${MAC}:$pair
        ((i=$i + 2));
    done
}

generate_mac

virt-install --name=${NAME} \
    --ram=1024 \
    --file=${IMG_ROOT}/${NAME}${FILE_SUFFIX} \
    --file-size=${FILE_SIZE} \
    --nographics \
    --location=${MIRROR} \
    --extra-args="dhcp ks=${KICKSTART}" \
    --bridge=${BRIDGE} \
    --mac=${MAC} \
    --debug
