diff options
Diffstat (limited to 'keyboard/mounter')
-rwxr-xr-x | keyboard/mounter | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/keyboard/mounter b/keyboard/mounter new file mode 100755 index 0000000..d45e0b9 --- /dev/null +++ b/keyboard/mounter @@ -0,0 +1,64 @@ +#!/bin/sh +# TODO: add support for mounting discs +sudo=doas +mps=/media/mnt # User should have ownership of this directory +notify='notify-send --urgency low' + +devexist() { [ -z $1 ] || ([ ! -e "$1" ] && $notify "Device $1 does not exist!") && exit 1; } + +mnt() { + # Select a device with unmounted partitions + awk_printdisks=' + /^disk/ { diskinfo = $2 " " toupper($3) " Device \"" substr($0, i ? i : i = index($0, $5)) "\" (" $4 ")" } # Fifth field is label since mp is null + /^part/ && !$4 && diskinfo { print diskinfo; diskinfo = "" } # Fourth field is the mp since type is null + ' + sed_getdev='s/.*" (\(.*\))$/\/dev\/\1/; s/.* /\/dev\//' + devexist ${disk=$(lsblk -no TYPE,SIZE,TRAN,KNAME,MOUNTPOINT,MODEL | awk "$awk_printdisks" | dmenu -p 'Disk: ' | sed "$sed_getdev")} + + # Select an unmounted partition + awk_printparts='/^part\s*[0-9]/ { print $2 " " toupper($3) " Partition " ($5 ? "\"" substr($0, i ? i : i = index($0, $5)) "\" (" $4 ")" : $4) }' + devexist ${part=$(lsblk -no TYPE,MOUNTPOINT,SIZE,FSTYPE,KNAME,LABEL $disk | awk "$awk_printparts" | dmenu -p 'Mount partition: ' | sed "$sed_getdev")} + + # Create a mountpoint automatically + awk_mpname='{ print $3 ? substr($0, index($0, $3)) : $1 " " toupper($2) " Volume" }' + mp="$mps/$(lsblk -no SIZE,FSTYPE,LABEL $part | awk "$awk_mpname")" + [ -e "$mp" ] && mp="$mp $(lsblk -no PARTUUID $part)" + mkdir -p "$mp" + + # Handle filesystem cases + case `lsblk -no FSTYPE $part` in + vfat) opts="-o rw,umask=0000" ;; + exfat) opts="-o uid=$(id -u),gid=$(id -g)" ;; + esac + + # Mount the partition + mountout="`$sudo mount $part "$mp" $opts 2>&1`" \ + && $notify "Device Successfully Mounted" "Mounted \"$part\" to \"$mp\"" \ + || $notify "Error Mounting Device \"$part\" to \"$mp\"" "$mountout" +} + +umnt() { + # Exclude mountpoints from /etc/fstab + awk_fstabmps='!/$^/ && !/^#/ { gsub(/\//, "\\/"); regex = (regex ? regex "|" : "") $2 "$" } END { print regex }' + excludere="$(awk "$awk_fstabmps" /etc/fstab)" + + # Select a partition to unmount + awk_mpname='$2 &&'"!/$excludere/"'{ print $1 " on " substr($0, index($0, $2)) }' + selpart="$(lsblk -no KNAME,MOUNTPOINT | awk "$awk_mpname" | dmenu -p 'Unmount Partition: ')" + devexist ${part=$(echo "$selpart" | sed 's/^/\/dev\//; s/ .*//')} + mp="$(echo "$selpart" | cut -d' ' -f3-)" + + # Unmount the partition + umountout="`$sudo umount -A $part`" \ + && $notify "Device Successfully Unmounted" "Unmounted \"$part\" from \"$mp\"" \ + || $notify "Error Unmounting Device \"$part\" from \"$mp\"" "$umountout" + + # Remove mp directory if it is in mps + [ "$(echo "$mp" | cut -c1-$(expr `echo $mps | wc -m` - 1))" = $mps ] && rm -rf "$mp" +} + +case $1 in + -m) mnt ;; + -u) umnt ;; + *) echo "Usage: $(basename "$0") [-m|-u]\n\nOPTIONS:\n -m\tMount mode\n -u\tUnmount mode"; exit 2 ;; +esac |