XenServer: new build and install scripts

Change-Id: Ia13a9c8073e59edf98415ba5b9f3a9cbd1453d32
diff --git a/stack.sh b/stack.sh
index debbc4e..8fa3902 100755
--- a/stack.sh
+++ b/stack.sh
@@ -313,7 +313,7 @@
 if [ "$VIRT_DRIVER" = 'xenserver' ]; then
     PUBLIC_INTERFACE_DEFAULT=eth3
     # allow build_domU.sh to specify the flat network bridge via kernel args
-    FLAT_NETWORK_BRIDGE_DEFAULT=$(grep -o 'flat_network_bridge=[^.]*' /proc/cmdline | cut -d= -f 2)
+    FLAT_NETWORK_BRIDGE_DEFAULT=$(grep -o 'flat_network_bridge=[[:alnum:]]*' /proc/cmdline | cut -d= -f 2 | sort -u)
     GUEST_INTERFACE_DEFAULT=eth1
 else
     PUBLIC_INTERFACE_DEFAULT=br100
diff --git a/tools/xen/build_xva.sh b/tools/xen/build_xva.sh
index c235485..2f28b5f 100755
--- a/tools/xen/build_xva.sh
+++ b/tools/xen/build_xva.sh
@@ -1,5 +1,27 @@
 #!/bin/bash
 
+set -e
+
+declare -a on_exit_hooks
+
+on_exit()
+{
+    for i in $(seq $((${#on_exit_hooks[*]} - 1)) -1 0)
+    do
+        eval "${on_exit_hooks[$i]}"
+    done
+}
+
+add_on_exit()
+{
+    local n=${#on_exit_hooks[*]}
+    on_exit_hooks[$n]="$*"
+    if [[ $n -eq 0 ]]
+    then
+        trap on_exit EXIT
+    fi
+}
+
 # Abort if localrc is not set
 if [ ! -e ../../localrc ]; then
     echo "You must have a localrc with ALL necessary passwords defined before proceeding."
@@ -16,27 +38,11 @@
 # Echo commands
 set -o xtrace
 
+GUEST_NAME="$1"
+
 # Directory where we stage the build
-STAGING_DIR=$TOP_DIR/stage
-
-# Option to clean out old stuff
-CLEAN=${CLEAN:-0}
-if [ "$CLEAN" = "1" ]; then
-    rm -rf $STAGING_DIR
-fi
-
-# Download our base image.  This image is made using prepare_guest.sh
-BASE_IMAGE_URL=${BASE_IMAGE_URL:-http://images.ansolabs.com/xen/stage.tgz}
-if [ ! -e $STAGING_DIR ]; then
-    if [ ! -e /tmp/stage.tgz ]; then
-        wget $BASE_IMAGE_URL -O /tmp/stage.tgz
-    fi
-    tar xfz /tmp/stage.tgz
-    cd $TOP_DIR
-fi
-
-# Free up precious disk space
-rm -f /tmp/stage.tgz
+STAGING_DIR=$($TOP_DIR/scripts/manage-vdi open $GUEST_NAME 0 1 | grep -o "/tmp/tmp.[[:alnum:]]*")
+add_on_exit "$TOP_DIR/scripts/manage-vdi close $GUEST_NAME 0 1"
 
 # Make sure we have a stage
 if [ ! -d $STAGING_DIR/etc ]; then
@@ -55,63 +61,26 @@
 UBUNTU_VERSION=`cat $STAGING_DIR/etc/lsb-release | grep "DISTRIB_CODENAME=" | sed "s/DISTRIB_CODENAME=//"`
 KERNEL_VERSION=`ls $STAGING_DIR/boot/vmlinuz* | head -1 | sed "s/.*vmlinuz-//"`
 
-# Directory for xvas
-XVA_DIR=$TOP_DIR/xvas
-
-# Create xva dir
-mkdir -p $XVA_DIR
-
-# Path to xva
-XVA=$XVA_DIR/$GUEST_NAME.xva
-
-# Setup fake grub
-rm -rf $STAGING_DIR/boot/grub/
-mkdir -p $STAGING_DIR/boot/grub/
-cp $TEMPLATES_DIR/menu.lst.in $STAGING_DIR/boot/grub/menu.lst
-sed -e "s,@KERNEL_VERSION@,$KERNEL_VERSION,g" -i $STAGING_DIR/boot/grub/menu.lst
-
-# Setup fstab, tty, and other system stuff
-cp $FILES_DIR/fstab $STAGING_DIR/etc/fstab
-cp $FILES_DIR/hvc0.conf $STAGING_DIR/etc/init/
-
-# Put the VPX into UTC.
-rm -f $STAGING_DIR/etc/localtime
-
 # Configure dns (use same dns as dom0)
 cp /etc/resolv.conf $STAGING_DIR/etc/resolv.conf
 
 # Copy over devstack
 rm -f /tmp/devstack.tar
-tar --exclude='stage' --exclude='xen/xvas' --exclude='xen/nova' -cvf /tmp/devstack.tar $TOP_DIR/../../../devstack
-cd $STAGING_DIR/opt/stack/
-tar xf /tmp/devstack.tar
+cd $TOP_DIR/../../
+tar --exclude='stage' --exclude='xen/xvas' --exclude='xen/nova' -cvf /tmp/devstack.tar .
+mkdir -p $STAGING_DIR/opt/stack/devstack
+tar xf /tmp/devstack.tar -C $STAGING_DIR/opt/stack/devstack
 cd $TOP_DIR
 
-# Configure OVA
-VDI_SIZE=$(($VDI_MB*1024*1024))
-PRODUCT_BRAND=${PRODUCT_BRAND:-openstack}
-PRODUCT_VERSION=${PRODUCT_VERSION:-001}
-BUILD_NUMBER=${BUILD_NUMBER:-001}
-LABEL="$PRODUCT_BRAND $PRODUCT_VERSION-$BUILD_NUMBER"
-OVA=$STAGING_DIR/tmp/ova.xml
-cp $TEMPLATES_DIR/ova.xml.in  $OVA
-sed -e "s,@VDI_SIZE@,$VDI_SIZE,g" -i $OVA
-sed -e "s,@PRODUCT_BRAND@,$PRODUCT_BRAND,g" -i $OVA
-sed -e "s,@PRODUCT_VERSION@,$PRODUCT_VERSION,g" -i $OVA
-sed -e "s,@BUILD_NUMBER@,$BUILD_NUMBER,g" -i $OVA
-
 # Run devstack on launch
 cat <<EOF >$STAGING_DIR/etc/rc.local
 # network restart required for getting the right gateway
 /etc/init.d/networking restart
 GUEST_PASSWORD=$GUEST_PASSWORD STAGING_DIR=/ DO_TGZ=0 bash /opt/stack/devstack/tools/xen/prepare_guest.sh > /opt/stack/prepare_guest.log 2>&1
-su -c "/opt/stack/run.sh > /opt/stack/run.sh.log" stack
+su -c "/opt/stack/run.sh > /opt/stack/run.sh.log 2>&1" stack
 exit 0
 EOF
 
-# Clean old xva. In the future may not do this every time.
-rm -f $XVA
-
 # Configure the hostname
 echo $GUEST_NAME > $STAGING_DIR/etc/hostname
 
@@ -151,10 +120,6 @@
     sed -e "s,@ETH3_NETMASK@,$PUB_NETMASK,g" -i $INTERFACES
 fi
 
-if [ -h $STAGING_DIR/sbin/dhclient3 ]; then
-    rm -f $STAGING_DIR/sbin/dhclient3
-fi
-
 # Gracefully cp only if source file/dir exists
 function cp_it {
     if [ -e $1 ] || [ -d $1 ]; then
@@ -181,11 +146,4 @@
 EOF
 chmod 755 $STAGING_DIR/opt/stack/run.sh
 
-# Create xva
-if [ ! -e $XVA ]; then
-    rm -rf /tmp/mkxva*
-    UID=0 $SCRIPT_DIR/mkxva -o $XVA -t xva -x $OVA $STAGING_DIR $VDI_MB /tmp/
-fi
-
-echo "Built $(basename $XVA).  If your dom0 is on a different machine, copy this to [devstackdir]/tools/xen/$(basename $XVA)"
-echo "Also copy your localrc to [devstackdir]"
+echo "Done"
diff --git a/tools/xen/build_domU.sh b/tools/xen/install_os_domU.sh
similarity index 69%
rename from tools/xen/build_domU.sh
rename to tools/xen/install_os_domU.sh
index 5fa7aa8..31bcc40 100755
--- a/tools/xen/build_domU.sh
+++ b/tools/xen/install_os_domU.sh
@@ -1,5 +1,8 @@
 #!/bin/bash
 
+# Exit on errors
+set -o errexit
+
 # Abort if localrc is not set
 if [ ! -e ../../localrc ]; then
     echo "You must have a localrc with ALL necessary passwords defined before proceeding."
@@ -19,25 +22,29 @@
 # Echo commands
 set -o xtrace
 
-# Check for xva file
-if [ ! -e $XVA ]; then
-    echo "Missing xva file.  Please run build_xva.sh (ideally on a non dom0 host since the build can require lots of space)."
-    echo "Place the resulting xva file in $XVA"
-    exit 1
-fi
+xe_min()
+{
+  local cmd="$1"
+  shift
+  xe "$cmd" --minimal "$@"
+}
 
-# Make sure we have git
-if ! which git; then
-    GITDIR=/tmp/git-1.7.7
-    cd /tmp
-    rm -rf $GITDIR*
-    wget http://git-core.googlecode.com/files/git-1.7.7.tar.gz
-    tar xfv git-1.7.7.tar.gz
-    cd $GITDIR
-    ./configure --with-curl --with-expat
-    make install
-    cd $TOP_DIR
+cd $TOP_DIR
+if [ -f ./master ]
+then
+    rm -rf ./master
+    rm -rf ./nova
 fi
+wget https://github.com/openstack/nova/zipball/master --no-check-certificate
+unzip -o master -d ./nova
+cp -pr ./nova/*/plugins/xenserver/xenapi/etc/xapi.d /etc/
+chmod a+x /etc/xapi.d/plugins/*
+
+mkdir -p /boot/guest
+
+GUEST_NAME=${GUEST_NAME:-"DevStackOSDomU"}
+SNAME="ubuntusnapshot"
+TNAME="ubuntuready"
 
 # Helper to create networks
 # Uses echo trickery to return network uuid
@@ -48,23 +55,23 @@
     netname=$4
     if [ -z $br ]
     then
-        pif=$(xe pif-list --minimal device=$dev VLAN=$vlan)
+        pif=$(xe_min pif-list device=$dev VLAN=$vlan)
         if [ -z $pif ]
         then
             net=$(xe network-create name-label=$netname)
         else
-            net=$(xe network-list --minimal PIF-uuids=$pif)
+            net=$(xe_min network-list  PIF-uuids=$pif)
         fi
         echo $net
         return 0
     fi
-    if [ ! $(xe network-list --minimal params=bridge | grep -w --only-matching $br) ]
+    if [ ! $(xe_min network-list  params=bridge | grep -w --only-matching $br) ]
     then
         echo "Specified bridge $br does not exist"
         echo "If you wish to use defaults, please keep the bridge name empty"
         exit 1
     else
-        net=$(xe network-list --minimal bridge=$br)
+        net=$(xe_min network-list  bridge=$br)
         echo $net
     fi
 }
@@ -95,13 +102,13 @@
     then
         return
     fi
-    if [ -z $(xe vlan-list --minimal tag=$vlan) ]
+    if [ -z $(xe_min vlan-list  tag=$vlan) ]
     then
-        pif=$(xe pif-list --minimal network-uuid=$net)
+        pif=$(xe_min pif-list  network-uuid=$net)
         # We created a brand new network this time
         if [ -z $pif ]
         then
-            pif=$(xe pif-list --minimal device=$dev VLAN=-1)
+            pif=$(xe_min pif-list  device=$dev VLAN=-1)
             xe vlan-create pif-uuid=$pif vlan=$vlan network-uuid=$net
         else
             echo "VLAN does not exist but PIF attached to this network"
@@ -133,24 +140,11 @@
 # Enable ip forwarding at runtime as well
 echo 1 > /proc/sys/net/ipv4/ip_forward
 
-# Set local storage il8n
-SR_UUID=`xe sr-list --minimal name-label="Local storage"`
-xe sr-param-set uuid=$SR_UUID other-config:i18n-key=local-storage
-
-# Checkout nova
-git_clone $NOVA_REPO $TOP_DIR/nova $NOVA_BRANCH
-
-# Install plugins
-cp -pr $TOP_DIR/nova/plugins/xenserver/xenapi/etc/xapi.d /etc/
-chmod a+x /etc/xapi.d/plugins/*
-yum --enablerepo=base install -y parted
-mkdir -p /boot/guest
-
 # Shutdown previous runs
 DO_SHUTDOWN=${DO_SHUTDOWN:-1}
 if [ "$DO_SHUTDOWN" = "1" ]; then
     # Shutdown all domU's that created previously
-    xe vm-list --minimal name-label="$LABEL" | xargs ./scripts/uninstall-os-vpx.sh
+    xe_min vm-list  name-label="$GUEST_NAME" | xargs ./scripts/uninstall-os-vpx.sh
 
     # Destroy any instances that were launched
     for uuid in `xe vm-list | grep -1 instance | grep uuid | sed "s/.*\: //g"`; do
@@ -168,18 +162,54 @@
 
 # Start guest
 if [ -z $VM_BR ]; then
-    VM_BR=$(xe network-list --minimal uuid=$VM_NET params=bridge)
+    VM_BR=$(xe_min network-list  uuid=$VM_NET params=bridge)
 fi
 if [ -z $MGT_BR ]; then
-    MGT_BR=$(xe network-list --minimal uuid=$MGT_NET params=bridge)
+    MGT_BR=$(xe_min network-list  uuid=$MGT_NET params=bridge)
 fi
 if [ -z $PUB_BR ]; then
-    PUB_BR=$(xe network-list --minimal uuid=$PUB_NET params=bridge)
+    PUB_BR=$(xe_min network-list  uuid=$PUB_NET params=bridge)
 fi
-$TOP_DIR/scripts/install-os-vpx.sh -f $XVA -v $VM_BR -m $MGT_BR -p $PUB_BR -l $GUEST_NAME -w -k "flat_network_bridge=${VM_BR}"
+
+templateuuid=$(xe template-list name-label="$TNAME")
+if [ -n "$templateuuid" ]
+then
+        vm_uuid=$(xe vm-install template="$TNAME" new-name-label="$GUEST_NAME")
+else
+    template=$(xe_min template-list name-label="Ubuntu 11.10 (64-bit)")
+    if [ -z "$template" ]
+    then
+        $TOP_DIR/scripts/xenoneirictemplate.sh
+    fi
+    $TOP_DIR/scripts/install-os-vpx.sh -t "Ubuntu 11.10 (64-bit)" -v $VM_BR -m $MGT_BR -p $PUB_BR -l $GUEST_NAME -r $OSDOMU_MEM_MB -k "flat_network_bridge=${VM_BR}"
+
+    # Wait for install to finish
+    while true
+    do
+        state=$(xe_min vm-list name-label="$GUEST_NAME" power-state=halted)
+        if [ -n "$state" ]
+        then
+            break
+        else
+            echo "Waiting for "$GUEST_NAME" to finish installation..."
+            sleep 30
+        fi
+    done
+
+    vm_uuid=$(xe_min vm-list name-label="$GUEST_NAME")
+    xe vm-param-set actions-after-reboot=Restart uuid="$vm_uuid"
+
+    # Make template from VM
+    snuuid=$(xe vm-snapshot vm="$GUEST_NAME" new-name-label="$SNAME")
+    template_uuid=$(xe snapshot-clone uuid=$snuuid new-name-label="$TNAME")
+fi
+
+$TOP_DIR/build_xva.sh "$GUEST_NAME"
+
+xe vm-start vm="$GUEST_NAME"
 
 if [ $PUB_IP == "dhcp" ]; then
-    PUB_IP=$(xe vm-list --minimal name-label=$GUEST_NAME params=networks |  sed -ne 's,^.*3/ip: \([0-9.]*\).*$,\1,p')
+    PUB_IP=$(xe_min vm-list  name-label=$GUEST_NAME params=networks |  sed -ne 's,^.*3/ip: \([0-9.]*\).*$,\1,p')
 fi
 
 # If we have copied our ssh credentials, use ssh to monitor while the installation runs
diff --git a/tools/xen/prepare_guest.sh b/tools/xen/prepare_guest.sh
old mode 100644
new mode 100755
index 77ce54a..5d39ac6
--- a/tools/xen/prepare_guest.sh
+++ b/tools/xen/prepare_guest.sh
@@ -6,13 +6,6 @@
 GUEST_PASSWORD=${GUEST_PASSWORD:-secrete}
 STAGING_DIR=${STAGING_DIR:-stage}
 DO_TGZ=${DO_TGZ:-1}
-KERNEL_VERSION=3.0.0-12-virtual
-
-# Debootstrap base system
-if [ ! -d $STAGING_DIR ]; then
-    apt-get install debootstrap
-    debootstrap --arch amd64 oneiric $STAGING_DIR http://us.archive.ubuntu.com/ubuntu/
-fi
 
 # Sources.list
 cat <<EOF >$STAGING_DIR/etc/apt/sources.list
@@ -28,7 +21,6 @@
 
 # Install basics
 chroot $STAGING_DIR apt-get update
-chroot $STAGING_DIR apt-get install -y linux-image-$KERNEL_VERSION
 chroot $STAGING_DIR apt-get install -y cracklib-runtime curl wget ssh openssh-server tcpdump ethtool
 chroot $STAGING_DIR apt-get install -y curl wget ssh openssh-server python-pip git vim-nox sudo
 chroot $STAGING_DIR pip install xenapi
diff --git a/tools/xen/scripts/install-os-vpx.sh b/tools/xen/scripts/install-os-vpx.sh
index 14240a7..fe5e810 100755
--- a/tools/xen/scripts/install-os-vpx.sh
+++ b/tools/xen/scripts/install-os-vpx.sh
@@ -332,17 +332,11 @@
 {
   local v="$1"
   local args=$KERNEL_PARAMS
-  local cmdline=$(cat /proc/cmdline)
-  for word in $cmdline
-  do
-    if echo "$word" | grep -q "geppetto"
-    then
-      args="$word $args"
-    fi
-  done
   if [ "$args" != "" ]
   then
     echo "Passing Geppetto args to VPX: $args."
+    pvargs=$(xe vm-param-get param-name=PV-args uuid="$v")
+    args="$pvargs $args"
     xe vm-param-set PV-args="$args" uuid="$v"
   fi
 }
@@ -429,13 +423,17 @@
 elif [ "$TEMPLATE_NAME" ]
 then
   echo $TEMPLATE_NAME
-  vm_uuid=$(xe_min vm-install template="$TEMPLATE_NAME" new-name-label="DevstackOSDomu")
+  vm_uuid=$(xe_min vm-install template="$TEMPLATE_NAME" new-name-label="$NAME_LABEL")
   destroy_vifs "$vm_uuid"
   set_auto_start "$vm_uuid"
   create_gi_vif "$vm_uuid"
   create_vm_vif "$vm_uuid"
   create_management_vif "$vm_uuid"
   create_public_vif "$vm_uuid"
+  set_kernel_params "$vm_uuid"
+  xe vm-param-set other-config:os-vpx=true uuid="$vm_uuid"
+  xe vm-param-set actions-after-reboot=Destroy uuid="$vm_uuid"
+  set_memory "$vm_uuid"
 else
   if [ ! -f "$VPX_FILE" ]
   then
diff --git a/tools/xen/xenrc b/tools/xen/xenrc
index 58fda31..f434b11 100644
--- a/tools/xen/xenrc
+++ b/tools/xen/xenrc
@@ -1,10 +1,10 @@
 #!/bin/bash
 
 # Name of this guest
-GUEST_NAME=${GUEST_NAME:-ALLINONE}
+GUEST_NAME=${GUEST_NAME:-DevStackOSDomU}
 
 # Size of image
-VDI_MB=${VDI_MB:-2500}
+VDI_MB=${VDI_MB:-5000}
 
 # VM Password
 GUEST_PASSWORD=${GUEST_PASSWORD:-secrete}
@@ -35,11 +35,7 @@
 MGT_VLAN=${MGT_VLAN:-101}
 MGT_DEV=${MGT_DEV:-eth0}
 
-# XVA Directory
-XVA_DIR=${XVA_DIR:-`pwd`/xvas}
-
-# Path to xva file
-XVA=${XVA:-$XVA_DIR/$GUEST_NAME.xva }
+OSDOMU_MEM_MB=1024
 
 # Source params
 cd ../.. && source ./stackrc && cd $TOP_DIR