summaryrefslogtreecommitdiff
path: root/.local/bin/umounter
blob: 126f1b816325f229312fbeb5626ade96ba19746f (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
#!/bin/sh

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

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 -i -p "Unmount which drive?")"
chosen="${chosen%% *}"
test -n "$chosen"

doas umount -l "/${chosen#*/}"
notify-send "Device unmounted." "$chosen has been unmounted."

# Close the chosen drive if decrypted.
cryptid="$(echo "$lsblkoutput" | grep "/${chosen#*/}$")"
cryptid="${cryptid%% *}"
test -b /dev/mapper/"${cryptid##*/}"
doas cryptsetup close "$cryptid"
notify-send "🔒Device dencryption closed." "Drive is now securely locked again."