tools/ctdb: "ctdb runstate" now accepts optional expected run state arguments
[obnox/ctdb.git] / config / ctdb.init
1 #!/bin/sh
2 #
3 ##############################
4 # ctdb:                        Starts the clustered tdb daemon
5 #
6 # chkconfig:           - 90 01
7 #
8 # description:                 Starts and stops the clustered tdb daemon
9 # pidfile:             /var/run/ctdb/ctdbd.pid
10 #
11
12 ### BEGIN INIT INFO
13 # Provides:            ctdb
14 # Required-Start:      $network
15 # Required-Stop:       $network
16 # Default-Stop:
17 # Default-Start:       3 5
18 # Short-Description:   start and stop ctdb service
19 # Description:         initscript for the ctdb service
20 ### END INIT INFO
21
22 # Source function library.
23 if [ -f /etc/init.d/functions ] ; then
24     . /etc/init.d/functions
25 elif [ -f /etc/rc.d/init.d/functions ] ; then
26     . /etc/rc.d/init.d/functions
27 fi
28
29 [ -f /etc/rc.status ] && {
30     . /etc/rc.status
31     rc_reset
32     LC_ALL=en_US.UTF-8
33 }
34
35 if [ -f /lib/lsb/init-functions ] ; then
36     . /lib/lsb/init-functions
37 fi
38
39 # Avoid using root's TMPDIR
40 unset TMPDIR
41
42 [ -z "$CTDB_BASE" ] && {
43     export CTDB_BASE="/etc/ctdb"
44 }
45
46 . $CTDB_BASE/functions
47 loadconfig network
48 loadconfig ctdb
49
50 # check networking is up (for redhat)
51 [ "$NETWORKING" = "no" ] && exit 0
52
53 detect_init_style
54 export CTDB_INIT_STYLE
55
56 ctdbd=${CTDBD:-/usr/sbin/ctdbd}
57 pidfile="/var/run/ctdb/ctdbd.pid"
58
59 if [ "$CTDB_VALGRIND" = "yes" ]; then
60     init_style="valgrind"
61 else
62     init_style="$CTDB_INIT_STYLE"
63 fi
64
65 build_ctdb_options () {
66
67     maybe_set () {
68         # If the 2nd arg is null then return - don't set anything.
69         # Else if the 3rd arg is set and it doesn't match the 2nd arg
70         # then return
71         [ -z "$2" -o \( -n "$3" -a "$3" != "$2" \) ] && return
72
73         val="'$2'"
74         case "$1" in
75             --*) sep="=" ;;
76             -*)  sep=" " ;;
77         esac
78         # For these options we're only passing a value-less flag.
79         [ -n "$3" ] && {
80             val=""
81             sep=""
82         }
83
84         CTDB_OPTIONS="${CTDB_OPTIONS}${CTDB_OPTIONS:+ }${1}${sep}${val}"
85     }
86
87     [ -z "$CTDB_RECOVERY_LOCK" ] && {
88         echo "No recovery lock specified. Starting CTDB without split brain prevention"
89     }
90     maybe_set "--reclock"                "$CTDB_RECOVERY_LOCK"
91
92     mkdir -p $(dirname "$pidfile")
93     maybe_set "--pidfile"                "$pidfile"
94
95     # build up CTDB_OPTIONS variable from optional parameters
96     maybe_set "--logfile"                "$CTDB_LOGFILE"
97     maybe_set "--nlist"                  "$CTDB_NODES"
98     maybe_set "--socket"                 "$CTDB_SOCKET"
99     maybe_set "--public-addresses"       "$CTDB_PUBLIC_ADDRESSES"
100     maybe_set "--public-interface"       "$CTDB_PUBLIC_INTERFACE"
101     maybe_set "--dbdir"                  "$CTDB_DBDIR"
102     maybe_set "--dbdir-persistent"       "$CTDB_DBDIR_PERSISTENT"
103     maybe_set "--event-script-dir"       "$CTDB_EVENT_SCRIPT_DIR"
104     maybe_set "--transport"              "$CTDB_TRANSPORT"
105     maybe_set "-d"                       "$CTDB_DEBUGLEVEL"
106     maybe_set "--notification-script"    "$CTDB_NOTIFY_SCRIPT"
107     maybe_set "--start-as-disabled"      "$CTDB_START_AS_DISABLED"    "yes"
108     maybe_set "--start-as-stopped "      "$CTDB_START_AS_STOPPED"     "yes"
109     maybe_set "--no-recmaster"           "$CTDB_CAPABILITY_RECMASTER" "no"
110     maybe_set "--no-lmaster"             "$CTDB_CAPABILITY_LMASTER"   "no"
111     maybe_set "--lvs --single-public-ip" "$CTDB_LVS_PUBLIC_IP"
112     maybe_set "--script-log-level"       "$CTDB_SCRIPT_LOG_LEVEL"
113     maybe_set "--log-ringbuf-size"       "$CTDB_LOG_RINGBUF_SIZE"
114     maybe_set "--syslog"                 "$CTDB_SYSLOG"               "yes"
115     maybe_set "--max-persistent-check-errors" "$CTDB_MAX_PERSISTENT_CHECK_ERRORS"
116 }
117
118 export_debug_variables ()
119 {
120     export CTDB_DEBUG_HUNG_SCRIPT CTDB_EXTERNAL_TRACE
121 }
122
123 # Log given message or stdin to either syslog or a CTDB log file
124 do_log ()
125 {
126     script_log "ctdb.init" "$@"
127 }
128
129 select_tdb_checker ()
130 {
131     # Find the best TDB consistency check available.
132     use_tdb_tool_check=false
133     if which tdbtool >/dev/null 2>&1 && \
134         echo "help" | tdbtool | grep -q check ; then
135
136         use_tdb_tool_check=true
137     elif which tdbtool >/dev/null 2>&1 && which tdbdump >/dev/null 2>&1 ; then
138             do_log <<EOF
139 WARNING: The installed 'tdbtool' does not offer the 'check' subcommand.
140  Using 'tdbdump' for database checks.
141  Consider updating 'tdbtool' for better checks!
142 EOF
143     elif which tdbdump >/dev/null 2>&1 ; then
144         do_log <<EOF
145 WARNING: 'tdbtool' is not available.
146  Using 'tdbdump' to check the databases.
147  Consider installing a recent 'tdbtool' for better checks!
148 EOF
149     else
150         do_log <<EOF
151 WARNING: Cannot check databases since neither
152  'tdbdump' nor 'tdbtool check' is available.
153  Consider installing tdbtool or at least tdbdump!
154 EOF
155         return 1
156     fi
157 }
158
159 check_tdb ()
160 {
161     _db="$1"
162
163     if $use_tdb_tool_check ; then
164         # tdbtool always exits with 0  :-(
165         if tdbtool "$_db" check 2>/dev/null |
166             grep -q "Database integrity is OK" ; then
167             return 0
168         else
169             return 1
170         fi
171     else
172         tdbdump "$_db" >/dev/null 2>/dev/null
173         return $?
174     fi
175 }
176
177 check_persistent_databases ()
178 {
179     _dir="${CTDB_DBDIR_PERSISTENT:-${CTDB_DBDIR:-/var/ctdb}/persistent}"
180     mkdir -p "$_dir" 2>/dev/null
181
182     [ "${CTDB_MAX_PERSISTENT_CHECK_ERRORS:-0}" = "0" ] || return 0
183
184     for _db in $(ls "$_dir/"*.tdb.*[0-9] 2>/dev/null) ; do
185         check_tdb $_db || {
186             do_log "Persistent database $_db is corrupted! CTDB will not start."
187             return 1
188         }
189     done
190 }
191
192 check_non_persistent_databases ()
193 {
194     _dir="${CTDB_DBDIR:-/var/ctdb}"
195     mkdir -p "$_dir" 2>/dev/null
196
197     for _db in $(ls "${_dir}/"*.tdb.*[0-9] 2>/dev/null) ; do
198         check_tdb $_db || {
199             _backup="${_db}.$(date +'%Y%m%d.%H%M%S.%N').corrupt"
200             do_log <<EOF
201 WARNING: database ${_db} is corrupted.
202  Moving to backup ${_backup} for later analysis.
203 EOF
204             mv "$_db" "$_backup"
205
206             # Now remove excess backups
207             ls -td "${_db}."*".corrupt" |
208             tail -n +$((${CTDB_MAX_CORRUPT_DB_BACKUPS:-10} + 1)) |
209             xargs rm -f
210             
211         }
212     done
213 }
214
215 set_retval() {
216     return $1
217 }
218
219 wait_until_ready () {
220     _timeout="${1:-10}" # default is 10 seconds
221
222     _count=0
223     while ! ctdb runstate startup running >/dev/null 2>&1 ; do
224         if [ $_count -ge $_timeout ] ; then
225             return 1
226         fi
227         sleep 1
228         _count=$(($_count + 1))
229     done
230 }
231
232 start() {
233     echo -n $"Starting ctdbd service: "
234
235     ctdb ping >/dev/null 2>&1 && {
236         echo $"CTDB is already running"
237         return 0
238     }
239
240     # About to start new $ctdbd.  The ping above has failed and any
241     # new $ctdbd will destroy the Unix domain socket, so any processes
242     # that aren't yet completely useless soon will be...  so kill
243     # them.
244     pkill -9 -f "$ctdbd"
245
246     build_ctdb_options
247
248     export_debug_variables
249
250     # make sure we drop any ips that might still be held if previous
251     # instance of ctdb got killed with -9 or similar
252     drop_all_public_ips "ctdb.init"
253
254     if select_tdb_checker ; then
255         check_persistent_databases || return $?
256         check_non_persistent_databases
257     fi
258
259     if [ "$CTDB_SUPPRESS_COREFILE" = "yes" ]; then
260         ulimit -c 0
261     else
262         ulimit -c unlimited
263     fi
264
265     case $init_style in
266         valgrind)
267             eval valgrind -q --log-file=/var/log/ctdb_valgrind \
268                 $ctdbd --valgrinding "$CTDB_OPTIONS"
269             RETVAL=$?
270             echo
271             ;;
272         suse)
273             eval startproc $ctdbd "$CTDB_OPTIONS"
274             RETVAL=$?
275             ;;
276         redhat)
277             eval $ctdbd "$CTDB_OPTIONS"
278             RETVAL=$?
279             [ $RETVAL -eq 0 ] && touch /var/lock/subsys/ctdb || RETVAL=1
280             ;;
281         debian)
282             eval start-stop-daemon --start --quiet --background \
283                 --exec $ctdbd -- "$CTDB_OPTIONS"
284             RETVAL=$?
285             ;;
286     esac
287
288     if [ $RETVAL -eq 0 ] ; then
289         if ! wait_until_ready ; then
290             RETVAL=1
291             echo "Timed out waiting for initialisation - killing CTDB"
292             pkill -9 -f $ctdbd >/dev/null 2>&1
293         fi
294     fi
295
296     case $init_style in
297         suse)
298             set_retval $RETVAL
299             rc_status -v
300             ;;
301         redhat)
302             [ $RETVAL -eq 0 ] && success || failure
303             echo
304             ;;
305     esac
306
307     return $RETVAL
308 }
309
310 stop() {
311     echo -n $"Shutting down ctdbd service: "
312     pkill -0 -f $ctdbd || {
313         echo -n "  Warning: ctdbd not running ! "
314         case $init_style in
315             suse)
316                 rc_status -v
317                 ;;
318             redhat)
319                 echo ""
320                 ;;
321         esac
322         return 0
323     }
324     ctdb shutdown >/dev/null 2>&1
325     RETVAL=$?
326     count=0
327     while pkill -0 -f $ctdbd ; do
328         sleep 1
329         count=$(($count + 1))
330         [ $count -gt 30 ] && {
331             echo -n $"killing ctdbd "
332             pkill -9 -f $ctdbd
333             pkill -9 -f $CTDB_BASE/events.d/
334         }
335     done
336     # make sure all ips are dropped, pfkill -9 might leave them hanging around
337     drop_all_public_ips
338
339     rm -f "$pidfile"
340
341     case $init_style in
342         suse)
343             # re-set the return code to the recorded RETVAL in order
344             # to print the correct status message
345             set_retval $RETVAL
346             rc_status -v
347             ;;
348         redhat)
349             [ $RETVAL -eq 0 ] && success || failure
350             [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/ctdb
351             echo ""
352             ;;
353     esac
354
355     return $RETVAL
356 }
357
358 restart() {
359     stop
360     start
361 }
362
363 # Given that CTDB_VALGRIND is a debug option we don't support the pid
364 # file.  We just do a quick and dirty hack instead.  Otherwise we just
365 # end up re-implementing each distro's pidfile support...
366 check_status_valgrind ()
367 {
368     if pkill -0 -f "valgrind.*${ctdbd}" ; then
369         echo "ctdbd is running under valgrind..."
370         return 0
371     else
372         echo "ctdbd is not running"
373         return 1
374     fi
375 }
376
377 check_status ()
378 {
379     # Backward compatibility.  When we arrange to pass --pidfile to
380     # ctdbd we also create the directory that will contain it.  If
381     # that directory is missing then we don't use the pidfile to check
382     # status.
383     if [ -d $(dirname "$pidfile") ] ; then
384         _pf_opt="-p $pidfile"
385     else
386         _pf_opt=""
387     fi
388
389     case "$init_style" in
390         valgrind)
391             check_status_valgrind
392             ;;
393         suse)
394             checkproc $_pf_opt "$ctdbd"
395             rc_status -v
396             ;;
397         redhat)
398             status $_pf_opt -l "ctdb" "$ctdbd"
399             ;;
400         debian)
401             status_of_proc $_pf_opt "$ctdbd" "ctdb"
402             ;;
403     esac
404 }
405
406
407 [ -x "$CTDB_BASE/rc.ctdb" ] && "$CTDB_BASE/rc.ctdb" $1
408
409 case "$1" in
410     start)
411         start
412         ;;
413     stop)
414         stop
415         ;;
416     restart|reload|force-reload)
417         restart
418         ;;
419     status)
420         check_status
421         ;;
422     condrestart|try-restart)
423         if check_status >/dev/null ; then
424             restart
425         fi
426         ;;
427     cron)
428         # used from cron to auto-restart ctdb
429         check_status >/dev/null || restart
430         ;;
431     *)
432         echo $"Usage: $0 {start|stop|restart|reload|force-reload|status|cron|condrestart|try-restart}"
433         exit 1
434 esac
435
436 exit $?