summaryrefslogtreecommitdiff
path: root/scripts/.local/bin/dmnt
blob: 7175b6e53d0e9f473f9ecb81cdc4c83c3619c735 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/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 decrypt for drives in crtypttab
# TODO: Add some support for connecting iPhones (although they are annoying).


DMENU="$(command -v cdmenu)"
set -e

# Check for phones.
phones="$(simple-mtpfs -l 2>/dev/null | sed "s/^/📱/")"
# Check for drives.
lsblkoutput="$(lsblk -rpo "uuid,name,type,size,label,mountpoint,fstype")"
# Get all LUKS drives
allluks="$(echo "$lsblkoutput" | grep crypto_LUKS)" || true
# Get a list of the LUKS drive UUIDs already decrypted.
decrypted="$(find /dev/disk/by-id/dm-uuid-CRYPT-LUKS2-* | sed "s|.*LUKS2-||;s|-.*||")" || true
# 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 "$lsblkoutput"| 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 -l 16 -p "Mount which drive?" -i)"

# Function for prompting user for a mountpoint.
getmount(){
	mp="$(find /mnt /media /mount /home -maxdepth 1 -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
}

attemptmount(){
		# Attempt to mount without a mountpoint, to see if drive is in fstab.
		sudo -A mount "$1" || return 1
		notify-send "💾Drive Mounted." "$chosen mounted."
		exit
}

case "$chosen" in
	💾*)
		chosen="${chosen%% *}"
		chosen="${chosen:1}"	# This is a bashism.
		attemptmount "$chosen" || getmount
		sudo -A mount "$chosen" "$mp" -o uid="$(id -u)",gid="$(id -g)"
		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"

		echo $num
		echo "$mp"

		attemptmount "/dev/mapper/usb$num" || getmount

		if [ "$(lsblk -n -ofstype /dev/mapper/usb$num)" == "btrfs" ]; then
			# BTRFS can't list subvolumes of unmounted partitions
			sudo -A mount -o ro "/dev/mapper/usb$num" "$mp"
			subvol="$(sudo btrfs subvolume list $mp | cut -d' ' -f9 | $DMENU -i -p 'Which subvolume to mount')"
			# "mount -o remount,rw" does not change the subvolume, so remount ourselves
			sudo umount "$mp"
			# default zstd:3 is fast enough even for solid-state drives and compression is OK
			# autodefrag is broken in 5.16 kernel
			sudo -A mount -o compress-force=zstd,autodefrag,subvol="$subvol" "/dev/mapper/usb$num" "$mp"
		else
			sudo -A mount -o uid="$(id -u)",gid="$(id -g)" "/dev/mapper/usb$num" "$mp"
		fi

		notify-send "🔓Decrypted drive Mounted." "$chosen decrypted and mounted to $mp."

		;;

	📱*)
		notify-send "❗Note" "Remember to allow file access on your phone now."
		getmount
		chosen="${chosen%%:*}"
		chosen="${chosen:1}"	# This is a bashism.
		sudo -A simple-mtpfs -o allow_other --device "$chosen" "$mp"
		notify-send "🤖 Android Mounted." "Android device mounted to $mp."
		;;
esac