From 38ccd88ced5790dc941157fed56c5ac4756acd7f Mon Sep 17 00:00:00 2001
From: Sam Chudnick <sam@chudnick.com>
Date: Thu, 16 Jun 2022 21:11:00 -0400
Subject: Added support for LUKS devices

Added support for LUKS devices to mount and unmount scripts. Scripts
will now detect if a device or partition is LUKS encrypted and
automatically map and mount the device.
---
 .local/bin/dmenu/mount-device   | 24 ++++++++++++++++++------
 .local/bin/dmenu/unmount-device | 17 +++++++++++++----
 2 files changed, 31 insertions(+), 10 deletions(-)

(limited to '.local/bin')

diff --git a/.local/bin/dmenu/mount-device b/.local/bin/dmenu/mount-device
index e325a65..c2decd5 100755
--- a/.local/bin/dmenu/mount-device
+++ b/.local/bin/dmenu/mount-device
@@ -2,16 +2,22 @@
 # Script for mounting block devices
 
 # Set askpass program for authentication
-export SUDO_ASKPASS=/usr/bin/ssh-askpass
+#export SUDO_ASKPASS=/usr/bin/ssh-askpass
 
 # Check for and get device to mount from user
-devs="$(lsblk -lp | grep "part $" | awk '{print $1,"-",$4}')"
+devs="$(lsblk -lpo NAME,FSTYPE,SIZE,TYPE,MOUNTPOINT | grep "part\s*$\|crypto_LUKS" | awk '{print $1,"-",$3}')"
 [ "$devs" = "" ] && exit 0
-dev="$(echo $devs | dmenu -i -p "Select device" | cut -d ' ' -f 1)"
+dev="$(echo "$devs" | dmenu -i -p "Select device" | cut -d ' ' -f 1)"
 [ "$dev" = "" ] && exit 0
 
+# Open and map drive if it is encrypted
+crypt=0
+[ "$(lsblk -no FSTYPE $dev)" = "crypto_LUKS" ] && crypt=1 && \
+		mapname="$(echo -n "" | dmenu -i -p "Enter device mapper name")" && \
+		sudo cryptsetup open $dev $mapname
+
 # Attempt to mount without mountpoint for devices in /etc/fstab
-sudo -A mount "$dev" 2>/dev/null && exit 0
+sudo mount "$dev" 2>/dev/null && exit 0
 
 # Get mountpoint from user
 mntpnt="$(find /mnt -maxdepth 3 -type d 2>/dev/null | dmenu -i -p "Select mountpoint")"
@@ -23,5 +29,11 @@ mntpnt="$(find /mnt -maxdepth 3 -type d 2>/dev/null | dmenu -i -p "Select mountp
 		dmenu -i -p "$mntpnt does not exist, would you like to create it?")" && \
 		([ "$create" = "Yes" ] && sudo -A mkdir -p $mntpnt || exit 0)
 
-sudo -A mount $dev $mntpnt && pgrep -x dunst && notify-send "$dev mounted to $mntpnt"
-
+# Mount the device
+if [ $crypt -eq 1 ]; then
+	sudo -A mount /dev/mapper/$mapname $mntpnt && pgrep -x dunst && \
+			notify-send "$dev mounted to $mntpnt"
+else
+	sudo -A mount $dev $mntpnt && pgrep -x dunst && \
+			notify-send "$dev mounted to $mntpnt"
+fi
diff --git a/.local/bin/dmenu/unmount-device b/.local/bin/dmenu/unmount-device
index 75cb10f..239663d 100755
--- a/.local/bin/dmenu/unmount-device
+++ b/.local/bin/dmenu/unmount-device
@@ -2,18 +2,27 @@
 # Script for unmounting filesystems
 
 # Set askpass program for authentication
-export SUDO_ASKPASS=/usr/bin/ssh-askpass
+#export SUDO_ASKPASS=/usr/bin/ssh-askpass
 
 # Get list of unmountable filesystems excluding critical ones (/, /home, /boot, etc...)
 exclude="\(/\|/boot\|/boot/efi\|/var\|/tmp\|/home\)$"
-parts="$(lsblk -lp | grep "part /" | grep -v "$exclude" | awk '{print $7,"-",$4}')"
+parts="$(lsblk -lp | grep "\(part\|crypt\)\s*/" | grep -v "$exclude" | awk '{print $7,"-",$4}')"
 [ "$parts" = "" ] && exit 0
 
 # Get filesystem to unmount from user
+crypt=0
 select="$(echo "$parts" | dmenu -i -p "Select filesystem to unmount" | cut -d ' ' -f 1)"
 [ "$select" = "" ] && exit 0
 dev="$(lsblk -lp | grep "$select" | awk '{print $1}')"
+[ "$(lsblk -no TYPE $dev)" = "crypt" ] && crypt=1
 
 # Unmount the filesystem
-sudo -A umount $select && pgrep -x dunst && notify-send "Unmounted $select ($dev)" \
-		|| notify-send "Error: unable to unmount $select" "Are you in it?"
+umount=0
+sudo umount $select && pgrep -x dunst && \
+	notify-send "Unmounted $select ($dev)" && umount=1 \
+	|| notify-send "Error: unable to unmount $select" "Are you in it?"
+
+[ $crypt -eq 1 -a $umount -eq 1 ] && sudo cryptsetup close $dev && pgrep -x dunst && \
+		notify-send "Unmapped $dev" && exit 14
+
+
-- 
cgit v1.2.3