mirror of
https://github.com/xen-project/xen.git
synced 2026-10-28 18:21:39 +01:00
These are provided using the "script=" syntax described in docs/misc/xl-disk-configuration.txt. The existing hotplug scripts currently conflate two different concepts, namely that of making a datapath available in the backend domain (logging into iSCSI LUNs and the like) and that of actually connecting that datapath to a Xen backend path (e.g. writing "physical-device" node in xenstore to bring up blkback). For this reason the script support implemented here is only supported in conjunction with backendtype=phy. Eventually we hope to rework the hotplug scripts to separate the to concepts, but that is not 4.2 material. In addition there are some other subtleties: - Previously in the blktap case we would add "script = .../blktap" to the backend flex array, but then jumped to the PHY case which added "script = .../block" too. The block one takes precendence since it comes second. This was, accidentally, correct. The blktap script is for blktap1 devices and not blktap2 devices. libxl completely manages the blktap2 side of things without resorting to hotplug scripts and creates a blkback device directly. Therefore the "block" script is always the correct one to call. Custom script are not supported in this context. - libxl should not write the "physical-device" node. This is the responsibility of the block script. Writing the "physical-device" node in libxl basically completely short-cuts the standard block hotplug script which uses "physical-device" to know if it has run already or not. In the case of more complex scripts libxl cannot know the right value to write here anyway, in particular the device may not exist until after the script is called. This change has the side effect of re-enabling the checks for device sharing aspect of the default block script, which I have tested and which now cause libxl to properly abort now that libxl properly checks for hotplug script errors. There is no sharing check for blktap2 since even if you reuse the same vhd the resulting tap device is different. I would have preferred to simply write the "physical-device" node for the blktap2 case but the hotplug script infrastructure is not currently setup to handle LIBXL__DEVICE_KIND_VBD devices without a hotplug script (backendtype phy and tap both end up as KIND_VBD). Changing this was more surgery than I was happy doing for 4.2 and therefore I have simply hardcoded to the block script for the LIBXL_DISK_BACKEND_TAP case. - libxl__device_disk_set_backend running against a phy device with a script cannot stat the device to check its properties since it may not exist until the script is run. Therefore I have special cased this in disk_try_backend to simply assume that backend == phy is always ok if a script was configured. Similarly the other backend types are always rejected if a script was configured. Note that the reason for implementing the default script behaviour in device_disk_add instead of libxl__device_disk_setdefault is because we need to be able to tell when the script was user-supplied rather than defaulted by libxl in order to correctly implement the above. The setdefault function must be idempotent so we cannot simply update disk->script. I suspect that for 4.3 a script member should be added to libxl__device, this would also help in the case above of handling devices with no script in a consistent manner. This is not 4.2 material. - When the block script falls through and shells out to a block-$type script it used to pass "$node" however the only place this was assigned was in the remove+phy case (in which case it contains the file:// derived /dev/loopN device), and in that case the script exits without falling through to the block-$type case. Since libxl never creates a type other than phy this never happens in practice anyway and we now call the correct block-$type script directly. But fix it up anyway since it is confusing. - The block-nbd and block-enbd scripts which we supply appear to be broken WRT the hotplug calling convention, in that they seem to expect a command line parameter (perhaps the $node described above) rather than reading the appropriate node from xenstore. I rather suspect this was broken by 7774:e2e7f47e6f79 in November 2005. I think it is safe to say no one is using these scripts! I haven't fixed this here. It would be good to track down some working scripts and either incorproate them or defer to them in their existing home (e.g. if they live somewhere useful like the nbd tools package). - Added a few block script related entries to check-xl-disk-parse from http://backdrift.org/xen-block-iscsi-script-with-multipath-support and http://lists.linbit.com/pipermail/drbd-user/2008-September/010221.html / http://www.drbd.org/users-guide-emb/s-xen-configure-domu.html (and snuck in another interesting empty CDROM case) This highlighted two bugs in the libxlu disk parser handling of the deprecated "<script>:" prefix: - It was failing to prefix with "block-" to construct the actual script name - The regex for matching iscsi or drdb or e?nbd was incorrect - Use libxl__abs_path for the nic script too. Just because the existing code nearly tricked me into repeating the mistake I have tested with a custom block script which uses "lvchange -a" to dynamically add remove the referenced device (simulates iSCSI login/logout without requiring me to faff around setting up an iSCSI target). I also tested on a blktap2 system. I haven't directly tested anything more complex like iscsi: or nbd: other than what check-xl-disk-parse exercises. [ reran flex/bison -iwj ] Signed-off-by: Ian Campbell <ian.campbell@citrix.com> Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
346 lines
7.5 KiB
Bash
346 lines
7.5 KiB
Bash
#!/bin/bash
|
|
|
|
dir=$(dirname "$0")
|
|
. "$dir/block-common.sh"
|
|
|
|
expand_dev() {
|
|
local dev
|
|
case $1 in
|
|
/*)
|
|
dev=$1
|
|
;;
|
|
*)
|
|
dev=/dev/$1
|
|
;;
|
|
esac
|
|
echo -n $dev
|
|
}
|
|
|
|
find_free_loopback_helper() {
|
|
local next_devnum=0
|
|
local busy_devnum
|
|
while read busy_devnum; do
|
|
if [ "$next_devnum" != "$busy_devnum" ]; then
|
|
break
|
|
fi
|
|
let next_devnum=$next_devnum+1
|
|
done
|
|
echo "/dev/loop${next_devnum}"
|
|
}
|
|
|
|
# Not all distros have "losetup -f"
|
|
find_free_loopback_dev() {
|
|
local loopdev
|
|
loopdev=$(losetup -a | sed -e 's+^/dev/loop++' -e 's/:.*//' | find_free_loopback_helper)
|
|
if [ -n "$loopdev" ] && [ -b "$loopdev" ]; then
|
|
echo "$loopdev"
|
|
fi
|
|
}
|
|
|
|
##
|
|
# check_sharing device mode
|
|
#
|
|
# Check whether the device requested is already in use. To use the device in
|
|
# read-only mode, it may be in use in read-only mode, but may not be in use in
|
|
# read-write anywhere at all. To use the device in read-write mode, it must
|
|
# not be in use anywhere at all.
|
|
#
|
|
# Prints one of
|
|
#
|
|
# 'local': the device may not be used because it is mounted in the current
|
|
# (i.e. the privileged domain) in a way incompatible with the
|
|
# requested mode;
|
|
# 'guest': the device may not be used because it already mounted by a guest
|
|
# in a way incompatible with the requested mode; or
|
|
# 'ok': the device may be used.
|
|
#
|
|
check_sharing()
|
|
{
|
|
local dev="$1"
|
|
local mode="$2"
|
|
|
|
local devmm=$(device_major_minor "$dev")
|
|
local file
|
|
|
|
if [ "$mode" = 'w' ]
|
|
then
|
|
toskip="^$"
|
|
else
|
|
toskip="^[^ ]* [^ ]* [^ ]* ro[, ]"
|
|
fi
|
|
|
|
for file in $(cat /proc/mounts | grep -v "$toskip" | cut -f 1 -d ' ')
|
|
do
|
|
if [ -e "$file" ]
|
|
then
|
|
local d=$(device_major_minor "$file")
|
|
|
|
if [ "$d" = "$devmm" ]
|
|
then
|
|
echo 'local'
|
|
return
|
|
fi
|
|
fi
|
|
done
|
|
|
|
local base_path="$XENBUS_BASE_PATH/$XENBUS_TYPE"
|
|
for dom in $(xenstore-list "$base_path")
|
|
do
|
|
for dev in $(xenstore-list "$base_path/$dom")
|
|
do
|
|
d=$(xenstore_read_default "$base_path/$dom/$dev/physical-device" "")
|
|
|
|
if [ "$d" = "$devmm" ]
|
|
then
|
|
if [ "$mode" = 'w' ]
|
|
then
|
|
if ! same_vm $dom
|
|
then
|
|
echo 'guest'
|
|
return
|
|
fi
|
|
else
|
|
local m=$(xenstore_read_default "$base_path/$dom/$dev/mode" "")
|
|
m=$(canonicalise_mode "$m")
|
|
|
|
if [ "$m" = 'w' ]
|
|
then
|
|
if ! same_vm $dom
|
|
then
|
|
echo 'guest'
|
|
return
|
|
fi
|
|
fi
|
|
fi
|
|
fi
|
|
done
|
|
done
|
|
|
|
echo 'ok'
|
|
}
|
|
|
|
|
|
##
|
|
# check_device_sharing dev mode
|
|
#
|
|
# Perform the sharing check for the given physical device and mode.
|
|
#
|
|
check_device_sharing()
|
|
{
|
|
local dev="$1"
|
|
local mode=$(canonicalise_mode "$2")
|
|
local result
|
|
|
|
if [ "x$mode" = 'x!' ]
|
|
then
|
|
return 0
|
|
fi
|
|
|
|
result=$(check_sharing "$dev" "$mode")
|
|
|
|
if [ "$result" != 'ok' ]
|
|
then
|
|
do_ebusy "Device $dev is mounted " "$mode" "$result"
|
|
fi
|
|
}
|
|
|
|
|
|
##
|
|
# check_device_sharing file dev mode
|
|
#
|
|
# Perform the sharing check for the given file mounted through the given
|
|
# loopback interface, in the given mode.
|
|
#
|
|
check_file_sharing()
|
|
{
|
|
local file="$1"
|
|
local dev="$2"
|
|
local mode="$3"
|
|
|
|
result=$(check_sharing "$dev" "$mode")
|
|
|
|
if [ "$result" != 'ok' ]
|
|
then
|
|
do_ebusy "File $file is loopback-mounted through $dev,
|
|
which is mounted " "$mode" "$result"
|
|
fi
|
|
}
|
|
|
|
|
|
##
|
|
# do_ebusy prefix mode result
|
|
#
|
|
# Helper function for check_device_sharing check_file_sharing, calling ebusy
|
|
# with an error message constructed from the given prefix, mode, and result
|
|
# from a call to check_sharing.
|
|
#
|
|
do_ebusy()
|
|
{
|
|
local prefix="$1"
|
|
local mode="$2"
|
|
local result="$3"
|
|
|
|
if [ "$result" = 'guest' ]
|
|
then
|
|
dom='a guest '
|
|
when='now'
|
|
else
|
|
dom='the privileged '
|
|
when='by a guest'
|
|
fi
|
|
|
|
if [ "$mode" = 'w' ]
|
|
then
|
|
m1=''
|
|
m2=''
|
|
else
|
|
m1='read-write '
|
|
m2='read-only '
|
|
fi
|
|
|
|
release_lock "block"
|
|
ebusy \
|
|
"${prefix}${m1}in ${dom}domain,
|
|
and so cannot be mounted ${m2}${when}."
|
|
}
|
|
|
|
|
|
t=$(xenstore_read_default "$XENBUS_PATH/type" 'MISSING')
|
|
|
|
case "$command" in
|
|
add)
|
|
phys=$(xenstore_read_default "$XENBUS_PATH/physical-device" 'MISSING')
|
|
if [ "$phys" != 'MISSING' ]
|
|
then
|
|
# Depending upon the hotplug configuration, it is possible for this
|
|
# script to be called twice, so just bail.
|
|
exit 0
|
|
fi
|
|
|
|
if [ -n "$t" ]
|
|
then
|
|
p=$(xenstore_read "$XENBUS_PATH/params")
|
|
mode=$(xenstore_read "$XENBUS_PATH/mode")
|
|
fi
|
|
FRONTEND_ID=$(xenstore_read "$XENBUS_PATH/frontend-id")
|
|
FRONTEND_UUID=$(xenstore_read_default \
|
|
"/local/domain/$FRONTEND_ID/vm" 'unknown')
|
|
|
|
case $t in
|
|
phy)
|
|
dev=$(expand_dev $p)
|
|
|
|
if [ -L "$dev" ]
|
|
then
|
|
dev=$(readlink -f "$dev") || fatal "$dev link does not exist."
|
|
fi
|
|
test -e "$dev" || fatal "$dev does not exist."
|
|
test -b "$dev" || fatal "$dev is not a block device."
|
|
|
|
claim_lock "block"
|
|
check_device_sharing "$dev" "$mode"
|
|
write_dev "$dev"
|
|
release_lock "block"
|
|
exit 0
|
|
;;
|
|
|
|
file)
|
|
# Canonicalise the file, for sharing check comparison, and the mode
|
|
# for ease of use here.
|
|
file=$(readlink -f "$p") || fatal "$p does not exist."
|
|
test -f "$file" || fatal "$file does not exist."
|
|
mode=$(canonicalise_mode "$mode")
|
|
|
|
claim_lock "block"
|
|
|
|
# Avoid a race with the remove if the path has been deleted, or
|
|
# otherwise changed from "InitWait" state e.g. due to a timeout
|
|
xenbus_state=$(xenstore_read_default "$XENBUS_PATH/state" 'unknown')
|
|
if [ "$xenbus_state" != '2' ]
|
|
then
|
|
release_lock "block"
|
|
fatal "Path closed or removed during hotplug add: $XENBUS_PATH state: $xenbus_state"
|
|
fi
|
|
|
|
if [ "$mode" = 'w' ] && ! stat "$file" -c %A | grep -q w
|
|
then
|
|
release_lock "block"
|
|
ebusy \
|
|
"File $file is read-only, and so I will not
|
|
mount it read-write in a guest domain."
|
|
fi
|
|
|
|
if [ "x$mode" != 'x!' ]
|
|
then
|
|
inode=$(stat -c '%i' "$file")
|
|
dev=$(stat -c '%D' "$file")
|
|
if [ -z "$inode" ] || [ -z "$dev" ]
|
|
then
|
|
fatal "Unable to lookup $file: dev: $dev inode: $inode"
|
|
fi
|
|
|
|
shared_list=$(losetup -a |
|
|
sed -n -e "s@^\([^:]\+\)\(:[[:blank:]]\[0*${dev}\]:${inode}[[:blank:]](.*)\)@\1@p" )
|
|
for dev in $shared_list
|
|
do
|
|
if [ -n "$dev" ]
|
|
then
|
|
check_file_sharing "$file" "$dev" "$mode"
|
|
fi
|
|
done
|
|
fi
|
|
|
|
loopdev=$(losetup -f 2>/dev/null || find_free_loopback_dev)
|
|
if [ "$loopdev" = '' ]
|
|
then
|
|
release_lock "block"
|
|
fatal 'Failed to find an unused loop device'
|
|
fi
|
|
|
|
if LANG=C losetup -h 2>&1 | grep read-only >/dev/null
|
|
then
|
|
roflag="-$mode"; roflag="${roflag#-w}"; roflag="${roflag#-!}"
|
|
else
|
|
roflag=''
|
|
fi
|
|
do_or_die losetup $roflag "$loopdev" "$file"
|
|
xenstore_write "$XENBUS_PATH/node" "$loopdev"
|
|
write_dev "$loopdev"
|
|
release_lock "block"
|
|
exit 0
|
|
;;
|
|
|
|
"")
|
|
claim_lock "block"
|
|
success
|
|
release_lock "block"
|
|
;;
|
|
esac
|
|
;;
|
|
|
|
remove)
|
|
case $t in
|
|
phy)
|
|
exit 0
|
|
;;
|
|
|
|
file)
|
|
claim_lock "block"
|
|
node=$(xenstore_read "$XENBUS_PATH/node")
|
|
losetup -d "$node"
|
|
release_lock "block"
|
|
exit 0
|
|
;;
|
|
|
|
"")
|
|
exit 0
|
|
;;
|
|
esac
|
|
;;
|
|
|
|
esac
|
|
|
|
# If we've reached here, $t is neither phy nor file, so fire a helper script.
|
|
[ -x ${XEN_SCRIPT_DIR}/block-"$t" ] && \
|
|
${XEN_SCRIPT_DIR}/block-"$t" "$command"
|