Compare commits

...
2 Commits
Author SHA1 Message Date
madumlao ed4cd63073 Add attach commands for losetup 2026-09-08 03:51:41 +08:00
madumlao 8d301fe101 Actually unmount nbd 2026-09-08 03:46:52 +08:00
3 changed files with 67 additions and 2 deletions
+36
View File
@@ -0,0 +1,36 @@
#!/bin/bash
# readies a VHD device in Linux
# nbd-mount /path/to/vhd /dev/nbd0
APP_DIR="$(dirname "$0")"
DRIVE="$1"
DISK_LABEL="$2"
DISK_PATH="/dev/disk/by-label/$DISK_LABEL"
SLEEP_INTERVAL=0.5
# refuse to connect twice
EXISTING_DRIVE="$(losetup -l | tail -n +2 | tr -s ' ' | cut -f 6 | grep -w "$DRIVE")"
if [ -n "$EXISTING_DRIVE" ]; then
EXISTING_LOOP="$(losetup -l | tail -n +2 | tr -s ' ' | grep -w "$DRIVE" | cut -f 1 -d ' ')"
if ! [ -L "$DISK_PATH" ] || ! [ -b "$DISK_PATH" ]; then
echo "$DRIVE already mounted to $EXISTING_LOOP"
exit 1
fi
LOOP_DEST="$(readlink -f $DISK_PATH)"
if [ "$LOOP_DEST" != "$EXISTING_LOOP" ]; then
echo "$DRIVE already mounted to $EXISTING_LOOP"
exit 1
fi
# it's already mounted to the same destination
exit 0
fi
# wait for device to appear
if ! [ -e "$DISK_PATH" ]; then
losetup -f "$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
+30
View File
@@ -0,0 +1,30 @@
#!/bin/bash
# disconnects a VHD device from linux (should be unmounted)
# nbd-unmount path-to-vhd
DRIVE="$1"
if [ -z "$DRIVE" ]; then
echo "Drive not provided"
exit 1
fi
if ! [ -f "$DRIVE" ]; then
echo "$DRIVE not existing"
exit 1
fi
EXISTING_DRIVE="$(losetup -l | tail -n +2 | tr -s ' ' | cut -f 6 | grep -w "$DRIVE")"
if [ -z "$EXISTING_DRIVE" ]; then
echo "$DRIVE not mounted"
exit 1
fi
EXISTING_LOOP="$(losetup -l | tail -n +2 | tr -s ' ' | grep -w "$DRIVE" | cut -f 1 -d ' ')"
LOOP_DEST="$(readlink -f $EXISTING_LOOP)"
if mount | grep ^"$LOOP_DEST" -q ; then
echo "$LOOP_DEST mounted, refusing to detach"
exit 1
fi
losetup -d "$LOOP_DEST"
+1 -2
View File
@@ -27,10 +27,9 @@ if [ -z "$NBD_TARGET" ]; then
exit 1
fi
echo going to unmount "$NBD_TARGET"
if mount | grep ^"$NBD_TARGET" -q ; then
echo "$NBD_TARGET mounted, refusing to detach"
exit 1
fi
echo qemu-nbd --disconnect=$NBD_TARGET
qemu-nbd --disconnect=$NBD_TARGET