config: try to use tdbtool <tdb> check instead of tdbdump for persistent db checks
[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 "--syslog"                 "$CTDB_SYSLOG"               "yes"
106 }
107
108 check_tdb () {
109         local PDBASE=$1
110
111         local TDBTOOL_HAS_CHECK=`echo "help" | /usr/bin/tdbtool | grep check | wc -l`
112
113         test x"$TDBTOOL_HAS_CHECK" = x"1" && {
114                 #
115                 # Note tdbtool always exits with 0
116                 #
117                 local OK=`/usr/bin/tdbtool $PDBASE check | grep "Database integrity is OK" | wc -l`
118                 test x"$OK" = x"1" || {
119                         return 1;
120                 }
121
122                 return 0;
123         }
124
125         /usr/bin/tdbdump $PDBASE >/dev/null 2>/dev/null || {
126                 return $?;
127         }
128
129         return 0;
130 }
131
132 check_persistent_databases () {
133     PERSISTENT_DB_DIR="${CTDB_DBDIR:-/var/ctdb}/persistent"
134     mkdir -p $PERSISTENT_DB_DIR 2>/dev/null
135     for PDBASE in `ls $PERSISTENT_DB_DIR/*.tdb.[0-9] 2>/dev/null`; do
136         check_tdb $PDBASE || {
137             echo "Persistent database $PDBASE is corrupted! CTDB will not start."
138             return 1
139         }
140     done
141 }
142
143 set_ctdb_variables () {
144     # set any tunables from the config file
145     set | grep ^CTDB_SET_ | cut -d_ -f3- | 
146     while read v; do
147         varname=`echo $v | cut -d= -f1`
148         value=`echo $v | cut -d= -f2`
149         ctdb setvar $varname $value || RETVAL=1
150     done || exit 1
151 }
152
153 set_retval() {
154     return $1
155 }
156
157 ctdbd=${CTDBD:-/usr/sbin/ctdbd}
158
159 start() {
160     echo -n $"Starting ctdbd service: "
161
162     ctdb ping >/dev/null 2>&1 && {
163         echo $"CTDB is already running"
164         return 1
165     }
166
167     build_ctdb_options
168
169     check_persistent_databases || return $?
170
171     if [ yes == "$CTDB_SUPPRESS_COREFILE" ]; then
172         ulimit -c 0
173     else
174         ulimit -c unlimited
175     fi
176
177     case $init_style in
178         valgrind)
179             valgrind -q --log-file=/var/log/ctdb_valgrind \
180                 $ctdbd --nosetsched $CTDB_OPTIONS 
181             RETVAL=$?
182             echo
183             ;;
184         suse)
185             startproc $ctdbd $CTDB_OPTIONS
186             rc_status -v
187             RETVAL=$?
188             ;;
189         redhat)
190             daemon $ctdbd $CTDB_OPTIONS
191             RETVAL=$?
192             echo
193             [ $RETVAL -eq 0 ] && touch /var/lock/subsys/ctdb || RETVAL=1
194             ;;
195         debian)
196             start-stop-daemon --start --quiet --background \
197                 --exec $ctdbd -- $CTDB_OPTIONS
198             RETVAL=$?
199             ;;
200     esac
201
202     sleep 1
203
204     set_ctdb_variables
205
206     return $RETVAL
207 }       
208
209 stop() {
210     echo -n $"Shutting down ctdbd service: "
211     pkill -0 -f $ctdbd || {
212         echo -n "  Warning: ctdbd not running ! "
213         case $init_style in
214             suse)
215                 rc_status -v
216                 ;;
217             redhat)
218                 echo ""
219                 ;;
220         esac
221         return 0
222     }
223     ctdb shutdown >/dev/null 2>&1
224     RETVAL=$?
225     count=0
226     while pkill -0 -f $ctdbd ; do
227         sleep 1
228         count=$(($count + 1))
229         [ $count -gt 10 ] && {
230             echo -n $"killing ctdbd "
231             pkill -9 -f $ctdbd
232             pkill -9 -f $CTDB_BASE/events.d/
233         }
234     done
235     case $init_style in
236         suse)
237             # re-set the return code to the recorded RETVAL in order
238             # to print the correct status message
239             set_retval $RETVAL
240             rc_status -v
241             ;;
242         redhat)
243             [ $RETVAL -eq 0 ] && success || failure
244             [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/ctdb
245             echo ""
246             ;;
247     esac
248     return $RETVAL
249 }
250
251 restart() {
252     stop
253     start
254 }       
255
256 status() {
257     echo -n $"Checking for ctdbd service: "
258     ctdb ping >/dev/null 2>&1 || {
259         RETVAL=$?
260         echo -n "  ctdbd not running. "
261         case $init_style in
262             suse)
263                 set_retval $RETVAL
264                 rc_status -v
265                 ;;
266             redhat)
267                 echo ""
268                 ;;
269         esac
270         return $RETVAL
271     }
272     echo ""
273     ctdb status
274 }
275
276
277 case "$1" in
278     start)
279         start
280         ;;
281     stop)
282         stop
283         ;;
284     restart|reload)
285         restart
286         ;;
287     status)
288         status
289         ;;
290     condrestart)
291         ctdb status > /dev/null && restart || :
292         ;;
293     cron)
294         # used from cron to auto-restart ctdb
295         ctdb status > /dev/null || restart
296         ;;
297     *)
298         echo $"Usage: $0 {start|stop|restart|status|cron|condrestart}"
299         exit 1
300 esac
301
302 exit $?