2 # ctdb event script for Samba
4 [ -n "$CTDB_BASE" ] || \
5 export CTDB_BASE=$(cd -P $(dirname "$0") ; dirname "$PWD")
11 case $CTDB_INIT_STYLE in
13 CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-smb}
14 CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-nmb}
17 CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-samba}
18 CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-""}
21 # Use redhat style as default:
22 CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-smb}
23 CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-""}
31 ctdb_setup_service_state_dir
35 # make sure samba is not already started
36 service "$CTDB_SERVICE_SMB" stop > /dev/null 2>&1
37 if [ -n "$CTDB_SERVICE_NMB" ] ; then
38 service "$CTDB_SERVICE_NMB" stop > /dev/null 2>&1
40 killall -0 -q smbd && {
42 # make absolutely sure samba is dead
45 killall -0 -q nmbd && {
47 # make absolutely sure samba is dead
51 # start Samba service. Start it reniced, as under very heavy load
52 # the number of smbd processes will mean that it leaves few cycles
56 if [ -n "$CTDB_SERVICE_NMB" ] ; then
57 nice_service "$CTDB_SERVICE_NMB" start || die "Failed to start nmbd"
60 nice_service "$CTDB_SERVICE_SMB" start || die "Failed to start samba"
65 service "$CTDB_SERVICE_SMB" stop
66 if [ -n "$CTDB_SERVICE_NMB" ] ; then
67 service "$CTDB_SERVICE_NMB" stop
71 # we keep a cached copy of smb.conf here
72 smbconf_cache="$service_state_dir/smb.conf.cache"
75 #############################################
76 # update the smb.conf cache in the foreground
77 testparm_foreground_update() {
78 testparm -s 2> /dev/null | egrep -v 'registry.shares.=|include.=' > "$smbconf_cache"
81 #############################################
82 # update the smb.conf cache in the background
83 testparm_background_update() {
84 # if the cache doesn't exist, then update in the foreground
85 [ -f $smbconf_cache ] || {
86 testparm_foreground_update
88 # otherwise do a background update
90 tmpfile="${smbconf_cache}.$$"
91 testparm -s > $tmpfile 2> /dev/null &
92 # remember the pid of the teamparm process
94 # give it 10 seconds to run
96 while [ $timeleft -gt 0 ]; do
97 timeleft=$(($timeleft - 1))
98 # see if the process still exists
99 kill -0 $pid > /dev/null 2>&1 || {
100 # it doesn't exist, grab its exit status
103 echo "50.samba: smb.conf background update exited with status $?"
107 # put the new smb.conf contents in the cache (atomic rename)
108 # make sure we remove references to the registry while doing
109 # this to ensure that running testparm on the cache does
110 # not use the registry
111 egrep -v 'registry.shares.=|include.=' < "$tmpfile" > "${tmpfile}.2"
113 mv -f "${tmpfile}.2" "$smbconf_cache" || {
114 echo "50.samba: failed to update background cache"
120 # keep waiting for testparm to finish
123 # it took more than 10 seconds - kill it off
125 kill -9 "$pid" > /dev/null 2>&1
126 echo "50.samba: timed out updating smbconf cache in background"
131 ##################################################
132 # show the testparm output using a cached smb.conf
133 # to avoid registry access
135 [ -f $smbconf_cache ] || {
136 testparm_foreground_update
138 testparm -v -s "$smbconf_cache" "$@" 2>/dev/null
144 sed -n -e 's@^[[:space:]]*path[[:space:]]*=[[:space:]]@@p' |
149 ###########################
151 ctdb_start_stop_service
153 is_ctdb_managed_service || exit 0
155 ctdb_service_check_reconfigure
157 ###########################
169 if [ "$CTDB_SAMBA_SKIP_SHARE_CHECK" != "yes" ] ; then
170 testparm_background_update
172 testparm_cat | egrep '^WARNING|^ERROR|^Unknown' && {
173 testparm_foreground_update
174 testparm_cat | egrep '^WARNING|^ERROR|^Unknown' && \
175 die "ERROR: testparm shows smb.conf is not clean"
178 list_samba_shares | ctdb_check_directories_probe || {
179 testparm_foreground_update
181 ctdb_check_directories
185 smb_ports="$CTDB_SAMBA_CHECK_PORTS"
186 if [ -z "$smb_ports" ] ; then
187 smb_ports=`testparm_cat --parameter-name="smb ports"`
189 ctdb_check_tcp_ports $smb_ports || exit $?
193 ctdb_standard_event_handler "$@"