-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautomountnfs
More file actions
executable file
·476 lines (412 loc) · 16.6 KB
/
automountnfs
File metadata and controls
executable file
·476 lines (412 loc) · 16.6 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#!/bin/bash
AMN_SERVER_FQDN="nas.alvaone.net"
AMN_DATE_OPTS="%a %b %d %H:%M %Y"
AMN_CHECK=0
AMN_UNMOUNT=0
AMN_STATUS=0
AMN_MOUNT_POINT=""
trap 'trap_abort' INT QUIT TERM HUP
trap 'trap_exit' EXIT
cleanup() {
exit $1 || true
}
abort() {
loge 'Aborting...'
cleanup 0
}
trap_abort() {
trap - EXIT INT QUIT TERM HUP
abort
}
trap_exit() {
log "Received exit..."
trap - EXIT INT QUIT TERM HUP
cleanup
}
log() {
echo -e $(date +"$AMN_DATE_OPTS")": $1"
}
loge() {
echo -e $(date +"$AMN_DATE_OPTS")": ERROR: $1" >&2
}
logc() {
echo -e $(date +"$AMN_DATE_OPTS")": Exec: $1"
}
run_cmd() {
# $1: command to run
# $2: Redirection
cmd=$1
logc $cmd
$cmd $2
}
# Helper function to get mount point name from path
get_mount_point_name() {
local path="$1"
basename "$path"
}
# Helper function to filter NFS entries by mount point name
filter_nfs_entries() {
local mount_point_name="$1"
local filtered_entries=()
for entry in "${NFS_ENTRIES[@]}"; do
if [ -n "$entry" ]; then
local target=$(echo "$entry" | awk '{print $2}')
local target_name=$(get_mount_point_name "$target")
if [[ "$target_name" == "$mount_point_name" ]]; then
filtered_entries+=("$entry")
fi
fi
done
printf '%s\n' "${filtered_entries[@]}"
}
usage() {
echo "${0} - Auto Mount or Unmount NFS shares via systemd units"
echo
echo "Usage: ${0} [options] (mount|unmount|check|status) [mount_point]"
echo
echo "Options:"
echo
echo " -h, --help: Show help information."
echo
echo "Commands:"
echo
echo " mount [mount_point] Create and start systemd mount units for NFS shares"
echo " If mount_point specified, only mount that specific share"
echo
echo " unmount [mount_point] Stop mount services and unmount shares"
echo " If mount_point specified, only unmount that specific share"
echo
echo " check Check server availability and mount shares if available"
echo " (Used by timer for automatic monitoring)"
echo
echo " status List configured mount units and their status"
echo
echo "Mount points:"
echo " arch_repo, arch_pkg_cache, projects, coding, videography, backups,"
echo " documents, files, games, movies, music, pictures, shows, stage, torrents, videos"
}
if [[ $# -lt 1 ]]; then
usage
exit 0
fi
# Check for help flags first, before logging starts
ARGS=("$@")
for ((a = 0; a < $#; a++)); do
if [[ ${ARGS[$a]} == "-h" || ${ARGS[$a]} == "--help" ]]; then
trap - EXIT INT QUIT TERM HUP
usage
exit 0
fi
done
log "AutoMountNFS started: $0::$$"
# Track if we found a valid command
VALID_COMMAND=0
for ((a = 0; a < $#; a++)); do
if [[ ${ARGS[$a]} == "check" ]]; then
log "Found check command argument"
AMN_CHECK=1
VALID_COMMAND=1
elif [[ ${ARGS[$a]} == "unmount" ]]; then
log "Found unmount command argument"
AMN_UNMOUNT=1
VALID_COMMAND=1
# Check if next argument is a mount point
if [[ $((a + 1)) -lt $# ]] && [[ ! ${ARGS[$((a + 1))]} =~ ^- ]]; then
AMN_MOUNT_POINT="${ARGS[$((a + 1))]}"
log "Found mount point argument: $AMN_MOUNT_POINT"
fi
elif [[ ${ARGS[$a]} == "mount" ]]; then
log "Found mount command argument"
VALID_COMMAND=1
# Check if next argument is a mount point
if [[ $((a + 1)) -lt $# ]] && [[ ! ${ARGS[$((a + 1))]} =~ ^- ]]; then
AMN_MOUNT_POINT="${ARGS[$((a + 1))]}"
log "Found mount point argument: $AMN_MOUNT_POINT"
fi
elif [[ ${ARGS[$a]} == "status" ]]; then
log "Found status command argument"
AMN_STATUS=1
VALID_COMMAND=1
elif [[ ${ARGS[$a]} =~ ^-- ]]; then
loge "Invalid option: ${ARGS[$a]}"
loge "Use -h or --help for usage information"
exit 1
fi
done
# Check if no valid command was found
if [[ $VALID_COMMAND -eq 0 ]]; then
loge "No valid command specified"
usage
exit 1
fi
# Hardcoded NFS entries array
NFS_ENTRIES=(
"nas.alvaone.net:/mnt/bigdata/arch_repo/alvaone_repo /mnt/arch_repo nfs4 _netdev,noauto,noatime,nodiratime,rsize=131072,wsize=131072,timeo=10"
"nas.alvaone.net:/mnt/bigdata/arch_repo/pac_cache /mnt/arch_pkg_cache nfs4 _netdev,noauto,noatime,nodiratime,rsize=131072,wsize=131072,timeo=10"
"nas.alvaone.net:/mnt/bigdata/projects /mnt/projects nfs4 _netdev,noauto,noatime,nodiratime,rsize=131072,wsize=131072,timeo=10"
"nas.alvaone.net:/mnt/bigdata/projects/coding /mnt/coding nfs4 _netdev,noauto,noatime,nodiratime,rsize=131072,wsize=131072,timeo=10"
"nas.alvaone.net:/mnt/bigdata/projects/videography /mnt/videography nfs4 _netdev,noauto,noatime,nodiratime,rsize=131072,wsize=131072,timeo=10"
"nas.alvaone.net:/mnt/bigdata/backups /mnt/backups nfs4 _netdev,noauto,noatime,nodiratime,rsize=131072,wsize=131072,timeo=10"
"nas.alvaone.net:/mnt/bigdata/documents /mnt/documents nfs4 _netdev,noauto,noatime,nodiratime,rsize=131072,wsize=131072,timeo=10"
"nas.alvaone.net:/mnt/bigdata/files /mnt/files nfs4 _netdev,noauto,noatime,nodiratime,rsize=131072,wsize=131072,timeo=10"
"nas.alvaone.net:/mnt/bigdata/games /mnt/games nfs4 _netdev,noauto,noatime,nodiratime,rsize=131072,wsize=131072,timeo=10"
"nas.alvaone.net:/mnt/bigdata/movies /mnt/movies nfs4 _netdev,noauto,noatime,nodiratime,rsize=131072,wsize=131072,timeo=10"
"nas.alvaone.net:/mnt/bigdata/music /mnt/music nfs4 _netdev,noauto,noatime,nodiratime,rsize=131072,wsize=131072,timeo=10"
"nas.alvaone.net:/mnt/bigdata/pictures /mnt/pictures nfs4 _netdev,noauto,noatime,nodiratime,rsize=131072,wsize=131072,timeo=10"
"nas.alvaone.net:/mnt/bigdata/shows /mnt/shows nfs4 _netdev,noauto,noatime,nodiratime,rsize=131072,wsize=131072,timeo=10"
"nas.alvaone.net:/mnt/bigdata/stage /mnt/stage nfs4 _netdev,noauto,noatime,nodiratime,rsize=131072,wsize=131072,timeo=10"
"nas.alvaone.net:/mnt/bigdata/torrents /mnt/torrents nfs4 _netdev,noauto,noatime,nodiratime,rsize=131072,wsize=131072,timeo=10"
"nas.alvaone.net:/mnt/bigdata/videos /mnt/videos nfs4 _netdev,noauto,noatime,nodiratime,rsize=131072,wsize=131072,timeo=10"
)
create_systemd_mount_unit() {
# $1: fstab entry line
local fstab_entry="$1"
local source=$(echo "$fstab_entry" | awk '{print $1}')
local target=$(echo "$fstab_entry" | awk '{print $2}')
local fstype=$(echo "$fstab_entry" | awk '{print $3}')
local options=$(echo "$fstab_entry" | awk '{print $4}')
# Skip if target is empty or invalid
if [[ -z "$target" || "$target" =~ ^[[:space:]]*$ ]]; then
return
fi
# Convert mount path to systemd unit name
local unit_name=$(systemd-escape -p --suffix=mount "$target")
local unit_path="/etc/systemd/system/${unit_name}"
log "Creating systemd mount unit: $unit_name"
cat > "$unit_path" << EOF
[Unit]
Description=NFS mount for $target
After=network-online.target
Wants=network-online.target
[Mount]
What=$source
Where=$target
Type=$fstype
Options=$options
EOF
systemctl daemon-reload
}
list_mounts() {
log "Listing configured NFS mount units and their status"
echo
printf "%-20s %-30s %-10s %-15s\n" "MOUNT POINT" "UNIT NAME" "LOADED" "ACTIVE"
printf "%-20s %-30s %-10s %-15s\n" "-----------" "---------" "------" "------"
for entry in "${NFS_ENTRIES[@]}"; do
if [ -n "$entry" ]; then
local target=$(echo "$entry" | awk '{print $2}')
local target_name=$(get_mount_point_name "$target")
# Skip if target is empty or invalid
if [[ -z "$target" || "$target" =~ ^[[:space:]]*$ ]]; then
continue
fi
local unit_name=$(systemd-escape -p --suffix=mount "$target")
# Get unit status
local loaded=$(systemctl is-enabled "$unit_name" 2>/dev/null || echo "disabled")
local active_output
active_output=$(systemctl is-active "$unit_name" 2>/dev/null) || active_output="inactive"
local active="$active_output"
printf "%-20s %-30s %-10s %-15s\n" "$target_name" "$unit_name" "$loaded" "$active"
fi
done
echo
}
create_systemd_units() {
local entries_to_create=("${NFS_ENTRIES[@]}")
# Filter entries if specific mount point is specified
if [ -n "$AMN_MOUNT_POINT" ]; then
mapfile -t entries_to_create < <(filter_nfs_entries "$AMN_MOUNT_POINT")
fi
if [ ${#entries_to_create[@]} -eq 0 ]; then
log "No NFS entries found"
return
fi
log "Creating systemd units for NFS entries"
for entry in "${entries_to_create[@]}"; do
if [ -n "$entry" ]; then
create_systemd_mount_unit "$entry"
fi
done
}
unmount_all() {
# Stop systemd mount units for NFS entries
if [ ${#NFS_ENTRIES[@]} -eq 0 ]; then
log "No NFS entries found"
return
fi
log "Stopping systemd mount units for NFS entries"
for entry in "${NFS_ENTRIES[@]}"; do
if [ -n "$entry" ]; then
local target=$(echo "$entry" | awk '{print $2}')
# Skip if target is empty or invalid
if [[ -z "$target" || "$target" =~ ^[[:space:]]*$ ]]; then
continue
fi
local unit_name=$(systemd-escape -p --suffix=mount "$target")
log "Stopping systemd mount unit: $unit_name"
systemctl stop "$unit_name"
fi
done
}
disable_all() {
local entries_to_unmount=("${NFS_ENTRIES[@]}")
# Filter entries if specific mount point is specified
if [ -n "$AMN_MOUNT_POINT" ]; then
log "Disabling mount services for specific mount point: $AMN_MOUNT_POINT"
mapfile -t entries_to_unmount < <(filter_nfs_entries "$AMN_MOUNT_POINT")
if [ ${#entries_to_unmount[@]} -eq 0 ]; then
loge "No NFS entries found for mount point: $AMN_MOUNT_POINT"
return 1
fi
else
log "Disabling all mount services and unmounting shares"
fi
# Stop units from NFS entries
if [ ${#entries_to_unmount[@]} -gt 0 ]; then
log "Stopping systemd mount units for NFS entries"
for entry in "${entries_to_unmount[@]}"; do
if [ -n "$entry" ]; then
local target=$(echo "$entry" | awk '{print $2}')
# Skip if target is empty or invalid
if [[ -z "$target" || "$target" =~ ^[[:space:]]*$ ]]; then
continue
fi
local unit_name=$(systemd-escape -p --suffix=mount "$target")
log "Stopping and disabling systemd mount unit: $unit_name"
# Stop the mount unit
systemctl stop "$unit_name" 2>/dev/null || true
# Disable the mount unit
systemctl disable "$unit_name" 2>/dev/null || true
# Force unmount if still mounted
if mountpoint -q "$target" 2>/dev/null; then
log "Force unmounting: $target"
umount -f "$target" 2>/dev/null || umount -l "$target" 2>/dev/null || true
fi
fi
done
fi
# Force unmount any remaining NFS mounts from the configured server
REMAINING_MOUNTS=$(mount | grep "type nfs" | grep "$AMN_SERVER_FQDN" | awk '{print $3}')
if [ -n "$REMAINING_MOUNTS" ]; then
log "Force unmounting remaining NFS shares:"
echo "$REMAINING_MOUNTS" | while read -r mountpoint; do
log "Unmounting: $mountpoint"
umount -f "$mountpoint" 2>/dev/null || umount -l "$mountpoint" 2>/dev/null || true
done
fi
# Stop the automountnfs service itself
log "Stopping automountnfs service"
systemctl stop automountnfs 2>/dev/null || true
# Stop and disable the check timer
log "Stopping automountnfs-check timer"
systemctl stop automountnfs-check.timer 2>/dev/null || true
systemctl disable automountnfs-check.timer 2>/dev/null || true
systemctl daemon-reload
log "All mount services stopped, shares unmounted, automountnfs service stopped, and check timer disabled"
}
mount_all() {
# Create and start systemd mount units for NFS entries
local entries_to_mount=("${NFS_ENTRIES[@]}")
# Filter entries if specific mount point is specified
if [ -n "$AMN_MOUNT_POINT" ]; then
log "Filtering for mount point: $AMN_MOUNT_POINT"
mapfile -t entries_to_mount < <(filter_nfs_entries "$AMN_MOUNT_POINT")
if [ ${#entries_to_mount[@]} -eq 0 ]; then
loge "No NFS entries found for mount point: $AMN_MOUNT_POINT"
return 1
fi
fi
if [ ${#entries_to_mount[@]} -eq 0 ]; then
log "No NFS entries found"
return
fi
create_systemd_units
log "Starting systemd mount units for NFS entries"
for entry in "${entries_to_mount[@]}"; do
if [ -n "$entry" ]; then
local source=$(echo "$entry" | awk '{print $1}')
local target=$(echo "$entry" | awk '{print $2}')
# Skip if target is empty or invalid
if [[ -z "$target" || "$target" =~ ^[[:space:]]*$ ]]; then
continue
fi
local unit_name=$(systemd-escape -p --suffix=mount "$target")
# Check if the NFS share is available from server
if [[ $NFS_SHARES != *$(echo $source | cut -d: -f2)* ]]; then
log "$target not provided by server!"
continue
fi
# Check if already mounted
cmd="mountpoint -q $target"
logc "$cmd"
$cmd
if [ $? -ne 0 ]; then
# Ensure mount point exists
if [ ! -d "$target" ]; then
log "Creating mount point: $target"
mkdir -p "$target"
fi
# Set ownership to bigdata group and permissions (g+ws) before mounting
log "Setting ownership and permissions for $target"
chgrp bigdata "$target" 2>/dev/null || log "Warning: Could not change group ownership"
chmod g+ws "$target" 2>/dev/null || log "Warning: Could not change permissions"
log "Starting systemd mount unit: $unit_name"
systemctl start "$unit_name"
if [ $? -ne 0 ]; then
loge "Starting systemd mount unit failed! ($?)"
continue
fi
log "started systemd mount unit $unit_name"
fi
fi
done
# Enable and start the check timer only when mounting all shares
if [ -z "$AMN_MOUNT_POINT" ]; then
log "Starting automountnfs-check timer"
systemctl enable automountnfs-check.timer 2>/dev/null || true
systemctl start automountnfs-check.timer 2>/dev/null || true
fi
}
if [[ "${AMN_CHECK}" -eq 1 ]]; then
ping -c 1 "${AMN_SERVER_FQDN}"
if [ $? -ne 0 ]; then
log "The server could not be reached."
unmount_all
else
log "Asking the server for mountpoints"
cmd="showmount -e $AMN_SERVER_FQDN"
logc "$cmd"
NFS_SHARES=$(
$cmd 2>&1 | sed -e '/^\//!d' | cut -f1 -d" "
test ${PIPESTATUS[0]} -eq 0
)
smount_ret=$?
if [ $smount_ret -ne 0 ]; then
loge "No shares available from host! ($smount_ret)"
exit
fi
log "Server mount points:\n$NFS_SHARES"
mount_all
fi
fi
if [[ "${AMN_UNMOUNT}" -eq 1 ]]; then
disable_all
elif [[ "${AMN_STATUS}" -eq 1 ]]; then
list_mounts
elif [[ "${AMN_CHECK}" -eq 0 && "${AMN_UNMOUNT}" -eq 0 && "${AMN_STATUS}" -eq 0 ]]; then
# Default behavior when just "mount" command is used
log "Asking the server for mountpoints"
cmd="showmount -e $AMN_SERVER_FQDN"
logc "$cmd"
NFS_SHARES=$(
$cmd 2>&1 | sed -e '/^\//!d' | cut -f1 -d" "
test ${PIPESTATUS[0]} -eq 0
)
smount_ret=$?
if [ $smount_ret -ne 0 ]; then
loge "No shares available from host! ($smount_ret)"
exit 1
fi
log "Server mount points:\n$NFS_SHARES"
mount_all
fi
# vim: ft=sh