unmounter (1025B)
1 #!/bin/sh 2 # $ ummounter 3 # you will then be guided through the rest of the steps 4 # works best with USB drives, not tested with other devices 5 6 set -e 7 8 sudo -v || exit 1 9 10 mapped_devices="" 11 for path in /dev/mapper/usb*; do 12 [ -e "$path" ] || continue 13 mapped_devices="$mapped_devices $(basename "$path")" 14 done 15 16 set -- $mapped_devices 17 18 [ "$#" -eq 0 ] && exit 1 19 20 echo "Devices Found" 21 i=1 22 for dev in "$@"; do 23 echo "[$i] /dev/mapper/$dev" 24 i=$((i + 1)) 25 done 26 27 if [ $i -eq 2 ]; then 28 index=1 29 else 30 printf "select a device to unmount [1-%d]: " $(( $# )) 31 read index 32 33 case "$index" in 34 ''|*[!1-9]*) 35 exit 1 36 ;; 37 esac 38 39 if [ "$index" -lt 1 ] || [ "$index" -ge "$#" ]; then 40 exit 1 41 fi 42 fi 43 44 i=1 45 for dev in "$@"; do 46 [ "$i" -eq "$index" ] && name="$dev" && break 47 i=$((i + 1)) 48 done 49 50 mount_point="/mnt/$name" 51 52 if mountpoint -q "$mount_point" 2>/dev/null; then 53 echo "umounting $mount_point" 54 sudo umount "$mount_point" 55 fi 56 57 echo "cryptsetup closing $name" 58 sudo cryptsetup close "$name" 59 60 echo "/dev/mapper/$name unmounted successfully"