Merge pull request #145 from cloudbuilders/screen-name

change screen name to stack
diff --git a/.gitignore b/.gitignore
index 22a7898..e482090 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,5 @@
 proto
 *~
+*.log
+src
 localrc
diff --git a/files/apts/nova b/files/apts/nova
index 8ae74a2..17eb877 100644
--- a/files/apts/nova
+++ b/files/apts/nova
@@ -1,6 +1,7 @@
 dnsmasq-base
 kpartx
 parted
+arping # used for send_arp_for_ha option in nova-network
 mysql-server
 python-mysqldb
 kvm
diff --git a/stack.sh b/stack.sh
index c7edb11..cba2db2 100755
--- a/stack.sh
+++ b/stack.sh
@@ -843,6 +843,10 @@
 # Using the cloud
 # ===============
 
+echo ""
+echo ""
+echo ""
+
 # If you installed the horizon on this server, then you should be able
 # to access the site using your browser.
 if [[ "$ENABLED_SERVICES" =~ "horizon" ]]; then
diff --git a/tools/build_kvm.sh b/tools/build_kvm.sh
deleted file mode 100755
index 1b33926..0000000
--- a/tools/build_kvm.sh
+++ /dev/null
@@ -1,402 +0,0 @@
-#!/usr/bin/env bash
-
-# exit on error to stop unexpected errors
-set -o errexit
-
-# Make sure that we have the proper version of ubuntu
-UBUNTU_VERSION=`cat /etc/lsb-release | grep CODENAME | sed 's/.*=//g'`
-if [ ! "oneiric" = "$UBUNTU_VERSION" ]; then
-    if [ ! "natty" = "$UBUNTU_VERSION" ]; then
-        echo "This script only works with oneiric and natty"
-        exit 1
-    fi
-fi
-
-# Echo commands
-set -o xtrace
-
-# Keep track of the current directory
-TOOLS_DIR=$(cd $(dirname "$0") && pwd)
-TOP_DIR=$TOOLS_DIR/..
-
-# Where to store files and instances
-KVMSTACK_DIR=${KVMSTACK_DIR:-/opt/kvmstack}
-
-# Where to store images
-IMAGES_DIR=$KVMSTACK_DIR/images
-
-# Create images dir
-mkdir -p $IMAGES_DIR
-
-# Move to top devstack dir
-cd $TOP_DIR
-
-# Abort if localrc is not set
-if [ ! -e ./localrc ]; then
-    echo "You must have a localrc with ALL necessary passwords defined before proceeding."
-    echo "See stack.sh for required passwords."
-    exit 1
-fi
-
-# Source params
-source ./stackrc
-
-# Configure the root password of the vm to be the same as ``ADMIN_PASSWORD``
-ROOT_PASSWORD=${ADMIN_PASSWORD:-password}
-
-
-# Base image (natty by default)
-DIST_NAME=${DIST_NAME:-natty}
-IMAGE_FNAME=$DIST_NAME.raw
-
-# Name of our instance, used by libvirt
-GUEST_NAME=${GUEST_NAME:-kvmstack}
-
-# Original version of built image
-BASE_IMAGE=$KVMSTACK_DIR/images/$DIST_NAME.raw
-
-# Copy of base image, which we pre-install with tasty treats
-VM_IMAGE=$IMAGES_DIR/$DIST_NAME.$GUEST_NAME.raw
-
-# Mop up after previous runs
-virsh destroy $GUEST_NAME || true
-
-# Where this vm is stored
-VM_DIR=$KVMSTACK_DIR/instances/$GUEST_NAME
-
-# Create vm dir
-mkdir -p $VM_DIR
-
-# Mount point into copied base image
-COPY_DIR=$VM_DIR/copy
-mkdir -p $COPY_DIR
-
-# Create the base image if it does not yet exist
-if [ ! -e $IMAGES_DIR/$IMAGE_FNAME ]; then
-    cd $TOOLS_DIR
-    ./make_image.sh -m -r 5000  $DIST_NAME raw
-    mv $DIST_NAME.raw $BASE_IMAGE
-    cd $TOP_DIR
-fi
-
-# Create a copy of the base image
-if [ ! -e $VM_IMAGE ]; then
-    cp -p $BASE_IMAGE $VM_IMAGE
-fi
-
-# Unmount the copied base image
-function unmount_images() {
-    # unmount the filesystem
-    while df | grep -q $COPY_DIR; do
-        umount $COPY_DIR || echo 'ok'
-        sleep 1
-    done
-}
-
-# Unmount from failed runs
-unmount_images
-
-# Ctrl-c catcher
-function kill_unmount() {
-    unmount_images
-    exit 1
-}
-
-# Install deps if needed
-dpkg -l kvm libvirt-bin kpartx || apt-get install -y --force-yes kvm libvirt-bin kpartx
-
-# Let Ctrl-c kill tail and exit
-trap kill_unmount SIGINT
-
-# Where Openstack code will live in image
-DEST=${DEST:-/opt/stack}
-
-# Mount the file system
-mount -o loop,offset=32256 $VM_IMAGE  $COPY_DIR
-
-# git clone only if directory doesn't exist already.  Since ``DEST`` might not
-# be owned by the installation user, we create the directory and change the
-# ownership to the proper user.
-function git_clone {
-    if [ ! -d $2 ]; then
-        sudo mkdir $2
-        sudo chown `whoami` $2
-        git clone $1 $2
-        cd $2
-        # This checkout syntax works for both branches and tags
-        git checkout $3
-    fi
-}
-
-# Make sure that base requirements are installed
-cp /etc/resolv.conf $COPY_DIR/etc/resolv.conf
-chroot $COPY_DIR apt-get update
-chroot $COPY_DIR apt-get install -y --force-yes `cat files/apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"`
-chroot $COPY_DIR apt-get install -y --download-only rabbitmq-server libvirt-bin mysql-server
-chroot $COPY_DIR pip install `cat files/pips/*`
-
-# Clean out code repos if directed to do so
-if [ "$CLEAN" = "1" ]; then
-    rm -rf $COPY_DIR/$DEST
-fi
-
-# Cache openstack code
-mkdir -p $COPY_DIR/$DEST
-git_clone $NOVA_REPO $COPY_DIR/$DEST/nova $NOVA_BRANCH
-git_clone $GLANCE_REPO $COPY_DIR/$DEST/glance $GLANCE_BRANCH
-git_clone $KEYSTONE_REPO $COPY_DIR/$DESTkeystone $KEYSTONE_BRANCH
-git_clone $NOVNC_REPO $COPY_DIR/$DEST/noVNC $NOVNC_BRANCH
-git_clone $HORIZON_REPO $COPY_DIR/$DEST/horizon $HORIZON_BRANCH $HORIZON_TAG
-git_clone $NOVACLIENT_REPO $COPY_DIR/$DEST/python-novaclient $NOVACLIENT_BRANCH
-git_clone $OPENSTACKX_REPO $COPY_DIR/$DEST/openstackx $OPENSTACKX_BRANCH
-git_clone $KEYSTONE_REPO $COPY_DIR/$DEST/keystone $KEYSTONE_BRANCH
-git_clone $NOVNC_REPO $COPY_DIR/$DEST/noVNC $NOVNC_BRANCH
-
-# Back to devstack
-cd $TOP_DIR
-
-# Unmount the filesystems
-unmount_images
-
-# Network configuration variables
-BRIDGE=${BRIDGE:-br0}
-GUEST_IP=${GUEST_IP:-192.168.1.50}
-GUEST_CIDR=${GUEST_CIDR:-$GUEST_IP/24}
-GUEST_NETMASK=${GUEST_NETMASK:-255.255.255.0}
-GUEST_GATEWAY=${GUEST_GATEWAY:-192.168.1.1}
-GUEST_MAC=${GUEST_MAC:-"02:16:3e:07:69:`printf '%02X' $(echo $GUEST_IP | sed "s/.*\.//")`"}
-GUEST_RAM=${GUEST_RAM:-1524288}
-GUEST_CORES=${GUEST_CORES:-1}
-
-# libvirt.xml configuration
-LIBVIRT_XML=$VM_DIR/libvirt.xml
-cat > $LIBVIRT_XML <<EOF
-<domain type='kvm'>
-    <name>$GUEST_NAME</name>
-    <memory>$GUEST_RAM</memory>
-    <os>
-        <type>hvm</type>
-        <bootmenu enable='yes'/>
-    </os>
-    <features>
-        <acpi/>
-    </features>
-    <vcpu>$GUEST_CORES</vcpu>
-    <devices>
-        <disk type='file'>
-            <driver type='qcow2'/>
-            <source file='$VM_DIR/disk'/>
-            <target dev='vda' bus='virtio'/>
-        </disk>
-
-        <interface type='bridge'>
-            <source bridge='$BRIDGE'/>
-            <mac address='$GUEST_MAC'/>
-        </interface>
-
-        <!-- The order is significant here.  File must be defined first -->
-        <serial type="file">
-            <source path='$VM_DIR/console.log'/>
-            <target port='1'/>
-        </serial>
-
-        <console type='pty' tty='/dev/pts/2'>
-            <source path='/dev/pts/2'/>
-            <target port='0'/>
-        </console>
-
-        <serial type='pty'>
-            <source path='/dev/pts/2'/>
-            <target port='0'/>
-        </serial>
-
-        <graphics type='vnc' port='-1' autoport='yes' keymap='en-us' listen='0.0.0.0'/>
-    </devices>
-</domain>
-EOF
-
-# Mount point for instance fs
-ROOTFS=$VM_DIR/root
-mkdir -p $ROOTFS
-
-# Make sure we have nbd-ness
-modprobe nbd max_part=63
-
-# Which NBD device to use?
-NBD=${NBD:-/dev/nbd5}
-
-# Clean up from previous runs
-umount $ROOTFS || echo 'ok'
-qemu-nbd -d $NBD || echo 'ok'
-
-# Clean up old runs
-cd $VM_DIR
-rm -f $VM_DIR/disk
-
-# Create our instance fs
-qemu-img create -f qcow2 -b $VM_IMAGE disk
-
-# Connect our nbd and wait till it is mountable
-qemu-nbd -c $NBD disk
-NBD_DEV=`basename $NBD`
-if ! timeout 60 sh -c "while ! [ -e /sys/block/$NBD_DEV/pid ]; do sleep 1; done"; then
-    echo "Couldn't connect $NBD"
-    exit 1
-fi
-
-# Mount the instance
-mount $NBD $ROOTFS -o offset=32256 -t ext4
-
-# Configure instance network
-INTERFACES=$ROOTFS/etc/network/interfaces
-cat > $INTERFACES <<EOF
-auto lo
-iface lo inet loopback
-
-auto eth0
-iface eth0 inet static
-        address $GUEST_IP
-        netmask $GUEST_NETMASK
-        gateway $GUEST_GATEWAY
-EOF
-
-# User configuration for the instance
-chroot $ROOTFS groupadd libvirtd || true
-chroot $ROOTFS useradd stack -s /bin/bash -d $DEST -G libvirtd
-cp -pr $TOOLS_DIR/.. $ROOTFS/$DEST/devstack
-echo "root:$ROOT_PASSWORD" | chroot $ROOTFS chpasswd
-echo "stack:pass" | chroot $ROOTFS chpasswd
-echo "stack ALL=(ALL) NOPASSWD: ALL" >> $ROOTFS/etc/sudoers
-
-# Gracefully cp only if source file/dir exists
-function cp_it {
-    if [ -e $1 ] || [ -d $1 ]; then
-        cp -pRL $1 $2
-    fi
-}
-
-# Copy over your ssh keys and env if desired
-COPYENV=${COPYENV:-1}
-if [ "$COPYENV" = "1" ]; then
-    cp_it ~/.ssh $ROOTFS/$DEST/.ssh
-    cp_it ~/.ssh/id_rsa.pub $ROOTFS/$DEST/.ssh/authorized_keys
-    cp_it ~/.gitconfig $ROOTFS/$DEST/.gitconfig
-    cp_it ~/.vimrc $ROOTFS/$DEST/.vimrc
-    cp_it ~/.bashrc $ROOTFS/$DEST/.bashrc
-fi
-
-# pre-cache uec images
-for image_url in ${IMAGE_URLS//,/ }; do
-    IMAGE_FNAME=`basename "$image_url"`
-    if [ ! -f $IMAGES_DIR/$IMAGE_FNAME ]; then
-        wget -c $image_url -O $IMAGES_DIR/$IMAGE_FNAME
-    fi
-    cp $IMAGES_DIR/$IMAGE_FNAME $ROOTFS/$DEST/devstack/files
-done
-
-# Configure the runner
-RUN_SH=$ROOTFS/$DEST/run.sh
-cat > $RUN_SH <<EOF
-#!/usr/bin/env bash
-
-# Kill any existing screens
-killall screen
-
-# Install and run stack.sh
-sudo apt-get update
-sudo apt-get -y --force-yes install git-core vim-nox sudo
-if [ ! -d "$DEST/devstack" ]; then
-    git clone git://github.com/cloudbuilders/devstack.git $DEST/devstack
-fi
-cd $DEST/devstack && $STACKSH_PARAMS FORCE=yes ./stack.sh > /$DEST/run.sh.log
-echo >> /$DEST/run.sh.log
-echo >> /$DEST/run.sh.log
-echo "All done! Time to start clicking." >> /$DEST/run.sh.log
-cat $DEST/run.sh.log
-EOF
-chmod 755 $RUN_SH
-
-# Make runner launch on boot
-RC_LOCAL=$ROOTFS/etc/init.d/local
-cat > $RC_LOCAL <<EOF
-#!/bin/sh -e
-# Reboot if this is our first run to enable console log on $DIST_NAME :(
-if [ ! -e /root/firstlaunch ]; then
-    touch /root/firstlaunch
-    reboot -f
-    exit 0
-fi
-su -c "$DEST/run.sh" stack
-EOF
-chmod +x $RC_LOCAL
-chroot $ROOTFS sudo update-rc.d local defaults 80
-
-# Make our ip address hostnames look nice at the command prompt
-echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/$DEST/.bashrc
-echo "export PS1='${debian_chroot:+($debian_chroot)}\\u@\\H:\\w\\$ '" >> $ROOTFS/etc/profile
-
-# Give stack ownership over $DEST so it may do the work needed
-chroot $ROOTFS chown -R stack $DEST
-
-# Change boot params so that we get a console log
-sudo sed -e "s/quiet splash/splash console=ttyS0 console=ttyS1,19200n8/g" -i $ROOTFS/boot/grub/menu.lst
-sudo sed -e "s/^hiddenmenu//g" -i $ROOTFS/boot/grub/menu.lst
-
-# Set the hostname
-echo $GUEST_NAME > $ROOTFS/etc/hostname
-
-# We need the hostname to resolve for rabbit to launch
-if ! grep -q $GUEST_NAME $ROOTFS/etc/hosts; then
-    echo "$GUEST_IP $GUEST_NAME" >> $ROOTFS/etc/hosts
-fi
-
-# Unmount
-umount $ROOTFS || echo 'ok'
-qemu-nbd -d $NBD
-
-# Create the instance
-cd $VM_DIR && virsh create libvirt.xml
-
-# Tail the console log till we are done
-WAIT_TILL_LAUNCH=${WAIT_TILL_LAUNCH:-1}
-if [ "$WAIT_TILL_LAUNCH" = "1" ]; then
-    # Done creating the container, let's tail the log
-    echo
-    echo "============================================================="
-    echo "                          -- YAY! --"
-    echo "============================================================="
-    echo
-    echo "We're done launching the vm, about to start tailing the"
-    echo "stack.sh log. It will take a second or two to start."
-    echo
-    echo "Just CTRL-C at any time to stop tailing."
-
-    while [ ! -e "$VM_DIR/console.log" ]; do
-      sleep 1
-    done
-
-    tail -F $VM_DIR/console.log &
-
-    TAIL_PID=$!
-
-    function kill_tail() {
-        kill $TAIL_PID
-        exit 1
-    }
-
-    # Let Ctrl-c kill tail and exit
-    trap kill_tail SIGINT
-
-    echo "Waiting stack.sh to finish..."
-    while ! cat $VM_DIR/console.log | grep -q 'All done' ; do
-        sleep 5
-    done
-
-    kill $TAIL_PID
-
-    if grep -q "stack.sh failed" $VM_DIR/console.log; then
-        exit 1
-    fi
-    echo ""
-    echo "Finished - Zip-a-dee Doo-dah!"
-fi
diff --git a/tools/build_libvirt.sh b/tools/build_libvirt.sh
index 5ae8fda..fc281d3 100755
--- a/tools/build_libvirt.sh
+++ b/tools/build_libvirt.sh
@@ -12,6 +12,27 @@
     fi
 fi
 
+# Clean up any resources that may be in use
+cleanup() {
+    set +o errexit
+    unmount_images
+
+    if [ -n "$ROOTFS" ]; then
+        umount $ROOTFS/dev
+        umount $ROOTFS
+    fi
+
+    # Release NBD devices
+    if [ -n "$NBD" ]; then
+        qemu-nbd -d $NBD
+    fi
+
+    # Kill ourselves to signal any calling process
+    trap 2; kill -2 $$
+}
+
+trap cleanup SIGHUP SIGINT SIGTERM
+
 # Echo commands
 set -o xtrace
 
@@ -100,9 +121,6 @@
 # Install deps if needed
 dpkg -l kvm libvirt-bin kpartx || apt-get install -y --force-yes kvm libvirt-bin kpartx
 
-# Let Ctrl-c kill tail and exit
-trap kill_unmount SIGINT
-
 # Where Openstack code will live in image
 DEST=${DEST:-/opt/stack}
 
@@ -156,6 +174,7 @@
 
 # Network configuration variables
 GUEST_NETWORK=${GUEST_NETWORK:-1}
+GUEST_RECREATE_NET=${GUEST_RECREATE_NET:-yes}
 
 GUEST_IP=${GUEST_IP:-192.168.$GUEST_NETWORK.50}
 GUEST_CIDR=${GUEST_CIDR:-$GUEST_IP/24}
@@ -176,8 +195,10 @@
 </network>
 EOF
 
-virsh net-destroy devstack-$GUEST_NETWORK || true
-virsh net-create $VM_DIR/net.xml
+if [[ "$GUEST_RECREATE_NET" == "yes" ]]; then
+    virsh net-destroy devstack-$GUEST_NETWORK || true
+    virsh net-create $VM_DIR/net.xml
+fi
 
 # libvirt.xml configuration
 LIBVIRT_XML=$VM_DIR/libvirt.xml
@@ -239,26 +260,35 @@
 # Create our instance fs
 qemu-img create -f qcow2 -b $VM_IMAGE disk
 
+# Finds the next available NBD device
+# Exits script if error connecting or none free
+# map_nbd image
+# returns full nbd device path
+function map_nbd {
+    for i in `seq 0 15`; do
+        if [ ! -e /sys/block/nbd$i/pid ]; then
+            NBD=/dev/nbd$i
+            # Connect to nbd and wait till it is ready
+            qemu-nbd -c $NBD $1
+            if ! timeout 60 sh -c "while ! [ -e ${NBD}p1 ]; do sleep 1; done"; then
+                echo "Couldn't connect $NBD"
+                exit 1
+            fi
+            break
+        fi
+    done
+    if [ -z "$NBD" ]; then
+        echo "No free NBD slots"
+        exit 1
+    fi
+    echo $NBD
+}
+
 # Make sure we have nbd-ness
 modprobe nbd max_part=63
 
 # Set up nbd
-for i in `seq 0 15`; do
-    if [ ! -e /sys/block/nbd$i/pid ]; then
-        NBD=/dev/nbd$i
-        # Connect to nbd and wait till it is ready
-        qemu-nbd -c $NBD disk
-        if ! timeout 60 sh -c "while ! [ -e ${NBD}p1 ]; do sleep 1; done"; then
-            echo "Couldn't connect $NBD"
-            exit 1
-        fi
-        break
-    fi
-done
-if [ -z "$NBD" ]; then
-    echo "No free NBD slots"
-    exit 1
-fi
+NBD=`map_nbd disk`
 NBD_DEV=`basename $NBD`
 
 # Mount the instance
@@ -381,7 +411,9 @@
 
 # Unmount
 umount $ROOTFS || echo 'ok'
+ROOTFS=""
 qemu-nbd -d $NBD
+NBD=""
 
 # Create the instance
 cd $VM_DIR && virsh create libvirt.xml
diff --git a/tools/build_pxe_boot.sh b/tools/build_pxe_boot.sh
index da8bbcc..ab64098 100755
--- a/tools/build_pxe_boot.sh
+++ b/tools/build_pxe_boot.sh
@@ -11,6 +11,22 @@
 OPWD=`pwd`
 PROGDIR=`dirname $0`
 
+# Clean up any resources that may be in use
+cleanup() {
+    set +o errexit
+
+    # Mop up temporary files
+    if [ -n "$MNTDIR" -a -d "$MNTDIR" ]; then
+        umount $MNTDIR
+        rmdir $MNTDIR
+    fi
+
+    # Kill ourselves to signal any calling process
+    trap 2; kill -2 $$
+}
+
+trap cleanup SIGHUP SIGINT SIGTERM
+
 mkdir -p $DEST_DIR/pxelinux.cfg
 cd $DEST_DIR
 for i in memdisk menu.c32 pxelinux.0; do
diff --git a/tools/build_ramdisk.sh b/tools/build_ramdisk.sh
index 43f8999..187112a 100755
--- a/tools/build_ramdisk.sh
+++ b/tools/build_ramdisk.sh
@@ -1,55 +1,114 @@
 #!/bin/bash
 # build_ramdisk.sh - Build RAM disk images
 
+# exit on error to stop unexpected errors
+set -o errexit
+
 if [ ! "$#" -eq "1" ]; then
-    echo "$0 builds a gziped natty openstack install"
+    echo "$0 builds a gziped Ubuntu OpenStack install"
     echo "usage: $0 dest"
     exit 1
 fi
 
+# Clean up any resources that may be in use
+cleanup() {
+    set +o errexit
+
+    # Mop up temporary files
+    if [ -n "$MNTDIR" -a -d "$MNTDIR" ]; then
+        umount $MNTDIR
+        rmdir $MNTDIR
+    fi
+    if [ -n "$DEV_FILE_TMP" -a -e "$DEV_FILE_TMP "]; then
+        rm -f $DEV_FILE_TMP
+    fi
+    if [ -n "$IMG_FILE_TMP" -a -e "$IMG_FILE_TMP" ]; then
+        rm -f $IMG_FILE_TMP
+    fi
+
+    # Release NBD devices
+    if [ -n "$NBD" ]; then
+        qemu-nbd -d $NBD
+    fi
+
+    # Kill ourselves to signal any calling process
+    trap 2; kill -2 $$
+}
+
+trap cleanup SIGHUP SIGINT SIGTERM
+
+# Set up nbd
+modprobe nbd max_part=63
+
+# Echo commands
+set -o xtrace
+
 IMG_FILE=$1
 
-PROGDIR=`dirname $0`
-CHROOTCACHE=${CHROOTCACHE:-/var/cache/devstack}
-
-# Source params
-source ./stackrc
+# Keep track of the current directory
+TOOLS_DIR=$(cd $(dirname "$0") && pwd)
+TOP_DIR=`cd $TOOLS_DIR/..; pwd`
 
 # Store cwd
 CWD=`pwd`
 
+cd $TOP_DIR
+
+# Source params
+source ./stackrc
+
+CACHEDIR=${CACHEDIR:-/var/cache/devstack}
+
 DEST=${DEST:-/opt/stack}
 
+# Configure the root password of the vm to be the same as ``ADMIN_PASSWORD``
+ROOT_PASSWORD=${ADMIN_PASSWORD:-password}
+
+# Base image (natty by default)
+DIST_NAME=${DIST_NAME:-natty}
+
 # Param string to pass to stack.sh.  Like "EC2_DMZ_HOST=192.168.1.1 MYSQL_USER=nova"
 STACKSH_PARAMS=${STACKSH_PARAMS:-}
 
 # Option to use the version of devstack on which we are currently working
 USE_CURRENT_DEVSTACK=${USE_CURRENT_DEVSTACK:-1}
 
-# Set up nbd
-modprobe nbd max_part=63
-NBD=${NBD:-/dev/nbd9}
-NBD_DEV=`basename $NBD`
-
-# clean install of natty
-if [ ! -r $CHROOTCACHE/natty-base.img ]; then
-    $PROGDIR/get_uec_image.sh natty $CHROOTCACHE/natty-base.img
-#    # copy kernel modules...
-#    # NOTE(ja): is there a better way to do this?
-#    cp -pr /lib/modules/`uname -r` $CHROOTCACHE/natty-base/lib/modules
-#    # a simple password - pass
-#    echo root:pass | chroot $CHROOTCACHE/natty-base chpasswd
+# clean install
+if [ ! -r $CACHEDIR/$DIST_NAME-base.img ]; then
+    $TOOLS_DIR/get_uec_image.sh $DIST_NAME $CACHEDIR/$DIST_NAME-base.img
 fi
 
-# prime natty with as many apt/pips as we can
-if [ ! -r $CHROOTCACHE/natty-dev.img ]; then
-    cp -p $CHROOTCACHE/natty-base.img $CHROOTCACHE/natty-dev.img
-
-    qemu-nbd -c $NBD $CHROOTCACHE/natty-dev.img
-    if ! timeout 60 sh -c "while ! [ -e /sys/block/$NBD_DEV/pid ]; do sleep 1; done"; then
-        echo "Couldn't connect $NBD"
+# Finds the next available NBD device
+# Exits script if error connecting or none free
+# map_nbd image
+# returns full nbd device path
+function map_nbd {
+    for i in `seq 0 15`; do
+        if [ ! -e /sys/block/nbd$i/pid ]; then
+            NBD=/dev/nbd$i
+            # Connect to nbd and wait till it is ready
+            qemu-nbd -c $NBD $1
+            if ! timeout 60 sh -c "while ! [ -e ${NBD}p1 ]; do sleep 1; done"; then
+                echo "Couldn't connect $NBD"
+                exit 1
+            fi
+            break
+        fi
+    done
+    if [ -z "$NBD" ]; then
+        echo "No free NBD slots"
         exit 1
     fi
+    echo $NBD
+}
+
+# prime image with as many apt/pips as we can
+DEV_FILE=$CACHEDIR/$DIST_NAME-dev.img
+DEV_FILE_TMP=`mktemp $DEV_FILE.XXXXXX`
+if [ ! -r $DEV_FILE ]; then
+    cp -p $CACHEDIR/$DIST_NAME-base.img $DEV_FILE_TMP
+
+    NBD=`map_nbd $DEV_FILE_TMP`
     MNTDIR=`mktemp -d --tmpdir mntXXXXXXXX`
     mount -t ext4 ${NBD}p1 $MNTDIR
     cp -p /etc/resolv.conf $MNTDIR/etc/resolv.conf
@@ -66,6 +125,7 @@
 
     # a simple password - pass
     echo stack:pass | chroot $MNTDIR chpasswd
+    echo root:$ROOT_PASSWORD | chroot $MNTDIR chpasswd
 
     # and has sudo ability (in the future this should be limited to only what
     # stack requires)
@@ -74,27 +134,31 @@
     umount $MNTDIR
     rmdir $MNTDIR
     qemu-nbd -d $NBD
+    NBD=""
+    mv $DEV_FILE_TMP $DEV_FILE
 fi
+rm -f $DEV_FILE_TMP
 
 # clone git repositories onto the system
 # ======================================
 
+IMG_FILE_TMP=`mktemp $IMG_FILE.XXXXXX`
+
 if [ ! -r $IMG_FILE ]; then
-    qemu-nbd -c $NBD $CHROOTCACHE/natty-dev.img
-    if ! timeout 60 sh -c "while ! [ -e ${NBD}p1 ]; do sleep 1; done"; then
-        echo "Couldn't connect $NBD"
-        exit 1
-    fi
+    NBD=`map_nbd $DEV_FILE`
 
     # Pre-create the image file
     # FIXME(dt): This should really get the partition size to
     #            pre-create the image file
-    dd if=/dev/zero of=$IMG_FILE bs=1 count=1 seek=$((2*1024*1024*1024))
+    dd if=/dev/zero of=$IMG_FILE_TMP bs=1 count=1 seek=$((2*1024*1024*1024))
     # Create filesystem image for RAM disk
-    dd if=${NBD}p1 of=$IMG_FILE bs=1M
+    dd if=${NBD}p1 of=$IMG_FILE_TMP bs=1M
 
     qemu-nbd -d $NBD
+    NBD=""
+    mv $IMG_FILE_TMP $IMG_FILE
 fi
+rm -f $IMG_FILE_TMP
 
 MNTDIR=`mktemp -d --tmpdir mntXXXXXXXX`
 mount -t ext4 -o loop $IMG_FILE $MNTDIR
diff --git a/tools/build_usb_boot.sh b/tools/build_usb_boot.sh
index fc5e969..e4dabc0 100755
--- a/tools/build_usb_boot.sh
+++ b/tools/build_usb_boot.sh
@@ -11,6 +11,26 @@
 OPWD=`pwd`
 PROGDIR=`dirname $0`
 
+# Clean up any resources that may be in use
+cleanup() {
+    set +o errexit
+
+    # Mop up temporary files
+    if [ -n "$DEST_DEV" ]; then
+        umount $DEST_DIR
+        rmdir $DEST_DIR
+    fi
+    if [ -n "$MNTDIR" -a -d "$MNTDIR" ]; then
+        umount $MNTDIR
+        rmdir $MNTDIR
+    fi
+
+    # Kill ourselves to signal any calling process
+    trap 2; kill -2 $$
+}
+
+trap cleanup SIGHUP SIGINT SIGTERM
+
 if [ -b $DEST_DIR ]; then
     # We have a block device, install syslinux and mount it
     DEST_DEV=$DEST_DIR
diff --git a/tools/get_uec_image.sh b/tools/get_uec_image.sh
index 935feba..3d62bba 100755
--- a/tools/get_uec_image.sh
+++ b/tools/get_uec_image.sh
@@ -26,6 +26,24 @@
     exit 1
 }
 
+# Clean up any resources that may be in use
+cleanup() {
+    set +o errexit
+
+    # Mop up temporary files
+    if [ -n "$IMG_FILE_TMP" -a -e "$IMG_FILE_TMP" ]; then
+        rm -f $IMG_FILE_TMP
+    fi
+
+    # Release NBD devices
+    if [ -n "$NBD" ]; then
+        qemu-nbd -d $NBD
+    fi
+
+    # Kill ourselves to signal any calling process
+    trap 2; kill -2 $$
+}
+
 while getopts f:hmr: c; do
     case $c in
         f)  FORMAT=$OPTARG
@@ -89,6 +107,8 @@
                 ;;
 esac
 
+trap cleanup SIGHUP SIGINT SIGTERM
+
 # Prepare the base image
 
 # Get the UEC image
@@ -111,25 +131,33 @@
     qemu-img resize $IMG_FILE_TMP +$((ROOTSIZE - 2000))M
 fi
 
+# Finds the next available NBD device
+# Exits script if error connecting or none free
+# map_nbd image
+# returns full nbd device path
+function map_nbd {
+    for i in `seq 0 15`; do
+        if [ ! -e /sys/block/nbd$i/pid ]; then
+            NBD=/dev/nbd$i
+            # Connect to nbd and wait till it is ready
+            qemu-nbd -c $NBD $1
+            if ! timeout 60 sh -c "while ! [ -e ${NBD}p1 ]; do sleep 1; done"; then
+                echo "Couldn't connect $NBD"
+                exit 1
+            fi
+            break
+        fi
+    done
+    if [ -z "$NBD" ]; then
+        echo "No free NBD slots"
+        exit 1
+    fi
+    echo $NBD
+}
+
 # Set up nbd
 modprobe nbd max_part=63
-for i in `seq 1 15`; do
-    if [ ! -e /sys/block/nbd$i/pid ]; then
-        NBD=/dev/nbd$i
-        # Connect to nbd and wait till it is ready
-        qemu-nbd -c $NBD $IMG_FILE_TMP
-        if ! timeout 60 sh -c "while ! [ -e ${NBD}p1 ]; do sleep 1; done"; then
-            echo "Couldn't connect $NBD"
-            exit 1
-        fi
-        break
-    fi
-done
-if [ -z "$NBD" ]; then
-    echo "No free NBD slots"
-    exit 1
-fi
-NBD_DEV=`basename $NBD`
+NBD=`map_nbd $IMG_FILE_TMP`
 
 # Resize partition 1 to full size of the disk image
 echo "d
@@ -162,5 +190,6 @@
 umount $MNTDIR
 rmdir $MNTDIR
 qemu-nbd -d $NBD
+NBD=""
 
 mv $IMG_FILE_TMP $IMG_FILE
diff --git a/tools/xen/build_domU.sh b/tools/xen/build_domU.sh
index 8e40225..65049af 100755
--- a/tools/xen/build_domU.sh
+++ b/tools/xen/build_domU.sh
@@ -7,6 +7,12 @@
     exit 1
 fi
 
+# This directory
+TOP_DIR=$(cd $(dirname "$0") && pwd)
+
+# Source params
+cd ../.. && source ./stackrc && cd $TOP_DIR
+
 # Echo commands
 set -o xtrace
 
@@ -41,9 +47,6 @@
 # Size of image
 VDI_MB=${VDI_MB:-2500}
 
-# This directory
-TOP_DIR=$(cd $(dirname "$0") && pwd)
-
 # Make sure we have git
 if ! which git; then
     GITDIR=/tmp/git-1.7.7
@@ -229,6 +232,7 @@
 # Destroy any instances that were launched
 for uuid in `xe vm-list | grep -1 instance | grep uuid | sed "s/.*\: //g"`; do
     echo "Shutting down nova instance $uuid"
+    xe vm-unpause uuid=$uuid || true
     xe vm-shutdown uuid=$uuid
     xe vm-destroy uuid=$uuid
 done