XenServer: Add script to mount OS domU in dom0
Change-Id: I1ad3d63c55b95f2588007c5e88704022f54e1c06
diff --git a/tools/xen/scripts/manage-vdi b/tools/xen/scripts/manage-vdi
new file mode 100755
index 0000000..a0a27e8
--- /dev/null
+++ b/tools/xen/scripts/manage-vdi
@@ -0,0 +1,52 @@
+#!/bin/bash
+
+set -eux
+
+action="$1"
+vm="$2"
+device="${3-0}"
+part="${4-}"
+
+xe_min()
+{
+ local cmd="$1"
+ shift
+ xe "$cmd" --minimal "$@"
+}
+
+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)
+
+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"
+
+ udevsettle
+ dev=$(xe_min vbd-list params=device uuid="$vbd_uuid")
+ mount "/dev/$dev$part" "$mp"
+ echo "Your vdi is mounted at $mp"
+}
+
+close_vdi()
+{
+ vbd_uuid=$(xe_min vbd-list vm-uuid="$dom0_uuid" vdi-uuid="$vdi_uuid")
+ dev=$(xe_min vbd-list params=device uuid="$vbd_uuid")
+ umount "/dev/$dev$part"
+
+ xe vbd-unplug uuid=$vbd_uuid
+ xe vbd-destroy uuid=$vbd_uuid
+}
+
+if [ "$action" == "open" ]
+then
+ open_vdi
+elif [ "$action" == "close" ]
+then
+ close_vdi
+fi