try to restart statd everytime it fails, not just the first time
[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 . $CTDB_BASE/functions
43 loadconfig network
44 loadconfig ctdb
45
46 [ -z "$CTDB_RECOVERY_LOCK" ] && {
47     echo "No recovery lock specified. Starting CTDB without split brain prevention"
48 }
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     maybe_set "--reclock"                "$CTDB_RECOVERY_LOCK"
87
88     # build up CTDB_OPTIONS variable from optional parameters
89     maybe_set "--logfile"                "$CTDB_LOGFILE"
90     maybe_set "--nlist"                  "$CTDB_NODES"
91     maybe_set "--socket"                 "$CTDB_SOCKET"
92     maybe_set "--public-addresses"       "$CTDB_PUBLIC_ADDRESSES"
93     maybe_set "--public-interface"       "$CTDB_PUBLIC_INTERFACE"
94     maybe_set "--dbdir"                  "$CTDB_DBDIR"
95     maybe_set "--dbdir-persistent"       "$CTDB_DBDIR_PERSISTENT"
96     maybe_set "--event-script-dir"       "$CTDB_EVENT_SCRIPT_DIR"
97     maybe_set "--transport"              "$CTDB_TRANSPORT"
98     maybe_set "-d"                       "$CTDB_DEBUGLEVEL"
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 }
107
108 check_persistent_databases () {
109     PERSISTENT_DB_DIR="${CTDB_DBDIR:-/var/ctdb}/persistent"
110     mkdir -p $PERSISTENT_DB_DIR 2>/dev/null
111     for PDBASE in `ls $PERSISTENT_DB_DIR/*.tdb.[0-9] 2>/dev/null`; do
112         /usr/bin/tdbdump $PDBASE >/dev/null 2>/dev/null || {
113             echo "Persistent database $PDBASE is corrupted! CTDB will not start."
114             return 1
115         }
116     done
117 }
118
119 set_ctdb_variables () {
120     # set any tunables from the config file
121     set | grep ^CTDB_SET_ | cut -d_ -f3- | 
122     while read v; do
123         varname=`echo $v | cut -d= -f1`
124         value=`echo $v | cut -d= -f2`
125         ctdb setvar $varname $value || RETVAL=1
126     done || exit 1
127 }
128
129 set_retval() {
130     return $1
131 }
132
133 ctdbd=${CTDBD:-/usr/sbin/ctdbd}
134
135 start() {
136     echo -n $"Starting ctdbd service: "
137
138     ctdb ping >/dev/null 2>&1 && {
139         echo $"CTDB is already running"
140         return 1
141     }
142
143     build_ctdb_options
144
145     check_persistent_databases || return $?
146
147     case $init_style in
148         valgrind)
149             valgrind -q --log-file=/var/log/ctdb_valgrind \
150                 $ctdbd --nosetsched $CTDB_OPTIONS 
151             RETVAL=$?
152             echo
153             ;;
154         suse)
155             startproc $ctdbd $CTDB_OPTIONS
156             rc_status -v
157             RETVAL=$?
158             ;;
159         redhat)
160             daemon $ctdbd $CTDB_OPTIONS
161             RETVAL=$?
162             echo
163             [ $RETVAL -eq 0 ] && touch /var/lock/subsys/ctdb || RETVAL=1
164             ;;
165         ubuntu)
166             start-stop-daemon --start --quiet --background \
167                 --exec $ctdbd -- $CTDB_OPTIONS
168             RETVAL=$?
169             ;;
170     esac
171
172     sleep 1
173
174     set_ctdb_variables
175
176     return $RETVAL
177 }       
178
179 stop() {
180     echo -n $"Shutting down ctdbd service: "
181     pkill -0 -f $ctdbd || {
182         echo -n "  Warning: ctdbd not running ! "
183         case $init_style in
184             suse)
185                 rc_status -v
186                 ;;
187             redhat)
188                 echo ""
189                 ;;
190         esac
191         return 0
192     }
193     ctdb shutdown >/dev/null 2>&1
194     RETVAL=$?
195     count=0
196     while pkill -0 -f $ctdbd ; do
197         sleep 1
198         count=$(($count + 1))
199         [ $count -gt 10 ] && {
200             echo -n $"killing ctdbd "
201             pkill -9 -f $ctdbd
202             pkill -9 -f $CTDB_BASE/events.d/
203         }
204     done
205     case $init_style in
206         suse)
207             # re-set the return code to the recorded RETVAL in order
208             # to print the correct status message
209             set_retval $RETVAL
210             rc_status -v
211             ;;
212         redhat)
213             echo
214             [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/ctdb
215             echo ""
216             ;;
217     esac
218     return $RETVAL
219 }
220
221 restart() {
222     stop
223     start
224 }       
225
226 status() {
227     echo -n $"Checking for ctdbd service: "
228     ctdb ping >/dev/null 2>&1 || {
229         RETVAL=$?
230         echo -n "  ctdbd not running. "
231         case $init_style in
232             suse)
233                 set_retval $RETVAL
234                 rc_status -v
235                 ;;
236             redhat)
237                 echo ""
238                 ;;
239         esac
240         return $RETVAL
241     }
242     echo ""
243     ctdb status
244 }
245
246
247 case "$1" in
248     start)
249         start
250         ;;
251     stop)
252         stop
253         ;;
254     restart|reload)
255         restart
256         ;;
257     status)
258         status
259         ;;
260     condrestart)
261         ctdb status > /dev/null && restart || :
262         ;;
263     cron)
264         # used from cron to auto-restart ctdb
265         ctdb status > /dev/null || restart
266         ;;
267     *)
268         echo $"Usage: $0 {start|stop|restart|status|cron|condrestart}"
269         exit 1
270 esac
271
272 exit $?