blob: 05c4b0745c4be3406ab44ba13e4b147c7bd1c262 [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")
35 if [[ "$dev" =~ "sm/" ]]; then
36 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
John Garbutt030fb232012-04-27 18:28:28 +010044 echo "/dev/mapper/${mapping}"
John Garbuttdaadf742012-04-27 18:28:28 +010045 else
46 echo "/dev/$dev$part"
47 fi
48}
49
John Garbutt030fb232012-04-27 18:28:28 +010050function clean_dev_mappings() {
51 dev=$(xe_min vbd-list params=device uuid="$vbd_uuid")
52 if [[ "$dev" =~ "sm/" ]]; then
53 kpartx -dv "/dev/$dev"
54 fi
55}
56
57function open_vdi() {
Renuka Apte83f8b1a2012-04-02 15:45:27 -070058 vbd_uuid=$(xe vbd-create vm-uuid="$dom0_uuid" vdi-uuid="$vdi_uuid" \
59 device=autodetect)
60 mp=$(mktemp -d)
61 xe vbd-plug uuid="$vbd_uuid"
62
John Garbutt030fb232012-04-27 18:28:28 +010063 run_udev_settle
John Garbuttdaadf742012-04-27 18:28:28 +010064
65 mount_device=$(get_mount_device "$vbd_uuid")
66 mount "$mount_device" "$mp"
Renuka Apte83f8b1a2012-04-02 15:45:27 -070067 echo "Your vdi is mounted at $mp"
68}
69
John Garbutt030fb232012-04-27 18:28:28 +010070function close_vdi() {
Renuka Apte83f8b1a2012-04-02 15:45:27 -070071 vbd_uuid=$(xe_min vbd-list vm-uuid="$dom0_uuid" vdi-uuid="$vdi_uuid")
John Garbuttdaadf742012-04-27 18:28:28 +010072 mount_device=$(get_mount_device "$vbd_uuid")
John Garbutt030fb232012-04-27 18:28:28 +010073 run_udev_settle
John Garbuttdaadf742012-04-27 18:28:28 +010074 umount "$mount_device"
Renuka Apte83f8b1a2012-04-02 15:45:27 -070075
John Garbutt030fb232012-04-27 18:28:28 +010076 clean_dev_mappings
77
Renuka Apte83f8b1a2012-04-02 15:45:27 -070078 xe vbd-unplug uuid=$vbd_uuid
79 xe vbd-destroy uuid=$vbd_uuid
80}
81
John Garbuttdaadf742012-04-27 18:28:28 +010082if [ "$action" == "open" ]; then
Renuka Apte83f8b1a2012-04-02 15:45:27 -070083 open_vdi
John Garbuttdaadf742012-04-27 18:28:28 +010084elif [ "$action" == "close" ]; then
Renuka Apte83f8b1a2012-04-02 15:45:27 -070085 close_vdi
86fi