version 1.2.200
[sahlberg/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 [ -z "$CTDB_VARDIR" ] && {
43     export CTDB_VARDIR="/var/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
58 if [ "$CTDB_VALGRIND" = "yes" ]; then
59     init_style="valgrind"
60 else
61     init_style="$CTDB_INIT_STYLE"
62 fi
63
64 build_ctdb_options () {
65
66     maybe_set () {
67         # If the 2nd arg is null then return - don't set anything.
68         # Else if the 3rd arg is set and it doesn't match the 2nd arg
69         # then return
70         [ -z "$2" -o \( -n "$3" -a "$3" != "$2" \) ] && return
71
72         val="'$2'"
73         case "$1" in
74             --*) sep="=" ;;
75             -*)  sep=" " ;;
76         esac
77         # For these options we're only passing a value-less flag.
78         [ -n "$3" ] && {
79             val=""
80             sep=""
81         }
82
83         CTDB_OPTIONS="${CTDB_OPTIONS}${CTDB_OPTIONS:+ }${1}${sep}${val}"
84     }
85
86     [ -z "$CTDB_RECOVERY_LOCK" ] && {
87         echo "No recovery lock specified. Starting CTDB without split brain prevention"
88     }
89     maybe_set "--reclock"                "$CTDB_RECOVERY_LOCK"
90
91     # build up CTDB_OPTIONS variable from optional parameters
92     maybe_set "--logfile"                "$CTDB_LOGFILE"
93     maybe_set "--nlist"                  "$CTDB_NODES"
94     maybe_set "--socket"                 "$CTDB_SOCKET"
95     maybe_set "--public-addresses"       "$CTDB_PUBLIC_ADDRESSES"
96     maybe_set "--public-interface"       "$CTDB_PUBLIC_INTERFACE"
97     maybe_set "--dbdir"                  "$CTDB_DBDIR"
98     maybe_set "--dbdir-persistent"       "$CTDB_DBDIR_PERSISTENT"
99     maybe_set "--event-script-dir"       "$CTDB_EVENT_SCRIPT_DIR"
100     maybe_set "--transport"              "$CTDB_TRANSPORT"
101     maybe_set "-d"                       "$CTDB_DEBUGLEVEL"
102     maybe_set "--notification-script"    "$CTDB_NOTIFY_SCRIPT"
103     maybe_set "--start-as-disabled"      "$CTDB_START_AS_DISABLED"    "yes"
104     maybe_set "--start-as-stopped "      "$CTDB_START_AS_STOPPED"     "yes"
105     maybe_set "--no-recmaster"           "$CTDB_CAPABILITY_RECMASTER" "no"
106     maybe_set "--no-lmaster"             "$CTDB_CAPABILITY_LMASTER"   "no"
107     maybe_set "--lvs --single-public-ip" "$CTDB_LVS_PUBLIC_IP"
108     maybe_set "--script-log-level"       "$CTDB_SCRIPT_LOG_LEVEL"
109     maybe_set "--log-ringbuf-size"       "$CTDB_LOG_RINGBUF_SIZE"
110     maybe_set "--syslog"                 "$CTDB_SYSLOG"               "yes"
111     maybe_set "--max-persistent-check-errors" "$CTDB_MAX_PERSISTENT_CHECK_ERRORS"
112 }
113
114 check_tdb () {
115         local PDBASE=$1
116
117         local TDBTOOL_HAS_CHECK=`echo "help" | /usr/bin/tdbtool | grep check | wc -l`
118
119         test x"$TDBTOOL_HAS_CHECK" = x"1" && {
120                 #
121                 # Note tdbtool always exits with 0
122                 #
123                 local OK=`/usr/bin/tdbtool $PDBASE check | grep "Database integrity is OK" | wc -l`
124                 test x"$OK" = x"1" || {
125                         return 1;
126                 }
127
128                 return 0;
129         }
130
131         /usr/bin/tdbdump $PDBASE >/dev/null 2>/dev/null || {
132                 return $?;
133         }
134
135         return 0;
136 }
137
138 check_persistent_databases () {
139     PERSISTENT_DB_DIR="${CTDB_DBDIR:-/var/ctdb}/persistent"
140     mkdir -p $PERSISTENT_DB_DIR 2>/dev/null
141     local ERRCOUNT=$CTDB_MAX_PERSISTENT_CHECK_ERRORS
142
143     test -z "$ERRCOUNT" && {
144         ERRCOUNT="0"
145     }
146     test x"$ERRCOUNT" != x"0" && {
147         return 0;
148     }
149     for PDBASE in `ls $PERSISTENT_DB_DIR/*.tdb.[0-9] 2>/dev/null`; do
150         check_tdb $PDBASE || {
151             echo "Persistent database $PDBASE is corrupted! CTDB will not start."
152             return 1
153         }
154     done
155 }
156
157 set_ctdb_variables () {
158     # set any tunables from the config file
159     set | grep ^CTDB_SET_ | cut -d_ -f3- | 
160     while read v; do
161         varname=`echo $v | cut -d= -f1`
162         value=`echo $v | cut -d= -f2`
163         ctdb setvar $varname $value || RETVAL=1
164     done || exit 1
165 }
166
167 set_retval() {
168     return $1
169 }
170
171 wait_until_ready () {
172     _timeout="${1:-10}" # default is 10 seconds
173
174     _count=0
175     while ! ctdb ping >/dev/null 2>&1 ; do
176         if [ $_count -ge $_timeout ] ; then
177             return 1
178         fi
179         sleep 1
180         _count=$(($_count + 1))
181     done
182 }
183
184 ctdbd=${CTDBD:-/usr/sbin/ctdbd}
185
186 start() {
187     echo -n $"Starting ctdbd service: "
188
189     ctdb ping >/dev/null 2>&1 && {
190         echo $"CTDB is already running"
191         return 0
192     }
193
194     build_ctdb_options
195
196     check_persistent_databases || return $?
197
198     if [ "$CTDB_SUPPRESS_COREFILE" = "yes" ]; then
199         ulimit -c 0
200     else
201         ulimit -c unlimited
202     fi
203
204     case $init_style in
205         valgrind)
206             eval valgrind -q --log-file=/var/log/ctdb_valgrind \
207                 $ctdbd --valgrinding "$CTDB_OPTIONS"
208             RETVAL=$?
209             echo
210             ;;
211         suse)
212             eval startproc $ctdbd "$CTDB_OPTIONS"
213             RETVAL=$?
214             ;;
215         redhat)
216             eval $ctdbd "$CTDB_OPTIONS"
217             RETVAL=$?
218             [ $RETVAL -eq 0 ] && touch /var/lock/subsys/ctdb || RETVAL=1
219             ;;
220         debian)
221             eval start-stop-daemon --start --quiet --background \
222                 --exec $ctdbd -- "$CTDB_OPTIONS"
223             RETVAL=$?
224             ;;
225     esac
226
227     if [ $RETVAL -eq 0 ] ; then
228         if wait_until_ready ; then
229             set_ctdb_variables
230         else
231             RETVAL=1
232             pkill -9 -f $ctdbd >/dev/null 2>&1
233         fi
234     fi
235
236     case $init_style in
237         suse)
238             set_retval $RETVAL
239             rc_status -v
240             ;;
241         redhat)
242             [ $RETVAL -eq 0 ] && success || failure
243             echo
244             ;;
245     esac
246
247     return $RETVAL
248 }
249
250 stop() {
251     echo -n $"Shutting down ctdbd service: "
252     pkill -0 -f $ctdbd || {
253         echo -n "  Warning: ctdbd not running ! "
254         case $init_style in
255             suse)
256                 rc_status -v
257                 ;;
258             redhat)
259                 echo ""
260                 ;;
261         esac
262         return 0
263     }
264     ctdb shutdown >/dev/null 2>&1
265     RETVAL=$?
266     count=0
267     while pkill -0 -f $ctdbd ; do
268         sleep 1
269         count=$(($count + 1))
270         [ $count -gt 10 ] && {
271             echo -n $"killing ctdbd "
272             pkill -9 -f $ctdbd
273             pkill -9 -f $CTDB_BASE/events.d/
274         }
275     done
276     case $init_style in
277         suse)
278             # re-set the return code to the recorded RETVAL in order
279             # to print the correct status message
280             set_retval $RETVAL
281             rc_status -v
282             ;;
283         redhat)
284             [ $RETVAL -eq 0 ] && success || failure
285             [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/ctdb
286             echo ""
287             ;;
288     esac
289     return $RETVAL
290 }
291
292 restart() {
293     stop
294     start
295 }
296
297 status() {
298     echo -n $"Checking for ctdbd service: "
299     ctdb ping >/dev/null 2>&1 || {
300         RETVAL=$?
301         echo -n "  ctdbd not running. "
302         case $init_style in
303             suse)
304                 set_retval $RETVAL
305                 rc_status -v
306                 ;;
307             redhat)
308                 if [ -f /var/lock/subsys/ctdb ]; then
309                         echo $"ctdb dead but subsys locked"
310                         RETVAL=2
311                 else
312                         echo $"ctdb is stopped"
313                         RETVAL=3
314                 fi
315                 ;;
316         esac
317         return $RETVAL
318     }
319     echo ""
320     ctdb status
321 }
322
323
324 case "$1" in
325     start)
326         start
327         ;;
328     stop)
329         stop
330         ;;
331     restart|reload|force-reload)
332         restart
333         ;;
334     status)
335         status
336         ;;
337     condrestart|try-restart)
338         ctdb status > /dev/null && restart || :
339         ;;
340     cron)
341         # used from cron to auto-restart ctdb
342         ctdb status > /dev/null || restart
343         ;;
344     *)
345         echo $"Usage: $0 {start|stop|restart|reload|force-reload|status|cron|condrestart|try-restart}"
346         exit 1
347 esac
348
349 exit $?