2 # ctdb event script for Samba
8 case $CTDB_INIT_STYLE in
10 CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-smb}
11 CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-nmb}
12 CTDB_SERVICE_WINBIND=${CTDB_SERVICE_WINBIND:-winbind}
15 CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-samba}
16 CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-""}
17 CTDB_SERVICE_WINBIND=${CTDB_SERVICE_WINBIND:-winbind}
20 # should not happen, but for now use redhat style as default:
21 CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-smb}
22 CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-""}
23 CTDB_SERVICE_WINBIND=${CTDB_SERVICE_WINBIND:-winbind}
28 service_start="start_samba"
29 service_stop="stop_samba"
34 # create the state directory for samba
35 mkdir -p $CTDB_VARDIR/state/samba
37 # make sure samba is not already started
38 [ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
39 service "$CTDB_SERVICE_SMB" stop > /dev/null 2>&1
40 service "$CTDB_SERVICE_NMB" stop > /dev/null 2>&1
41 killall -0 -q smbd && {
43 # make absolutely sure samba is dead
47 killall -0 -q nmbd && {
49 # make absolutely sure samba is dead
54 # make sure winbind is not already started
55 check_ctdb_manages_winbind
56 [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
57 service "$CTDB_SERVICE_WINBIND" stop > /dev/null 2>&1
58 killall -0 -q winbindd && {
60 # make absolutely sure winbindd is dead
61 killall -q -9 winbindd
66 # start the winbind service
67 [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
68 service "$CTDB_SERVICE_WINBIND" start || {
69 echo failed to start winbind
75 # start Samba service. Start it reniced, as under very heavy load
76 # the number of smbd processes will mean that it leaves few cycles for
78 [ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
81 nice_service "$CTDB_SERVICE_NMB" start || {
82 echo failed to start nmbd
86 nice_service "$CTDB_SERVICE_SMB" start || {
87 echo failed to start samba
95 # shutdown Samba when ctdb goes down
96 [ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
97 service "$CTDB_SERVICE_SMB" stop
98 service "$CTDB_SERVICE_NMB" stop
101 # stop the winbind service
102 check_ctdb_manages_winbind
103 [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
104 service "$CTDB_SERVICE_WINBIND" stop
110 # set default samba cleanup period - in minutes
111 [ -z "$SAMBA_CLEANUP_PERIOD" ] && {
112 SAMBA_CLEANUP_PERIOD=10
115 # we keep a cached copy of smb.conf here
116 smbconf_cache="$CTDB_VARDIR/state/samba/smb.conf.cache"
119 #############################################
120 # update the smb.conf cache in the foreground
121 testparm_foreground_update() {
122 mkdir -p "$CTDB_VARDIR/state/samba" || exit 1
123 testparm -s 2> /dev/null | egrep -v 'registry.shares.=|include.=' > "$smbconf_cache"
126 #############################################
127 # update the smb.conf cache in the background
128 testparm_background_update() {
129 # if the cache doesn't exist, then update in the foreground
130 [ -f $smbconf_cache ] || {
131 testparm_foreground_update
133 # otherwise do a background update
135 tmpfile="${smbconf_cache}.$$"
136 testparm -s > $tmpfile 2> /dev/null &
137 # remember the pid of the teamparm process
139 # give it 10 seconds to run
141 while [ $timeleft -gt 0 ]; do
142 timeleft=$(($timeleft - 1))
143 # see if the process still exists
144 kill -0 $pid > /dev/null 2>&1 || {
145 # it doesn't exist, grab its exit status
148 echo "50.samba: smb.conf background update exited with status $?"
152 # put the new smb.conf contents in the cache (atomic rename)
153 # make sure we remove references to the registry while doing
154 # this to ensure that running testparm on the cache does
155 # not use the registry
156 egrep -v 'registry.shares.=|include.=' < "$tmpfile" > "${tmpfile}.2"
158 mv -f "${tmpfile}.2" "$smbconf_cache" || {
159 echo "50.samba: failed to update background cache"
165 # keep waiting for testparm to finish
168 # it took more than 10 seconds - kill it off
170 kill -9 "$pid" > /dev/null 2>&1
171 echo "50.samba: timed out updating smbconf cache in background"
176 ##################################################
177 # show the testparm output using a cached smb.conf
178 # to avoid registry access
180 [ -f $smbconf_cache ] || {
181 testparm_foreground_update
183 testparm -s "$smbconf_cache" "$@" 2>/dev/null
186 # function to see if ctdb manages winbind
187 check_ctdb_manages_winbind() {
188 [ -z "$CTDB_MANAGES_WINBIND" ] && {
189 secmode=`testparm_cat --parameter-name=security`
192 CTDB_MANAGES_WINBIND="yes";
195 CTDB_MANAGES_WINBIND="no";
204 sed -n -e 's@^[[:space:]]*path[[:space:]]*=[[:space:]]@@p' |
209 ###########################
210 # periodic cleanup function
212 # running smbstatus scrubs any dead entries from the connections
213 # and sessionid database
214 # echo "Running periodic cleanup of samba databases"
215 smbstatus -np > /dev/null 2>&1 &
218 ###########################
220 ctdb_start_stop_service
221 ctdb_start_stop_service "winbind"
223 is_ctdb_managed_service || is_ctdb_managed_service "winbind" || exit 0
225 ###########################
237 # Create a dummy file to track when we need to do periodic cleanup
239 [ -f $CTDB_VARDIR/state/samba/periodic_cleanup ] || {
240 touch $CTDB_VARDIR/state/samba/periodic_cleanup
242 [ `find $CTDB_VARDIR/state/samba/periodic_cleanup -mmin +$SAMBA_CLEANUP_PERIOD | wc -l` -eq 1 ] && {
243 # Cleanup the databases
245 touch $CTDB_VARDIR/state/samba/periodic_cleanup
248 [ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
249 [ "$CTDB_SAMBA_SKIP_SHARE_CHECK" = "yes" ] || {
250 testparm_background_update
252 testparm_cat | egrep '^WARNING|^ERROR|^Unknown' && {
253 testparm_foreground_update
254 testparm_cat | egrep '^WARNING|^ERROR|^Unknown' && {
255 echo "ERROR: testparm shows smb.conf is not clean"
261 ctdb_check_directories_probe || {
262 testparm_foreground_update
264 ctdb_check_directories
268 smb_ports="$CTDB_SAMBA_CHECK_PORTS"
269 [ -z "$smb_ports" ] && {
270 smb_ports=`testparm_cat --parameter-name="smb ports"`
272 ctdb_check_tcp_ports $smb_ports || exit $?
275 # check winbind is OK
276 check_ctdb_manages_winbind
277 [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
278 ctdb_check_command "winbind" "wbinfo -p"
287 smbcontrol winbindd ip-dropped $ip >/dev/null 2>/dev/null
290 ctdb_standard_event_handler "$@"