summaryrefslogtreecommitdiff
path: root/scripts/.local/bin/dumnt
blob: 837aa59eeaa75e106da5dce9a09736e5d20dcd21 (plain)
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
#!/bin/sh

# Unmount USB drives or Android phones. Replaces the older `dmenuumount`. Fewer
# prompt and also de-decrypts LUKS drives that are unmounted.

DMENU="$(command -v cdmenu)"
set -e

mounteddroids="$(grep simple-mtpfs /etc/mtab | awk '{print "📱" $2}')"
lsblkoutput="$(lsblk -nrpo "name,type,size,mountpoint")"
mounteddrives="$(echo "$lsblkoutput" | awk '($2=="part"||$2="crypt")&&$4!~/\/boot|\/home$|SWAP/&&length($4)>1{printf "💾%s (%s)\n",$4,$3}')"

allunmountable="$(echo "$mounteddroids
$mounteddrives" | sed "/^$/d;s/ *$//")"
test -n "$allunmountable"

chosen="$(echo "$allunmountable" | $DMENU -l 16 -i -p "Unmount which drive?")"
chosen="${chosen%% *}"
test -n "$chosen"

# check for blocking processes
bprocs=$(lsof "/${chosen#*/}" | tail -n +2 | awk '{print $1 " " $2}' | uniq)
if [ -n "$bprocs" ]; then
    bPIDs="$(echo "$bprocs" | awk '{print $2}')"
    notify-send "Unmount $chosen" "Blocking processes:\n$bprocs"
    action=$(printf 'nothing\nlazy\nterminate\nkill' | "$DMENU" -i -p "$chosen is busy: (abort), (lazy) unmount or (terminate)/(kill) blocking processes")
    case $action in
        nothing)
            notify-send "Unmount aborted" "$chosen"
            exit 1;;
        lazy)       sudo -A umount -l "/${chosen#*/}";;
        terminate)  echo "$bPIDs" | while IFS="$(printf '\n')" read -r pid ; do kill "$pid"; done ;;
        kill)       echo "$bPIDs" | while IFS="$(printf '\n')" read -r pid ; do kill -9 "$pid"; done ;;
    esac
fi

sudo -A umount "/${chosen#*/}"
notify-send "Unmounted" "$chosen"

# Close the chosen drive if decrypted.
cryptid="$(echo "$lsblkoutput" | grep "/${chosen#*/}$")"
cryptid="${cryptid%% *}"
test -b /dev/mapper/"${cryptid##*/}"
sudo -A cryptsetup close "$cryptid"
notify-send "Encryption closed" "$chosen"