Make devstack work with xcp-xapi package on Ubuntu 12.04

- allow you to configure the xenapi_user (often other than root)
- allow you to disable the guest installer network
- install the plugins in the xcp-xapi location
- use alternate webserver location when adding the preseed file
- skip the centos specific ip forwarding configuration
- make use xcp inventory, if no xensource-inventory is found
- correctly deal with kpartx to mount the VM VDI in manage_vdi

Change-Id: I8d51725fc97f0bcaa27a46f7a7ced13c369c809e
diff --git a/tools/xen/build_xva.sh b/tools/xen/build_xva.sh
index 1803504..fdc6a60 100755
--- a/tools/xen/build_xva.sh
+++ b/tools/xen/build_xva.sh
@@ -118,6 +118,13 @@
     sed -e "s,@ETH3_NETMASK@,$PUB_NETMASK,g" -i $INTERFACES
 fi
 
+if [ "$ENABLE_GI" == "true" ]; then
+    cat <<EOF >>$INTERFACES
+auto eth0
+iface eth0 inet dhcp
+EOF
+fi
+
 # Gracefully cp only if source file/dir exists
 function cp_it {
     if [ -e $1 ] || [ -d $1 ]; then
diff --git a/tools/xen/scripts/manage-vdi b/tools/xen/scripts/manage-vdi
index 7f12ebc..05c4b07 100755
--- a/tools/xen/scripts/manage-vdi
+++ b/tools/xen/scripts/manage-vdi
@@ -7,64 +7,74 @@
 device="${3-0}"
 part="${4-}"
 
-xe_min()
-{
+function xe_min() {
   local cmd="$1"
   shift
   xe "$cmd" --minimal "$@"
 }
 
+function run_udev_settle() {
+  which_udev=$(which udevsettle) || true
+  if [ -n "$which_udev" ]; then
+      udevsettle
+  else
+      udevadm settle
+  fi
+}
+
 vm_uuid=$(xe_min vm-list name-label="$vm")
 vdi_uuid=$(xe_min vbd-list params=vdi-uuid vm-uuid="$vm_uuid" \
                            userdevice="$device")
 
 dom0_uuid=$(xe_min vm-list is-control-domain=true)
 
-get_mount_device()
-{
+function get_mount_device() {
   vbd_uuid=$1
 
   dev=$(xe_min vbd-list params=device uuid="$vbd_uuid")
   if [[ "$dev" =~ "sm/" ]]; then
     DEBIAN_FRONTEND=noninteractive \
         apt-get --option "Dpkg::Options::=--force-confold" --assume-yes \
-        install kpartx || true &> /dev/null
-    mapping=$(kpartx -av "/dev/$dev" | sed -ne 's,^add map \([a-f0-9\-]*\).*$,\1,p' | sed -ne "s,^\(.*${part}\)\$,\1,p")
+        install kpartx &> /dev/null || true
+    mapping=$(kpartx -av "/dev/$dev" | sed -ne 's,^add map \([a-z0-9\-]*\).*$,\1,p' | sed -ne "s,^\(.*${part}\)\$,\1,p")
     if [ -z "$mapping" ]; then
        echo "Failed to find mapping"
        exit -1
     fi
-    echo "mapper/${mapping}"
+    echo "/dev/mapper/${mapping}"
   else
     echo "/dev/$dev$part"
   fi
 }
 
-open_vdi()
-{
+function clean_dev_mappings() {
+  dev=$(xe_min vbd-list params=device uuid="$vbd_uuid")
+  if [[ "$dev" =~ "sm/" ]]; then
+    kpartx -dv "/dev/$dev"
+  fi
+}
+
+function open_vdi() {
   vbd_uuid=$(xe vbd-create vm-uuid="$dom0_uuid" vdi-uuid="$vdi_uuid" \
                          device=autodetect)
   mp=$(mktemp -d)
   xe vbd-plug uuid="$vbd_uuid"
 
-  which_udev=$(which udevsettle) || true
-  if [ -n "$which_udev" ]; then
-      udevsettle
-  else
-      udevadm settle
-  fi
+  run_udev_settle
 
   mount_device=$(get_mount_device "$vbd_uuid")
   mount "$mount_device" "$mp"
   echo "Your vdi is mounted at $mp"
 }
 
-close_vdi()
-{
+function close_vdi() {
   vbd_uuid=$(xe_min vbd-list vm-uuid="$dom0_uuid" vdi-uuid="$vdi_uuid")
   mount_device=$(get_mount_device "$vbd_uuid")
+  run_udev_settle
   umount "$mount_device"
 
+  clean_dev_mappings
+
   xe vbd-unplug uuid=$vbd_uuid
   xe vbd-destroy uuid=$vbd_uuid
 }
diff --git a/tools/xen/templates/interfaces.in b/tools/xen/templates/interfaces.in
index e315a8c..74b41cc 100644
--- a/tools/xen/templates/interfaces.in
+++ b/tools/xen/templates/interfaces.in
@@ -21,6 +21,3 @@
 iface eth2 inet static
         address @ETH2_IP@
         netmask @ETH2_NETMASK@
-
-auto eth0
-iface eth0 inet dhcp
diff --git a/tools/xen/xenrc b/tools/xen/xenrc
index bf3d16a..102a492 100644
--- a/tools/xen/xenrc
+++ b/tools/xen/xenrc
@@ -38,5 +38,8 @@
 MGT_VLAN=${MGT_VLAN:-101}
 MGT_DEV=${MGT_DEV:-eth0}
 
+# Guest installer network
+ENABLE_GI=true
+
 # Source params
 cd ../.. && source ./stackrc && cd $TOP_DIR