| 1 |
#!/bin/bash -e |
| 2 |
# |
| 3 |
# dummy Xen block device hotplug script |
| 4 |
# |
| 5 |
# Author George Dunlap <[email protected]> |
| 6 |
# |
| 7 |
# Based on block-iscsi by Roger Pau Monné <[email protected]> |
| 8 |
# |
| 9 |
# This program is free software; you can redistribute it and/or modify |
| 10 |
# it under the terms of the GNU Lesser General Public License as published |
| 11 |
# by the Free Software Foundation; version 2.1 only. with the special |
| 12 |
# exception on linking described in file LICENSE. |
| 13 |
# |
| 14 |
# This program is distributed in the hope that it will be useful, |
| 15 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
# GNU Lesser General Public License for more details. |
| 18 |
# |
| 19 |
# Usage: |
| 20 |
# |
| 21 |
# Target should be specified using the following syntax: |
| 22 |
# |
| 23 |
# script=block-dummy,vdev=xvda,target=dummy:<file> |
| 24 |
|
| 25 |
dir=$(dirname "$0") |
| 26 |
. "$dir/block-common.sh" |
| 27 |
|
| 28 |
check_tools() |
| 29 |
{ |
| 30 |
if ! command -v losetup > /dev/null 2>&1; then |
| 31 |
fatal "Unable to find losetup" |
| 32 |
fi |
| 33 |
} |
| 34 |
|
| 35 |
# Sets the following global variables based on the params field passed in as |
| 36 |
# a parameter: type file |
| 37 |
parse_target() |
| 38 |
{ |
| 39 |
params=($(echo "$1" | tr ":" "\n")) |
| 40 |
|
| 41 |
type=${params[0]} |
| 42 |
file=${params[1]} |
| 43 |
if [ -z "$type" ] || [ -z "$file" ]; then |
| 44 |
fatal "Cannot parse required parameters" |
| 45 |
fi |
| 46 |
|
| 47 |
if [ "$type" != "dummy" ] ; then |
| 48 |
fatal "Invalid type: $type" |
| 49 |
fi |
| 50 |
} |
| 51 |
|
| 52 |
# Attaches the device and writes xenstore backend entries to connect |
| 53 |
# the device |
| 54 |
add() |
| 55 |
{ |
| 56 |
test -f "$file" || fatal "$file does not exist." |
| 57 |
|
| 58 |
loopdev=$(losetup -f 2>/dev/null || find_free_loopback_dev) |
| 59 |
if [ "$loopdev" = '' ] |
| 60 |
then |
| 61 |
fatal 'Failed to find an unused loop device' |
| 62 |
fi |
| 63 |
|
| 64 |
if LANG=C losetup -h 2>&1 | grep read-only >/dev/null |
| 65 |
then |
| 66 |
roflag="-$mode"; roflag="${roflag#-w}"; roflag="${roflag#-!}" |
| 67 |
else |
| 68 |
roflag='' |
| 69 |
fi |
| 70 |
|
| 71 |
do_or_die losetup $roflag "$loopdev" "$file" |
| 72 |
# FIXME Is this OK? |
| 73 |
xenstore_write "$XENBUS_PATH/node" "$loopdev" |
| 74 |
write_dev "$loopdev" |
| 75 |
} |
| 76 |
|
| 77 |
# Disconnects the device |
| 78 |
remove() |
| 79 |
{ |
| 80 |
node=$(xenstore_read "$XENBUS_PATH/node") |
| 81 |
losetup -d "$node" |
| 82 |
} |
| 83 |
|
| 84 |
command=$1 |
| 85 |
target=$(xenstore-read $XENBUS_PATH/params || true) |
| 86 |
if [ -z "$target" ]; then |
| 87 |
fatal "No information about the target" |
| 88 |
fi |
| 89 |
|
| 90 |
parse_target "$target" |
| 91 |
|
| 92 |
mode=$(xenstore_read "$XENBUS_PATH/mode") |
| 93 |
mode=$(canonicalise_mode "$mode") |
| 94 |
|
| 95 |
check_tools || exit 1 |
| 96 |
|
| 97 |
case $command in |
| 98 |
add) |
| 99 |
add |
| 100 |
;; |
| 101 |
remove) |
| 102 |
remove |
| 103 |
;; |
| 104 |
*) |
| 105 |
exit 1 |
| 106 |
;; |
| 107 |
esac |