commit 37d2bac2abd0a41bf36bfa2620f9a358ceeec415
parent 113db082355cc24e70f1357bc5b71004bca93d20
Author: Jake Bauer <jbauer@paritybit.ca>
Date: Tue, 9 Feb 2021 04:08:38 -0500
Add versions of (un)mount_device scripts for OpenBSD
Diffstat:
2 files changed, 84 insertions(+), 0 deletions(-)
diff --git a/.local/bin/mount_device_openbsd b/.local/bin/mount_device_openbsd
@@ -0,0 +1,42 @@
+#!/bin/sh
+
+# Copyright (C) 2021 Jake Bauer
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+# Present the user with a dmenu prompt of devices to mount and mount the
+# selected device to /mnt.
+# Requires `permit nopass <user> cmd <full_path_to_this_script>` in doas.conf
+
+disks=$(sysctl hw.disknames | cut -d= -f2 | tr ',' ' ')
+for entry in $disks; do
+ disk="$(echo "$entry" | cut -d: -f1)"
+ devices=$(disklabel -p G "$disk" | tail -n +19 | \
+ awk -v d=$disk '$9=="" {printf "%s%s %s (%s)\n", d, $1, $4, $2}')
+done
+
+choice="$(echo "$devices" | dmenu -i -p "Mount:")" || exit 1
+choice="$(echo "$choice" | awk '{print $1}' | tr -d ':')"
+
+mkdir -p /mnt/"$choice"
+mount /dev/"$choice" /mnt/"$choice" && notify-send "Mounted Drive" \
+ "$choice mounted to\n/mnt/$choice" && mounted=1
+
+# Clean up if unsuccessful mount
+if [ -z "$mounted" ]; then
+ rmdir /mnt/"$choice"
+ notify-send "Failed to Mount" "$choice not mounted. Is it already mounted?"
+ exit 1
+fi
+
+exit 0
diff --git a/.local/bin/unmount_device_openbsd b/.local/bin/unmount_device_openbsd
@@ -0,0 +1,42 @@
+#!/bin/sh
+
+# Copyright (C) 2020 Jake Bauer
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+# Present the user with a dmenu prompt of mounted devices and umount the
+# selected device.
+# Requires `permit nopass <user> cmd <full_path_to_this_script>` in doas.conf
+
+disks=$(sysctl hw.disknames | cut -d= -f2 | tr ',' ' ')
+for entry in $disks; do
+ disk="$(echo "$entry" | cut -d: -f1)"
+ devices=$(disklabel -p G "$disk" | tail -n +19 | \
+ awk -v d=$disk '$9!~/\/boot|\/home$|\/usr|\/var/ {printf "%s%s (%s)\n", d, $1, $2}')
+done
+
+choice="$(echo "$devices" | dmenu -i -p "Unmount:")" || exit 1
+choice="$(echo "$choice" | awk '{print $1}' | tr -d ':')"
+
+umount /mnt/"$choice" && notify-send "Unounted Drive" \
+ "$choice from /mnt/$choice" && unmounted=1
+
+# Clean up after successful unmount
+if [ "$unmounted" ]; then
+ rmdir /mnt/"$choice"
+ exit 0
+else
+ notify-send "Failed to Unmount" \
+ "Could not unmount $choice: device not mounted or busy."
+ exit 1
+fi