Debian Codegraph @

ctdb/config/nfs-linux-kernel-callout

raw ◀ ▶
1 Debian patch touch this file
1 #!/bin/sh
2
3 # Exit on 1st error
4 set -e
5
6 # NFS exports file. Some code below keeps a cache of output derived
7 # from exportfs(8). When this file is updated the cache is invalid
8 # and needs to be regenerated.
9 nfs_exports_file="${CTDB_NFS_EXPORTS_FILE:-/var/lib/nfs/etab}"
10
11 # Do not set CTDB_NFS_DISTRO_STYLE - it isn't a configuration
12 # variable, just a hook for testing. To change the style, edit the
13 # default value below.
•14 nfs_distro_style="${CTDB_NFS_DISTRO_STYLE:-systemd-debian}"
15
16 # As above, edit the default value below. CTDB_SYS_ETCDIR is a
17 # test variable only.
18 etc_dir="${CTDB_SYS_ETCDIR:-/etc}"
19
20 # A value of "AUTO" for any service means that service is usually
21 # automatically started and stopped by one of the other services.
22 # Such services will still be restarted by hand on failure, if
23 # configured to do so. This allows services that should not be
24 # running to be set to "".
25
26 case "$nfs_distro_style" in
27 systemd-*)
28 # Defaults
29 nfs_service="nfs-server"
30 nfs_lock_service="rpc-statd"
31 nfs_mountd_service="nfs-mountd"
32 nfs_status_service="rpc-statd"
33 nfs_rquotad_service="rpc-rquotad"
34 nfs_config="${etc_dir}/sysconfig/nfs"
35 nfs_rquotad_config="" # Not use with systemd, restart via service
36
37 case "$nfs_distro_style" in
38 *-redhat | *-suse)
39 : # Defaults only
40 ;;
41 *-debian)
•42 # XXX
43 # Undefine nfs_rquotad_services because the quotarpc service won't
44 # start unless there are specific "quota" mount options in /etc/fstab.
45 # In this way, we let ctdb start it up manually once the
46 # /etc/ctdb/nfs-checks.d/50.rquotad.check detects rpc.rquotad isn't
47 # running.
48 # Users who really don't want rpc.rquotad running should then move
49 # the 50.rquotad.check script away.
50 nfs_rquotad_service=""
51 nfs_service="nfs-kernel-server"
52 if type nfsconf >/dev/null 2>&1; then
53 nfs_config=""
54 else
55 nfs_config="/etc/default/nfs-kernel-server"
56 fi
57 nfs_rquotad_config="/etc/default/quota"
58 ;;
59 *)
60 echo "Internal error"
61 exit 1
62 ;;
63 esac
64 ;;
65
66 sysvinit-*)
67 # Defaults
68 nfs_service="nfs"
69 nfs_lock_service="AUTO"
70 nfs_mountd_service="AUTO"
71 nfs_status_service="AUTO"
72 nfs_rquotad_service="AUTO"
73 nfs_config="${etc_dir}/sysconfig/nfs"
74 nfs_rquotad_config="$nfs_config"
75
76 case "$nfs_distro_style" in
77 *-redhat)
78 nfs_lock_service="nfslock"
79 ;;
80 *-suse)
81 nfs_service="nfsserver"
82 ;;
83 *-debian)
84 nfs_service="nfs-kernel-server"
85 nfs_config="${etc_dir}/default/nfs-kernel-server"
86 nfs_rquotad_config="${etc_dir}/default/quota"
87 ;;
88 *)
89 echo "Internal error"
90 exit 1
91 ;;
92 esac
93 ;;
94
95 *)
96 echo "Internal error"
97 exit 1
98 ;;
99 esac
100
101 # Override for unit testing
102 if [ -z "$PROCFS_PATH" ]; then
103 PROCFS_PATH="/proc"
104 fi
105
106 ##################################################
107
108 usage()
109 {
110 _c=$(basename "$0")
111 cat <<EOF
112 usage: $_c { shutdown | startup }
113 $_c { stop | start } { nfs | nlockmgr }
114 $_c { monitor-list-shares | monitor-post }
115 $_c { register }
116 EOF
117 exit 1
118 }
119
120 ##################################################
121
122 nfs_load_config()
123 {
124 _config="${1:-${nfs_config}}"
125
126 if [ -r "$_config" ]; then
127 . "$_config"
128 fi
129 }
130
131 ##################################################
132
133 service_is_auto_started()
134 {
135 [ "$1" = "AUTO" ]
136 }
137
138 service_is_defined()
139 {
140 _service="$1"
141
142 [ -n "$_service" ] && ! service_is_auto_started "$_service"
143 }
144
145 service_if_defined()
146 {
147 _service="$1"
148 _action="$2"
149
150 if service_is_defined "$_service"; then
151 service "$_service" "$_action"
152 fi
153 }
154
155 ##################################################
156 # Overall NFS service stop and start
157
158 nfs_service_stop()
159 {
160 service_if_defined "$nfs_rquotad_service" stop
161
162 service "$nfs_service" stop
163
164 service_if_defined "$nfs_lock_service" stop
165 }
166
167 nfs_service_start()
168 {
169 service_if_defined "$nfs_lock_service" start
170
171 service "$nfs_service" start
172
173 service_if_defined "$nfs_rquotad_service" start
174 }
175
176 ##################################################
177 # service "stop" and "start" options for restarting
178
179 manual_stop()
180 {
181 case "$1" in
182 mountd)
183 killall -q -9 rpc.mountd
184 ;;
185 rquotad)
186 killall -q -9 rpc.rquotad
187 ;;
188 status)
189 killall -q -9 rpc.statd
190 ;;
191 *)
192 echo "$0: Internal error - invalid call to manual_stop()"
193 exit 1
194 ;;
195 esac
196 }
197
198 service_or_manual_stop()
199 {
200 _rpc_service="$1"
201 _system_service="$2"
202
203 if service_is_defined "$_system_service"; then
204 service "$_system_service" stop
205 elif service_is_auto_started "$_system_service"; then
206 manual_stop "$_rpc_service"
207 fi
208 }
209
210 service_stop()
211 {
212 _rpc_service="$1"
213
214 case "$_rpc_service" in
215 nfs)
216 echo 0 >"${PROCFS_PATH}/fs/nfsd/threads"
217 nfs_service_stop >/dev/null 2>&1 || true
218 pkill -9 nfsd
219 ;;
220 nlockmgr)
221 if service_is_defined "$nfs_lock_service" ; then
222 service "$nfs_lock_service" stop >/dev/null 2>&1 || true
223 else
224 service "$nfs_service" stop >/dev/null 2>&1 || true
225 fi
226 ;;
227 mountd)
228 service_or_manual_stop "$_rpc_service" "$nfs_mountd_service"
229 ;;
230 rquotad)
231 service_or_manual_stop "$_rpc_service" "$nfs_rquotad_service"
232 ;;
233 status)
234 service_or_manual_stop "$_rpc_service" "$nfs_status_service"
235 ;;
236 *)
237 usage
238 ;;
239 esac
240 }
241
242 manual_start()
243 {
244 case "$1" in
245 mountd)
246 nfs_load_config
247 if [ -z "$RPCMOUNTDOPTS" ]; then
248 RPCMOUNTDOPTS="${MOUNTD_PORT:+-p }$MOUNTD_PORT"
249 fi
250 # shellcheck disable=SC2086
251 rpc.mountd $RPCMOUNTDOPTS
252 ;;
253 rquotad)
254 nfs_load_config "$nfs_rquotad_config"
255 if [ -z "$RPCRQUOTADOPTS" ]; then
256 RPCRQUOTADOPTS="${RQUOTAD_PORT:+-p }$RQUOTAD_PORT"
257 fi
258 # shellcheck disable=SC2086
259 rpc.rquotad $RPCRQUOTADOPTS
260 ;;
261 status)
262 nfs_load_config
263 # Red Hat uses STATDARG, Debian uses STATDOPTS
264 opts="${STATDARG:-${STATDOPTS:-''}}"
265 if [ -z "$opts" ]; then
266 # shellcheck disable=SC2086
267 set -- \
268 ${STATD_HA_CALLOUT:+-H} $STATD_HA_CALLOUT \
269 ${STATD_HOSTNAME:+-n} $STATD_HOSTNAME \
270 ${STATD_PORT:+-p} $STATD_PORT \
271 ${STATD_OUTGOING_PORT:+-o} $STATD_OUTGOING_PORT
272 opts="$*"
273 fi
274 # shellcheck disable=SC2086
275 rpc.statd $opts
276 ;;
277 *)
278 echo "$0: Internal error - invalid call to manual_start()"
279 exit 1
280 ;;
281 esac
282 }
283
284 service_or_manual_start()
285 {
286 _rpc_service="$1"
287 _system_service="$2"
288
289 if service_is_defined "$_system_service"; then
290 service "$_system_service" start
291 elif service_is_auto_started "$_system_service"; then
292 manual_start "$_rpc_service"
293 fi
294 }
295
296 service_start()
297 {
298 _rpc_service="$1"
299
300 case "$_rpc_service" in
301 nfs)
302 nfs_service_start
303 ;;
304 nlockmgr)
305 if service_is_defined "$nfs_lock_service" ; then
306 service "$nfs_lock_service" start
307 else
308 service "$nfs_service" start
309 fi
310 ;;
311 mountd)
312 service_or_manual_start "$_rpc_service" "$nfs_mountd_service"
313 ;;
314 rquotad)
315 service_or_manual_start "$_rpc_service" "$nfs_rquotad_service"
316 ;;
317 status)
318 service_or_manual_start "$_rpc_service" "$nfs_status_service"
319 ;;
320 *)
321 usage
322 ;;
323 esac
324 }
325
326 ##################################################
327 # service init startup and final shutdown
328
329 nfs_shutdown()
330 {
331 nfs_service_stop
332 }
333
334 nfs_startup()
335 {
336 nfs_service_stop || true
337 nfs_service_start
338 _f="${PROCFS_PATH}/sys/net/ipv4/tcp_tw_recycle"
339 if [ -f "$_f" ]; then
340 echo 1 >"$_f"
341 fi
342 }
343
344 ##################################################
345 # monitor-post support
346
347 nfs_check_thread_count()
348 {
349 # Load NFS configuration to get desired number of threads.
350 nfs_load_config
351
352 # If $RPCNFSDCOUNT/$USE_KERNEL_NFSD_NUMBER isn't set then we could
353 # guess the default from the initscript. However, let's just
354 # assume that those using the default don't care about the number
355 # of threads and that they have switched on this feature in error.
356 _configured_threads="${RPCNFSDCOUNT:-${USE_KERNEL_NFSD_NUMBER}}"
357 if [ -z "$_configured_threads" ] && type nfsconf >/dev/null 2>&1; then
358 _configured_threads=$(nfsconf --get nfsd threads) || true
359 fi
360 [ -n "$_configured_threads" ] || return 0
361
362 _threads_file="${PROCFS_PATH}/fs/nfsd/threads"
363
364 # nfsd should be running the configured number of threads. If
365 # there are a different number of threads then tell nfsd the
366 # correct number.
367 read -r _running_threads <"$_threads_file" || {
368 echo "WARNING: Reading \"${_threads_file}\" unexpectedly failed"
369 exit 0
370 }
371
372 # Intentionally not arithmetic comparison - avoids extra errors
373 # when above read fails in an unexpected way...
374 if [ "$_running_threads" != "$_configured_threads" ]; then
375 echo "Attempting to correct number of nfsd threads from ${_running_threads} to ${_configured_threads}"
376 echo "$_configured_threads" >"$_threads_file"
377 fi
378 }
379
380 ##################################################
381 # list share directories
382
383 nfs_monitor_list_shares()
384 {
385 _cache_file="${CTDB_NFS_CALLOUT_STATE_DIR}/list_shares_cache"
386 # -nt operator is well supported in Linux: dash, bash, ksh, ...
387 # shellcheck disable=SC2039,SC3013
388 if [ ! -r "$nfs_exports_file" ] || [ ! -r "$_cache_file" ] ||
389 [ "$nfs_exports_file" -nt "$_cache_file" ]; then
390 mkdir -p "$CTDB_NFS_CALLOUT_STATE_DIR"
391 # We could just use the contents of $nfs_exports_file.
392 # However, let's regard that file as internal to NFS and use
393 # exportfs, which is the public API.
394 if ! _exports=$(exportfs -v); then
395 echo "WARNING: failed to run exportfs to list NFS shares" >&2
396 return
397 fi
398
399 echo "$_exports" |
400 grep '^/' |
401 sed -e 's@[[:space:]][[:space:]]*[^[:space:]()][^[:space:]()]*([^[:space:]()][^[:space:]()]*)$@@' |
402 sort -u >"$_cache_file"
403 fi
404
405 cat "$_cache_file"
406 }
407
408 ##################################################
409
410 nfs_register()
411 {
412 cat <<EOF
413 shutdown
414 startup
415 stop
416 start
417 monitor-list-shares
418 monitor-post
419 EOF
420 }
421
422 ##################################################
423
424 case "$1" in
425 shutdown)
426 nfs_shutdown
427 ;;
428 startup)
429 nfs_startup
430 ;;
431 stop)
432 service_stop "$2"
433 ;;
434 start)
435 service_start "$2"
436 ;;
437 monitor-list-shares)
438 nfs_monitor_list_shares
439 ;;
440 monitor-post)
441 nfs_check_thread_count
442 ;;
443 register)
444 nfs_register
445 ;;
446 monitor-pre | releaseip | takeip | startipreallocate)
447 # Not required/implemented
448 :
449 ;;
450 *)
451 usage
452 ;;
453 esac