Renuka Apte | 83f8b1a | 2012-04-02 15:45:27 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | set -eux |
| 4 | |
| 5 | action="$1" |
| 6 | vm="$2" |
| 7 | device="${3-0}" |
| 8 | part="${4-}" |
| 9 | |
John Garbutt | 030fb23 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 10 | function xe_min() { |
Renuka Apte | 83f8b1a | 2012-04-02 15:45:27 -0700 | [diff] [blame] | 11 | local cmd="$1" |
| 12 | shift |
| 13 | xe "$cmd" --minimal "$@" |
| 14 | } |
| 15 | |
John Garbutt | 030fb23 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 16 | function 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 Apte | 83f8b1a | 2012-04-02 15:45:27 -0700 | [diff] [blame] | 25 | vm_uuid=$(xe_min vm-list name-label="$vm") |
| 26 | vdi_uuid=$(xe_min vbd-list params=vdi-uuid vm-uuid="$vm_uuid" \ |
| 27 | userdevice="$device") |
| 28 | |
| 29 | dom0_uuid=$(xe_min vm-list is-control-domain=true) |
| 30 | |
John Garbutt | 030fb23 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 31 | function get_mount_device() { |
John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 32 | vbd_uuid=$1 |
| 33 | |
| 34 | dev=$(xe_min vbd-list params=device uuid="$vbd_uuid") |
Euan Harris | 12229a7 | 2013-07-03 17:51:01 +0100 | [diff] [blame] | 35 | if [[ "$dev" =~ "sm/" || "$dev" =~ "blktap-2/" ]]; then |
John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 36 | DEBIAN_FRONTEND=noninteractive \ |
| 37 | apt-get --option "Dpkg::Options::=--force-confold" --assume-yes \ |
John Garbutt | 030fb23 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 38 | 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 Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 40 | if [ -z "$mapping" ]; then |
| 41 | echo "Failed to find mapping" |
| 42 | exit -1 |
| 43 | fi |
Euan Harris | a3ec804 | 2013-07-04 16:25:33 +0100 | [diff] [blame] | 44 | |
| 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 Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 55 | else |
| 56 | echo "/dev/$dev$part" |
| 57 | fi |
| 58 | } |
| 59 | |
John Garbutt | 030fb23 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 60 | function clean_dev_mappings() { |
| 61 | dev=$(xe_min vbd-list params=device uuid="$vbd_uuid") |
Euan Harris | 12229a7 | 2013-07-03 17:51:01 +0100 | [diff] [blame] | 62 | if [[ "$dev" =~ "sm/" || "$dev" =~ "blktap-2/" ]]; then |
John Garbutt | 030fb23 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 63 | kpartx -dv "/dev/$dev" |
| 64 | fi |
| 65 | } |
| 66 | |
| 67 | function open_vdi() { |
Renuka Apte | 83f8b1a | 2012-04-02 15:45:27 -0700 | [diff] [blame] | 68 | 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 Garbutt | 030fb23 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 73 | run_udev_settle |
John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 74 | |
| 75 | mount_device=$(get_mount_device "$vbd_uuid") |
| 76 | mount "$mount_device" "$mp" |
Renuka Apte | 83f8b1a | 2012-04-02 15:45:27 -0700 | [diff] [blame] | 77 | echo "Your vdi is mounted at $mp" |
| 78 | } |
| 79 | |
John Garbutt | 030fb23 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 80 | function close_vdi() { |
Renuka Apte | 83f8b1a | 2012-04-02 15:45:27 -0700 | [diff] [blame] | 81 | vbd_uuid=$(xe_min vbd-list vm-uuid="$dom0_uuid" vdi-uuid="$vdi_uuid") |
John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 82 | mount_device=$(get_mount_device "$vbd_uuid") |
John Garbutt | 030fb23 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 83 | run_udev_settle |
John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 84 | umount "$mount_device" |
Renuka Apte | 83f8b1a | 2012-04-02 15:45:27 -0700 | [diff] [blame] | 85 | |
John Garbutt | 030fb23 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 86 | clean_dev_mappings |
| 87 | |
Renuka Apte | 83f8b1a | 2012-04-02 15:45:27 -0700 | [diff] [blame] | 88 | xe vbd-unplug uuid=$vbd_uuid |
| 89 | xe vbd-destroy uuid=$vbd_uuid |
| 90 | } |
| 91 | |
John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 92 | if [ "$action" == "open" ]; then |
Renuka Apte | 83f8b1a | 2012-04-02 15:45:27 -0700 | [diff] [blame] | 93 | open_vdi |
John Garbutt | daadf74 | 2012-04-27 18:28:28 +0100 | [diff] [blame] | 94 | elif [ "$action" == "close" ]; then |
Renuka Apte | 83f8b1a | 2012-04-02 15:45:27 -0700 | [diff] [blame] | 95 | close_vdi |
| 96 | fi |