diff options
| author | Luke Smith <luke@lukesmith.xyz> | 2023-01-27 17:32:33 -0500 |
|---|---|---|
| committer | Luke Smith <luke@lukesmith.xyz> | 2023-01-27 17:32:33 -0500 |
| commit | 2ca01a2886f23a7171ce52f1154f6f105b481720 (patch) | |
| tree | 6aabda6d5e29dfe63988f26906dd82ddd378c2de /.local/bin/mounter | |
| parent | a7dc0d288dcf75508d43f41c36824f43534618e8 (diff) | |
| download | eibhear-2ca01a2886f23a7171ce52f1154f6f105b481720.tar.gz eibhear-2ca01a2886f23a7171ce52f1154f6f105b481720.tar.zst eibhear-2ca01a2886f23a7171ce52f1154f6f105b481720.zip | |
usb/android/luks (un)mount scripts
to replace dmenumount and dmenuumount
Diffstat (limited to '.local/bin/mounter')
| -rwxr-xr-x | .local/bin/mounter | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/.local/bin/mounter b/.local/bin/mounter new file mode 100755 index 0000000..a663685 --- /dev/null +++ b/.local/bin/mounter @@ -0,0 +1,93 @@ +#!/bin/bash + +# Mounts Android Phones and USB drives (encrypted or not). This script will +# replace the older `dmenumount` which had extra steps and couldn't handle +# encrypted drives. +# TODO: Remove already mounted Android phones from prompt. +# TODO: Try mount first for drives if in fstab to avoid directory prompt? +# TODO: Add some support for connecting iPhones (although they are annoying). + +set -e + +# Check for phones. +phones="$(simple-mtpfs -l 2>/dev/null | sed "s/^/๐ฑ/")" +# Check for drives. +alldrives="$(lsblk -rpo "uuid,name,type,size,label,mountpoint,fstype")" +# Get all LUKS drives +allluks="$(echo "$alldrives" | grep crypto_LUKS)" +# Get a list of the LUKS drive UUIDs already decrypted. +decrypted="$(ls /dev/disk/by-id/dm-uuid-CRYPT-LUKS2-* | sed "s|.*LUKS2-||;s|-.*||")" +# Functioning for formatting drives correctly for dmenu: +filter() { sed "s/ /:/g" | awk -F':' '$7==""{printf "%s%s (%s) %s\n",$1,$3,$5,$6}' ; } + +# Get only LUKS drives that are not decrypted. +IFS=' +' +unopenedluks="$(for drive in $allluks; do + uuid="${drive%% *}" + uuid="${uuid//-}" # This is a bashism. + for open in $decrypted; do + [ "$uuid" = "$open" ] && break 1 + done && continue 1 + echo "๐ $drive" +done | filter)" + +# Get all normal, non-encrypted or decrypted partitions that are not mounted. +normalparts="$(echo "$alldrives"| grep -v crypto_LUKS | grep 'part\|rom\|crypt' | sed "s/^/๐พ /" | filter )" + +# Add all to one variable. If no mountable drives found, exit. +alldrives="$(echo "$phones +$unopenedluks +$normalparts" | sed "/^$/d;s/ *$//")" +test -n "$alldrives" + +# Feed all found drives to dmenu and get user choice. +chosen="$(echo "$alldrives" | dmenu -p "Mount which drive?" -i)" + +# Function for prompting user for a mountpoint. +getmount(){ + mp="$(find /mnt /media /mount /home -maxdepth 5 -type d 2>/dev/null | dmenu -i -p "Mount this drive where?")" + test -n "$mp" + if [ ! -d "$mp" ]; then + mkdiryn=$(printf "No\\nYes" | dmenu -i -p "$mp does not exist. Create it?") + [ "$mkdiryn" = "Yes" ] && (mkdir -p "$mp" || sudo -A mkdir -p "$mp") + fi +} + +case "$chosen" in + ๐พ*) + chosen="${chosen%% *}" + chosen="${chosen:1}" # This is a bashism. + getmount + sudo mount "$chosen" "$mp" + notify-send "๐พDrive Mounted." "$chosen mounted to $mp." + ;; + + ๐*) + chosen="${chosen%% *}" + chosen="${chosen:1}" # This is a bashism. + # Number the drive. + while true; do + [ -f "/dev/mapper/usb$num" ] || break + num="$(printf "%02d" "$((num +1))")" + done + + # Decrypt in a terminal window + ${TERMINAL:-st} -n floatterm -g 60x1 -e sudo cryptsetup open "$chosen" "usb$num" + # Check if now decrypted. + test -b /dev/mapper/usb$num + + getmount + sudo mount "/dev/mapper/usb$num" "$mp" + notify-send "๐Decrypted drive Mounted." "$chosen decrypted and mounted to $mp." + ;; + + ๐ฑ*) + getmount + echo "OK" | dmenu -i -p "Tap Allow on your phone if it asks for permission and then press enter" + chosen="${chosen%%:*}" + chosen="${chosen:1}" # This is a bashism. + simple-mtpfs --device "$chosen" "$mp" + notify-send "๐ค Android Mounted." "Android device mounted to $mp." + ;; +esac |
