From 5c97a9979ee2eda55da44dff8f55a57997cb66d1 Mon Sep 17 00:00:00 2001 From: Robert Günzler Date: Wed, 30 Nov 2022 15:50:15 +0100 Subject: bin/mnts: don't depend on udisks --- bin/mnts | 75 +++++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 48 insertions(+), 27 deletions(-) (limited to 'bin/mnts') diff --git a/bin/mnts b/bin/mnts index f8bfc4c..57eca9f 100755 --- a/bin/mnts +++ b/bin/mnts @@ -1,55 +1,76 @@ -#!/bin/sh +#!/bin/bash # # DESCRIPTION: # dmenu-based mount/unmount helper # [ -n "$DEBUG" ] && set -x -[ "$1" = "-o" ] && open=1 + +export MENU_PROMPT="󰕓 mnts" # check requirements -for app in awk jq lsblk notify-send udisksctl swx vifm; do +for app in awk jq lsblk notify-send; do if ! command -v "$app" >/dev/null; then printf "%s: %s not installed but required\n" "$(basename "$0")" "$app" >&2 exit 1 fi done -export MENU_PROMPT="󰕓 mnts" +exec= +while getopts hx: o; do + case "$o" in + x) exec="$OPTARG" ;; + h|?) exit 2 ;; + esac +done +shift $((OPTIND-1)) +# mount <1:mode> <2:device> <3:label> <4:path> mount() { - line=$1 - path=$(echo "${line}" | awk -F\\t '{print $3}' | xargs) - mode=$(echo "${line}" | awk -F\\t '{if(length($3)<1) print "mount";else print "unmount";}' | xargs) - drv=$(echo "${line}" | awk -F\\t '{print $1}' | xargs) - label=$(echo "${line}" | awk -F\\t '{print $2}' | xargs) - [ -n "${DEBUG}" ] && printf 'DEBUG: mode:%s, drv:%s, path:%s\n' "${mode}" "${drv}" "${path}" - logfile=$(mktemp --tmpdir -t "mnts-${label}-XXXXX") - if udisksctl "${mode}" -b "${drv}" >"${logfile}" 2>&1; then - if [ "${mode}" = "mount" ]; then - op=plus - _op="+" - else - op=remove - _op="-" + local ok=0 + local logfile=$(mktemp --tmpdir -t "mnts-${3}-XXXXX") + local icon=/usr/share/icons/mdi/scalable/harddisk.svg + trap '{ rm -f "$logfile"; }' EXIT + + # udisksctl "${1}" -b "${2}" >"${logfile}" 2>&1; then + case "$1" in + mount) + local path + + icon=/usr/share/icons/mdi/scalable/harddisk-plus.svg + local path="/media/$(tr '[:upper:]' '[:lower:]' <<<"$3")" + [ ! -d "$path" ] && mkdir -p "$path" + doas -n mount -v "$2" "$path" >"$logfile" 2>&1 + if [ -n "${exec}" ]; then + sh -c "$exec $path" & disown fi - notify-send -i /usr/share/icons/mdi/scalable/harddisk-${op}.svg -a mnts mnts "$(cat "${logfile}")" - printf "%s\t%s\t%s\n" "${_op}" "${drv}" "${path}" - [ -n "${open}" ] && [ "${mode}" = "mount" ] && swx -t -- vifm "${path}" + ;; + unmount) + icon=/usr/share/icons/mdi/scalable/harddisk-remove.svg + doas -n umount -v "$2" >"$logfile" 2>&1 + ;; + esac + + if [ "$ok" -eq 0 ]; then + notify-send -i "${icon}" -a mnts mnts "$(cat "${logfile}")" else - notify-send -i /usr/share/icons/mdi/scalable/harddisk.svg -a mnts -u critical -t 30000 mnts "${mode}ing ${label} failed:\n$(cat "${logfile}")" + notify-send -i "${icon}" -a mnts -u critical -t 30000 mnts "${1}ing ${3} [${2}] failed:\n$(cat "${logfile}")" fi - rm "${logfile}" } handle() { while read -r line; do - mount "${line}" & + dev=$(awk '{print $1}' <<<"$line") + label=$(awk '{print $2}' <<<"$line") + path=$(awk '{print $3}' <<<"$line") + mode=$(awk '{if(length($3)<1) print "mount";else print "unmount";}' <<<"$line") + [ -n "${DEBUG}" ] && printf 'DEBUG: mode:%s, dev:%s, path:%s\n' "${mode}" "${dev}" "${path}" + mount "${mode}" "${dev}" "${label}" "${path}" & done } -lsblk -flJ | - jq -r '.. | select(.uuid?!="" and .name?) | select((.name | test("hibiki|nvme|loop[0-9]+") | not) and .fstype?) | [ .name, .label, .mountpoints[0] // "" ] | @tsv' | - awk -F'\t' '{printf "/dev/%-10s\t%-15s\t%s\n", $1, $2, $3;}' | +doas -n lsblk -flJ | + jq -r '.blockdevices[] | select(.uuid?!="" and .name?) | select((.name | test("hibiki|luks|nvme|loop[0-9]+") | not)) | [("/dev/"+.name), .label, (.mountpoints[0] // "")] | @tsv' | + column -s $'\t' -t | menu --list=8 | handle -- cgit 1.4.1