dmount (3939 bytes)
1 #!/bin/bash 2 3 # Mounts Android Phones and USB drives (encrypted or not). This script will 4 # replace the older `dmenumount` which had extra steps and couldn't handle 5 # encrypted drives. 6 # TODO: Try decrypt for drives in crtypttab 7 # TODO: Add some support for connecting iPhones (although they are annoying). 8 9 IFS=' 10 ' 11 # Function for escaping cell-phone names. 12 escape(){ echo "$@" | iconv -cf UTF-8 -t ASCII//TRANSLIT | tr -d '[:punct:]' | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | sed "s/-\+/-/g;s/\(^-\|-\$\)//g" ;} 13 14 # Check for phones. 15 phones="$(simple-mtpfs -l 2>/dev/null | sed "s/^/📱/")" 16 mountedphones="$(grep "simple-mtpfs" /etc/mtab)" 17 # If there are already mounted phones, remove them from the list of mountables. 18 [ -n "$mountedphones" ] && phones="$(for phone in $phones; do 19 for mounted in $mountedphones; do 20 escphone="$(escape "$phone")" 21 [[ "$mounted" =~ "$escphone" ]] && break 1 22 done && continue 1 23 echo "$phone" 24 done)" 25 26 # Check for drives. 27 lsblkoutput="$(lsblk -rpo "uuid,name,type,size,label,mountpoint,fstype")" 28 # Get all LUKS drives 29 allluks="$(echo "$lsblkoutput" | grep crypto_LUKS)" 30 # Get a list of the LUKS drive UUIDs already decrypted. 31 decrypted="$(find /dev/disk/by-id/dm-uuid-CRYPT-LUKS2-* | sed "s|.*LUKS2-||;s|-.*||")" 32 # Functioning for formatting drives correctly for dmenu: 33 filter() { sed "s/ /:/g" | awk -F':' '$7==""{printf "%s%s (%s) %s\n",$1,$3,$5,$6}' ; } 34 35 # Get only LUKS drives that are not decrypted. 36 unopenedluks="$(for drive in $allluks; do 37 uuid="${drive%% *}" 38 uuid="${uuid//-}" # This is a bashism. 39 for open in $decrypted; do 40 [ "$uuid" = "$open" ] && break 1 41 done && continue 1 42 echo "🔒 $drive" 43 done | filter)" 44 45 # Get all normal, non-encrypted or decrypted partitions that are not mounted. 46 normalparts="$(echo "$lsblkoutput"| grep -v crypto_LUKS | grep 'part\|rom\|crypt' | sed "s/^/💾 /" | filter )" 47 48 # Add all to one variable. If no mountable drives found, exit. 49 alldrives="$(echo "$phones 50 $unopenedluks 51 $normalparts" | sed "/^$/d;s/ *$//")" 52 53 # Quit the script if a sequential command fails. 54 set -e 55 56 test -n "$alldrives" 57 58 # Feed all found drives to dmenu and get user choice. 59 chosen="$(echo "$alldrives" | dmenu -p "Mount which drive?" -i)" 60 61 # Function for prompting user for a mountpoint. 62 getmount(){ 63 mp="$(find /mnt /media /mount /home -maxdepth 1 -type d 2>/dev/null | dmenu -i -p "Mount this drive where?")" 64 test -n "$mp" 65 if [ ! -d "$mp" ]; then 66 mkdiryn=$(printf "No\\nYes" | dmenu -i -p "$mp does not exist. Create it?") 67 [ "$mkdiryn" = "Yes" ] && (mkdir -p "$mp" || sudo -A mkdir -p "$mp") 68 fi 69 } 70 71 attemptmount(){ 72 # Attempt to mount without a mountpoint, to see if drive is in fstab. 73 sudo -A mount "$chosen" || return 1 74 notify-send "💾Drive Mounted." "$chosen mounted." 75 exit 76 } 77 78 case "$chosen" in 79 💾*) 80 chosen="${chosen%% *}" 81 chosen="${chosen:1}" # This is a bashism. 82 attemptmount || getmount 83 sudo -A mount "$chosen" "$mp" -o uid="$(id -u)",gid="$(id -g)" 84 notify-send "💾Drive Mounted." "$chosen mounted to $mp." 85 ;; 86 87 🔒*) 88 chosen="${chosen%% *}" 89 chosen="${chosen:1}" # This is a bashism. 90 # Number the drive. 91 while true; do 92 [ -f "/dev/mapper/usb$num" ] || break 93 num="$(printf "%02d" "$((num +1))")" 94 done 95 96 # Decrypt in a terminal window 97 ${TERMINAL:-st} -n floatterm -g 60x1 -e sudo cryptsetup open "$chosen" "usb$num" 98 # Check if now decrypted. 99 test -b "/dev/mapper/usb$num" 100 101 attemptmount || getmount 102 sudo -A mount "/dev/mapper/usb$num" "$mp" -o uid="$(id -u)",gid="$(id -g)" 103 notify-send "🔓Decrypted drive Mounted." "$chosen decrypted and mounted to $mp." 104 ;; 105 106 📱*) 107 notify-send "❗Note" "Remember to allow file access on your phone now." 108 getmount 109 number="${chosen%%:*}" 110 number="${chosen:1}" # This is a bashism. 111 sudo -A simple-mtpfs -o allow_other -o fsname="simple-mtpfs-$(escape "$chosen")" --device "$number" "$mp" 112 notify-send "🤖 Android Mounted." "Android device mounted to $mp." 113 ;; 114 esac