/
mountdrive.sh
225 lines (203 loc) · 7.55 KB
/
mountdrive.sh
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/bin/bash
# Easily add USB storage devices to RPi-OS (Debian)
# passed https://www.shellcheck.net/
# WARNING: script do not attempt to be portable
# verified platform is RPi-OS Lite 32-bit (Bullseye)
# List storage devices from 'blkid' exluding sd with PARTUUID
# or UUID already configured in 'fstab'. For user then to select
# single partition to mount where appropriate mount directive
# are written to 'fstab' before 'mount -a' are executed.
# Option 1: Create relative symlink to newly mounted partition
# in user's home directory.
# Option 2: Create alias to commands 'cd [mount point] && ls -l'
version="0.0.4" # 2022-8
# not recommended to use /media if any automount are running
#mountdir="/media/"
mountdir="/mnt/"
user=$(logname)
function printversion {
printf '%b\n' "$0 version $version
This is free software, you are free to change and redistribute it.
Included you find there is NO WARRANTY!"
}
function printhelp {
printf '%b\n' "Usage: sudo $0
Files and directory $0 can alter if user accept:
/etc/fstab - append to the end of the file
/home/$user/.bash_aliases - append to the end of the file
/home/$user/ - add relative symlink to mounted partition"
}
if test $# = 1; then
case "$1" in
--help | --hel | --he | --h | -h | "-?")
printhelp; exit 0 ;;
--version | --versio | --versi | --vers | --ver | --ve | --v | -v)
printversion; exit 0 ;;
esac
fi
if [[ ! "$EUID" = 0 ]]; then
printf '%b\n' "We need root privileges to mount new partition\nPlease run 'sudo mountdrive'."
exit
fi
cd "/home/$user" || exit 1
# read PARTUUID and UUID from already mounted storage devices
partuuidetc=$(while IFS= read -r line ; do
if [[ $line =~ "PARTUUID" ]] || [[ $line =~ "UUID" ]]; then
line="$(cut -f1 -d' ' <<< "$line")"
line="$(cut -f1 <<< "$line")"
line="$(cut -f2 -d'=' <<< "$line")"
echo "$line"
fi
done </etc/fstab)
# filter connected storage devices not already enabled in fstab
id=$(sudo blkid)
newsd=$(while IFS= read -r line; do
sd=$(grep '^/dev/sd' <<< "$line")
if [[ -n "$sd" ]]; then
while IFS= read -r uuid; do
sd=$(grep -v "$uuid" <<< "$sd")
done <<< "$partuuidetc"
if [[ -n "$sd" ]]; then echo "$sd"; fi
fi
done <<< "${id//\"/}")
# create and print numbered list
printf '%s\n' "Select partition to mount using PARTUUID or UUID with default settings:"
printf '%s\n' "-----------------------------------------------------------------------"
if [[ -z $newsd ]]; then
printf '%s\n' "No new drives detected. Note if PARTUUID or UUID already registered in fstab these drives will not be listed. [END]"
exit 0
fi
x=1
while IFS= read -r line;
do printf '%s\n' "$x: $line"; ((x++));
done <<< "$newsd"
printf '%s\n' "$x: Exit without doing anything"
printf '%s\n' "-----------------------------------------------------------------------"
printf '%s' "Select [1-$x]: "
# wait for user input
while true; do
read -r -s -n1 inst
case $inst in
[1-"$x"]* ) break;;
* ) echo "Please select option 1 - $x";;
esac
done
if [ "$inst" = $x ]; then echo "Bye bye!"; exit 0; fi
printf '\n%s\n' "-----------------------------------------------------------------------"
# get string from user selection
x=0
while read -r line; do
((x++));
test "$x" = "$inst" && break;
done <<< "$newsd"
# get label from selected partition
if [[ "$line" =~ LABEL=(.*) ]]; then
label=$(cut -f1 -d' ' <<< "${BASH_REMATCH[1]}")
elif [[ "$line" =~ UUID=(.*) ]]; then
label=$(cut -f1 -d' ' <<< "${BASH_REMATCH[1]}")
label="sd${label:(-4)}"
else
label="usbsd"
fi
if [[ -z "$label" ]] || [ "$label" = " " ]; then label="usbsd"; fi
# wait for user input
printf '%b\n' "Do you want to mount using label name '$label'?\nPress 'n' to type in another label [Y/n]"
while true; do read -r -s -n1 inst
case $inst in
"" ) inst="y" ;break;;
[yY]|[nN]* ) break;;
* ) echo "Please select option y(es) - n(o)";;
esac
done
if [ "$inst" = "n" ] || [ "$inst" = "N" ]; then
read -r -p "Enter label name for mount point (directory): " label
fi
# '\040' to replace space in fstab and a few other chars !$+@_[[:space:]]
labelfst="$(sed -e "s/!/%21/g;s/\\$/%24/g;s/+/%2B/g;s/@/%40/g;s/_/%5F/g;s/ /\\\040/g" <<< "$label")"
# check if mount point already in use
mountpoint="$mountdir$labelfst/"
fsta=$(cat /etc/fstab)
if [[ "$fsta" =~ $mountpoint ]]; then
printf '%s\n' "Mount point '$mountpoint' already in use. Please start over using another label."
printf '%s\n' "Tip: Use 'sudo nano /etc/fstab' to manually edit mount point"
exit 1
fi
# from selected partition get the PARTUUID, if none found use UUID
if [[ "$line" =~ PARTUUID=(.*) ]]; then
partuuid=$(cut -f1 -d' ' <<< "${BASH_REMATCH[1]}")
uuidstr="PARTUUID"
elif [[ "$line" =~ UUID=(.*) ]]; then
partuuid=$(cut -f1 -d' ' <<< "${BASH_REMATCH[1]}")
uuidstr="UUID"
else
printf '%s\n' "Error - Not able to resolve PARTUUID or UUID from selected partition. Sorry but we have to end here."
exit 1
fi
# from selected partition get the filesystem TYPE
if [[ "$line" =~ TYPE=(.*) ]]; then
type=$(cut -f1 -d' ' <<< "${BASH_REMATCH[1]}")
else
printf '%s\n' "Error - Not able to resolve filesystem from selected partition. Sorry but we have to end here."
exit 1
fi
# format the string used to mount the partition
mountstr="$uuidstr=$partuuid $mountpoint $type defaults,noatime,nofail,x-systemd.device-timeout=30 0 0"
printf '%s\n' "Make directory $mountpoint (mount point)"
if ! sudo mkdir -p "$mountpoint";
then printf '%s\n' "Error - Not able to create directory for mount point"; exit; fi
printf '%s\n' "Backup /etc/fstab.backup"
if ! sudo cp /etc/fstab /etc/fstab.backup;
then printf '%s\n' "Error not able to make backup of '/etc/fstab'"; exit 1; fi
if ! sudo printf '\n%s' "$mountstr" | tee -a /etc/fstab > /dev/null;
then printf '%s\n' "Error writing to '/etc/fstab'"; exit 1;
else printf '%s\n' "New entry to /etc/fstab:"
printf '%s\n' "$mountstr"; fi
printf '%s\n' "Mount new storage device using 'mount -a'"
if ! sudo mount -a;
then printf '%s\n' "Error executing 'sudo mount -a'"; exit 1; fi
# Create Symbolic Links
printf '%s\n' "-----------------------------------------------------------------------"
printf '%s\n' "Do you want to create symbolic link to newly mounted directory? [Y/n]"
while true; do read -r -s -n1 inst
case $inst in
"" ) inst=y; break;;
[yY]|[nN]* ) break;;
* ) echo "Please select option y(es) - n(o)";;
esac
done
if [ "$inst" = "y" ] || [ "$inst" = "Y" ]; then
sym="/home/$user/$labelfst"
if [[ -f "$sym" ]]; then
printf '%s\n' "WARNING: $labelfst already exist. Symlink not created!"
else
if ! ln -s -r "$mountpoint" "$sym";
then printf '%s\n' "Error creating symlink";
else
if [[ -f "$sym" ]]; then
printf '%s\n' "Symlink '$label' successfully added";
else
printf '%s\n' "Error - Creating symlink '$label' failed";
fi
fi
fi
fi
# Create alias
printf '%s\n' "-----------------------------------------------------------------------"
printf '%b\n' "Do you want to create an alias (shortcut) to $mountpoint?\nexample of alias could be short command words like 'sd1' 'sd2' 'flash' 'usb'"
printf '%s' "[Y/n] "
while true; do read -r -s -n1 inst
case $inst in
"" ) inst=y; break;;
[yY]|[nN]* ) break;;
* ) echo "Please select option y(es) - n(o)";;
esac
done
if [ "$inst" = "y" ] || [ "$inst" = "Y" ]; then
read -r -p "Enter alias name: " al
cd "/home/$user" || exit 1
if ! printf '\n%s' "alias ""$al""=\"cd $mountpoint && ls -l\"" | tee -a "/home/$user/.bash_aliases" > /dev/null;
then printf '%s\n' "Error creating alias'"; exit 1;
else printf '%s\n' "Alias '$al' available at next login"; fi
fi
printf '\n%s\n\n' "[END]"
exit 0