xenapi: Set VM memory before starting it

If someone was re-using an existing template, for a memory change, he
needed to re-install the vm. This change sets the osdomu mem before
starting it, so it doesn't matter how much memory did the VM had
according to the template. It also removes the memory manipulation bits
from install-os-vpx.sh.

Change-Id: Iad85f573b90c23140012c20c552a17277d9c97a0
diff --git a/tools/xen/functions b/tools/xen/functions
index 7616a5f..a5c4b70 100644
--- a/tools/xen/functions
+++ b/tools/xen/functions
@@ -268,3 +268,22 @@
 
     xe network-attach uuid=$net host-uuid=$host
 }
+
+function set_vm_memory() {
+    local vm_name_label
+    local memory
+
+    vm_name_label="$1"
+    memory="$2"
+
+    local vm
+
+    vm=$(_vm_uuid "$vm_name_label")
+
+    xe vm-memory-limits-set \
+        static-min=${memory}MiB \
+        static-max=${memory}MiB \
+        dynamic-min=${memory}MiB \
+        dynamic-max=${memory}MiB \
+        uuid=$vm
+}
diff --git a/tools/xen/install_os_domU.sh b/tools/xen/install_os_domU.sh
index 997644d..dc7959a 100755
--- a/tools/xen/install_os_domU.sh
+++ b/tools/xen/install_os_domU.sh
@@ -228,8 +228,11 @@
     $THIS_DIR/scripts/install-os-vpx.sh \
         -t "$UBUNTU_INST_TEMPLATE_NAME" \
         -n "$UBUNTU_INST_BRIDGE_OR_NET_NAME" \
-        -l "$GUEST_NAME" \
-        -r "$OSDOMU_MEM_MB"
+        -l "$GUEST_NAME"
+
+    set_vm_memory "$GUEST_NAME" "$OSDOMU_MEM_MB"
+
+    xe vm-start vm="$GUEST_NAME"
 
     # wait for install to finish
     wait_for_VM_to_halt
@@ -255,6 +258,9 @@
 # Install XenServer tools, and other such things
 $THIS_DIR/prepare_guest_template.sh "$GUEST_NAME"
 
+# Set virtual machine parameters
+set_vm_memory "$GUEST_NAME" "$OSDOMU_MEM_MB"
+
 # start the VM to run the prepare steps
 xe vm-start vm="$GUEST_NAME"
 
diff --git a/tools/xen/scripts/install-os-vpx.sh b/tools/xen/scripts/install-os-vpx.sh
index 8ee8b67..c94a593 100755
--- a/tools/xen/scripts/install-os-vpx.sh
+++ b/tools/xen/scripts/install-os-vpx.sh
@@ -20,8 +20,6 @@
 set -eux
 
 BRIDGE=
-RAM=
-BALLOONING=
 NAME_LABEL=
 TEMPLATE_NAME=
 
@@ -29,7 +27,7 @@
 {
 cat << EOF
 
-  Usage: $0 -t TEMPLATE_NW_INSTALL -l NAME_LABEL [-n BRIDGE] [-r RAM] [-b] 
+  Usage: $0 -t TEMPLATE_NW_INSTALL -l NAME_LABEL [-n BRIDGE]
 
   Install a VM from a template
 
@@ -37,9 +35,6 @@
 
      -h           Shows this message.
      -t template  VM template to use
-     -b           Enable memory ballooning. When set min_RAM=RAM/2 max_RAM=RAM.
-     -r MiB       Specifies RAM used by the VPX, in MiB.
-                  By default it will take the value from the XVA.
      -l name      Specifies the name label for the VM.
      -n bridge    The bridge/network to use for eth0. Defaults to xenbr0
 EOF
@@ -53,12 +48,6 @@
       h) usage
          exit 1
          ;;
-      b)
-         BALLOONING=1
-         ;;
-      r)
-         RAM=$OPTARG
-         ;;
       n)
          BRIDGE=$OPTARG
          ;;
@@ -119,19 +108,6 @@
 }
 
 
-set_memory()
-{
-  local v="$1"
-  if [ "$RAM" != "" ]
-  then
-    echo "Setting RAM to $RAM MiB."
-    [ "$BALLOONING" == 1 ] && RAM_MIN=$(($RAM / 2)) || RAM_MIN=$RAM
-    xe vm-memory-limits-set static-min=16MiB static-max=${RAM}MiB \
-                            dynamic-min=${RAM_MIN}MiB dynamic-max=${RAM}MiB \
-                            uuid="$v"
-  fi
-}
-
 
 # Make the VM auto-start on server boot.
 set_auto_start()
@@ -161,5 +137,3 @@
 create_vif "$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"
-xe vm-start uuid=$vm_uuid