1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
#!/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
}
##
# 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 "$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
case $t in
phy)
dev=$(expand_dev $p)
FRONTEND_ID=$(xenstore_read "$XENBUS_PATH/frontend-id")
FRONTEND_UUID=$(xenstore_read_default \
"/local/domain/$FRONTEND_ID/vm" 'unknown')
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"
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
loopdev=''
for dev in /dev/loop*
do
if [ ! -b "$dev" ]
then
continue
fi
f=$(losetup "$dev" 2>/dev/null) || f=''
if [ "$f" ]
then
# $dev is in use. Check sharing.
if [ "x$mode" = 'x!' ]
then
continue
fi
f=$(echo "$f" | sed -e 's/.*(\(.*\)).*/\1/g')
# $f is the filename, as read from losetup, but the loopback
# driver truncates filenames at 64 characters, so we need to go
# trawling through the store if it's longer than that. Truncation
# is indicated by an asterisk at the end of the filename.
if expr index "$f" '*' >/dev/null
then
found=""
for dom in $(xenstore-list "$XENBUS_BASE_PATH")
do
for domdev in $(xenstore-list "$XENBUS_BASE_PATH/$dom")
do
d=$(xenstore_read_default \
"$XENBUS_BASE_PATH/$dom/$domdev/node" "")
if [ "$d" = "$dev" ]
then
f=$(xenstore_read "$XENBUS_BASE_PATH/$dom/$domdev/params")
found=1
break 2
fi
done
done
if [ ! "$found" ]
then
# This loopback device is in use by someone else, so skip it.
log debug "Loopback sharing check skips device $dev."
continue
fi
fi
# Canonicalise the filename for the comparison.
# I have seen this readlink fails because the filename given by
# losetup is only the basename. This cannot happen when the loop
# device is set up through this script, because file is
# canonicalised above, but it may happen when loop devices are set
# up some other way. This readlink may also conceivably fail if
# the file backing this loop device has been removed.
# For maximum safety, in the case that $f does not resolve, we
# assume that $file and $f are in the same directory.
# If you create a loopback filesystem, remove it and continue to
# run on it, and then create another file with the same name, then
# this check will block that -- don't do that.
# If you create loop devices through some other mechanism, use
# relative filenames, and then use the same filename through this
# script, then this check will block that -- don't do that either.
f=$(readlink -f "$f" || echo $(dirname "$file")/$(basename "$f"))
if [ "$f" = "$file" ]
then
check_file_sharing "$file" "$dev" "$mode"
fi
else
# $dev is not in use, so we'll remember it for use later; we want
# to finish the sharing check first.
if [ "$loopdev" = '' ]
then
loopdev="$dev"
fi
fi
done
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}"
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)
node=$(xenstore_read "$XENBUS_PATH/node")
losetup -d "$node"
exit 0
;;
"")
exit 0
;;
esac
;;
esac
# If we've reached here, $t is neither phy nor file, so fire a helper script.
[ -x /etc/xen/scripts/block-"$t" ] && \
/etc/xen/scripts/block-"$t" "$command" $node
|