Add attach commands for losetup

This commit is contained in:
2026-09-08 03:51:41 +08:00
parent 8d301fe101
commit ed4cd63073
2 changed files with 66 additions and 0 deletions
+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"