blob: 909ce328b0b6bcabe34690b7cb79e094ff8dfdcf [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
John Garbutt030fb232012-04-27 18:28:28 +010010function xe_min() {
Renuka Apte83f8b1a2012-04-02 15:45:27 -070011 local cmd="$1"
12 shift
13 xe "$cmd" --minimal "$@"
14}
15
John Garbutt030fb232012-04-27 18:28:28 +010016function run_udev_settle() {
17 which_udev=$(which udevsettle) || true
18 if [ -n "$which_udev" ]; then
19 udevsettle
20 else
21 udevadm settle
22 fi
23}
24
Renuka Apte83f8b1a2012-04-02 15:45:27 -070025vm_uuid=$(xe_min vm-list name-label="$vm")
26vdi_uuid=$(xe_min vbd-list params=vdi-uuid vm-uuid="$vm_uuid" \
27 userdevice="$device")
28
29dom0_uuid=$(xe_min vm-list is-control-domain=true)
30
John Garbutt030fb232012-04-27 18:28:28 +010031function get_mount_device() {
John Garbuttdaadf742012-04-27 18:28:28 +010032 vbd_uuid=$1
33
34 dev=$(xe_min vbd-list params=device uuid="$vbd_uuid")
Euan Harris12229a72013-07-03 17:51:01 +010035 if [[ "$dev" =~ "sm/" || "$dev" =~ "blktap-2/" ]]; then
John Garbuttdaadf742012-04-27 18:28:28 +010036 DEBIAN_FRONTEND=noninteractive \
37 apt-get --option "Dpkg::Options::=--force-confold" --assume-yes \
John Garbutt030fb232012-04-27 18:28:28 +010038 install kpartx &> /dev/null || true
39 mapping=$(kpartx -av "/dev/$dev" | sed -ne 's,^add map \([a-z0-9\-]*\).*$,\1,p' | sed -ne "s,^\(.*${part}\)\$,\1,p")
John Garbuttdaadf742012-04-27 18:28:28 +010040 if [ -z "$mapping" ]; then
41 echo "Failed to find mapping"
42 exit -1
43 fi
Euan Harrisa3ec8042013-07-04 16:25:33 +010044
45 local device="/dev/mapper/${mapping}"
46 for (( i = 0; i < 5; i++ )) ; do
47 if [ -b $device ] ; then
48 echo $device
49 return
50 fi
51 sleep 1
52 done
53 echo "ERROR: timed out waiting for dev-mapper"
54 exit 1
John Garbuttdaadf742012-04-27 18:28:28 +010055 else
56 echo "/dev/$dev$part"
57 fi
58}
59
John Garbutt030fb232012-04-27 18:28:28 +010060function clean_dev_mappings() {
61 dev=$(xe_min vbd-list params=device uuid="$vbd_uuid")
Euan Harris12229a72013-07-03 17:51:01 +010062 if [[ "$dev" =~ "sm/" || "$dev" =~ "blktap-2/" ]]; then
John Garbutt030fb232012-04-27 18:28:28 +010063 kpartx -dv "/dev/$dev"
64 fi
65}
66
67function open_vdi() {
Renuka Apte83f8b1a2012-04-02 15:45:27 -070068 vbd_uuid=$(xe vbd-create vm-uuid="$dom0_uuid" vdi-uuid="$vdi_uuid" \
69 device=autodetect)
70 mp=$(mktemp -d)
71 xe vbd-plug uuid="$vbd_uuid"
72
John Garbutt030fb232012-04-27 18:28:28 +010073 run_udev_settle
John Garbuttdaadf742012-04-27 18:28:28 +010074
75 mount_device=$(get_mount_device "$vbd_uuid")
76 mount "$mount_device" "$mp"
Renuka Apte83f8b1a2012-04-02 15:45:27 -070077 echo "Your vdi is mounted at $mp"
78}
79
John Garbutt030fb232012-04-27 18:28:28 +010080function close_vdi() {
Renuka Apte83f8b1a2012-04-02 15:45:27 -070081 vbd_uuid=$(xe_min vbd-list vm-uuid="$dom0_uuid" vdi-uuid="$vdi_uuid")
John Garbuttdaadf742012-04-27 18:28:28 +010082 mount_device=$(get_mount_device "$vbd_uuid")
John Garbutt030fb232012-04-27 18:28:28 +010083 run_udev_settle
John Garbuttdaadf742012-04-27 18:28:28 +010084 umount "$mount_device"
Renuka Apte83f8b1a2012-04-02 15:45:27 -070085
John Garbutt030fb232012-04-27 18:28:28 +010086 clean_dev_mappings
87
Renuka Apte83f8b1a2012-04-02 15:45:27 -070088 xe vbd-unplug uuid=$vbd_uuid
89 xe vbd-destroy uuid=$vbd_uuid
90}
91
John Garbuttdaadf742012-04-27 18:28:28 +010092if [ "$action" == "open" ]; then
Renuka Apte83f8b1a2012-04-02 15:45:27 -070093 open_vdi
John Garbuttdaadf742012-04-27 18:28:28 +010094elif [ "$action" == "close" ]; then
Renuka Apte83f8b1a2012-04-02 15:45:27 -070095 close_vdi
96fi