-#!/bin/bash
+#!/bin/sh
-export PATH
+# If this script is interrupted, it may not clean up after itself.
+# In particular, it may leave ${TARGET}1 mounted somewhere in tmp.
+# Unmount it, then kpartx -d $TARGET to clean up.
-HOSTNAME="$2"
-DIST="$3"
-MIRROR="$4"
-IMAGESIZE="$5"
-IP="$6"
+set -e
-VOLUME=/dev/mapper/xenvg-d_${HOSTNAME}_hda
+export PATH #WTF?
-cat > $VOLUME <<EOF
-HOSTNAME=$HOSTNAME
-DIST=$DIST
-MIRROR=$MIRROR
-IMAGESIZE=$IMAGESIZE
-IP=$IP
-return
-EOF
+NAME="$1"
+export HOSTNAME="$2"
+export DIST="$3"
+export MIRROR="$4"
+export FSSIZE="$5" # in megabytes
+SWAPSIZE="$6" # in megabytes
+export IP="$7"
-xm create autoinstall.cfg target=$HOSTNAME guestip=$IP
+export ARCH=amd64
+VGNAME=xenvg
+
+#LVSIZE=`lvs --noheadings --units M --nosuffix -o Size /dev/xenvg/d_foobar_hda | sed 's/\..*//'`
+
+if [ -z "$IP" ]; then
+ IP=`host $HOSTNAME.xvm.mit.edu | sed 's/.* //' | head -n 1`
+fi
+
+/sbin/lvcreate -L $(($FSSIZE+$SWAPSIZE))M -n "${NAME}_hda" $VGNAME
+
+export TARGET=/dev/mapper/xenvg-"$(echo "${NAME}_hda" | sed -e 's/-/--/g')"
+
+/usr/sbin/sipb-xen-create-image