ctdb-scripts: Add support for CTDB_DBDIR in tmpfs
[samba.git] / ctdb / config / ctdbd_wrapper
1 #!/bin/sh
2
3 # ctdbd wrapper - start or stop CTDB
4
5 usage ()
6 {
7     echo "usage: ctdbd_wrapper <pidfile> { start | stop }"
8     exit 1
9 }
10
11 [ $# -eq 2 ] || usage
12
13 pidfile="$1"
14 action="$2"
15
16 ############################################################
17
18 if [ -z "$CTDB_BASE" ] ; then
19     export CTDB_BASE="/usr/local/etc/ctdb"
20 fi
21
22 . "${CTDB_BASE}/functions"
23 loadconfig "ctdb"
24
25 [ -n "$CTDB_SOCKET" ] && export CTDB_SOCKET
26
27 ctdbd="${CTDBD:-/usr/local/sbin/ctdbd}"
28 ctdb_rundir="/usr/local/var/run/ctdb"
29
30 ############################################################
31
32 # ctdbd_is_running()
33
34 # 1. Check if ctdbd is running.
35 #    - If the PID file is being used then, if the PID file is present,
36 #      ctdbd is only considered to running if the PID in the file is
37 #      active.
38 #    - If the PID file is not being used (i.e. we're upgrading from a
39 #      version that doesn't support it) then the presence of any ctdbd
40 #      processes is enough proof.
41
42 # 2. Print a comma-separated list of PIDs that can be
43 #    used with "pkill -s".
44 #    - If the PID file is being used then this is just the PID in that
45 #      file.  This also happens to be the session ID, so can be used
46 #      to kill all CTDB processes.
47 #    - If the PID file is not being used (i.e. upgrading) then this is
48 #      just any ctdbd processes that are running.  Hopefully one of
49 #      them is the session ID so that it can be used to kill all CTDB
50 #      processes.
51
52 # Combining these 2 checks is an optimisation to avoid potentially
53 # running too many pgrep/pkill processes on an already loaded system.
54 # Trawling through /proc/ can be very expensive.
55
56 ctdbd_is_running ()
57 {
58     # If the directory for the PID file exists then respect the
59     # existence of a PID file.
60     _pidfile_dir=$(dirname "$pidfile")
61     if [ -d "$_pidfile_dir" ] ; then
62         if read _pid 2>/dev/null <"$pidfile" ; then
63             echo "$_pid"
64
65             # Return value of kill is used
66             kill -0 $_pid 2>/dev/null
67         else
68             # Missing/empty PID file
69             return 1
70         fi
71     else
72         if _pid=$(pgrep -f "${ctdbd}\>") ; then
73             echo $_pid | sed -e 's@ @,@g'
74             return 0
75         else
76             return 1
77         fi
78     fi
79 }
80
81 ############################################################
82
83 # Mount given database directories on tmpfs
84 dbdir_tmpfs_start ()
85 {
86     for _var ; do
87         # $_var is the name of the configuration varable, so get the
88         # value
89         eval _val="\$${_var}"
90
91         case "$_val" in
92             tmpfs|tmpfs:*)
93                 _opts_defaults="mode=700"
94                 # Get any extra options specified after colon
95                 if [ "$_val" = "tmpfs" ] ; then
96                     _opts=""
97                 else
98                     _opts="${_val#tmpfs:}"
99                 fi
100                 # It is OK to repeat options - last value wins
101                 _opts_all="${_opts_defaults}${_opts:+,}${_opts}"
102
103                 # Last component of mountpoint is variable name
104                 _mnt="${ctdb_rundir}/${_var}"
105                 mkdir -p "$_mnt" || exit $?
106
107                 # If already mounted then remount, otherwise mount
108                 if findmnt -t tmpfs "$_mnt" >/dev/null ; then
109                     mount -t tmpfs -o "remount,$_opts_all" none "$_mnt" || \
110                         exit $?
111                 else
112                     mount -t tmpfs -o "$_opts_all" none "$_mnt" || exit $?
113                 fi
114
115                 # Replace specified value with mountpoint, to be
116                 # passed to ctdbd
117                 eval "${_var}=${_mnt}"
118                 ;;
119         esac
120     done
121 }
122
123 # Unmount database tmpfs directories on exit
124 dbdir_tmpfs_stop ()
125 {
126     for _var ; do
127         eval _val="\$${_var}"
128
129         case "$_val" in
130             tmpfs|tmpfs:*)
131                 _mnt="${ctdb_rundir}/${_var}"
132                 if [ -d "$_mnt" ] && findmnt -t tmpfs "$_mnt" >/dev/null ; then
133                     umount "$_mnt"
134                 fi
135                 ;;
136         esac
137     done
138 }
139
140 build_ctdb_options ()
141 {
142     ctdb_options=""
143
144     maybe_set ()
145     {
146         # If the given variable isn't set then do nothing
147         [ -n "$2" ] || return
148         # If a required value for the variable and it doesn't match,
149         # then do nothing
150         [ -z "$3" -o "$3" = "$2" ] || return
151
152         val="'$2'"
153         case "$1" in
154             --*) sep="=" ;;
155             -*)  sep=" " ;;
156         esac
157         # For these options we're only passing a value-less flag.
158         if [ -n "$3" ] ; then
159             val=""
160             sep=""
161         fi
162
163         ctdb_options="${ctdb_options}${ctdb_options:+ }${1}${sep}${val}"
164     }
165
166     if [ -z "$CTDB_RECOVERY_LOCK" ] ; then
167         echo "No recovery lock specified. Starting CTDB without split brain prevention."
168     fi
169     maybe_set "--reclock"                "$CTDB_RECOVERY_LOCK"
170
171     maybe_set "--pidfile"                "$pidfile"
172
173     # build up ctdb_options variable from optional parameters
174     maybe_set "--logging"                "$CTDB_LOGGING"
175     maybe_set "--nlist"                  "$CTDB_NODES"
176     maybe_set "--socket"                 "$CTDB_SOCKET"
177     maybe_set "--listen"                 "$CTDB_NODE_ADDRESS"
178     maybe_set "--public-addresses"       "$CTDB_PUBLIC_ADDRESSES"
179     maybe_set "--public-interface"       "$CTDB_PUBLIC_INTERFACE"
180     maybe_set "--dbdir"                  "$CTDB_DBDIR"
181     maybe_set "--dbdir-persistent"       "$CTDB_DBDIR_PERSISTENT"
182     maybe_set "--dbdir-state"            "$CTDB_DBDIR_STATE"
183     maybe_set "--event-script-dir"       "$CTDB_EVENT_SCRIPT_DIR"
184     maybe_set "--transport"              "$CTDB_TRANSPORT"
185     maybe_set "-d"                       "$CTDB_DEBUGLEVEL"
186     maybe_set "--notification-script"    "$CTDB_NOTIFY_SCRIPT"
187     maybe_set "--start-as-disabled"      "$CTDB_START_AS_DISABLED"    "yes"
188     maybe_set "--start-as-stopped "      "$CTDB_START_AS_STOPPED"     "yes"
189     maybe_set "--no-recmaster"           "$CTDB_CAPABILITY_RECMASTER" "no"
190     maybe_set "--no-lmaster"             "$CTDB_CAPABILITY_LMASTER"   "no"
191     maybe_set "--lvs --single-public-ip" "$CTDB_LVS_PUBLIC_IP"
192     maybe_set "--script-log-level"       "$CTDB_SCRIPT_LOG_LEVEL"
193     maybe_set "--max-persistent-check-errors" "$CTDB_MAX_PERSISTENT_CHECK_ERRORS"
194 }
195
196 export_debug_variables ()
197 {
198     [ -n "$CTDB_DEBUG_HUNG_SCRIPT" ] && export CTDB_DEBUG_HUNG_SCRIPT
199     [ -n "$CTDB_EXTERNAL_TRACE" ] && export CTDB_EXTERNAL_TRACE
200     [ -n "$CTDB_DEBUG_LOCKS" ] && export CTDB_DEBUG_LOCKS
201 }
202
203 kill_ctdbd ()
204 {
205     _session="$1"
206
207     if [ -n "$_session" ] ; then
208         pkill -9 -s "$_session" 2>/dev/null
209         rm -f "$pidfile"
210     fi
211 }
212
213 ############################################################
214
215 start()
216 {
217     if _session=$(ctdbd_is_running) ; then
218         echo "CTDB is already running"
219         return 0
220     fi
221
222     # About to start new $ctdbd.  The main daemon is not running but
223     # there may still be other processes around, so do some cleanup.
224     kill_ctdbd "$_session"
225
226     dbdir_tmpfs_start CTDB_DBDIR
227
228     build_ctdb_options
229
230     export_debug_variables
231
232     if [ "$CTDB_SUPPRESS_COREFILE" = "yes" ]; then
233         ulimit -c 0
234     else
235         ulimit -c unlimited
236     fi
237
238     if [ -n "$CTDB_MAX_OPEN_FILES" ]; then
239         ulimit -n $CTDB_MAX_OPEN_FILES
240     fi
241
242     mkdir -p $(dirname "$pidfile")
243
244     if [ -n "$CTDB_VALGRIND" -a "$CTDB_VALGRIND" != "no" ] ; then
245         if [ "$CTDB_VALGRIND" = "yes" ] ; then
246             ctdbd="valgrind -q --log-file=/usr/local/var/log/ctdb_valgrind ${ctdbd}"
247         else
248             ctdbd="${CTDB_VALGRIND} ${ctdbd}"
249         fi
250         ctdb_options="${ctdb_options} --valgrinding"
251     fi
252
253     case "$CTDB_LOGGING" in
254         syslog:udp|syslog:udp-rfc5424)
255             logger -t ctdbd "CTDB is being run with ${CTDB_LOGGING}.  If nothing is logged then check your syslogd configuration"
256             ;;
257         syslog|syslog:*) : ;;
258         file:*)
259             logger -t ctdbd "CTDB is being run without syslog enabled.  Logs will be in ${CTDB_LOGGING#file:}"
260             ;;
261         *)
262             logger -t ctdbd "CTDB is being run without syslog enabled.  Logs will be in log.ctdb"
263     esac
264
265     eval "$ctdbd" "$ctdb_options" || return 1
266
267     # Wait until ctdbd has started and is ready to respond to clients.
268     _pid=""
269     _timeout="${CTDB_STARTUP_TIMEOUT:-10}"
270     _count=0
271     while [ $_count -lt $_timeout ] ; do
272         # If we don't have the PID then try to read it.
273         [ -n "$_pid" ] || read _pid 2>/dev/null <"$pidfile"
274
275         # If we got the PID but the PID file has gone or the process
276         # is no longer running then stop waiting... CTDB is dead.
277         if [ -n "$_pid" ] ; then
278             if [ ! -e "$pidfile" ] || ! kill -0 "$_pid" 2>/dev/null ; then
279                 echo "CTDB exited during initialisation - check logs."
280                 kill_ctdbd "$_pid"
281                 drop_all_public_ips >/dev/null 2>&1
282                 return 1
283             fi
284
285             if ctdb runstate first_recovery startup running >/dev/null 2>&1 ; then
286                 return 0
287             fi
288         fi
289
290         _count=$(($_count + 1))
291         sleep 1
292     done
293
294     echo "Timed out waiting for initialisation - check logs - killing CTDB"
295     kill_ctdbd "$_pid"
296     drop_all_public_ips >/dev/null 2>&1
297     return 1
298 }
299
300 stop()
301 {
302     if ! _session=$(ctdbd_is_running) ; then
303         echo "CTDB is not running"
304         return 0
305     fi
306
307     ctdb shutdown
308
309     # Wait for remaining CTDB processes to exit...
310     _timeout=${CTDB_SHUTDOWN_TIMEOUT:-30}
311     _count=0
312     _terminated=false
313     while [ $_count -lt $_timeout ] ; do
314         if ! pkill -0 -s "$_session" 2>/dev/null ; then
315             _terminated=true
316             break
317         fi
318
319         _count=$(($_count + 1))
320         sleep 1
321     done
322
323     if ! $_terminated ; then
324         echo "Timed out waiting for CTDB to shutdown.  Killing CTDB processes."
325         kill_ctdbd "$_session"
326         drop_all_public_ips >/dev/null 2>&1
327
328         sleep 1
329
330         if pkill -0 -s "$_session" ; then
331             # If SIGKILL didn't work then things are bad...
332             echo "Failed to kill all CTDB processes.  Giving up."
333             return 1
334         fi
335     fi
336
337     dbdir_tmpfs_stop CTDB_DBDIR
338
339     return 0
340 }
341
342 ############################################################
343
344 # Allow notifications for start/stop.
345 if [ -x "$CTDB_BASE/rc.ctdb" ] ; then
346     "$CTDB_BASE/rc.ctdb" "$action"
347 fi
348
349 case "$action" in
350     start) start ;;
351     stop)  stop  ;;
352     *)
353         echo "usage: $0 {start|stop}"
354         exit 1
355 esac