Add tools for mounting VHD as NBD from Linux
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
#!/bin/bash
|
||||
# readies a VHD device in Linux
|
||||
# nbd-mount /path/to/vhd /dev/nbd0
|
||||
APP_DIR="$(dirname "$0")"
|
||||
NBD_FIND_FREE="$APP_DIR/nbd-find-free"
|
||||
DRIVE="$1"
|
||||
DISK_LABEL="$2"
|
||||
DISK_PATH="/dev/disk/by-label/$DISK_LABEL"
|
||||
SLEEP_INTERVAL=0.5
|
||||
|
||||
# find an empty nbd device
|
||||
NBD_TARGET="$($NBD_FIND_FREE)"
|
||||
if [ -z "$NBD_TARGET" ]; then
|
||||
echo "Cannot find free nbd device"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# refuse to connect twice
|
||||
EXISTING_DRIVE=$(pgrep -f "qemu-nbd.*$DRIVE")
|
||||
if [ -n "$EXISTING_DRIVE" ]; then
|
||||
echo "$DRIVE already mounted" >&2
|
||||
exit 1
|
||||
fi
|
||||
EXISTING_NBD="$(pgrep -f "qemu-nbd.*$NBD_TARGET")"
|
||||
if [ -n "$EXISTING_NBD" ]; then
|
||||
echo "$NBD_TARGET already mounted" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
||||
# wait for device to appear
|
||||
if ! [ -e "$DISK_PATH" ]; then
|
||||
qemu-nbd --connect="$NBD_TARGET" "$DRIVE"
|
||||
|
||||
# poll until the block device node appears or task times out
|
||||
until [ -L "$DISK_PATH" ] && [ -b "$DISK_PATH" ]; do
|
||||
sleep "$SLEEP_INTERVAL"
|
||||
done
|
||||
fi
|
||||
Reference in New Issue
Block a user