-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathumount.sh
More file actions
47 lines (34 loc) · 1.16 KB
/
umount.sh
File metadata and controls
47 lines (34 loc) · 1.16 KB
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
#!/bin/bash
# umount.sh - Unmount a disk image mounted with mount.sh
# Usage: ./umount.sh [mount_point]
set -e
if [ "$EUID" -ne 0 ]; then
echo "Error: This script must be run as root"
exit 1
fi
MOUNT_POINT="${1:-/mnt/img}"
if [ ! -d "$MOUNT_POINT" ]; then
echo "Error: Mount point '$MOUNT_POINT' not found"
exit 1
fi
# Find the loop device associated with the mount point
LOOP_DEV=$(findmnt -n -o SOURCE "$MOUNT_POINT" | grep -o '/dev/loop[0-9]*' | head -1)
if [ -z "$LOOP_DEV" ]; then
echo "Error: Could not find loop device for mount point '$MOUNT_POINT'"
echo "Attempting recursive unmount anyway..."
umount -R "$MOUNT_POINT" || true
exit 1
fi
MAPPER_DEV="/dev/mapper/$(basename $LOOP_DEV)"
echo "==> Unmounting all filesystems recursively from $MOUNT_POINT..."
umount -R "$MOUNT_POINT"
echo "==> Removing partition mappings..."
kpartx -d "$LOOP_DEV"
echo "==> Detaching loop device $LOOP_DEV..."
losetup -d "$LOOP_DEV"
echo "==> Removing mapper symlink $MAPPER_DEV..."
unlink "$MAPPER_DEV" 2>/dev/null || true
echo ""
echo "==> Successfully unmounted!"
echo " Mount point: $MOUNT_POINT"
echo " Loop device: $LOOP_DEV (detached)"