#!/bin/bash
# Usage: no command-line args.
# "arguments" come in environment:
#  TARGET a device filename
#  FSSIZE the desired filesystem size in mebibytes (rest will be swap)
#  ARCH, DIST, MIRROR
#  HOSTNAME, IP
#
# Optional: GATEWAY, NETMASK.  These default to values in invirt-getconf, if installed

set -e

if [ "$GATEWAY" = "" ]; then
    GATEWAY=$(invirt-getconf autoinstaller.gateway)
fi

if [ "$NETMASK" = "" ]; then
    NETMASK=$(invirt-getconf autoinstaller.netmask)
fi

echo ,"${FSSIZE}"$',L,*\n,,S\n;\n;' | sfdisk -uM -H 255 -S 63 "$TARGET"

dmsetup info "$TARGET" 2>/dev/null && kpartx -a "$TARGET"

xen-create-image --image-dev "${TARGET}1" --swap-dev "${TARGET}2" --fs ext3 --ide \
 --hostname "$HOSTNAME" --ip="$IP" --netmask="$NETMASK" --gateway="$GATEWAY" \
 --arch "$ARCH" --dist "$DIST" --mirror "$MIRROR" --cache=yes --verbose

dmsetup info "$TARGET" 2>/dev/null && kpartx -d "$TARGET"

exit 0
