2013-07-09 09:22:07 +04:00
#!/bin/sh
# ctdbd wrapper - start or stop CTDB
usage ()
{
2018-03-05 13:02:40 +03:00
echo "usage: ctdbd_wrapper { start | stop }"
2013-07-09 09:22:07 +04:00
exit 1
}
2018-03-05 13:02:40 +03:00
[ $# -eq 1 ] || usage
2013-07-09 09:22:07 +04:00
2018-03-05 13:02:40 +03:00
action="$1"
2013-07-09 09:22:07 +04:00
############################################################
2015-08-17 13:47:58 +03:00
if [ -z "$CTDB_BASE" ] ; then
export CTDB_BASE="/usr/local/etc/ctdb"
fi
2013-07-09 09:22:07 +04:00
. "${CTDB_BASE}/functions"
loadconfig "ctdb"
2015-08-17 13:47:58 +03:00
ctdbd="${CTDBD:-/usr/local/sbin/ctdbd}"
2013-07-09 09:22:07 +04:00
############################################################
2015-11-17 06:57:44 +03:00
# If necessary, mount volatile database directory on tmpfs
2015-10-23 06:04:04 +03:00
dbdir_tmpfs_start ()
{
2015-11-17 06:57:44 +03:00
if [ -z "$CTDB_DBDIR_TMPFS_OPTIONS" ] ; then
return
fi
# Shortcut for readability
_opts="$CTDB_DBDIR_TMPFS_OPTIONS"
mkdir -p "$CTDB_DBDIR" || exit $?
# If already mounted then remount, otherwise mount
if findmnt -t tmpfs "$CTDB_DBDIR" >/dev/null ; then
mount -t tmpfs -o "remount,$_opts" none "$CTDB_DBDIR" || \
exit $?
else
mount -t tmpfs -o "$_opts" none "$CTDB_DBDIR" || exit $?
fi
2015-10-23 06:04:04 +03:00
}
2015-11-17 06:57:44 +03:00
# If necessary, unmount volatile database tmpfs directory on exit
2015-10-23 06:04:04 +03:00
dbdir_tmpfs_stop ()
{
2015-11-17 06:57:44 +03:00
if [ -z "$CTDB_DBDIR_TMPFS_OPTIONS" ] ; then
return
fi
if [ -d "$CTDB_DBDIR" ] && findmnt -t tmpfs "$CTDB_DBDIR" >/dev/null ; then
umount "$CTDB_DBDIR"
fi
2015-10-23 06:04:04 +03:00
}
2016-07-06 13:25:57 +03:00
# Only the nested function references its arguments
# shellcheck disable=SC2120
2013-07-09 09:22:07 +04:00
build_ctdb_options ()
{
2013-10-15 04:54:58 +04:00
ctdb_options=""
2013-07-09 09:22:07 +04:00
maybe_set ()
{
# If the given variable isn't set then do nothing
[ -n "$2" ] || return
# If a required value for the variable and it doesn't match,
# then do nothing
[ -z "$3" -o "$3" = "$2" ] || return
val="'$2'"
case "$1" in
--*) sep="=" ;;
-*) sep=" " ;;
esac
# For these options we're only passing a value-less flag.
if [ -n "$3" ] ; then
val=""
sep=""
fi
2013-10-15 04:54:58 +04:00
ctdb_options="${ctdb_options}${ctdb_options:+ }${1}${sep}${val}"
2013-07-09 09:22:07 +04:00
}
if [ -z "$CTDB_RECOVERY_LOCK" ] ; then
2014-09-16 06:00:10 +04:00
echo "No recovery lock specified. Starting CTDB without split brain prevention."
2013-07-09 09:22:07 +04:00
fi
maybe_set "--reclock" "$CTDB_RECOVERY_LOCK"
2013-10-15 04:54:58 +04:00
# build up ctdb_options variable from optional parameters
2014-08-11 11:07:41 +04:00
maybe_set "--logging" "$CTDB_LOGGING"
2015-04-20 02:53:23 +03:00
maybe_set "--listen" "$CTDB_NODE_ADDRESS"
2013-07-09 09:22:07 +04:00
maybe_set "--dbdir" "$CTDB_DBDIR"
maybe_set "--dbdir-persistent" "$CTDB_DBDIR_PERSISTENT"
2013-10-18 09:43:26 +04:00
maybe_set "--dbdir-state" "$CTDB_DBDIR_STATE"
2013-07-09 09:22:07 +04:00
maybe_set "--transport" "$CTDB_TRANSPORT"
maybe_set "-d" "$CTDB_DEBUGLEVEL"
maybe_set "--notification-script" "$CTDB_NOTIFY_SCRIPT"
maybe_set "--start-as-disabled" "$CTDB_START_AS_DISABLED" "yes"
maybe_set "--start-as-stopped " "$CTDB_START_AS_STOPPED" "yes"
maybe_set "--no-recmaster" "$CTDB_CAPABILITY_RECMASTER" "no"
maybe_set "--no-lmaster" "$CTDB_CAPABILITY_LMASTER" "no"
2016-06-20 07:21:18 +03:00
maybe_set "--nosetsched" "$CTDB_NOSETSCHED" "yes"
2013-07-09 09:22:07 +04:00
maybe_set "--script-log-level" "$CTDB_SCRIPT_LOG_LEVEL"
maybe_set "--max-persistent-check-errors" "$CTDB_MAX_PERSISTENT_CHECK_ERRORS"
}
export_debug_variables ()
{
2014-09-16 06:33:26 +04:00
[ -n "$CTDB_DEBUG_HUNG_SCRIPT" ] && export CTDB_DEBUG_HUNG_SCRIPT
[ -n "$CTDB_DEBUG_LOCKS" ] && export CTDB_DEBUG_LOCKS
2013-07-09 09:22:07 +04:00
}
############################################################
start()
{
2015-11-17 06:57:44 +03:00
dbdir_tmpfs_start
2015-10-23 06:04:04 +03:00
2016-07-06 13:25:57 +03:00
# build_ctdb_options() takes no arguments
# shellcheck disable=SC2119
2013-07-09 09:22:07 +04:00
build_ctdb_options
export_debug_variables
2016-07-12 05:15:12 +03:00
# Explicitly trying to disable core files, no other way
# shellcheck disable=SC2039
2013-07-09 09:22:07 +04:00
if [ "$CTDB_SUPPRESS_COREFILE" = "yes" ]; then
ulimit -c 0
else
ulimit -c unlimited
fi
2016-07-12 05:15:12 +03:00
# Unsupported option easily avoided by not using configuration variable
# shellcheck disable=SC2039
2015-04-28 16:04:53 +03:00
if [ -n "$CTDB_MAX_OPEN_FILES" ]; then
2016-07-06 10:31:51 +03:00
ulimit -n "$CTDB_MAX_OPEN_FILES"
2015-04-28 16:04:53 +03:00
fi
2013-07-09 09:22:07 +04:00
if [ -n "$CTDB_VALGRIND" -a "$CTDB_VALGRIND" != "no" ] ; then
if [ "$CTDB_VALGRIND" = "yes" ] ; then
2015-08-17 13:47:58 +03:00
ctdbd="valgrind -q --log-file=/usr/local/var/log/ctdb_valgrind ${ctdbd}"
2013-07-09 09:22:07 +04:00
else
ctdbd="${CTDB_VALGRIND} ${ctdbd}"
fi
2013-10-15 04:54:58 +04:00
ctdb_options="${ctdb_options} --valgrinding"
2013-07-09 09:22:07 +04:00
fi
2014-08-11 11:07:41 +04:00
case "$CTDB_LOGGING" in
2014-08-08 14:59:21 +04:00
syslog:udp|syslog:udp-rfc5424)
2014-08-08 14:59:21 +04:00
logger -t ctdbd "CTDB is being run with ${CTDB_LOGGING}. If nothing is logged then check your syslogd configuration"
;;
2014-10-18 07:39:30 +04:00
syslog|syslog:*) : ;;
2014-08-11 11:07:41 +04:00
file:*)
logger -t ctdbd "CTDB is being run without syslog enabled. Logs will be in ${CTDB_LOGGING#file:}"
;;
*)
logger -t ctdbd "CTDB is being run without syslog enabled. Logs will be in log.ctdb"
esac
2013-07-11 08:26:38 +04:00
2013-10-15 04:54:58 +04:00
eval "$ctdbd" "$ctdb_options" || return 1
2013-07-09 09:22:07 +04:00
# Wait until ctdbd has started and is ready to respond to clients.
_timeout="${CTDB_STARTUP_TIMEOUT:-10}"
_count=0
2016-07-06 10:31:51 +03:00
while [ "$_count" -lt "$_timeout" ] ; do
2017-10-23 03:50:51 +03:00
if $CTDB runstate first_recovery startup running >/dev/null 2>&1 ; then
return 0
2013-07-09 09:22:07 +04:00
fi
2016-07-06 09:50:30 +03:00
_count=$((_count + 1))
2013-07-09 09:22:07 +04:00
sleep 1
done
2018-03-05 12:16:00 +03:00
echo "Timed out waiting for initialisation - check logs"
# Attempt a shutdown just in case things are still running
$CTDB shutdown >/dev/null 2>&1
2013-07-09 09:22:07 +04:00
drop_all_public_ips >/dev/null 2>&1
return 1
}
stop()
{
2018-03-05 12:26:08 +03:00
$CTDB shutdown
# The above command is important and needs to stand out, so
# post-check exit status
# shellcheck disable=SC2181
if [ $? -ne 0 ] ; then
echo "Error while shutting down CTDB"
drop_all_public_ips >/dev/null 2>&1
return 1
2015-10-23 06:04:04 +03:00
fi
2013-07-09 09:22:07 +04:00
2018-03-05 12:26:08 +03:00
dbdir_tmpfs_stop
2015-10-23 06:04:04 +03:00
2018-03-05 12:26:08 +03:00
return 0
2013-07-09 09:22:07 +04:00
}
############################################################
# Allow notifications for start/stop.
if [ -x "$CTDB_BASE/rc.ctdb" ] ; then
"$CTDB_BASE/rc.ctdb" "$action"
fi
case "$action" in
start) start ;;
stop) stop ;;
*)
echo "usage: $0 {start|stop}"
exit 1
esac