s3:libnet_join: make use of create_builtin_guests()
[Samba.git] / ctdb / config / ctdbd_wrapper
blobc239e3d4a3b45405d2be050d51dde5a6c36a668f
1 #!/bin/sh
3 # ctdbd wrapper - start or stop CTDB
5 usage ()
7 echo "usage: ctdbd_wrapper { start | stop }"
8 exit 1
11 [ $# -eq 1 ] || usage
13 action="$1"
15 ############################################################
17 if [ -z "$CTDB_BASE" ] ; then
18 export CTDB_BASE="/usr/local/etc/ctdb"
21 . "${CTDB_BASE}/functions"
22 loadconfig "ctdb"
24 ctdbd="${CTDBD:-/usr/local/sbin/ctdbd}"
26 ############################################################
28 # If necessary, mount volatile database directory on tmpfs
29 dbdir_tmpfs_start ()
31 if [ -z "$CTDB_DBDIR_TMPFS_OPTIONS" ] ; then
32 return
35 # Shortcut for readability
36 _opts="$CTDB_DBDIR_TMPFS_OPTIONS"
38 mkdir -p "$CTDB_DBDIR" || exit $?
40 # If already mounted then remount, otherwise mount
41 if findmnt -t tmpfs "$CTDB_DBDIR" >/dev/null ; then
42 mount -t tmpfs -o "remount,$_opts" none "$CTDB_DBDIR" || \
43 exit $?
44 else
45 mount -t tmpfs -o "$_opts" none "$CTDB_DBDIR" || exit $?
49 # If necessary, unmount volatile database tmpfs directory on exit
50 dbdir_tmpfs_stop ()
52 if [ -z "$CTDB_DBDIR_TMPFS_OPTIONS" ] ; then
53 return
56 if [ -d "$CTDB_DBDIR" ] && findmnt -t tmpfs "$CTDB_DBDIR" >/dev/null ; then
57 umount "$CTDB_DBDIR"
61 # Only the nested function references its arguments
62 # shellcheck disable=SC2120
63 build_ctdb_options ()
65 ctdb_options=""
67 maybe_set ()
69 # If the given variable isn't set then do nothing
70 [ -n "$2" ] || return
71 # If a required value for the variable and it doesn't match,
72 # then do nothing
73 [ -z "$3" -o "$3" = "$2" ] || return
75 val="'$2'"
76 case "$1" in
77 --*) sep="=" ;;
78 -*) sep=" " ;;
79 esac
80 # For these options we're only passing a value-less flag.
81 if [ -n "$3" ] ; then
82 val=""
83 sep=""
86 ctdb_options="${ctdb_options}${ctdb_options:+ }${1}${sep}${val}"
89 if [ -z "$CTDB_RECOVERY_LOCK" ] ; then
90 echo "No recovery lock specified. Starting CTDB without split brain prevention."
92 maybe_set "--reclock" "$CTDB_RECOVERY_LOCK"
94 # build up ctdb_options variable from optional parameters
95 maybe_set "--logging" "$CTDB_LOGGING"
96 maybe_set "--listen" "$CTDB_NODE_ADDRESS"
97 maybe_set "--dbdir" "$CTDB_DBDIR"
98 maybe_set "--dbdir-persistent" "$CTDB_DBDIR_PERSISTENT"
99 maybe_set "--dbdir-state" "$CTDB_DBDIR_STATE"
100 maybe_set "--transport" "$CTDB_TRANSPORT"
101 maybe_set "-d" "$CTDB_DEBUGLEVEL"
102 maybe_set "--notification-script" "$CTDB_NOTIFY_SCRIPT"
103 maybe_set "--start-as-disabled" "$CTDB_START_AS_DISABLED" "yes"
104 maybe_set "--start-as-stopped " "$CTDB_START_AS_STOPPED" "yes"
105 maybe_set "--no-recmaster" "$CTDB_CAPABILITY_RECMASTER" "no"
106 maybe_set "--no-lmaster" "$CTDB_CAPABILITY_LMASTER" "no"
107 maybe_set "--nosetsched" "$CTDB_NOSETSCHED" "yes"
108 maybe_set "--script-log-level" "$CTDB_SCRIPT_LOG_LEVEL"
109 maybe_set "--max-persistent-check-errors" "$CTDB_MAX_PERSISTENT_CHECK_ERRORS"
112 export_debug_variables ()
114 [ -n "$CTDB_DEBUG_HUNG_SCRIPT" ] && export CTDB_DEBUG_HUNG_SCRIPT
115 [ -n "$CTDB_DEBUG_LOCKS" ] && export CTDB_DEBUG_LOCKS
118 ############################################################
120 start()
122 dbdir_tmpfs_start
124 # build_ctdb_options() takes no arguments
125 # shellcheck disable=SC2119
126 build_ctdb_options
128 export_debug_variables
130 # Explicitly trying to disable core files, no other way
131 # shellcheck disable=SC2039
132 if [ "$CTDB_SUPPRESS_COREFILE" = "yes" ]; then
133 ulimit -c 0
134 else
135 ulimit -c unlimited
138 # Unsupported option easily avoided by not using configuration variable
139 # shellcheck disable=SC2039
140 if [ -n "$CTDB_MAX_OPEN_FILES" ]; then
141 ulimit -n "$CTDB_MAX_OPEN_FILES"
144 if [ -n "$CTDB_VALGRIND" -a "$CTDB_VALGRIND" != "no" ] ; then
145 if [ "$CTDB_VALGRIND" = "yes" ] ; then
146 ctdbd="valgrind -q --log-file=/usr/local/var/log/ctdb_valgrind ${ctdbd}"
147 else
148 ctdbd="${CTDB_VALGRIND} ${ctdbd}"
150 ctdb_options="${ctdb_options} --valgrinding"
153 case "$CTDB_LOGGING" in
154 syslog:udp|syslog:udp-rfc5424)
155 logger -t ctdbd "CTDB is being run with ${CTDB_LOGGING}. If nothing is logged then check your syslogd configuration"
157 syslog|syslog:*) : ;;
158 file:*)
159 logger -t ctdbd "CTDB is being run without syslog enabled. Logs will be in ${CTDB_LOGGING#file:}"
162 logger -t ctdbd "CTDB is being run without syslog enabled. Logs will be in log.ctdb"
163 esac
165 eval "$ctdbd" "$ctdb_options" || return 1
167 # Wait until ctdbd has started and is ready to respond to clients.
168 _timeout="${CTDB_STARTUP_TIMEOUT:-10}"
169 _count=0
170 while [ "$_count" -lt "$_timeout" ] ; do
171 if $CTDB runstate first_recovery startup running >/dev/null 2>&1 ; then
172 return 0
175 _count=$((_count + 1))
176 sleep 1
177 done
179 echo "Timed out waiting for initialisation - check logs"
180 # Attempt a shutdown just in case things are still running
181 $CTDB shutdown >/dev/null 2>&1
182 drop_all_public_ips >/dev/null 2>&1
183 return 1
186 stop()
188 $CTDB shutdown
190 # The above command is important and needs to stand out, so
191 # post-check exit status
192 # shellcheck disable=SC2181
193 if [ $? -ne 0 ] ; then
194 echo "Error while shutting down CTDB"
195 drop_all_public_ips >/dev/null 2>&1
196 return 1
199 dbdir_tmpfs_stop
201 return 0
204 ############################################################
206 # Allow notifications for start/stop.
207 if [ -x "$CTDB_BASE/rc.ctdb" ] ; then
208 "$CTDB_BASE/rc.ctdb" "$action"
211 case "$action" in
212 start) start ;;
213 stop) stop ;;
215 echo "usage: $0 {start|stop}"
216 exit 1
217 esac