commit bacc66f97cf62b43f4bcc2fe0320ae31f78c7859
parent dbfd140806fa4b6fbec9ac63d3a2b7c093fa3cdf
Author: Jake Bauer <jbauer@paritybit.ca>
Date: Tue, 2 Feb 2021 21:34:31 -0500
Add dmenu scripts for (un)mounting drives
Diffstat:
2 files changed, 81 insertions(+), 0 deletions(-)
diff --git a/.local/bin/mount_device b/.local/bin/mount_device
@@ -0,0 +1,45 @@
+#!/bin/sh
+
+# Present the user with a dmenu prompt of devices (partitions and ROMs) to mount
+# and mount the selected device to ~/.mnt/<label> or ~/.mnt/<uuid> if the device
+# does not have a label.
+
+mount() {
+ choice="$(echo "$devices" | dmenu -i -p "Mount:")" || exit 1
+ choice="$(echo "$choice" | awk '{print $1}')"
+ deviceLabel="$(lsblk -r -o "NAME,LABEL" | grep "$choice" | awk '{print $2}')"
+ deviceUUID="$(lsblk -r -o "NAME,UUID" | grep "$choice" | awk '{print $2}')"
+
+ # Will require "permit nopass <username> cmd mount" in doas.conf
+ if [ -z "$deviceLabel" ]; then
+ mkdir -p ~/.mnt/"$deviceUUID"
+ doas mount /dev/"$choice" ~/.mnt/"$deviceUUID"/ && \
+ notify-send "Mounted Drive" \
+ "$choice mounted to ~/.mnt/$deviceUUID" && mounted=1
+ else
+ mkdir -p ~/.mnt/"$deviceLabel"
+ doas mount /dev/"$choice" ~/.mnt/"$deviceLabel"/ && \
+ notify-send "Mounted Drive" \
+ "$choice mounted to ~/.mnt/$deviceLabel" && mounted=1
+ fi
+
+ # Clean up if unsuccessful mount
+ if [ -z "$mounted" ]; then
+ rmdir ~/.mnt/"$deviceUUID" || rmdir ~/.mnt/"$deviceLabel"
+ exit 1
+ fi
+
+ exit 0
+}
+
+# Display options in the form device (size) (e.g. sdc1 (256G))
+# Is there a simple OS-agnostic (i.e. not Linux-only) way of doing this?
+devices="$(lsblk -r -o "NAME,TYPE,SIZE,MOUNTPOINT" | grep 'part\|rom' | \
+ awk '$4=="" {printf "%s (%s)\n", $1, $3}')"
+
+if [ -z "$devices" ]; then
+ echo "No mountable devices detected"
+ exit 1
+fi
+
+mount
diff --git a/.local/bin/unmount_device b/.local/bin/unmount_device
@@ -0,0 +1,36 @@
+#!/bin/sh
+
+# Present the user with a dmenu prompt of mounted devices to unmount.
+# Will not list /, /boot, /home, /usr*, /var* locations.
+# Will auto-remove the folder that the device used to be mounted to.
+
+unmount() {
+ choice="$(echo "$devices" | dmenu -i -p "Unmount:")" || exit 1
+ choice="$(echo "$choice" | awk '{print $1}')"
+ deviceLabel="$(lsblk -r -o "NAME,LABEL" | grep "$choice" | awk '{print $2}')"
+ deviceUUID="$(lsblk -r -o "NAME,UUID" | grep "$choice" | awk '{print $2}')"
+
+ # Will require "permit nopass <username> cmd umount" in doas.conf
+ if [ -z "$deviceLabel" ]; then
+ doas umount ~/.mnt/"$deviceUUID"/ && \
+ notify-send "Unmounted Drive" \
+ "$choice unmounted from ~/.mnt/$deviceUUID"
+ rmdir ~/.mnt/"$deviceUUID"
+ else
+ doas umount /dev/"$choice" ~/.mnt/"$deviceLabel"/ && \
+ notify-send "Unmounted Drive" \
+ "$choice unmounted from ~/.mnt/$deviceLabel"
+ rmdir ~/.mnt/"$deviceLabel"
+ fi
+ exit 0
+}
+
+devices=$(lsblk -n -r -o "NAME,TYPE,SIZE,MOUNTPOINT" | \
+ awk '$4!~/\/boot|\/home$|\/usr|\/var/&&length($4)>1 {printf "%s (%s)\n", $1, $4}')
+
+if [ -z "$devices" ]; then
+ echo "No unmountable devices detected"
+ exit 1
+fi
+
+unmount