summaryrefslogtreecommitdiff
path: root/.local/bin/mount-device
blob: 6b644dfe002c029ce5a2a1c74f76c2b71b15507d (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
#!/bin/sh
# Script for mounting block devices

# Set askpass program for authentication
#export SUDO_ASKPASS=/usr/bin/ssh-askpass

# Check for and get device to mount from user
devs="$(lsblk -lpo NAME,FSTYPE,SIZE,TYPE,MOUNTPOINT | grep "part\s*$\|crypto_LUKS" | awk '{print $1,"-",$3}')"
[ "$devs" = "" ] && exit 0
dev="$(echo "$devs" | dmenu -i -p "Select device" | cut -d ' ' -f 1)"
[ "$dev" = "" ] && exit 1

# Open and map drive if it is encrypted
crypt=0
[ "$(lsblk -no FSTYPE $dev)" = "crypto_LUKS" ] && crypt=1 && \
		mapname="$(echo -n "" | dmenu -i -p "Enter device mapper name")" && \
		sudo cryptsetup open $dev $mapname

# Attempt to mount without mountpoint for devices in /etc/fstab
sudo mount "$dev" 2>/dev/null && exit 0

# Get mountpoint from user
mntpnt="$(find /mnt -maxdepth 3 -type d 2>/dev/null | dmenu -i -p "Select mountpoint")"
[ "$mntpnt" = "" ] && exit 1

# If selected mountpoint does not exist ask to create it
# If user decides not to create non-existent drive, exit
[ ! -d $mntpnt ] && create="$(echo "No\nYes" | \
		dmenu -i -p "$mntpnt does not exist, would you like to create it?")" && \
		([ "$create" = "Yes" ] && sudo -A mkdir -p $mntpnt || exit 0)

# Mount the device
if [ $crypt -eq 1 ]; then
	sudo -A mount /dev/mapper/$mapname $mntpnt && pgrep -x dunst && \
			notify-send "$dev mounted to $mntpnt"
else
	sudo -A mount $dev $mntpnt && pgrep -x dunst && \
			notify-send "$dev mounted to $mntpnt"
fi