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