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}"
28 ctdb_rundir
="/usr/local/var/run/ctdb"
30 ############################################################
34 # 1. Check if ctdbd is running.
35 # - If the PID file is being used then, if the PID file is present,
36 # ctdbd is only considered to running if the PID in the file is
38 # - If the PID file is not being used (i.e. we're upgrading from a
39 # version that doesn't support it) then the presence of any ctdbd
40 # processes is enough proof.
42 # 2. Print a comma-separated list of PIDs that can be
43 # used with "pkill -s".
44 # - If the PID file is being used then this is just the PID in that
45 # file. This also happens to be the session ID, so can be used
46 # to kill all CTDB processes.
47 # - If the PID file is not being used (i.e. upgrading) then this is
48 # just any ctdbd processes that are running. Hopefully one of
49 # them is the session ID so that it can be used to kill all CTDB
52 # Combining these 2 checks is an optimisation to avoid potentially
53 # running too many pgrep/pkill processes on an already loaded system.
54 # Trawling through /proc/ can be very expensive.
58 # If the directory for the PID file exists then respect the
59 # existence of a PID file.
60 _pidfile_dir
=$
(dirname "$pidfile")
61 if [ -d "$_pidfile_dir" ] ; then
62 if read _pid
2>/dev
/null
<"$pidfile" ; then
65 # Return value of kill is used
66 kill -0 $_pid 2>/dev
/null
68 # Missing/empty PID file
72 if _pid
=$
(pgrep
-f "${ctdbd}\>") ; then
73 echo $_pid |
sed -e 's@ @,@g'
81 ############################################################
83 # Mount given database directories on tmpfs
87 # $_var is the name of the configuration varable, so get the
93 _opts_defaults
="mode=700"
94 # Get any extra options specified after colon
95 if [ "$_val" = "tmpfs" ] ; then
98 _opts
="${_val#tmpfs:}"
100 # It is OK to repeat options - last value wins
101 _opts_all
="${_opts_defaults}${_opts:+,}${_opts}"
103 # Last component of mountpoint is variable name
104 _mnt
="${ctdb_rundir}/${_var}"
105 mkdir
-p "$_mnt" ||
exit $?
107 # If already mounted then remount, otherwise mount
108 if findmnt
-t tmpfs
"$_mnt" >/dev
/null
; then
109 mount
-t tmpfs
-o "remount,$_opts_all" none
"$_mnt" || \
112 mount
-t tmpfs
-o "$_opts_all" none
"$_mnt" ||
exit $?
115 # Replace specified value with mountpoint, to be
117 eval "${_var}=${_mnt}"
123 # Unmount database tmpfs directories on exit
127 eval _val
="\$${_var}"
131 _mnt
="${ctdb_rundir}/${_var}"
132 if [ -d "$_mnt" ] && findmnt
-t tmpfs
"$_mnt" >/dev
/null
; then
140 build_ctdb_options
()
146 # If the given variable isn't set then do nothing
147 [ -n "$2" ] ||
return
148 # If a required value for the variable and it doesn't match,
150 [ -z "$3" -o "$3" = "$2" ] ||
return
157 # For these options we're only passing a value-less flag.
158 if [ -n "$3" ] ; then
163 ctdb_options
="${ctdb_options}${ctdb_options:+ }${1}${sep}${val}"
166 if [ -z "$CTDB_RECOVERY_LOCK" ] ; then
167 echo "No recovery lock specified. Starting CTDB without split brain prevention."
169 maybe_set
"--reclock" "$CTDB_RECOVERY_LOCK"
171 maybe_set
"--pidfile" "$pidfile"
173 # build up ctdb_options variable from optional parameters
174 maybe_set
"--logging" "$CTDB_LOGGING"
175 maybe_set
"--nlist" "$CTDB_NODES"
176 maybe_set
"--socket" "$CTDB_SOCKET"
177 maybe_set
"--listen" "$CTDB_NODE_ADDRESS"
178 maybe_set
"--public-addresses" "$CTDB_PUBLIC_ADDRESSES"
179 maybe_set
"--public-interface" "$CTDB_PUBLIC_INTERFACE"
180 maybe_set
"--dbdir" "$CTDB_DBDIR"
181 maybe_set
"--dbdir-persistent" "$CTDB_DBDIR_PERSISTENT"
182 maybe_set
"--dbdir-state" "$CTDB_DBDIR_STATE"
183 maybe_set
"--event-script-dir" "$CTDB_EVENT_SCRIPT_DIR"
184 maybe_set
"--transport" "$CTDB_TRANSPORT"
185 maybe_set
"-d" "$CTDB_DEBUGLEVEL"
186 maybe_set
"--notification-script" "$CTDB_NOTIFY_SCRIPT"
187 maybe_set
"--start-as-disabled" "$CTDB_START_AS_DISABLED" "yes"
188 maybe_set
"--start-as-stopped " "$CTDB_START_AS_STOPPED" "yes"
189 maybe_set
"--no-recmaster" "$CTDB_CAPABILITY_RECMASTER" "no"
190 maybe_set
"--no-lmaster" "$CTDB_CAPABILITY_LMASTER" "no"
191 maybe_set
"--lvs --single-public-ip" "$CTDB_LVS_PUBLIC_IP"
192 maybe_set
"--script-log-level" "$CTDB_SCRIPT_LOG_LEVEL"
193 maybe_set
"--max-persistent-check-errors" "$CTDB_MAX_PERSISTENT_CHECK_ERRORS"
196 export_debug_variables
()
198 [ -n "$CTDB_DEBUG_HUNG_SCRIPT" ] && export CTDB_DEBUG_HUNG_SCRIPT
199 [ -n "$CTDB_EXTERNAL_TRACE" ] && export CTDB_EXTERNAL_TRACE
200 [ -n "$CTDB_DEBUG_LOCKS" ] && export CTDB_DEBUG_LOCKS
207 if [ -n "$_session" ] ; then
208 pkill
-9 -s "$_session" 2>/dev
/null
213 ############################################################
217 if _session
=$
(ctdbd_is_running
) ; then
218 echo "CTDB is already running"
222 # About to start new $ctdbd. The main daemon is not running but
223 # there may still be other processes around, so do some cleanup.
224 kill_ctdbd
"$_session"
226 dbdir_tmpfs_start CTDB_DBDIR
230 export_debug_variables
232 if [ "$CTDB_SUPPRESS_COREFILE" = "yes" ]; then
238 if [ -n "$CTDB_MAX_OPEN_FILES" ]; then
239 ulimit -n $CTDB_MAX_OPEN_FILES
242 mkdir
-p $
(dirname "$pidfile")
244 if [ -n "$CTDB_VALGRIND" -a "$CTDB_VALGRIND" != "no" ] ; then
245 if [ "$CTDB_VALGRIND" = "yes" ] ; then
246 ctdbd
="valgrind -q --log-file=/usr/local/var/log/ctdb_valgrind ${ctdbd}"
248 ctdbd
="${CTDB_VALGRIND} ${ctdbd}"
250 ctdb_options
="${ctdb_options} --valgrinding"
253 case "$CTDB_LOGGING" in
254 syslog
:udp|syslog
:udp-rfc5424
)
255 logger
-t ctdbd
"CTDB is being run with ${CTDB_LOGGING}. If nothing is logged then check your syslogd configuration"
257 syslog|syslog
:*) : ;;
259 logger
-t ctdbd
"CTDB is being run without syslog enabled. Logs will be in ${CTDB_LOGGING#file:}"
262 logger
-t ctdbd
"CTDB is being run without syslog enabled. Logs will be in log.ctdb"
265 eval "$ctdbd" "$ctdb_options" ||
return 1
267 # Wait until ctdbd has started and is ready to respond to clients.
269 _timeout
="${CTDB_STARTUP_TIMEOUT:-10}"
271 while [ $_count -lt $_timeout ] ; do
272 # If we don't have the PID then try to read it.
273 [ -n "$_pid" ] ||
read _pid
2>/dev
/null
<"$pidfile"
275 # If we got the PID but the PID file has gone or the process
276 # is no longer running then stop waiting... CTDB is dead.
277 if [ -n "$_pid" ] ; then
278 if [ ! -e "$pidfile" ] ||
! kill -0 "$_pid" 2>/dev
/null
; then
279 echo "CTDB exited during initialisation - check logs."
281 drop_all_public_ips
>/dev
/null
2>&1
285 if ctdb runstate first_recovery startup running
>/dev
/null
2>&1 ; then
290 _count
=$
(($_count + 1))
294 echo "Timed out waiting for initialisation - check logs - killing CTDB"
296 drop_all_public_ips
>/dev
/null
2>&1
302 if ! _session
=$
(ctdbd_is_running
) ; then
303 echo "CTDB is not running"
309 # Wait for remaining CTDB processes to exit...
310 _timeout
=${CTDB_SHUTDOWN_TIMEOUT:-30}
313 while [ $_count -lt $_timeout ] ; do
314 if ! pkill
-0 -s "$_session" 2>/dev
/null
; then
319 _count
=$
(($_count + 1))
323 if ! $_terminated ; then
324 echo "Timed out waiting for CTDB to shutdown. Killing CTDB processes."
325 kill_ctdbd
"$_session"
326 drop_all_public_ips
>/dev
/null
2>&1
330 if pkill
-0 -s "$_session" ; then
331 # If SIGKILL didn't work then things are bad...
332 echo "Failed to kill all CTDB processes. Giving up."
337 dbdir_tmpfs_stop CTDB_DBDIR
342 ############################################################
344 # Allow notifications for start/stop.
345 if [ -x "$CTDB_BASE/rc.ctdb" ] ; then
346 "$CTDB_BASE/rc.ctdb" "$action"
353 echo "usage: $0 {start|stop}"