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