ctdb-test: Modify ctdb_io_test test_setup to provide queue reference
[vlendec/samba-autobuild/.git] / ctdb / config / nfs-linux-kernel-callout
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 #
10 # To change the file, edit the default value below.  Do not set
11 # CTDB_NFS_EXPORTS_FILE - it isn't a configuration variable, just a
12 # hook for testing.
13 nfs_exports_file="${CTDB_NFS_EXPORTS_FILE:-/var/lib/nfs/etab}"
14
15 # As above, edit the default value below.  CTDB_NFS_DISTRO_STYLE is a
16 # test variable only.
17 nfs_distro_style="${CTDB_NFS_DISTRO_STYLE:-systemd-redhat}"
18
19 case "$nfs_distro_style" in
20 systemd-*)
21         # Defaults
22         nfs_service="nfs-server"
23         nfs_lock_service="rpc-statd"
24         nfs_mountd_service="nfs-mountd"
25         nfs_status_service="rpc-statd"
26         nfs_rquotad_service="rpc-rquotad"
27         nfs_config="/etc/sysconfig/nfs"
28         nfs_rquotad_config="" # Not use with systemd, restart via service
29
30         case "$nfs_distro_style" in
31         *-redhat|*-suse)
32                 : # Defaults only
33                 ;;
34         *-debian)
35                 nfs_rquotad_service="quotarpc"
36                 ;;
37         *)
38                 echo "Internal error"
39                 exit 1
40         esac
41         ;;
42
43 sysvinit-*)
44         # Defaults
45         nfs_service="nfs"
46         nfs_lock_service=""
47         nfs_mountd_service=""
48         nfs_status_service=""
49         nfs_rquotad_service=""
50         nfs_config="/etc/sysconfig/nfs"
51         nfs_rquotad_config="$nfs_config"
52
53         case "$nfs_distro_style" in
54         *-redhat)
55                 nfs_lock_service="nfslock"
56                 ;;
57         *-suse)
58                 nfs_service="nfsserver"
59                 ;;
60         *-debian)
61                 nfs_service="nfs-kernel-server"
62                 nfs_config="/etc/default/nfs-kernel-server"
63                 nfs_rquotad_config="/etc/default/quota"
64                 ;;
65         *)
66                 echo "Internal error"
67                 exit 1
68         esac
69         ;;
70
71 *)
72         echo "Internal error"
73         exit 1
74 esac
75
76 # Override for unit testing
77 if [ -z "$PROCFS_PATH" ] ; then
78     PROCFS_PATH="/proc"
79 fi
80
81 ##################################################
82
83 usage ()
84 {
85     _c=$(basename "$0")
86     cat <<EOF
87 usage: $_c { shutdown | startup }
88        $_c { stop | start } { nfs | nlockmgr }
89        $_c { monitor-list-shares | monitor-post }
90        $_c { register }
91 EOF
92     exit 1
93 }
94
95 ##################################################
96
97 nfs_load_config ()
98 {
99         _config="${1:-${nfs_config}}"
100
101         if [ -r "$_config" ] ; then
102                 . "$_config"
103         fi
104 }
105
106 ##################################################
107 # Basic service stop and start
108
109 basic_stop ()
110 {
111         case "$1" in
112         nfs)
113                 if [ -n "$nfs_rquotad_service" ] ; then
114                         service "$nfs_rquotad_service" stop
115                 fi
116
117                 service "$nfs_service" stop
118
119                 if [ -n "$nfs_lock_service" ] ; then
120                         service "$nfs_lock_service" stop
121                 fi
122                 ;;
123         nfslock)
124                 if [ -n "$nfs_lock_service" ] ; then
125                         service "$nfs_lock_service" stop
126                 else
127                         service "$nfs_service" stop
128                 fi
129                 ;;
130         *)
131                 usage
132         esac
133 }
134
135 basic_start ()
136 {
137         case "$1" in
138         nfs)
139                 if [ -n "$nfs_lock_service" ] ; then
140                         service "$nfs_lock_service" start
141                 fi
142
143                 service "$nfs_service" start
144
145                 if [ -n "$nfs_rquotad_service" ] ; then
146                         service "$nfs_rquotad_service" start
147                 fi
148                 ;;
149         nfslock)
150                 if [ -n "$nfs_lock_service" ] ; then
151                         service "$nfs_lock_service" start
152                 else
153                         service "$nfs_service" start
154                 fi
155                 ;;
156         *)
157                 usage
158         esac
159 }
160
161 ##################################################
162 # service "stop" and "start" options for restarting
163
164 service_stop ()
165 {
166         case "$1" in
167         nfs)
168                 echo 0 >"${PROCFS_PATH}/fs/nfsd/threads"
169                 basic_stop "nfs" >/dev/null 2>&1 || true
170                 pkill -9 nfsd
171                 ;;
172         nlockmgr)
173                 basic_stop "nfslock" >/dev/null 2>&1 || true
174                 ;;
175         mountd)
176                 if [ -n "$nfs_mountd_service" ] ; then
177                         service "$nfs_mountd_service" stop
178                         return
179                 fi
180
181                 # Default to stopping by hand
182                 killall -q -9 rpc.mountd
183                 ;;
184         rquotad)
185                 if [ -n "$nfs_rquotad_service" ] ; then
186                         service "$nfs_rquotad_service" stop
187                         return
188                 fi
189
190                 # Default to stopping by hand
191                 killall -q -9 rpc.rquotad
192                 ;;
193         status)
194                 if [ -n "$nfs_status_service" ] ; then
195                         service "$nfs_status_service" stop
196                         return
197                 fi
198
199                 # Default to stopping by hand
200                 killall -q -9 rpc.statd
201                 ;;
202         *)
203                 usage
204         esac
205 }
206
207 service_start ()
208 {
209         case "$1" in
210         nfs)
211                 basic_start "nfs"
212                 ;;
213         nlockmgr)
214                 basic_start "nfslock"
215                 ;;
216         mountd)
217                 if [ -n "$nfs_mountd_service" ] ; then
218                         service "$nfs_mountd_service" start
219                         return
220                 fi
221
222                 # Default to starting by hand
223                 nfs_load_config
224                 if [ -z "$RPCMOUNTDOPTS" ] ; then
225                         RPCMOUNTDOPTS="${MOUNTD_PORT:+-p }$MOUNTD_PORT"
226                 fi
227                 # shellcheck disable=SC2086
228                 rpc.mountd $RPCMOUNTDOPTS
229                 ;;
230         rquotad)
231                 if [ -n "$nfs_rquotad_service" ] ; then
232                         service "$nfs_rquotad_service" start
233                         return
234                 fi
235
236                 # Default to starting by hand
237                 nfs_load_config "$nfs_rquotad_config"
238                 if [ -z "$RPCRQUOTADOPTS" ] ; then
239                         RPCRQUOTADOPTS="${RQUOTAD_PORT:+-p }$RQUOTAD_PORT"
240                 fi
241                 # shellcheck disable=SC2086
242                 rpc.rquotad $RPCRQUOTADOPTS
243                 ;;
244         status)
245                 if [ -n "$nfs_status_service" ] ; then
246                         service "$nfs_status_service" start
247                         return
248                 fi
249
250                 # Default to starting by hand
251                 nfs_load_config
252                 # Red Hat uses STATDARG, Debian uses STATDOPTS
253                 opts="${STATDARG:-${STATDOPTS:-''}}"
254                 if [ -z "$opts" ] ; then
255                         # shellcheck disable=SC2086
256                         set -- \
257                             ${STATD_HA_CALLOUT:+-H} $STATD_HA_CALLOUT \
258                             ${STATD_HOSTNAME:+-n} $STATD_HOSTNAME \
259                             ${STATD_PORT:+-p} $STATD_PORT \
260                             ${STATD_OUTGOING_PORT:+-o} $STATD_OUTGOING_PORT
261                         opts="$*"
262                 fi
263                 # shellcheck disable=SC2086
264                 rpc.statd $opts
265                 ;;
266         *)
267                 usage
268         esac
269 }
270
271 ##################################################
272 # service init startup and final shutdown
273
274 nfs_shutdown ()
275 {
276     basic_stop "nfs"
277 }
278
279 nfs_startup ()
280 {
281     basic_stop "nfs" || true
282     basic_start "nfs"
283     _f="${PROCFS_PATH}/sys/net/ipv4/tcp_tw_recycle"
284     if [ "$_f" ] ; then
285         echo 1 >"$_f"
286     fi
287 }
288
289 ##################################################
290 # monitor-post support
291
292 nfs_check_thread_count ()
293 {
294     # Load NFS configuration to get desired number of threads.
295     nfs_load_config
296
297     # If $RPCNFSDCOUNT/$USE_KERNEL_NFSD_NUMBER isn't set then we could
298     # guess the default from the initscript.  However, let's just
299     # assume that those using the default don't care about the number
300     # of threads and that they have switched on this feature in error.
301     _configured_threads="${RPCNFSDCOUNT:-${USE_KERNEL_NFSD_NUMBER}}"
302     [ -n "$_configured_threads" ] || return 0
303
304     _threads_file="${PROCFS_PATH}/fs/nfsd/threads"
305
306     # nfsd should be running the configured number of threads.  If
307     # there are a different number of threads then tell nfsd the
308     # correct number.
309     read _running_threads <"$_threads_file" || {
310             echo "WARNING: Reading \"${_threads_file}\" unexpectedly failed"
311             exit 0
312     }
313
314     # Intentionally not arithmetic comparison - avoids extra errors
315     # when above read fails in an unexpected way...
316     if [ "$_running_threads" != "0" ] && \
317                [ "$_running_threads" != "$_configured_threads" ] ; then
318         printf 'Attempting to correct number of nfsd threads from %s to %s\n' \
319                "$_running_threads" "$_configured_threads"
320         echo "$_configured_threads" >"$_threads_file"
321     fi
322 }
323
324 ##################################################
325 # list share directories
326
327 nfs_monitor_list_shares ()
328 {
329     _cache_file="${CTDB_NFS_CALLOUT_STATE_DIR}/list_shares_cache"
330     # -nt operator is well supported in Linux: dash, bash, ksh, ...
331     # shellcheck disable=SC2039
332     if  [ ! -r "$nfs_exports_file" ] || [ ! -r "$_cache_file" ] || \
333             [ "$nfs_exports_file" -nt "$_cache_file" ] ; then
334         mkdir -p "$CTDB_NFS_CALLOUT_STATE_DIR"
335         # We could just use the contents of $nfs_exports_file.
336         # However, let's regard that file as internal to NFS and use
337         # exportfs, which is the public API.
338         if ! _exports=$(exportfs -v) ; then
339             echo "WARNING: failed to run exportfs to list NFS shares" >&2
340             return
341         fi
342
343         echo "$_exports" |
344             grep '^/' |
345             sed -e 's@[[:space:]][[:space:]]*[^[:space:]()][^[:space:]()]*([^[:space:]()][^[:space:]()]*)$@@' |
346             sort -u >"$_cache_file"
347     fi
348
349     cat "$_cache_file"
350 }
351
352 ##################################################
353
354 nfs_register ()
355 {
356     cat <<EOF
357 shutdown
358 startup
359 stop
360 start
361 monitor-list-shares
362 monitor-post
363 EOF
364 }
365
366 ##################################################
367
368 case "$1" in
369     shutdown)
370         nfs_shutdown
371         ;;
372     startup)
373         nfs_startup
374         ;;
375     stop)
376         service_stop "$2"
377         ;;
378     start)
379         service_start "$2"
380         ;;
381     monitor-list-shares)
382         nfs_monitor_list_shares
383         ;;
384     monitor-post)
385         nfs_check_thread_count
386         ;;
387     register)
388         nfs_register
389         ;;
390     monitor-pre|releaseip|takeip|releaseip-pre|takeip-pre)
391         # Not required/implemented
392         :
393         ;;
394     *)
395         usage
396 esac