From 3aac1116680f89a81b12c820e6d7616fb54d7ca5 Mon Sep 17 00:00:00 2001 From: appeasementPolitik <108810900+appeasementPolitik@users.noreply.github.com> Date: Sat, 11 Feb 2023 23:03:31 +0000 Subject: [PATCH] unmounter: prompt to eject drive if possible --- .local/bin/unmounter | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/.local/bin/unmounter b/.local/bin/unmounter index 7f1dbf5d..188c0a03 100755 --- a/.local/bin/unmounter +++ b/.local/bin/unmounter @@ -23,6 +23,17 @@ 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##*/}" -sudo -A cryptsetup close "$cryptid" -notify-send "🔒Device dencryption closed." "Drive is now securely locked again." + +test -b /dev/mapper/"${cryptid##*/}" && + sudo -A cryptsetup close "$cryptid" && + notify-send "🔒Device dencryption closed." "Drive is now securely locked again." + +# Ask to eject drive if possible. +mntline=$(echo "$lsblkoutput" | tac | grep -m1 -n "/${chosen#*/}" | cut -d':' -f1) +[ -n "$mntline" ] && + devlabel=$(echo "$lsblkoutput" | tac | tail -n +"$mntline" | grep -m1 '[^ ]* disk' | cut -d'/' -f3 | cut -d' ' -f1) && + samedrivemounts=$(echo "$lsblkoutput" | tac | awk "\$1~/$devlabel/&&!devfound{devfound=1;cnt=0}devfound{if(\$2~/disk/||cnt>1){print cnt;exit}if(\$4){cnt+=1}}") && + [ "$samedrivemounts" = 1 ] && + [ $(printf "No\nYes" | dmenu -p 'Device fully unmounted. Eject this drive?') == 'Yes' ] && + sudo -A sh -c "echo 'offline' > /sys/block/$devlabel/device/state; echo '1' > /sys/block/$devlabel/device/delete" && + notify-send "Device ejected." "Drive can now be removed safely."