-
Notifications
You must be signed in to change notification settings - Fork 0
/
automountnfs
executable file
·152 lines (132 loc) · 3.27 KB
/
automountnfs
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
#!/bin/bash
AMN_SERVER_FQDN="lithium.alvaone.net"
AMN_TEMP_LOG="/var/log/automountnfs"
AMN_DATE_OPTS="%a %b %d %H:%M %Y"
AMN_CHECK=0
AMN_UNMOUNT=0
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" >> $AMN_TEMP_LOG
}
loge() {
echo -e `date +"$AMN_DATE_OPTS"`": ERROR: $1" >> $AMN_TEMP_LOG
}
logc() {
echo -e `date +"$AMN_DATE_OPTS"`": Exec: $1" >> $AMN_TEMP_LOG
}
run_cmd() {
# $1: command to run
# $2: Redirection
cmd=$1
logc $cmd
$cmd $2
}
usage() {
echo "${0} - Auto Mount or Unmount NFS shares"
echo
echo "Usage: ${0} [options] (mount|unmount)"
echo
echo "Options:"
echo
echo " -h: Show help information."
echo
echo "Commands:"
echo
echo " mount Mount fstab nfs mounts"
echo " unmount Unmount all fstab nfs mounts"
}
if [[ $# -lt 1 ]]; then
usage;
exit 0;
fi
log "AutoMountNFS started: $0::$$"
ARGS=("$@")
for (( a = 0; a < $#; a++ )); do
if [[ ${ARGS[$a]} == "check" ]]; then
log "Found check command argument"
AMN_CHECK=1
elif [[ ${ARGS[$a]} == "unmount" ]]; then
log "Found unmount command argument"
AMN_UNMOUNT=1
elif [[ ${ARGS[$a]} == "-h" ]]; then
usage;
exit 0;
fi
done
MOUNT_POINTS=$(sed -e '/^.*#/d' -e '/^.*:\//!d' -e 's/\t/ /g' /etc/fstab \
| tr -s " " | cut -f2 -d" ")
unmount_all() {
for umntpnt in ${MOUNT_POINTS}; do
log "Unmounting $umntpnt"
cmd="umount -l -f $umntpnt"
logc "$cmd"
$cmd 2>&1 >> $AMN_TEMP_LOG
if [ $? -ne 0 ]; then
loge "umounting failed! ($?)"
continue
fi
log "umounted $umntpnt"
done
}
mount_all() {
for mount_point in ${MOUNT_POINTS}; do
log "Processing point: $mount_point"
if [[ $NFS_SHARES != *$mount_point* ]]; then
log "$mount_point not provide by server!"
continue
fi
cmd="mountpoint -q $mount_point"
logc "$cmd"
$cmd 2>&1 >> $AMN_TEMP_LOG
if [ $? -ne 0 ]; then
log "Mounting $mount_point"
mount -v $mount_point >> $AMN_TEMP_LOG
if [ $? -ne 0 ]; then
loge "Mounting failed! ($?)"
continue
fi
log "mounted $mount_point"
fi
done
}
if [[ "${AMN_CHECK}" -eq 1 ]]; then
ping -c 1 "${AMN_SERVER_FQDN}" >> $AMN_TEMP_LOG
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>>$AMN_TEMP_LOG | 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
unmount_all
fi
# vim: ft=sh