-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathnfs_automount
executable file
·473 lines (378 loc) · 11.8 KB
/
nfs_automount
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
#!/usr/bin/env bash
# Distributed under MIT license
# Copyright (c) 2013 Ville Walveranta
# http://my.galagzee.com
# Absolute path to the configuration file
# (if empty or not defined, the default is
# 'nfs_automount.conf' in the script's directory)
CONFIG_FILE=/etc/nfs_automount.conf
###############################################################################
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/sbin:/usr/sbin:/usr/bin:/root/bin
export PATH
# -- DATESTAMP AT RUNTIME --
DATESTAMP=`date --rfc-3339=seconds`
# -- IMPORT CONFIGURATION --
if [ -z ${CONFIG_FILE} ] ; then
SCRIPT_PATH="`dirname \"$0\"`"
SCRIPT_PATH="`( cd \"$SCRIPT_PATH\" && pwd )`"
CONFIG=${SCRIPT_PATH}/nfs_automount.conf
else
CONFIG=${CONFIG_FILE}
fi
if [ -f ${CONFIG} ] ; then
source ${CONFIG}
else
echo "nfs_automount [${DATESTAMP}]: [CRIT] Configuration file (${CONFIG}) missing; cannot continue!"
exit 1
fi
counter=0
declare -a MOUNTDATA
for MOUNT in ${MOUNTS[@]}; do
MOUNTDATA[((counter++))]=${MOUNT}
done
# -- LOGGING --
function log {
if [ ! -z "$1" ] ; then
log_msg="$1"
else
log_msg="[WARN] Parameter missing (log)!"
fi
TAG=${log_msg:0:6}
if [ ${TAG} = "[CRIT]" ] || [ ${TAG} = "[NOTE]" ] ; then
critical=true
else
critical=false
fi
DATESTAMP=`date --rfc-3339=seconds`
if [ "${DEBUGLOG}" = "true" ] || [ "${critical}" = "true" ] ; then
message=`echo "nfs_automount [${DATESTAMP}]: ${log_msg}"`
if [ "${LOGTYPE}" = "log" ] && [ ! -z ${LOGFILEPATH} ]; then
_output_type="echo"
if [ -f ${LOGFILEPATH} ] ; then
_output_type="log"
else
touch ${LOGFILEPATH} > /dev/null 2>&1
if [ -f ${LOGFILEPATH} ] ; then
_output_type="log"
fi
fi
if [ "${_output_type}" = "log" ] ; then
echo ${message} >> ${LOGFILEPATH}
else
echo ${message}
fi
else
echo ${message}
fi
fi
if [ ${TAG} = "[CRIT]" ] ; then
logger_msg=`echo "nfs_automount: ${log_msg}"`
logger ${logger_msg}
fi
}
# -- CHECK & SET COMMAND LOCATIONS --
function check_command {
CMD=`which $1` > /dev/null 2>&1
if [ $? -ne 0 ] ; then
log "[CRIT] Command $1 not found. Cannot continue."
if [ "$1" = "showmount" ] ; then
log "[INFO] Install nfs-common (Debian/Ubuntu), or nfs-utils (RedHat/CentOS) first!"
fi
exit 1
fi
_RET=${CMD}
return 0
}
check_command showmount; showmountcmd=${_RET}
check_command mountpoint; mountpointcmd=${_RET}
check_command rpcinfo; rpcinfocmd=${_RET}
check_command grep; grepcmd=${_RET}
check_command mount; mountcmd=${_RET}
check_command umount; umountcmd=${_RET}
check_command touch; touchcmd=${_RET}
check_command rm; rmcmd=${_RET}
check_command awk; awkcmd=${_RET}
# -- OPERATIONS --
function get_mount_dataset {
if [ ! -z "$1" ] ; then
_mountdataset="$1"
else
log "[CRIT] Parameter missing (get_mount_dataset); process aborted!"
exit 1
fi
if [ -z ${DELIMITER} ] ; then
DELIMITER="|"
fi
_RET=(`echo ${_mountdataset//$DELIMITER/ }`)
if [ ! -z ${_RET[4]} ] ; then
_RET[4]=${_RET[3]}/${_RET[4]}
fi
# import and set rw/ro
if [ ${_RET[0]} = "rw" ]; then
_RET[0]="-o rw,${MOUNTOPTS}"
_RET[5]="rw"
else
_RET[0]="-o ro,${MOUNTOPTS}"
_RET[5]="ro"
fi
}
function check_mounted {
if [ ! -z "$1" ] ; then
_localmountpoint="$1"
else
log "[CRIT] Parameter missing (check_mounted); process aborted!"
exit 1
fi
if ${grepcmd} -qsE "^[^ ]+ ${_localmountpoint}" /proc/mounts; then
_RET=true
else
_RET=false
fi
}
function check_stale {
if [ ! -z "$1" ] ; then
_localmountpoint="$1"
else
log "[CRIT] Parameter missing (check_stale); process aborted!"
exit 1
fi
if [ ! -d "${_localmountpoint}" ] ; then
_RET=true
else
_RET=false
fi
}
function test_remotecheckfile {
if [ ! -z "$1" ] ; then
_remotecheckfile="$1"
else
_remotecheckfile=""
fi
if [ "${_remotecheckfile}" != "" ] ; then
if [ -f ${_remotecheckfile} ] ; then
_RET=true
else
_RET=false
fi
else
_RET=true
fi
}
function check_remotenfs {
if [ ! -z "$1" ] ; then
_remotesystem="$1"
else
log "[CRIT] Parameter missing (check_remotenfs); process aborted!"
exit 1
fi
read -t1 < <(${rpcinfocmd} -t ${_remotesystem} nfs 2>&-)
if [ $? -eq 0 ]; then
_RET=true
else
_RET=false
fi
}
function check_remoteshare {
if [ ! -z "$1" ] && [ ! -z $2 ] ; then
_remotesystem="$1"
_remoteshare="$2"
else
log "[CRIT] Parameter(s) missing (check_remoteshare); process aborted!"
exit 1
fi
remotesharecheck=`${showmountcmd} -e ${_remotesystem} | ${awkcmd} '{print $1}' | ${grepcmd} "${_remoteshare}"`
if [ "${remotesharecheck}" != "" ] ; then
_RET=true
else
_RET=false
fi
}
function valid_for_mount {
if [ ! -z "$1" ] ; then
_localmountpoint="$1"
else
log "[CRIT] Parameter missing (valid_for_mount); process aborted!"
exit 1
fi
if [ -d "${_localmountpoint}" ] ; then
if ! ${mountpointcmd} -q "${_localmountpoint}" ; then
_RET=true
else
_RET=false
fi
else
_RET=false
fi
}
function test_rw {
if [ ! -z "$1" ] ; then
_rw_testfile="$1"
else
_rw_testfile="nfs_automount_rw_test.Kk6MC2CkHoinbSE3CYqv"
fi
${touchcmd} ${_rw_testfile} > /dev/null 2>&1
if [ -f ${_rw_testfile} ] ; then
${rmcmd} -f ${_rw_testfile}
_RET=true
else
_RET=false
fi
}
function nfs_umount {
if [ ! -z "$1" ] ; then
_localmountpoint="$1"
else
log "[CRIT] Parameter missing (nfs_umount); process aborted!"
exit 1
fi
check_mounted "${_localmountpoint}"
if ${_RET} ; then
${umountcmd} -f -l "${_localmountpoint}"
check_mounted "${_localmountpoint}"
if ${_RET} ; then
_RET=false
else
_RET=true
fi
else
_RET=true
fi
}
function nfs_mount {
if [ ! -z "$1" ] && [ ! -z "$2" ] && [ ! -z "$3" ] && [ ! -z "$4" ] ; then
_mountopts="$1"
_remotesystem="$2"
_remoteshare="$3"
_localmountpoint="$4"
else
log "[CRIT] Parameter(s) missing (nfs_mount); process aborted!"
exit 1
fi
# Make sure remote NFS service is available
check_remotenfs ${_remotesystem}
if ${_RET} ; then
# Make sure the remote NFS share is available
check_remoteshare ${_remotesystem} ${_remoteshare}
if ${_RET} ; then
# Make sure the local mountpoint exists and is free
valid_for_mount ${_localmountpoint}
if ${_RET} ; then
log "[INFO] Attempting mount: ${mountcmd} ${_mountopts} ${_remotesystem}:${_remoteshare} ${_localmountpoint}"
${mountcmd} ${_mountopts} ${_remotesystem}:${_remoteshare} ${_localmountpoint}
if [ $? -ne 0 ] ; then
log "[CRIT] Unable to mount share '${_remoteshare}'!"
else
log "[INFO] Share '${_remoteshare}' mounted from '${_remotesystem}' at '${_localmountpoint}'."
fi
else
log "[CRIT] Local mount point '${_localmountpoint}' missing or already in use!"
fi
else
log "[CRIT] Remote share '${_remoteshare}' unavailable!"
fi
else
log "[CRIT] Remote NFS service unavailable at '${_remotesystem}'!"
fi
}
function nfs_remount {
if [ ! -z "$1" ] && [ ! -z "$2" ] && [ ! -z "$3" ] && [ ! -z "$4" ] ; then
_mountopts="$1"
_remotesystem="$2"
_remoteshare="$3"
_localmountpoint="$4"
else
log "[CRIT] Parameter(s) missing (nfs_remount); process aborted!"
exit 1
fi
nfs_umount ${_localmountpoint}
if ${_RET} ; then
log "[INFO] Unmounted '${_localmountpoint}'. Proceeding with remount."
nfs_mount "${_mountopts}" "${_remotesystem}" "${_remoteshare}" "${_localmountpoint}"
else
log "[CRIT] Could not unmount '${_localmountpoint}'. Cannot proceed with remount!"
fi
}
# -- LOGIC --
log "[NOTE] Monitoring started."
while : ; do
if ! ${DEBUGLOG} ; then
log "[NOTE] Checking shares. Debug logging disabled."
fi
mountscnt=${#MOUNTDATA[@]}
for (( i=0; i<${mountscnt}; i++)); do
get_mount_dataset ${MOUNTDATA[$i]}
((datasetno=${i} + 1))
if [ ${#_RET[@]} -lt 5 ] ; then
log "[CRIT] Incomplete mount dataset ${datasetno} in '${CONFIG}'. Skipping!"
if [ ${#_RET[@]} -eq 1 ] ; then
log "[NOTE] Only one parameter in dataset ${datasetno} in '${CONFIG}'. Bad delimiter ('${DELIMITER}')?"
fi
continue
fi
NFSMOUNTOPTS=${_RET[0]}
REMOTESYSTEM=${_RET[1]}
REMOTESHARE=${_RET[2]}
LOCALMOUNTPOINT=${_RET[3]}
REMOTECHECKFILE=${_RET[4]}
READWRITE=${_RET[5]}
check_remotenfs ${REMOTESYSTEM}
if ${_RET} ; then
log "[INFO] (dataset ${datasetno}) Remote server/NFS service at '${REMOTESYSTEM}' available."
check_mounted ${LOCALMOUNTPOINT}
if ${_RET} ; then
log "[INFO] (dataset ${datasetno}) Local mount point '${LOCALMOUNTPOINT}' active."
check_stale ${LOCALMOUNTPOINT}
if ! ${_RET} ; then
log "[INFO] (dataset ${datasetno}) Local mount point '${LOCALMOUNTPOINT}' not stale."
if [ ! -z ${REMOTECHECKFILE} ] ; then
test_remotecheckfile ${REMOTECHECKFILE}
if ${_RET} ; then
log "[INFO] (dataset ${datasetno}) Remote test file '${REMOTECHECKFILE}' is reachable via local the mount point '${LOCALMOUNTPOINT}'."
else
log "[CRIT] (dataset ${datasetno}) Remote test file '${REMOTECHECKFILE}' could not be reached via the mount point '${LOCALMOUNTPOINT}'. This may indicate a remote system problem!"
fi
else
log "[NOTE] (dataset ${datasetno}) Remote test file not defined. Skipping remote test file check for this dataset."
fi
# RO/RW PROCESSING
TESTFILE="${LOCALMOUNTPOINT}"/"${RW_TESTFILE}"
test_rw ${TESTFILE}
if ${_RET} ; then
log "[INFO] (dataset ${datasetno}) Mount point '${LOCALMOUNTPOINT}' is writable."
if [ "${READWRITE}" = "ro" ]; then
log "[INFO] (dataset ${datasetno}) Read Only mount has been specified. Attempting remount."
nfs_remount "${NFSMOUNTOPTS}" "${REMOTESYSTEM}" "${REMOTESHARE}" "${LOCALMOUNTPOINT}"
fi
else
log "[INFO] (dataset ${datasetno}) Mount point '${LOCALMOUNTPOINT}' is not writable."
if [ "${READWRITE}" = "rw" ]; then
log "[INFO] (dataset ${datasetno}) Read/Write mount has been specified. Attempting remount."
nfs_remount "${NFSMOUNTOPTS}" "${REMOTESYSTEM}" "${REMOTESHARE}" "${LOCALMOUNTPOINT}"
fi
fi
else
#STALE PROCESSING
log "[CRIT] (dataset ${datasetno}) Remote NFS share '${REMOTESYSTEM}:${REMOTESHARE}' is stale; attempting remount."
nfs_remount "${NFSMOUNTOPTS}" "${REMOTESYSTEM}" "${REMOTESHARE}" "${LOCALMOUNTPOINT}"
fi
else
#UNMOUNTED PROCESSING
log "[CRIT] (dataset ${datasetno}) Remote NFS share '${REMOTESYSTEM}:${REMOTESHARE}' is unmounted; attempting mount."
nfs_mount "${NFSMOUNTOPTS}" "${REMOTESYSTEM}" "${REMOTESHARE}" "${LOCALMOUNTPOINT}"
fi
else
#UNREACHABLE SERVER/NFS SERVICE PROCESSING
log "[CRIT] (dataset ${datasetno}) Remote server/NFS service at '${REMOTESYSTEM}' unreachable; confirming unmounted status of share '$REMOTESHARE'."
nfs_umount "${LOCALMOUNTPOINT}"
if ${_RET} ; then
log "[INFO] (dataset ${datasetno}) Remote server '${REMOTESYSTEM}' or its NFS service is unreachable. Share '${REMOTESHARE}' has been confirmed unmounted!"
else
log "[CRIT] (dataset ${datasetno}) Remote server '${REMOTESYSTEM}' or its NFS service is unreachable. Share '${REMOTESHARE}' could not be unmounted!"
fi
fi
done
# Break here for cron style run
[[ "${RUNTYPE}" = "service" ]] || break
log "[INFO] Sleeping for ${INTERVAL} seconds."
sleep $INTERVAL
done