diff --git a/tools/build_libvirt.sh b/tools/build_libvirt.sh index 16259b1f20..48e285348d 100755 --- a/tools/build_libvirt.sh +++ b/tools/build_libvirt.sh @@ -12,6 +12,27 @@ if [ ! "oneiric" = "$UBUNTU_VERSION" ]; then 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 @@ function kill_unmount() { # 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} @@ -390,7 +408,9 @@ sed -e 's/^PasswordAuthentication.*$/PasswordAuthentication yes/' -i $ROOTFS/etc # 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 da8bbcc348..ab6409831a 100755 --- a/tools/build_pxe_boot.sh +++ b/tools/build_pxe_boot.sh @@ -11,6 +11,22 @@ PXEDIR=${PXEDIR:-/var/cache/devstack/pxe} 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 06e5857bd2..187112a7a5 100755 --- a/tools/build_ramdisk.sh +++ b/tools/build_ramdisk.sh @@ -10,6 +10,33 @@ if [ ! "$#" -eq "1" ]; then 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 @@ -107,6 +134,7 @@ if [ ! -r $DEV_FILE ]; then umount $MNTDIR rmdir $MNTDIR qemu-nbd -d $NBD + NBD="" mv $DEV_FILE_TMP $DEV_FILE fi rm -f $DEV_FILE_TMP @@ -127,6 +155,7 @@ if [ ! -r $IMG_FILE ]; then 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 diff --git a/tools/build_usb_boot.sh b/tools/build_usb_boot.sh index fc5e96917f..e4dabc0edb 100755 --- a/tools/build_usb_boot.sh +++ b/tools/build_usb_boot.sh @@ -11,6 +11,26 @@ PXEDIR=${PXEDIR:-/var/cache/devstack/pxe} 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 de378422a3..3d62bbac7c 100755 --- a/tools/get_uec_image.sh +++ b/tools/get_uec_image.sh @@ -26,6 +26,24 @@ usage() { 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 @@ case $DIST_NAME in ;; esac +trap cleanup SIGHUP SIGINT SIGTERM + # Prepare the base image # Get the UEC image @@ -170,5 +190,6 @@ rm -f $MNTDIR/etc/resolv.conf umount $MNTDIR rmdir $MNTDIR qemu-nbd -d $NBD +NBD="" mv $IMG_FILE_TMP $IMG_FILE