-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmount_drive
More file actions
executable file
·169 lines (137 loc) · 4.71 KB
/
mount_drive
File metadata and controls
executable file
·169 lines (137 loc) · 4.71 KB
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
#!/bin/bash
## Bash settings
set -uo pipefail
## Script globals
script_name=$(basename "${BASH_SOURCE[0]}")
script_dir=$({ cd "$(dirname "${BASH_SOURCE[0]}")" || { echo "Failed to access script file directory" >&2; exit; } } && pwd)
script_dir_abs=$({ cd "$(dirname "${BASH_SOURCE[0]}")" || { echo "Failed to access script file directory" >&2; exit; } } && pwd -P)
script_file="${script_dir}/${script_name}"
if [ -L "${BASH_SOURCE[0]}" ]; then
script_file_abs=$(readlink "${BASH_SOURCE[0]}")
else
script_file_abs="${script_dir_abs}/${script_name}"
fi
export CURRENT_PARENT_BASH_SCRIPT_FILE="$script_file"
script_args=("$@")
## Script imports
lib_dir="${script_dir}/../lib"
bash_functions_script="${lib_dir}/bash_script_func.sh"
## Source imports
source "$bash_functions_script"
## Arguments
drive_arr=()
dryrun=false
## Custom globals
re_drive_letter='[a-zA-Z]'
## Script usage
read -r -d '' script_usage << EOM
Usage: ${script_name} DRIVE_LETTER...
Permanently mount Windows network/removable drive(s) by making
additions to the /etc/fstab file. Primarily useful for setup of
the Windows Subsystem for Linux Bash terminal.
Options:
-db,--debug
-dr,--dryrun
Print commands used to mount drive(s), without executing.
EOM
if (( $# < 1 )); then
echo_e "$script_usage"
exit_script_with_status 1
fi
## Parse arguments
set +u
while (( $# > 0 )); do
arg="$1"
if [ "$(string_startswith "$arg" '-')" = false ]; then
if [ "$(re_test "$re_drive_letter" "$arg")" = true ]; then
drive_arr+=( "$arg" )
else
echo_e "Unexpected argument: ${arg}"
exit_script_with_status 1
fi
else
arg_opt="$(string_lstrip "$arg" '-')"
arg_opt_nargs=''
if [ "$(string_contains "$arg_opt" '=')" = true ]; then
arg_val=$(printf '%s' "${arg_opt#*=}" | sed -r -e "s|^['\"]+||" -e "s|['\"]+$||")
arg_opt="${arg_opt%%=*}"
arg_opt_nargs_do_shift=false
else
arg_val="$2"
arg_opt_nargs_do_shift=true
fi
arg_val_can_start_with_dash=false
if [ "$arg_opt" = 'h' ] || [ "$arg_opt" = 'help' ]; then
arg_opt_nargs=0
echo "$script_usage"
exit 0
elif [ "$arg_opt" = 'db' ] || [ "$arg_opt" = 'debug' ]; then
arg_opt_nargs=0
dryrun=true
elif [ "$arg_opt" = 'dr' ] || [ "$arg_opt" = 'dryrun' ]; then
arg_opt_nargs=0
dryrun=true
else
echo_e "Unexpected argument: ${arg}"
exit_script_with_status 1
fi
if [ -z "$arg_opt_nargs" ]; then
echo_e "Developer error! "'$arg_opt_nargs'" was not set for argument: ${arg}"
exit_script_with_status 1
fi
if [ "$arg_opt_nargs_do_shift" = true ] && (( arg_opt_nargs >= 1 )); then
for arg_num in $(seq 1 $arg_opt_nargs); do
shift
arg_val="$1"
if [ -z "$arg_val" ]; then
echo_e "Missing expected value (#${arg_num}) for argument: ${arg}"
exit_script_with_status 1
elif [ "$arg_val_can_start_with_dash" = false ] && [ "$(string_startswith "$arg_val" '-')" = true ]; then
echo_e "Unexpected argument value: ${arg} ${arg_val}"
exit_script_with_status 1
fi
done
fi
fi
shift
done
set -u
## Validate arguments
if (( ${#drive_arr[@]} == 0 )); then
echo_e "At least one drive letter argument must be provided"
exit_script_with_status 1
fi
## Mount drives
for drive in "${drive_arr[@]}"; do
drive_lower=$(string_to_lowercase "$drive")
drive_upper=$(string_to_uppercase "$drive")
echo -e "\nConfiguring ${drive_upper} drive"
drive_path="/mnt/${drive_lower}"
if [ -d "$drive_path" ]; then
cmd="sudo chmod 777 ${drive_path}"
else
cmd="sudo mkdir -m 777 ${drive_path}"
fi
echo -e "-> Initializing drive path with command:\n ${cmd}"
if [ "$dryrun" = false ]; then
eval "$cmd"
fi
fstab_drive_line="${drive_upper}: /mnt/${drive_lower} drvfs defaults 0 0"
fstab_line_exists=$(grep "$fstab_drive_line" "/etc/fstab")
if [ -n "$fstab_line_exists" ]; then
echo -e "-> Drive setting already exists in /etc/fstab:\n ${fstab_drive_line}"
else
cmd="sudo bash -c 'echo ${fstab_drive_line} >> /etc/fstab'"
echo -e "-> Adding drive setting to /etc/fstab with command:\n ${cmd}"
if [ "$dryrun" = false ]; then
eval "$cmd"
fi
fi
done
echo
cmd="sudo mount -a"
echo -e "Mounting all drives with command:\n ${cmd}"
if [ "$dryrun" = false ]; then
eval "$cmd"
echo -e "\nDone!"
fi