ctdb-scripts: Rename variable nfslock_service to nfs_lock_service
[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 # Red Hat
16 nfs_service="nfs"
17 nfs_lock_service="nfslock"
18 nfs_config="/etc/sysconfig/nfs"
19
20 # SUSE
21 #nfs_service="nfsserver"
22 #nfs_lock_service=""
23 #nfs_config="/etc/sysconfig/nfs"
24
25 # Debian
26 #nfs_service="nfs-kernel-server"
27 #nfs_lock_service=""
28 #nfs_config="/etc/default/nfs-kernel-server"
29
30 # Override for unit testing
31 if [ -z "$PROCFS_PATH" ] ; then
32     PROCFS_PATH="/proc"
33 fi
34
35 ##################################################
36
37 usage ()
38 {
39     _c=$(basename "$0")
40     cat <<EOF
41 usage: $_c { shutdown | startup }
42        $_c { stop | start } { nfs | nlockmgr }
43        $_c { monitor-list-shares | monitor-post }
44        $_c { register }
45 EOF
46     exit 1
47 }
48
49
50 ##################################################
51 # Basic service stop and start
52
53 basic_stop ()
54 {
55         case "$1" in
56         nfs)
57                 service "$nfs_service" stop
58                 if [ -n "$nfs_lock_service" ] ; then
59                         service "$nfs_lock_service" stop
60                 fi
61                 ;;
62         nfslock)
63                 if [ -n "$nfs_lock_service" ] ; then
64                         service "$nfs_lock_service" stop
65                 else
66                         service "$nfs_service" stop
67                 fi
68                 ;;
69         *)
70                 usage
71         esac
72 }
73
74 basic_start ()
75 {
76         case "$1" in
77         nfs)
78                 if [ -n "$nfs_lock_service" ] ; then
79                         service "$nfs_lock_service" start
80                 fi
81                 service "$nfs_service" start
82                 ;;
83         nfslock)
84                 if [ -n "$nfs_lock_service" ] ; then
85                         service "$nfs_lock_service" start
86                 else
87                         service "$nfs_service" start
88                 fi
89                 ;;
90         *)
91                 usage
92         esac
93 }
94
95 ##################################################
96 # service "stop" and "start" options for restarting
97
98 service_stop ()
99 {
100         case "$1" in
101         nfs)
102                 echo 0 >"${PROCFS_PATH}/fs/nfsd/threads"
103                 basic_stop "nfs" >/dev/null 2>&1 || true
104                 pkill -9 nfsd
105                 ;;
106         nlockmgr)
107                 basic_stop "nfslock" >/dev/null 2>&1 || true
108                 ;;
109         *)
110                 usage
111         esac
112 }
113
114 service_start ()
115 {
116         case "$1" in
117         nfs)
118                 basic_start "nfs"
119                 ;;
120         nlockmgr)
121                 basic_start "nfslock"
122                 ;;
123         *)
124                 usage
125         esac
126 }
127
128 ##################################################
129 # service init startup and final shutdown
130
131 nfs_shutdown ()
132 {
133     basic_stop "nfs"
134 }
135
136 nfs_startup ()
137 {
138     basic_stop "nfs" || true
139     basic_start "nfs"
140     _f="${PROCFS_PATH}/sys/net/ipv4/tcp_tw_recycle"
141     if [ "$_f" ] ; then
142         echo 1 >"$_f"
143     fi
144 }
145
146 ##################################################
147 # monitor-post support
148
149 nfs_check_thread_count ()
150 {
151     # Load NFS configuration to get desired number of threads.
152     if [ -r "$nfs_config" ] ; then
153         . "$nfs_config"
154     fi
155
156     # If $RPCNFSDCOUNT/$USE_KERNEL_NFSD_NUMBER isn't set then we could
157     # guess the default from the initscript.  However, let's just
158     # assume that those using the default don't care about the number
159     # of threads and that they have switched on this feature in error.
160     _configured_threads="${RPCNFSDCOUNT:-${USE_KERNEL_NFSD_NUMBER}}"
161     [ -n "$_configured_threads" ] || return 0
162
163     _threads_file="${PROCFS_PATH}/fs/nfsd/threads"
164
165     # nfsd should be running the configured number of threads.  If
166     # there are a different number of threads then tell nfsd the
167     # correct number.
168     read _running_threads <"$_threads_file" || {
169             echo "WARNING: Reading \"${_threads_file}\" unexpectedly failed"
170             exit 0
171     }
172
173     # Intentionally not arithmetic comparison - avoids extra errors
174     # when above read fails in an unexpected way...
175     if [ "$_running_threads" != "$_configured_threads" ] ; then
176         echo "Attempting to correct number of nfsd threads from ${_running_threads} to ${_configured_threads}"
177         echo "$_configured_threads" >"$_threads_file"
178     fi
179 }
180
181 ##################################################
182 # list share directories
183
184 nfs_monitor_list_shares ()
185 {
186     _cache_file="${CTDB_NFS_CALLOUT_STATE_DIR}/list_shares_cache"
187     # -nt operator is well supported in Linux: dash, bash, ksh, ...
188     # shellcheck disable=SC2039
189     if  [ ! -r "$nfs_exports_file" ] || [ ! -r "$_cache_file" ] || \
190             [ "$nfs_exports_file" -nt "$_cache_file" ] ; then
191         mkdir -p "$CTDB_NFS_CALLOUT_STATE_DIR"
192         # We could just use the contents of $nfs_exports_file.
193         # However, let's regard that file as internal to NFS and use
194         # exportfs, which is the public API.
195         if ! _exports=$(exportfs -v) ; then
196             echo "WARNING: failed to run exportfs to list NFS shares" >&2
197             return
198         fi
199
200         echo "$_exports" |
201             grep '^/' |
202             sed -e 's@[[:space:]][[:space:]]*[^[:space:]()][^[:space:]()]*([^[:space:]()][^[:space:]()]*)$@@' |
203             sort -u >"$_cache_file"
204     fi
205
206     cat "$_cache_file"
207 }
208
209 ##################################################
210
211 nfs_register ()
212 {
213     cat <<EOF
214 shutdown
215 startup
216 stop
217 start
218 monitor-list-shares
219 monitor-post
220 EOF
221 }
222
223 ##################################################
224
225 case "$1" in
226     shutdown)
227         nfs_shutdown
228         ;;
229     startup)
230         nfs_startup
231         ;;
232     stop)
233         service_stop "$2"
234         ;;
235     start)
236         service_start "$2"
237         ;;
238     monitor-list-shares)
239         nfs_monitor_list_shares
240         ;;
241     monitor-post)
242         nfs_check_thread_count
243         ;;
244     register)
245         nfs_register
246         ;;
247     monitor-pre|releaseip|takeip|releaseip-pre|takeip-pre)
248         # Not required/implemented
249         :
250         ;;
251     *)
252         usage
253 esac