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