2023-01-27 22:32:33 +00:00
|
|
|
#!/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.
|
2023-02-17 16:01:07 +00:00
|
|
|
# TODO: Try decrypt for drives in crtypttab
|
2023-01-27 22:32:33 +00:00
|
|
|
# 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/^/📱/")"
|
2023-02-20 21:16:20 +00:00
|
|
|
for NUMBER in $(echo "$phones" | sed -rn 's/.([0-9]).*/\1/gp'); do
|
|
|
|
[[ $(sed -rn 's/simple-mtpfs-([0-9]).*/\1/gp' /etc/mtab) = $NUMBER ]] && phones="$(echo "$phones" | sed "/$NUMBER: .*/d")"
|
|
|
|
done
|
2023-01-27 22:32:33 +00:00
|
|
|
# Check for drives.
|
2023-02-13 13:12:40 +00:00
|
|
|
lsblkoutput="$(lsblk -rpo "uuid,name,type,size,label,mountpoint,fstype")"
|
2023-01-27 22:32:33 +00:00
|
|
|
# Get all LUKS drives
|
2023-02-13 13:12:40 +00:00
|
|
|
allluks="$(echo "$lsblkoutput" | grep crypto_LUKS)" || true
|
2023-01-27 22:32:33 +00:00
|
|
|
# Get a list of the LUKS drive UUIDs already decrypted.
|
2023-02-13 13:12:40 +00:00
|
|
|
decrypted="$(find /dev/disk/by-id/dm-uuid-CRYPT-LUKS2-* | sed "s|.*LUKS2-||;s|-.*||")" || true
|
2023-01-27 22:32:33 +00:00
|
|
|
# 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.
|
2023-02-13 13:12:40 +00:00
|
|
|
normalparts="$(echo "$lsblkoutput"| grep -v crypto_LUKS | grep 'part\|rom\|crypt' | sed "s/^/💾 /" | filter )"
|
2023-01-27 22:32:33 +00:00
|
|
|
|
|
|
|
# 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(){
|
2023-01-28 17:03:25 +00:00
|
|
|
mp="$(find /mnt /media /mount /home -maxdepth 1 -type d 2>/dev/null | dmenu -i -p "Mount this drive where?")"
|
2023-01-27 22:32:33 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-02-17 16:01:07 +00:00
|
|
|
attemptmount(){
|
|
|
|
# Attempt to mount without a mountpoint, to see if drive is in fstab.
|
|
|
|
sudo -A mount "$chosen" || return 1
|
|
|
|
notify-send "💾Drive Mounted." "$chosen mounted."
|
|
|
|
exit
|
|
|
|
}
|
|
|
|
|
2023-01-27 22:32:33 +00:00
|
|
|
case "$chosen" in
|
|
|
|
💾*)
|
|
|
|
chosen="${chosen%% *}"
|
|
|
|
chosen="${chosen:1}" # This is a bashism.
|
2023-02-17 16:01:07 +00:00
|
|
|
attemptmount || getmount
|
2023-02-17 15:17:35 +00:00
|
|
|
sudo -A mount "$chosen" "$mp" -o uid="$(id -u)",gid="$(id -g)"
|
2023-01-27 22:32:33 +00:00
|
|
|
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.
|
2023-01-28 17:03:25 +00:00
|
|
|
test -b "/dev/mapper/usb$num"
|
2023-01-27 22:32:33 +00:00
|
|
|
|
2023-02-17 16:01:07 +00:00
|
|
|
attemptmount || getmount
|
2023-02-17 15:17:35 +00:00
|
|
|
sudo -A mount "/dev/mapper/usb$num" "$mp" -o uid="$(id -u)",gid="$(id -g)"
|
2023-01-27 22:32:33 +00:00
|
|
|
notify-send "🔓Decrypted drive Mounted." "$chosen decrypted and mounted to $mp."
|
|
|
|
;;
|
|
|
|
|
|
|
|
📱*)
|
2023-02-17 16:01:07 +00:00
|
|
|
notify-send "❗Note" "Remember to allow file access on your phone now."
|
2023-01-27 22:32:33 +00:00
|
|
|
getmount
|
|
|
|
chosen="${chosen%%:*}"
|
|
|
|
chosen="${chosen:1}" # This is a bashism.
|
2023-02-20 21:16:20 +00:00
|
|
|
sudo -A simple-mtpfs -o allow_other -o fsname="simple-mtpfs-$chosen" --device "$chosen" "$mp"
|
2023-01-27 22:32:33 +00:00
|
|
|
notify-send "🤖 Android Mounted." "Android device mounted to $mp."
|
|
|
|
;;
|
|
|
|
esac
|