3 # ctdbd wrapper - start or stop CTDB
7 echo "usage: ctdbd_wrapper <pidfile> { start | stop }"
16 ############################################################
18 if [ -z "$CTDB_BASE" ] ; then
19 export CTDB_BASE="/usr/local/etc/ctdb"
22 . "${CTDB_BASE}/functions"
25 [ -n "$CTDB_SOCKET" ] && export CTDB_SOCKET
27 ctdbd="${CTDBD:-/usr/local/sbin/ctdbd}"
29 ############################################################
33 # 1. Check if ctdbd is running.
34 # - If the PID file is being used then, if the PID file is present,
35 # ctdbd is only considered to running if the PID in the file is
37 # - If the PID file is not being used (i.e. we're upgrading from a
38 # version that doesn't support it) then the presence of any ctdbd
39 # processes is enough proof.
41 # 2. Print a comma-separated list of PIDs that can be
42 # used with "pkill -s".
43 # - If the PID file is being used then this is just the PID in that
44 # file. This also happens to be the session ID, so can be used
45 # to kill all CTDB processes.
46 # - If the PID file is not being used (i.e. upgrading) then this is
47 # just any ctdbd processes that are running. Hopefully one of
48 # them is the session ID so that it can be used to kill all CTDB
51 # Combining these 2 checks is an optimisation to avoid potentially
52 # running too many pgrep/pkill processes on an already loaded system.
53 # Trawling through /proc/ can be very expensive.
57 # If the directory for the PID file exists then respect the
58 # existence of a PID file.
59 _pidfile_dir=$(dirname "$pidfile")
60 if [ -d "$_pidfile_dir" ] ; then
61 if read _pid 2>/dev/null <"$pidfile" ; then
64 # Return value of kill is used
65 kill -0 $_pid 2>/dev/null
67 # Missing/empty PID file
71 if _pid=$(pgrep -f "${ctdbd}\>") ; then
72 echo $_pid | sed -e 's@ @,@g'
80 ############################################################
82 # If necessary, mount volatile database directory on tmpfs
85 if [ -z "$CTDB_DBDIR_TMPFS_OPTIONS" ] ; then
89 # Shortcut for readability
90 _opts="$CTDB_DBDIR_TMPFS_OPTIONS"
92 mkdir -p "$CTDB_DBDIR" || exit $?
94 # If already mounted then remount, otherwise mount
95 if findmnt -t tmpfs "$CTDB_DBDIR" >/dev/null ; then
96 mount -t tmpfs -o "remount,$_opts" none "$CTDB_DBDIR" || \
99 mount -t tmpfs -o "$_opts" none "$CTDB_DBDIR" || exit $?
103 # If necessary, unmount volatile database tmpfs directory on exit
106 if [ -z "$CTDB_DBDIR_TMPFS_OPTIONS" ] ; then
110 if [ -d "$CTDB_DBDIR" ] && findmnt -t tmpfs "$CTDB_DBDIR" >/dev/null ; then
115 build_ctdb_options ()
121 # If the given variable isn't set then do nothing
122 [ -n "$2" ] || return
123 # If a required value for the variable and it doesn't match,
125 [ -z "$3" -o "$3" = "$2" ] || return
132 # For these options we're only passing a value-less flag.
133 if [ -n "$3" ] ; then
138 ctdb_options="${ctdb_options}${ctdb_options:+ }${1}${sep}${val}"
141 if [ -z "$CTDB_RECOVERY_LOCK" ] ; then
142 echo "No recovery lock specified. Starting CTDB without split brain prevention."
144 maybe_set "--reclock" "$CTDB_RECOVERY_LOCK"
146 maybe_set "--pidfile" "$pidfile"
148 # build up ctdb_options variable from optional parameters
149 maybe_set "--logging" "$CTDB_LOGGING"
150 maybe_set "--nlist" "$CTDB_NODES"
151 maybe_set "--socket" "$CTDB_SOCKET"
152 maybe_set "--listen" "$CTDB_NODE_ADDRESS"
153 maybe_set "--public-addresses" "$CTDB_PUBLIC_ADDRESSES"
154 maybe_set "--public-interface" "$CTDB_PUBLIC_INTERFACE"
155 maybe_set "--dbdir" "$CTDB_DBDIR"
156 maybe_set "--dbdir-persistent" "$CTDB_DBDIR_PERSISTENT"
157 maybe_set "--dbdir-state" "$CTDB_DBDIR_STATE"
158 maybe_set "--event-script-dir" "$CTDB_EVENT_SCRIPT_DIR"
159 maybe_set "--transport" "$CTDB_TRANSPORT"
160 maybe_set "-d" "$CTDB_DEBUGLEVEL"
161 maybe_set "--notification-script" "$CTDB_NOTIFY_SCRIPT"
162 maybe_set "--start-as-disabled" "$CTDB_START_AS_DISABLED" "yes"
163 maybe_set "--start-as-stopped " "$CTDB_START_AS_STOPPED" "yes"
164 maybe_set "--no-recmaster" "$CTDB_CAPABILITY_RECMASTER" "no"
165 maybe_set "--no-lmaster" "$CTDB_CAPABILITY_LMASTER" "no"
166 maybe_set "--lvs --single-public-ip" "$CTDB_LVS_PUBLIC_IP"
167 maybe_set "--script-log-level" "$CTDB_SCRIPT_LOG_LEVEL"
168 maybe_set "--max-persistent-check-errors" "$CTDB_MAX_PERSISTENT_CHECK_ERRORS"
171 export_debug_variables ()
173 [ -n "$CTDB_DEBUG_HUNG_SCRIPT" ] && export CTDB_DEBUG_HUNG_SCRIPT
174 [ -n "$CTDB_EXTERNAL_TRACE" ] && export CTDB_EXTERNAL_TRACE
175 [ -n "$CTDB_DEBUG_LOCKS" ] && export CTDB_DEBUG_LOCKS
182 if [ -n "$_session" ] ; then
183 pkill -9 -s "$_session" 2>/dev/null
188 ############################################################
192 if _session=$(ctdbd_is_running) ; then
193 echo "CTDB is already running"
197 # About to start new $ctdbd. The main daemon is not running but
198 # there may still be other processes around, so do some cleanup.
199 kill_ctdbd "$_session"
205 export_debug_variables
207 if [ "$CTDB_SUPPRESS_COREFILE" = "yes" ]; then
213 if [ -n "$CTDB_MAX_OPEN_FILES" ]; then
214 ulimit -n $CTDB_MAX_OPEN_FILES
217 mkdir -p $(dirname "$pidfile")
219 if [ -n "$CTDB_VALGRIND" -a "$CTDB_VALGRIND" != "no" ] ; then
220 if [ "$CTDB_VALGRIND" = "yes" ] ; then
221 ctdbd="valgrind -q --log-file=/usr/local/var/log/ctdb_valgrind ${ctdbd}"
223 ctdbd="${CTDB_VALGRIND} ${ctdbd}"
225 ctdb_options="${ctdb_options} --valgrinding"
228 case "$CTDB_LOGGING" in
229 syslog:udp|syslog:udp-rfc5424)
230 logger -t ctdbd "CTDB is being run with ${CTDB_LOGGING}. If nothing is logged then check your syslogd configuration"
232 syslog|syslog:*) : ;;
234 logger -t ctdbd "CTDB is being run without syslog enabled. Logs will be in ${CTDB_LOGGING#file:}"
237 logger -t ctdbd "CTDB is being run without syslog enabled. Logs will be in log.ctdb"
240 eval "$ctdbd" "$ctdb_options" || return 1
242 # Wait until ctdbd has started and is ready to respond to clients.
244 _timeout="${CTDB_STARTUP_TIMEOUT:-10}"
246 while [ $_count -lt $_timeout ] ; do
247 # If we don't have the PID then try to read it.
248 [ -n "$_pid" ] || read _pid 2>/dev/null <"$pidfile"
250 # If we got the PID but the PID file has gone or the process
251 # is no longer running then stop waiting... CTDB is dead.
252 if [ -n "$_pid" ] ; then
253 if [ ! -e "$pidfile" ] || ! kill -0 "$_pid" 2>/dev/null ; then
254 echo "CTDB exited during initialisation - check logs."
256 drop_all_public_ips >/dev/null 2>&1
260 if ctdb runstate first_recovery startup running >/dev/null 2>&1 ; then
265 _count=$(($_count + 1))
269 echo "Timed out waiting for initialisation - check logs - killing CTDB"
271 drop_all_public_ips >/dev/null 2>&1
277 if ! _session=$(ctdbd_is_running) ; then
278 echo "CTDB is not running"
284 # Wait for remaining CTDB processes to exit...
285 _timeout=${CTDB_SHUTDOWN_TIMEOUT:-30}
288 while [ $_count -lt $_timeout ] ; do
289 if ! pkill -0 -s "$_session" 2>/dev/null ; then
294 _count=$(($_count + 1))
298 if ! $_terminated ; then
299 echo "Timed out waiting for CTDB to shutdown. Killing CTDB processes."
300 kill_ctdbd "$_session"
301 drop_all_public_ips >/dev/null 2>&1
305 if pkill -0 -s "$_session" ; then
306 # If SIGKILL didn't work then things are bad...
307 echo "Failed to kill all CTDB processes. Giving up."
317 ############################################################
319 # Allow notifications for start/stop.
320 if [ -x "$CTDB_BASE/rc.ctdb" ] ; then
321 "$CTDB_BASE/rc.ctdb" "$action"
328 echo "usage: $0 {start|stop}"