blob: 7f12ebc182de14ca37d392108ef6d6d6b1edac5f [file] [log] [blame]
Renuka Apte83f8b1a2012-04-02 15:45:27 -07001#!/bin/bash
2
3set -eux
4
5action="$1"
6vm="$2"
7device="${3-0}"
8part="${4-}"
9
10xe_min()
11{
12 local cmd="$1"
13 shift
14 xe "$cmd" --minimal "$@"
15}
16
17vm_uuid=$(xe_min vm-list name-label="$vm")
18vdi_uuid=$(xe_min vbd-list params=vdi-uuid vm-uuid="$vm_uuid" \
19 userdevice="$device")
20
21dom0_uuid=$(xe_min vm-list is-control-domain=true)
22
John Garbuttdaadf742012-04-27 18:28:28 +010023get_mount_device()
24{
25 vbd_uuid=$1
26
27 dev=$(xe_min vbd-list params=device uuid="$vbd_uuid")
28 if [[ "$dev" =~ "sm/" ]]; then
29 DEBIAN_FRONTEND=noninteractive \
30 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes \
31 install kpartx || true &> /dev/null
32 mapping=$(kpartx -av "/dev/$dev" | sed -ne 's,^add map \([a-f0-9\-]*\).*$,\1,p' | sed -ne "s,^\(.*${part}\)\$,\1,p")
33 if [ -z "$mapping" ]; then
34 echo "Failed to find mapping"
35 exit -1
36 fi
37 echo "mapper/${mapping}"
38 else
39 echo "/dev/$dev$part"
40 fi
41}
42
Renuka Apte83f8b1a2012-04-02 15:45:27 -070043open_vdi()
44{
45 vbd_uuid=$(xe vbd-create vm-uuid="$dom0_uuid" vdi-uuid="$vdi_uuid" \
46 device=autodetect)
47 mp=$(mktemp -d)
48 xe vbd-plug uuid="$vbd_uuid"
49
John Garbuttdaadf742012-04-27 18:28:28 +010050 which_udev=$(which udevsettle) || true
51 if [ -n "$which_udev" ]; then
52 udevsettle
53 else
54 udevadm settle
55 fi
56
57 mount_device=$(get_mount_device "$vbd_uuid")
58 mount "$mount_device" "$mp"
Renuka Apte83f8b1a2012-04-02 15:45:27 -070059 echo "Your vdi is mounted at $mp"
60}
61
62close_vdi()
63{
64 vbd_uuid=$(xe_min vbd-list vm-uuid="$dom0_uuid" vdi-uuid="$vdi_uuid")
John Garbuttdaadf742012-04-27 18:28:28 +010065 mount_device=$(get_mount_device "$vbd_uuid")
66 umount "$mount_device"
Renuka Apte83f8b1a2012-04-02 15:45:27 -070067
68 xe vbd-unplug uuid=$vbd_uuid
69 xe vbd-destroy uuid=$vbd_uuid
70}
71
John Garbuttdaadf742012-04-27 18:28:28 +010072if [ "$action" == "open" ]; then
Renuka Apte83f8b1a2012-04-02 15:45:27 -070073 open_vdi
John Garbuttdaadf742012-04-27 18:28:28 +010074elif [ "$action" == "close" ]; then
Renuka Apte83f8b1a2012-04-02 15:45:27 -070075 close_vdi
76fi