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