Debian Codegraph @

ctdb/doc/examples/nfs-ganesha-callout

raw symbols SCIP
1 #!/bin/shscip-shell filesystem . . `/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 (a
15 # copy of) this script, making sure it is executable.
16 #
17 # * Create a new directory alongside the nfs-checks.d directory, for
18 # example nfs-checks-ganesha.d. Install 20.nfs_ganesha.check and
19 # 21.nfs.check in this directory. Symlink to any other check files
20 # from nfs-checks.d that should still be used, such as
21 # 00.portmapper.check. Set CTDB_NFS_CHECKS_DIR to point to this new
22 # directory of check files.
23 #
24 # * It is recommended, but not required, to install the grace_period
25 # script (usually shipped in a utils package for NFS-Ganesha) to
26 # /usr/bin/grace_period
27
28 # I (Martin Schwenke) hereby relicense all of my contributions to this
29 # callout (and, previously, to 60.ganesha) to a license compatible
30 # with NFS Ganesha (right now this is LGPLv3, but I'm flexible).
31 # There may be other contributions to be considered for relicensing,
32 # particularly those in commit 28cbe527d47822f870e8252495ab2a1c8fddd12f.
33
34 ######################################################################
35
36 # Exit on 1st error
37 set -e
38
39 diescip-shell shell samba 2:4.24.3+dfsg-1 die().()
40 {
41 echoscip-shell system . . echo. "$1"
42 exit 1
43 }
44
45 # Shared directory, typically on a cluster filesystem, that will
46 # contain the NFS-Ganesha state
47 if [scip-shell system . . `[`. -z "$CTDB_NFS_SHARED_STATE_DIR" ]; then
48 diescip-shell shell samba 2:4.24.3+dfsg-1 die(). "$0: CTDB_NFS_SHARED_STATE_DIR is not set"
49 fi
50
51 if [scip-shell system . . `[`. ! -d "$CTDB_NFS_SHARED_STATE_DIR" ]; then
52 t="$CTDB_NFS_SHARED_STATE_DIR" # Readability, below
53 diescip-shell shell samba 2:4.24.3+dfsg-1 die(). "$0: CTDB_NFS_SHARED_STATE_DIR=${t} not found"
54 fi
55
56 state_fs=$(findmntscip-shell system . . findmnt. -n --target "$CTDB_NFS_SHARED_STATE_DIR" -o FSTYPE)
57
58 case "$state_fs" in
59 glusterfs | gpfs)
60 :
61 ;;
62 fuse.glusterfs)
63 state_fs="glusterfs"
64 ;;
65 *)
66 d="$CTDB_NFS_SHARED_STATE_DIR"
67 diescip-shell shell samba 2:4.24.3+dfsg-1 die(). "$0: filesystem type \"${state_fs}\" is not supported for ${d}"
68 ;;
69 esac
70
71 # Always put NFS-Ganesha state in its own subdirectory
72 state_dir="${CTDB_NFS_SHARED_STATE_DIR}/ganesha"
73
74 # Location of exports file
75 nfs_exports_file="${CTDB_NFS_EXPORTS_FILE:-/etc/ganesha/ganesha.conf}"
76
77 # To change the following, edit the default values below. Do not set
78 # these - they aren't configuration variables, just hooks for testing.
79 nfs_service="${CTDB_NFS_SERVICE:-nfs-ganesha}"
80 procfs=${PROCFS_PATH:-/proc}
81
82 case "$state_fs" in
83 gpfs)
84 GANRECDIR="/var/lib/nfs/ganeshascip-shell filesystem . . `/var/lib/nfs/ganesha`."
85 ;;
86 glusterfs)
87 host=$(hostnamescip-shell system . . hostname.)
88 NODESTATEDIR="${state_dir}/${host}"
89 GANSTATEDIR="${state_dir}/.noderefs"
90 NODESTATELN="$GANSTATEDIR/$host"
91 ;;
92 esac
93
94 ##################################################
95
96 usagescip-shell shell samba 2:4.24.3+dfsg-1 usage().()
97 {
98 _c=$(basenamescip-shell system . . basename. "$0")
99 catscip-shell system . . cat. <<EOF
100 usage: $_c { shutdown | startup }
101 $_c { stop | start | check | stats } nfs
102 $_c { releaseip | takeip } <iface> <ip> <maskbits>
103 $_c { monitor-list-shares }
104 EOF
105 exit 1
106 }
107
108 ##################################################
109 # Basic service stop and start
110
111 basic_stopscip-shell shell samba 2:4.24.3+dfsg-1 basic_stop().()
112 {
113 case "$1" in
114 nfs)
115 servicescip-shell system . . service. "$nfs_service" stop
116 ;;
117 *)
118 usagescip-shell shell samba 2:4.24.3+dfsg-1 usage().
119 ;;
120 esac
121 }
122
123 basic_startscip-shell shell samba 2:4.24.3+dfsg-1 basic_start().()
124 {
125 case "$1" in
126 nfs)
127 servicescip-shell system . . service. "$nfs_service" start
128 ;;
129 *)
130 usagescip-shell shell samba 2:4.24.3+dfsg-1 usage().
131 ;;
132 esac
133 }
134
135 ##################################################
136 # "stop" and "start" options for restarting
137
138 service_stopscip-shell shell samba 2:4.24.3+dfsg-1 service_stop().()
139 {
140 case "$1" in
141 nfs)
142 basic_stopscip-shell shell samba 2:4.24.3+dfsg-1 basic_stop(). "nfs"
143 ;;
144 nlockmgr)
145 # Do nothing - used by statd-callout
146 :
147 ;;
148 *)
149 usagescip-shell shell samba 2:4.24.3+dfsg-1 usage().
150 ;;
151 esac
152 }
153
154 service_startscip-shell shell samba 2:4.24.3+dfsg-1 service_start().()
155 {
156 case "$1" in
157 nfs)
158 basic_startscip-shell shell samba 2:4.24.3+dfsg-1 basic_start(). "nfs"
159 ;;
160 nlockmgr)
161 # Do nothing - used by statd-callout
162 :
163 ;;
164 *)
165 usagescip-shell shell samba 2:4.24.3+dfsg-1 usage().
166 ;;
167 esac
168 }
169
170 ##################################################
171 # Nitty gritty - monitoring and IP handling
172
173 # Check that a symlink exists, create it otherwise.
174 # Usage: check_ln <TARGET> <LINK>
175 check_lnscip-shell shell samba 2:4.24.3+dfsg-1 check_ln().()
176 {
177 if [scip-shell system . . `[`. ! -L "${2}" ]; then
178 rmscip-shell system . . rm. -vrf "${2}"
179 else
180 _t=$(readlinkscip-shell system . . readlink. "${2}")
181 if [scip-shell system . . `[`. "$_t" != "${1}" ]; then
182 rmscip-shell system . . rm. -v "${2}"
183 fi
184 fi
185 # This is not an "else". It also re-creates the link if it was
186 # removed above!
187 if [scip-shell system . . `[`. ! -e "${2}" ]; then
188 lnscip-shell system . . ln. -sfv "${1}" "${2}"
189 fi
190 }
191
192 # Return 'active' if the shared filesystem is accessible.
193 get_cluster_fs_statescip-shell shell samba 2:4.24.3+dfsg-1 get_cluster_fs_state().()
194 {
195 case "$state_fs" in
196 gpfs)
197 /usr/lpp/mmfs/bin/mmgetstatescip-shell filesystem . . `/usr/lpp/mmfs/bin/mmgetstate`. | awkscip-shell system . . awk. 'NR == 4 { print $3 }'
198 ;;
199 glusterfs)
200 # Since we're past create_ganesha_recdirs(), we're active.
201 echoscip-shell system . . echo. "active"
202 ;;
203 *)
204 echoscip-shell system . . echo. "File system $state_fs not supported"
205 exit 1
206 ;;
207 esac
208 }
209
210 create_ganesha_recdirsscip-shell shell samba 2:4.24.3+dfsg-1 create_ganesha_recdirs().()
211 {
212 case "$state_fs" in
213 gpfs)
214 mkdirscip-shell system . . mkdir. -vp "$state_dir"
215 check_lnscip-shell shell samba 2:4.24.3+dfsg-1 check_ln(). "$state_dir" "$GANRECDIR"
216 ;;
217 glusterfs)
218 [scip-shell system . . `[`. -d /var/lib/nfs.backupscip-shell filesystem . . `/var/lib/nfs.backup`. ] ||
219 mvscip-shell system . . mv. /var/lib/nfsscip-shell filesystem . . `/var/lib/nfs`. /var/lib/nfs.backupscip-shell filesystem . . `/var/lib/nfs.backup`.
220 check_lnscip-shell shell samba 2:4.24.3+dfsg-1 check_ln(). "$NODESTATEDIR" /var/lib/nfsscip-shell filesystem . . `/var/lib/nfs`.
221
222 mkdirscip-shell system . . mkdir. -p "${NODESTATEDIR}/ganesha/v4recov"
223 mkdirscip-shell system . . mkdir. -p "${NODESTATEDIR}/ganesha/v4old"
224 mkdirscip-shell system . . mkdir. -p "${NODESTATEDIR}/statd/sm"
225 mkdirscip-shell system . . mkdir. -p "${NODESTATEDIR}/statd/sm.bak"
226 touchscip-shell system . . touch. "${NODESTATEDIR}/state"
227 touchscip-shell system . . touch. "${NODESTATEDIR}/statd/state"
228
229 mkdirscip-shell system . . mkdir. -p "$GANSTATEDIR"
230 check_lnscip-shell shell samba 2:4.24.3+dfsg-1 check_ln(). "$NODESTATEDIR" "$NODESTATELN"
231 for _dir in "${GANSTATEDIR}/"*; do
232 # Handle no directories case
233 if [scip-shell system . . `[`. ! -d "$_dir" ]; then
234 break
235 fi
236
237 _node="${_dir##*/}" # basename
238 if [scip-shell system . . `[`. "${_node}" != "${host}" ]; then
239 chscip-shell shell samba 2:4.24.3+dfsg-1 check_ln().eck_lnscip-shell shell samba 2:4.24.3+dfsg-1 check_ln(). "${GANSTATEDIR}/${_node}/ganesha" \
240 "${NODESTATEDIR}/ganesha/${_node}"
241 chscip-shell shell samba 2:4.24.3+dfsg-1 check_ln().eck_lnscip-shell shell samba 2:4.24.3+dfsg-1 check_ln(). "${GANSTATEDIR}/${_node}/statd" \
242 "${NODESTATEDIR}/statd/${_node}"
243 fi
244 done
245 ;;
246 esac
247 }
248
249 is_ganesha_runningscip-shell shell samba 2:4.24.3+dfsg-1 is_ganesha_running().()
250 {
251 # Check that NFS Ganesha is running, according to PID file
252 _pidfile="/var/run/ganesha/ganesha.pidscip-shell filesystem . . `/var/run/ganesha/ganesha.pid`."
253 _ganesha="ganesha.nfsd"
254 if ! {
255 read -r _pid <"$_pidfile" &&
256 [scip-shell system . . `[`. "$(psscip-shell system . . ps. -p "$_pid" -o comm=)" = "$_ganesha" ]
257 } >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then
258
259 return 1
260 fi
261
262 return 0
263 }
264
265 service_checkscip-shell shell samba 2:4.24.3+dfsg-1 service_check().()
266 {
267 create_ganesha_recdirsscip-shell shell samba 2:4.24.3+dfsg-1 create_ganesha_recdirs().
268
269 # Always succeed if cluster filesystem is not active
270 _cluster_fs_state=$(get_cluster_fs_statescip-shell shell samba 2:4.24.3+dfsg-1 get_cluster_fs_state().)
271 if [scip-shell system . . `[`. "$_cluster_fs_state" != "active" ]; then
272 return 0
273 fi
274
275 if ! is_ganesha_runningscip-shell shell samba 2:4.24.3+dfsg-1 is_ganesha_running().; then
276 echoscip-shell system . . echo. "ERROR: NFS Ganesha not running according to PID file"
277 return 1
278 fi
279
280 return 0
281 }
282
283 nfs_statsscip-shell shell samba 2:4.24.3+dfsg-1 nfs_stats().()
284 {
285 _service="$1"
286
287 case "$_service" in
288 nfs)
289 timeoutscip-shell system . . timeout. -v 5 ganesha_stats | grepscip-shell system . . grep. '^Total NFSv.* ops:'
290 ;;
291 *)
292 # This will never change, so is intentionally
293 # unhelpful for avoiding an unhealthy service
294 echoscip-shell system . . echo. "Not implemented" >&2
295 exit 1
296 ;;
297 esac
298 }
299
300 #-------------------------------------------------
301
302 grace_periodscip-shell shell samba 2:4.24.3+dfsg-1 grace_period().()
303 {
304 _arg="$1"
305
306 _gp_status=0
307 if [scip-shell system . . `[`. -x "/usr/bin/grace_periodscip-shell filesystem . . `/usr/bin/grace_period`." ]; then
308 _out=$(/usr/bin/grace_periodscip-shell filesystem . . `/usr/bin/grace_period`. "$_arg" 2>&1) ||
309 _gp_status=$?
310 _down_msg="Error: Can't talk to ganesha service on d-bus"
311 else
312 _out=$(dbus-send \
313 --print-reply --system --dest=org.ganesha.nfsd \
314 /org/ganesha/nfsd/admin org.ganesha.nfsd.admin.grace \
315 string:"$_arg" 2>&1) ||
316 _gp_status=$?
317 _down_msg="Error org.freedesktop.DBus.Error.ServiceUnknown"
318 fi
319
320 if [scip-shell system . . `[`. -n "$_out" ]; then
321 echoscip-shell system . . echo. "$_out"
322 fi
323
324 if [scip-shell system . . `[`. $_gp_status -ne 0 ]; then
325 # If $_out contains $_down_msg then NFS-Ganesha is
326 # either down or is starting, so will be in grace
327 # anyway.
328 if [scip-shell system . . `[`. "${_out#*"${_down_msg}"}" != "$_out" ]; then
329 return 3 # ESRCH - No such process
330 fi
331 fi
332
333 return $_gp_status
334 }
335
336 grace_period_if_runningscip-shell shell samba 2:4.24.3+dfsg-1 grace_period_if_running().()
337 {
338 _arg="$1"
339
340 if ! is_ganesha_runningscip-shell shell samba 2:4.24.3+dfsg-1 is_ganesha_running().; then
341 echoscip-shell system . . echo. "WARNING: NFS Ganesha not running according to PID file"
342 return 0
343 fi
344
345 _status=0
346 grace_periodscip-shell shell samba 2:4.24.3+dfsg-1 grace_period(). "$_arg" || _status=$?
347 case $_status in
348 3)
349 # Convert to success
350 return 0
351 ;;
352 *)
353 return $_status
354 ;;
355 esac
356 }
357
358 nfs_startipreallocatescip-shell shell samba 2:4.24.3+dfsg-1 nfs_startipreallocate().()
359 {
360 grace_period_if_runningscip-shell shell samba 2:4.24.3+dfsg-1 grace_period_if_running(). "0:"
361 }
362
363 nfs_releaseipscip-shell shell samba 2:4.24.3+dfsg-1 nfs_releaseip().()
364 {
365 _ip="$2"
366
367 # NFS-Ganesha recovery code only processes items matching $_ip
368 grace_period_if_runningscip-shell shell samba 2:4.24.3+dfsg-1 grace_period_if_running(). "2:${_ip}"
369 }
370
371 nfs_takeipscip-shell shell samba 2:4.24.3+dfsg-1 nfs_takeip().()
372 {
373 _ip="$2"
374
375 case "$state_fs" in
376 glusterfs)
377 check_lnscip-shell shell samba 2:4.24.3+dfsg-1 check_ln(). "$NODESTATEDIR" "${GANSTATEDIR}/${_ip}"
378 ;;
379 esac
380
381 grace_periodscip-shell shell samba 2:4.24.3+dfsg-1 grace_period(). "5:${_ip}"
382 }
383
384 ##################################################
385 # service init startup and final shutdown
386
387 nfs_shutdownscip-shell shell samba 2:4.24.3+dfsg-1 nfs_shutdown().()
388 {
389 basic_stopscip-shell shell samba 2:4.24.3+dfsg-1 basic_stop(). "nfs"
390 }
391
392 nfs_startupscip-shell shell samba 2:4.24.3+dfsg-1 nfs_startup().()
393 {
394 basic_stopscip-shell shell samba 2:4.24.3+dfsg-1 basic_stop(). "nfs" || truescip-shell system . . true.
395
396 create_ganesha_recdirsscip-shell shell samba 2:4.24.3+dfsg-1 create_ganesha_recdirs().
397
398 basic_startscip-shell shell samba 2:4.24.3+dfsg-1 basic_start(). "nfs"
399 _f="${procfs}/sys/net/ipv4/tcp_tw_recycle"
400 if [scip-shell system . . `[`. -f "$_f" ]; then
401 echoscip-shell system . . echo. 1 >"$_f"
402 fi
403 }
404
405 ##################################################
406 # list share directories
407
408 nfs_monitor_list_sharesscip-shell shell samba 2:4.24.3+dfsg-1 nfs_monitor_list_shares().()
409 {
410 # The 1st sed command prints anything after "Path = ", where
411 # Path is matched case-insensitively, and must be on a word
412 # boundary. This also deletes any semicolon-terminated items
413 # before Path. Each output line now starts with a value for
414 # Path, but may have other settings after a semicolon.
415 _s1='s/.*;*[[:space:]]*\<path\>[[:space:]]*=[[:space:]]*//ip'
416
417 # The 2nd sed command has 2 steps:
418 #
419 # a. Attempt to match an unquoted value not containing
420 # semicolon or double-quote, followed by an optional
421 # line-terminating semicolon or a semicolon followed by
422 # anything else. Keep the value and double-quote it. If
423 # the value was already quoted then the line will be
424 # unchanged. The pattern space now starts with a
425 # double-quoted value.
426 _s2a='s/^\([^";][^";]*\)[[:space:]]*\(;*[[:space:]]*$\|;.*\)/"\1"/'
427 # b. Finally, print the contents of double-quotes at the
428 # beginning of the pattern space, discarding anything
429 # that follows.
430 _s2b='s/^"\([^"][^"]*\)".*/\1/p'
431
432 sedscip-shell system . . sed. -n -e "$_s1" "$nfs_exports_file" | sedscip-shell system . . sed. -n -e "$_s2a" -e "$_s2b"
433 }
434
435 ##################################################
436
437 nfs_registerscip-shell shell samba 2:4.24.3+dfsg-1 nfs_register().()
438 {
439 catscip-shell system . . cat. <<EOF
440 shutdown
441 startup
442 stop
443 start
444 check
445 stats
446 startipreallocate
447 releaseip
448 takeip
449 monitor-list-shares
450 EOF
451 }
452
453 ##################################################
454
455 action="$1"
456 shift
457
458 case "$action" in
459 shutdown) nfs_shutdownscip-shell shell samba 2:4.24.3+dfsg-1 nfs_shutdown(). ;;
460 startup) nfs_startupscip-shell shell samba 2:4.24.3+dfsg-1 nfs_startup(). ;;
461 stop) service_stopscip-shell shell samba 2:4.24.3+dfsg-1 service_stop(). "$1" ;;
462 start) service_startscip-shell shell samba 2:4.24.3+dfsg-1 service_start(). "$1" ;;
463 check) service_checkscip-shell shell samba 2:4.24.3+dfsg-1 service_check(). "$1" ;;
464 stats) nfs_statsscip-shell shell samba 2:4.24.3+dfsg-1 nfs_stats(). "$1" ;;
465 startipreallocate) nfs_startipreallocatescip-shell shell samba 2:4.24.3+dfsg-1 nfs_startipreallocate(). ;;
466 releaseip) nfs_releaseipscip-shell shell samba 2:4.24.3+dfsg-1 nfs_releaseip(). "$@" ;;
467 takeip) nfs_takeipscip-shell shell samba 2:4.24.3+dfsg-1 nfs_takeip(). "$@" ;;
468 monitor-list-shares) nfs_monitor_list_sharesscip-shell shell samba 2:4.24.3+dfsg-1 nfs_monitor_list_shares(). ;;
469 register) nfs_registerscip-shell shell samba 2:4.24.3+dfsg-1 nfs_register(). ;;
470 monitor-pre | monitor-post)
471 # Not required/implemented
472 :
473 ;;
474 *)
475 usagescip-shell shell samba 2:4.24.3+dfsg-1 usage().
476 ;;
477 esac