Merge branch 'master' of github.com:cloudbuilders/devstack into uec-image
diff --git a/tools/build_ramdisk.sh b/tools/build_ramdisk.sh
index be6ca77..d169b9f 100755
--- a/tools/build_ramdisk.sh
+++ b/tools/build_ramdisk.sh
@@ -7,6 +7,8 @@
     exit 1
 fi
 
+IMG_FILE=$1
+
 PROGDIR=`dirname $0`
 CHROOTCACHE=${CHROOTCACHE:-/var/cache/devstack}
 
@@ -24,42 +26,83 @@
 # 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 [ ! -d $CHROOTCACHE/natty-base ]; then
-    $PROGDIR/make_image.sh -C natty $CHROOTCACHE/natty-base
-    # 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
+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
 fi
 
 # prime natty with as many apt/pips as we can
-if [ ! -d $CHROOTCACHE/natty-dev ]; then
-    rsync -azH $CHROOTCACHE/natty-base/ $CHROOTCACHE/natty-dev/
-    chroot $CHROOTCACHE/natty-dev apt-get install -y `cat files/apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"`
-    chroot $CHROOTCACHE/natty-dev pip install `cat files/pips/*`
+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"
+        exit 1
+    fi
+    MNTDIR=`mktemp -d --tmpdir mntXXXXXXXX`
+    mount -t ext4 ${NBD}p1 $MNTDIR
+    cp -p /etc/resolv.conf $MNTDIR/etc/resolv.conf
+
+    chroot $MNTDIR apt-get install -y `cat files/apts/* | cut -d\# -f1 | egrep -v "(rabbitmq|libvirt-bin|mysql-server)"`
+    chroot $MNTDIR pip install `cat files/pips/*`
 
     # Create a stack user that is a member of the libvirtd group so that stack
     # is able to interact with libvirt.
-    chroot $CHROOTCACHE/natty-dev groupadd libvirtd
-    chroot $CHROOTCACHE/natty-dev useradd stack -s /bin/bash -d $DEST -G libvirtd
-    mkdir -p $CHROOTCACHE/natty-dev/$DEST
-    chroot $CHROOTCACHE/natty-dev chown stack $DEST
+    chroot $MNTDIR groupadd libvirtd
+    chroot $MNTDIR useradd stack -s /bin/bash -d $DEST -G libvirtd
+    mkdir -p $MNTDIR/$DEST
+    chroot $MNTDIR chown stack $DEST
 
     # a simple password - pass
-    echo stack:pass | chroot $CHROOTCACHE/natty-dev chpasswd
+    echo stack:pass | chroot $MNTDIR chpasswd
 
     # and has sudo ability (in the future this should be limited to only what
     # stack requires)
-    echo "stack ALL=(ALL) NOPASSWD: ALL" >> $CHROOTCACHE/natty-dev/etc/sudoers
+    echo "stack ALL=(ALL) NOPASSWD: ALL" >> $MNTDIR/etc/sudoers
+
+    umount $MNTDIR
+    rmdir $MNTDIR
+    qemu-nbd -d $NBD
 fi
 
 # clone git repositories onto the system
 # ======================================
 
-if [ ! -d $CHROOTCACHE/natty-stack ]; then
-    rsync -azH $CHROOTCACHE/natty-dev/ $CHROOTCACHE/natty-stack/
+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
+
+    # 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))
+    # Create filesystem image for RAM disk
+    dd if=${NBD}p1 of=$IMG_FILE bs=1M
+
+    qemu-nbd -d $NBD
+fi
+
+MNTDIR=`mktemp -d --tmpdir mntXXXXXXXX`
+mount -t ext4 -o loop $IMG_FILE $MNTDIR
+cp -p /etc/resolv.conf $MNTDIR/etc/resolv.conf
+
+# We need to install a non-virtual kernel and modules to boot from
+if [ ! -r "`ls $MNTDIR/boot/vmlinuz-*-generic | head -1`" ]; then
+    chroot $MNTDIR apt-get install -y linux-generic
 fi
 
 # git clone only if directory doesn't exist already.  Since ``DEST`` might not
@@ -68,7 +111,7 @@
 function git_clone {
 
     # clone new copy or fetch latest changes
-    CHECKOUT=$CHROOTCACHE/natty-stack$2
+    CHECKOUT=${MNTDIR}$2
     if [ ! -d $CHECKOUT ]; then
         mkdir -p $CHECKOUT
         git clone $1 $CHECKOUT
@@ -88,7 +131,7 @@
     popd
 
     # give ownership to the stack user
-    chroot $CHROOTCACHE/natty-stack/ chown -R stack $2
+    chroot $MNTDIR chown -R stack $2
 }
 
 git_clone $NOVA_REPO $DEST/nova $NOVA_BRANCH
@@ -100,13 +143,13 @@
 git_clone $OPENSTACKX_REPO $DEST/openstackx $OPENSTACKX_BRANCH
 
 # Use this version of devstack
-rm -rf $CHROOTCACHE/natty-stack/$DEST/devstack
-cp -pr $CWD $CHROOTCACHE/natty-stack/$DEST/devstack
-chroot $CHROOTCACHE/natty-stack chown -R stack $DEST/devstack
+rm -rf $MNTDIR/$DEST/devstack
+cp -pr $CWD $MNTDIR/$DEST/devstack
+chroot $MNTDIR chown -R stack $DEST/devstack
 
 # Configure host network for DHCP
-mkdir -p $CHROOTCACHE/natty-stack/etc/network
-cat > $CHROOTCACHE/natty-stack/etc/network/interfaces <<EOF
+mkdir -p $MNTDIR/etc/network
+cat > $MNTDIR/etc/network/interfaces <<EOF
 auto lo
 iface lo inet loopback
 
@@ -115,11 +158,11 @@
 EOF
 
 # Set hostname
-echo "ramstack" >$CHROOTCACHE/natty-stack/etc/hostname
-echo "127.0.0.1		localhost	ramstack" >$CHROOTCACHE/natty-stack/etc/hosts
+echo "ramstack" >$MNTDIR/etc/hostname
+echo "127.0.0.1		localhost	ramstack" >$MNTDIR/etc/hosts
 
 # Configure the runner
-RUN_SH=$CHROOTCACHE/natty-stack/$DEST/run.sh
+RUN_SH=$MNTDIR/$DEST/run.sh
 cat > $RUN_SH <<EOF
 #!/usr/bin/env bash
 
@@ -140,27 +183,7 @@
 
 # Make the run.sh executable
 chmod 755 $RUN_SH
-chroot $CHROOTCACHE/natty-stack chown stack $DEST/run.sh
+chroot $MNTDIR chown stack $DEST/run.sh
 
-# build a new image
-BASE=$CHROOTCACHE/build.$$
-IMG=$BASE.img
-MNT=$BASE/
-
-# (quickly) create a 2GB blank filesystem
-dd bs=1 count=1 seek=$((2*1024*1024*1024)) if=/dev/zero of=$IMG
-# force it to be initialized as ext2
-mkfs.ext2 -F $IMG
-
-# mount blank image loopback and load it
-mkdir -p $MNT
-mount -o loop $IMG $MNT
-rsync -azH $CHROOTCACHE/natty-stack/ $MNT
-
-# umount and cleanup
-umount $MNT
-rmdir $MNT
-
-# gzip into final location
-gzip -1 $IMG -c > $1
-
+umount $MNTDIR
+rmdir $MNTDIR
diff --git a/tools/build_usb_boot.sh b/tools/build_usb_boot.sh
index 332c869..fc5e969 100755
--- a/tools/build_usb_boot.sh
+++ b/tools/build_usb_boot.sh
@@ -1,17 +1,11 @@
 #!/bin/bash -e
 # build_usb_boot.sh - Create a syslinux boot environment
 #
-# build_usb_boot.sh [-k kernel-version] destdev
+# build_usb_boot.sh destdev
 #
 # Assumes syslinux is installed
 # Needs to run as root
 
-KVER=`uname -r`
-if [ "$1" = "-k" ]; then
-    KVER=$2
-    shift;shift
-fi
-
 DEST_DIR=${1:-/tmp/syslinux-boot}
 PXEDIR=${PXEDIR:-/var/cache/devstack/pxe}
 OPWD=`pwd`
@@ -20,7 +14,7 @@
 if [ -b $DEST_DIR ]; then
     # We have a block device, install syslinux and mount it
     DEST_DEV=$DEST_DIR
-    DEST_DIR=`mktemp -d mntXXXXXX`
+    DEST_DIR=`mktemp -d --tmpdir mntXXXXXX`
     mount $DEST_DEV $DEST_DIR
 
     if [ ! -d $DEST_DIR/syslinux ]; then
@@ -39,7 +33,7 @@
 
 # Get some more stuff from syslinux
 for i in memdisk menu.c32; do
-    cp -p /usr/lib/syslinux/$i $DEST_DIR/syslinux
+    cp -pu /usr/lib/syslinux/$i $DEST_DIR/syslinux
 done
 
 CFG=$DEST_DIR/syslinux/syslinux.cfg
@@ -57,20 +51,41 @@
 if [ ! -d $PXEDIR ]; then
     mkdir -p $PXEDIR
 fi
-if [ ! -r $PXEDIR/vmlinuz-${KVER} ]; then
-    sudo chmod 644 /boot/vmlinuz-${KVER}
-    if [ ! -r /boot/vmlinuz-${KVER} ]; then
-        echo "No kernel found"
-    else
-        cp -p /boot/vmlinuz-${KVER} $PXEDIR
-    fi
-fi
-cp -p $PXEDIR/vmlinuz-${KVER} $DEST_DIR/ubuntu
-if [ ! -r $PXEDIR/stack-initrd.gz ]; then
+
+# Get image into place
+if [ ! -r $PXEDIR/stack-initrd.img ]; then
     cd $OPWD
-    sudo $PROGDIR/build_ramdisk.sh $PXEDIR/stack-initrd.gz
+    $PROGDIR/build_ramdisk.sh $PXEDIR/stack-initrd.img
 fi
-cp -p $PXEDIR/stack-initrd.gz $DEST_DIR/ubuntu
+if [ ! -r $PXEDIR/stack-initrd.gz ]; then
+    gzip -1 -c $PXEDIR/stack-initrd.img >$PXEDIR/stack-initrd.gz
+fi
+cp -pu $PXEDIR/stack-initrd.gz $DEST_DIR/ubuntu
+
+if [ ! -r $PXEDIR/vmlinuz-*-generic ]; then
+    MNTDIR=`mktemp -d --tmpdir mntXXXXXXXX`
+    mount -t ext4 -o loop $PXEDIR/stack-initrd.img $MNTDIR
+
+    if [ ! -r $MNTDIR/boot/vmlinuz-*-generic ]; then
+        echo "No kernel found"
+        umount $MNTDIR
+        rmdir $MNTDIR
+        if [ -n "$DEST_DEV" ]; then
+            umount $DEST_DIR
+            rmdir $DEST_DIR
+        fi
+        exit 1
+    else
+        cp -pu $MNTDIR/boot/vmlinuz-*-generic $PXEDIR
+    fi
+    umount $MNTDIR
+    rmdir $MNTDIR
+fi
+
+# Get generic kernel version
+KNAME=`basename $PXEDIR/vmlinuz-*-generic`
+KVER=${KNAME#vmlinuz-}
+cp -pu $PXEDIR/vmlinuz-$KVER $DEST_DIR/ubuntu
 cat >>$CFG <<EOF
 
 LABEL devstack
@@ -82,7 +97,7 @@
 
 # Get Ubuntu
 if [ -d $PXEDIR -a -r $PXEDIR/natty-base-initrd.gz ]; then
-    cp -p $PXEDIR/natty-base-initrd.gz $DEST_DIR/ubuntu
+    cp -pu $PXEDIR/natty-base-initrd.gz $DEST_DIR/ubuntu
     cat >>$CFG <<EOF
 
 LABEL ubuntu
diff --git a/tools/get_uec_image.sh b/tools/get_uec_image.sh
index e9d708d..b903ad7 100755
--- a/tools/get_uec_image.sh
+++ b/tools/get_uec_image.sh
@@ -131,16 +131,17 @@
 if [ $ROOTSIZE -gt 2000 ]; then
     # Resize the container
     qemu-img resize $IMG_FILE +$((ROOTSIZE - 2000))M
+fi
 
-    # Connect to nbd and wait till it is ready
-    qemu-nbd -c $NBD $IMG_FILE
-    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
+# Connect to nbd and wait till it is ready
+qemu-nbd -c $NBD $IMG_FILE
+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
 
-    # Resize partition 1 to full size of the disk image
-    echo "d
+# Resize partition 1 to full size of the disk image
+echo "d
 n
 p
 1
@@ -152,8 +153,7 @@
 1
 w
 " | fdisk $NBD
-    fsck -t ext4 -f ${NBD}p1
-    resize2fs ${NBD}p1
+fsck -t ext4 -f ${NBD}p1
+resize2fs ${NBD}p1
 
-    qemu-nbd -d $NBD
-fi
+qemu-nbd -d $NBD