1 | #!/bin/sh
|
---|
2 |
|
---|
3 | # This is an example CTDB NFS callout script for Ganesha. It is based
|
---|
4 | # on the last version of 60.ganesha shipped with CTDB. As such, it
|
---|
5 | # does not try to monitor RPC services that were not monitored by
|
---|
6 | # 60.ganesha - this might be a useful improvement. It has also not
|
---|
7 | # been properly tested.
|
---|
8 |
|
---|
9 | # You should check your version of NFS Ganesha to see if it ships with
|
---|
10 | # a newer callout.
|
---|
11 |
|
---|
12 | # To use this:
|
---|
13 | #
|
---|
14 | # * Set CTDB_NFS_CALLOUT in your CTDB configuration to point to this
|
---|
15 | # script
|
---|
16 | #
|
---|
17 | # * Rename nfs-checks.d/{20.nfs.check,30.nlockmgr.check,50.mountd.check}
|
---|
18 | # so that they no longer have the ".check" suffix
|
---|
19 | #
|
---|
20 | # * Install 20.nfs-ganesha.check to nfs-checks.d/20.nfs.check
|
---|
21 |
|
---|
22 | # I (Martin Schwenke) hereby relicense all of my contributions to this
|
---|
23 | # callout (and, previously, to 60.ganesha) to a license compatible
|
---|
24 | # with NFS Ganesha (right now this is LGPLv3, but I'm flexible).
|
---|
25 | # There may be other contributions to be considered for relicensing,
|
---|
26 | # particularly those in commit 28cbe527d47822f870e8252495ab2a1c8fddd12f.
|
---|
27 |
|
---|
28 | ######################################################################
|
---|
29 |
|
---|
30 | # Exit on 1st error
|
---|
31 | set -e
|
---|
32 |
|
---|
33 | if [ -z "$CTDB_CLUSTER_FILESYSTEM_TYPE" ] ; then
|
---|
34 | CTDB_CLUSTER_FILESYSTEM_TYPE="gpfs"
|
---|
35 | fi
|
---|
36 |
|
---|
37 | # Override for unit testing
|
---|
38 | if [ -z "$PROCFS_PATH" ] ; then
|
---|
39 | PROCFS_PATH="/proc"
|
---|
40 | fi
|
---|
41 |
|
---|
42 | ##################################################
|
---|
43 |
|
---|
44 | usage ()
|
---|
45 | {
|
---|
46 | _c=$(basename $0)
|
---|
47 | cat <<EOF
|
---|
48 | usage: $_c { shutdown | startup }
|
---|
49 | $_c { stop | start | check } nfs
|
---|
50 | $_c { releaseip | takeip }
|
---|
51 | $_c { monitor-list-shares }
|
---|
52 | EOF
|
---|
53 | exit 1
|
---|
54 | }
|
---|
55 |
|
---|
56 |
|
---|
57 | ##################################################
|
---|
58 | # Basic service stop and start
|
---|
59 |
|
---|
60 | nfs_service="nfs-ganesha-$CTDB_CLUSTER_FILESYSTEM_TYPE"
|
---|
61 |
|
---|
62 | basic_stop ()
|
---|
63 | {
|
---|
64 | case "$1" in
|
---|
65 | nfs)
|
---|
66 | service "$nfs_service" stop
|
---|
67 | ;;
|
---|
68 | *)
|
---|
69 | usage
|
---|
70 | esac
|
---|
71 | }
|
---|
72 |
|
---|
73 | basic_start ()
|
---|
74 | {
|
---|
75 | case "$1" in
|
---|
76 | nfs)
|
---|
77 | service "$nfs_service" start
|
---|
78 | ;;
|
---|
79 | *)
|
---|
80 | usage
|
---|
81 | esac
|
---|
82 | }
|
---|
83 |
|
---|
84 | ##################################################
|
---|
85 | # "stop" and "start" options for restarting
|
---|
86 |
|
---|
87 | service_stop ()
|
---|
88 | {
|
---|
89 | case "$1" in
|
---|
90 | nfs)
|
---|
91 | basic_stop "nfs"
|
---|
92 | ;;
|
---|
93 | nlockmgr)
|
---|
94 | # Do nothing - used by statd-callout
|
---|
95 | :
|
---|
96 | ;;
|
---|
97 | *)
|
---|
98 | usage
|
---|
99 | esac
|
---|
100 | }
|
---|
101 |
|
---|
102 | service_start ()
|
---|
103 | {
|
---|
104 | case "$1" in
|
---|
105 | nfs)
|
---|
106 | basic_start "nfs"
|
---|
107 | ;;
|
---|
108 | nlockmgr)
|
---|
109 | # Do nothing - used by statd-callout
|
---|
110 | :
|
---|
111 | ;;
|
---|
112 | *)
|
---|
113 | usage
|
---|
114 | esac
|
---|
115 | }
|
---|
116 |
|
---|
117 | ##################################################
|
---|
118 | # Nitty gritty - monitoring and IP handling
|
---|
119 |
|
---|
120 | GANRECDIR="/var/lib/nfs/ganesha"
|
---|
121 | GANRECDIR2="/var/lib/nfs/ganesha/recevents"
|
---|
122 | GANRECDIR3="/var/lib/nfs/ganesha_local"
|
---|
123 |
|
---|
124 | get_cluster_fs_state ()
|
---|
125 | {
|
---|
126 | case $CTDB_CLUSTER_FILESYSTEM_TYPE in
|
---|
127 | gpfs)
|
---|
128 | /usr/lpp/mmfs/bin/mmgetstate | awk 'NR == 4 { print $3 }'
|
---|
129 | ;;
|
---|
130 | *)
|
---|
131 | die "File system $CTDB_CLUSTER_FILESYSTEM_TYPE not supported"
|
---|
132 | ;;
|
---|
133 | esac
|
---|
134 | }
|
---|
135 |
|
---|
136 | create_ganesha_recdirs ()
|
---|
137 | {
|
---|
138 | [ -n "$CTDB_GANESHA_REC_SUBDIR" ] || CTDB_GANESHA_REC_SUBDIR=".ganesha"
|
---|
139 |
|
---|
140 | _mounts=$(mount -t $CTDB_CLUSTER_FILESYSTEM_TYPE)
|
---|
141 | if [ -z "$_mounts" ]; then
|
---|
142 | echo "startup $CTDB_CLUSTER_FILESYSTEM_TYPE not ready"
|
---|
143 | exit 0
|
---|
144 | fi
|
---|
145 | _mntpt=$(echo "$_mounts" | sort | awk 'NR == 1 {print $3}')
|
---|
146 | _link_dst="${_mntpt}/${CTDB_GANESHA_REC_SUBDIR}"
|
---|
147 | mkdir -vp "$_link_dst"
|
---|
148 | if [ -e "$GANRECDIR" ]; then
|
---|
149 | if [ ! -L "$GANRECDIR" ] ; then
|
---|
150 | rm -vrf "$GANRECDIR"
|
---|
151 | else
|
---|
152 | _t=$(readlink "$GANRECDIR")
|
---|
153 | if [ "$_t" != "$_link_dst" ] ; then
|
---|
154 | rm -v "$GANRECDIR"
|
---|
155 | fi
|
---|
156 | fi
|
---|
157 | fi
|
---|
158 | # This is not an "else". It also re-creates the link if it was
|
---|
159 | # removed above!
|
---|
160 | if [ ! -e "$GANRECDIR" ]; then
|
---|
161 | ln -sv "$_link_dst" "$GANRECDIR"
|
---|
162 | fi
|
---|
163 |
|
---|
164 | mkdir -p "$GANRECDIR2"
|
---|
165 | mkdir -p "$GANRECDIR3"
|
---|
166 | }
|
---|
167 |
|
---|
168 | service_check ()
|
---|
169 | {
|
---|
170 | create_ganesha_recdirs
|
---|
171 |
|
---|
172 | # Always succeed if cluster filesystem is not active
|
---|
173 | _cluster_fs_state=$(get_cluster_fs_state)
|
---|
174 | if [ $_cluster_fs_state != "active" ] ; then
|
---|
175 | exit 0
|
---|
176 | fi
|
---|
177 |
|
---|
178 | # Check that NFS Ganesha is running, according to PID file
|
---|
179 | _pidfile="/var/run/ganesha.pid"
|
---|
180 | _ganesha="/usr/bin/$CTDB_CLUSTER_FILESYSTEM_TYPE.ganesha.nfsd"
|
---|
181 | if ! { read _pid < "$_pidfile" && \
|
---|
182 | grep "$_ganesha" "${PROCFS_PATH}/${_pid}/cmdline" ; } >/dev/null 2>&1 ; then
|
---|
183 | echo "ERROR: NFS Ganesha not running according to PID file"
|
---|
184 | return 1
|
---|
185 | fi
|
---|
186 |
|
---|
187 | # Check red conditions against limit
|
---|
188 | _reds_max=2
|
---|
189 | _reds=$(ls $GANRECDIR3 | grep -c "red")
|
---|
190 |
|
---|
191 | if [ $_reds -ge $_reds_max ] ; then
|
---|
192 | echo "Too many red conditions (${_reds}/${_reds_max})"
|
---|
193 | return 1
|
---|
194 | fi
|
---|
195 |
|
---|
196 | # Check for stall
|
---|
197 | _stall_max=120
|
---|
198 | _now=$(date +"%s")
|
---|
199 | _last=$(ls -t $GANRECDIR3 | sed -n -e '1s@_.*@@p')
|
---|
200 | [ -n $_last ] || _last=$_now # Handle startup
|
---|
201 | _stall=$(($_now - $_last))
|
---|
202 | if [ $_stall -ge $_stall_max ] ; then
|
---|
203 | echo "ERROR: Stalled for ${_stall} second(s)"
|
---|
204 | return 1
|
---|
205 | fi
|
---|
206 |
|
---|
207 | return 0
|
---|
208 | }
|
---|
209 |
|
---|
210 | #-------------------------------------------------
|
---|
211 |
|
---|
212 | get_nodenum ()
|
---|
213 | {
|
---|
214 | _nodenum_file="${GANRECDIR}/gpfs_nodenum"
|
---|
215 |
|
---|
216 | if [ ! -f "$_nodenum_file" ]; then
|
---|
217 | /usr/lpp/mmfs/bin/mmlsconfig myNodeConfigNumber |
|
---|
218 | awk '{print $2}' >"$_nodenum_file"
|
---|
219 | fi
|
---|
220 |
|
---|
221 | cat "$_nodenum_file"
|
---|
222 | }
|
---|
223 |
|
---|
224 | nfs_releaseip ()
|
---|
225 | {
|
---|
226 | case $CLUSTER_FILESYSTEM_TYPE in
|
---|
227 | gpfs)
|
---|
228 | _nnum=$(get_nodenum)
|
---|
229 | _tdate=$(date +"%s")
|
---|
230 | _touchtgt="releaseip_${_tdate}_${_nnum}_${2}_${3}_${1}"
|
---|
231 | touch "${GANRECDIR2}/${_touchtgt}"
|
---|
232 | touch "$GANRECDIR2/my${_touchtgt}"
|
---|
233 | ;;
|
---|
234 | esac
|
---|
235 | }
|
---|
236 |
|
---|
237 | nfs_takeip ()
|
---|
238 | {
|
---|
239 | case $CLUSTER_FILESYSTEM_TYPE in
|
---|
240 | gpfs)
|
---|
241 | _nnum=$(get_nodenum)
|
---|
242 | _tdate=$(date +"%s")
|
---|
243 | _touchtgt="takeip_${_tdate}_${_nnum}_${2}_${3}_${1}"
|
---|
244 | touch "${GANRECDIR2}/${_touchtgt}"
|
---|
245 | ;;
|
---|
246 | esac
|
---|
247 | }
|
---|
248 |
|
---|
249 | ##################################################
|
---|
250 | # service init startup and final shutdown
|
---|
251 |
|
---|
252 | nfs_shutdown ()
|
---|
253 | {
|
---|
254 | basic_stop "nfs"
|
---|
255 | }
|
---|
256 |
|
---|
257 | nfs_startup ()
|
---|
258 | {
|
---|
259 | create_ganesha_recdirs
|
---|
260 |
|
---|
261 | basic_stop "nfs" || true
|
---|
262 | basic_start "nfs"
|
---|
263 | _f="${PROCFS_PATH}/sys/net/ipv4/tcp_tw_recycle"
|
---|
264 | if [ "$_f" ] ; then
|
---|
265 | echo 1 >"$_f"
|
---|
266 | fi
|
---|
267 | }
|
---|
268 |
|
---|
269 | ##################################################
|
---|
270 | # list share directories
|
---|
271 |
|
---|
272 | nfs_monitor_list_shares ()
|
---|
273 | {
|
---|
274 | grep Path /etc/ganesha/$CTDB_CLUSTER_FILESYSTEM_TYPE.ganesha.exports.conf |
|
---|
275 | cut -f2 -d\" |
|
---|
276 | sort -u
|
---|
277 | }
|
---|
278 |
|
---|
279 | ##################################################
|
---|
280 |
|
---|
281 | action="$1"
|
---|
282 | shift
|
---|
283 |
|
---|
284 | case "$action" in
|
---|
285 | shutdown) nfs_shutdown ;;
|
---|
286 | startup) nfs_startup ;;
|
---|
287 | stop) service_stop "$1" ;;
|
---|
288 | start) service_start "$1" ;;
|
---|
289 | check) service_check "$1" ;;
|
---|
290 | releaseip) nfs_releaseip "$@" ;;
|
---|
291 | takeip) nfs_takeip "$@" ;;
|
---|
292 | monitor-list-shares) nfs_monitor_list_shares ;;
|
---|
293 | register|monitor-pre|monitor-post)
|
---|
294 | # Not required/implemented
|
---|
295 | :
|
---|
296 | ;;
|
---|
297 | *)
|
---|
298 | usage
|
---|
299 | esac
|
---|