summaryrefslogtreecommitdiff
path: root/debian/.local/bin/unmounter
diff options
context:
space:
mode:
authorTheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com>2025-12-24 13:54:03 +0900
committerTheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com>2025-12-24 13:54:03 +0900
commit28e8bdf7f8286bd431b7f3b709e79f3827b31469 (patch)
tree85b44eff6da4d8443198fb6e04dfb6ee55244588 /debian/.local/bin/unmounter
parent8470ff001befcfd0f626dea69a9e76d43aee0511 (diff)
updates
Diffstat (limited to 'debian/.local/bin/unmounter')
-rwxr-xr-xdebian/.local/bin/unmounter57
1 files changed, 57 insertions, 0 deletions
diff --git a/debian/.local/bin/unmounter b/debian/.local/bin/unmounter
new file mode 100755
index 0000000..537fd25
--- /dev/null
+++ b/debian/.local/bin/unmounter
@@ -0,0 +1,57 @@
+#!/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}')"
+mountedios="$(grep ifuse /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}')"
+mountedcifs="$(grep cifs /etc/mtab | awk '{print "🪟" $2}')"
+
+allunmountable="$(echo "$mounteddroids
+$mountedios
+$mounteddrives
+$mountedcifs" | sed "/^$/d;s/ *$//")"
+test -n "$allunmountable"
+
+chosen="$(echo "$allunmountable" | dmenu -i -p "Unmount which drive?")"
+chosen="${chosen%% *}"
+test -n "$chosen"
+
+label=$(df "/${chosen#*/}" | tail -n 1 | awk '{print $1}' | xargs -I {} sudo blkid {} | awk -F '\"' '{print $2}')
+if [ -n "$label" ]; then
+ mountpath="$(sudo lsblk -no "mountpoints" "$(df "/${chosen#*/}" | tail -n 1 | awk '{print $1}')")"
+ sudo -A umount -l "/${chosen#*/}"
+ notify-send "⏏️ Device unmounted." "$chosen has been unmounted."
+ if [ "/media/$USER/${chosen##*/}" = "/${chosen#*/}" ] &&
+ [ "${chosen##*/}" = "$label" ] &&
+ [ "/media/$USER/${chosen##*/}" = "$mountpath" ] &&
+ [ -e "/${chosen#*/}" ] &&
+ [ ! -s "/${chosen#*/}" ]; then
+ chosen="/${chosen#*/}"
+ rm -r "${chosen:?}" >/dev/null 2>&1 || sudo -A rm -r "${chosen:?}"
+ notify-send "🚮 Mounted path removed." "$chosen has been removed."
+ fi
+else
+ if grep -q "/${chosen#*/}" /etc/mtab | grep -qE "cifs" /etc/mtab; then
+ sudo -A umount "/${chosen#*/}"
+ notify-send "⏏️ SMB Drive unmounted." "/${chosen#*/} has been unmounted."
+ elif grep -q "/${chosen#*/}" /etc/mtab | grep -q "ifus" /etc/mtab; then
+ sudo -A umount "/${chosen#*/}"
+ notify-send "⏏️ IOS Drive unmounted." "/${chosen#*/} has been unmounted."
+ fi
+ chosen="/${chosen#*/}"
+ [ -e "$chosen" ] && [ ! -s "$chosen" ] &&
+ sudo -A rm -r "${chosen:?}" &&
+ notify-send "🚮 Mounted path removed." "$chosen has been removed."
+fi
+
+# 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 "🔒 Device dencryption closed." "Drive is now securely locked again."