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 /bin/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
71 # start Samba service. Start it reniced, as under very heavy load
72 # the number of smbd processes will mean that it leaves few cycles for
74 [ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
75 /usr/bin/net serverid wipe
77 nice_service "$CTDB_SERVICE_NMB" start
78 nice_service "$CTDB_SERVICE_SMB" start
84 # shutdown Samba when ctdb goes down
85 [ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
86 service "$CTDB_SERVICE_SMB" stop
87 service "$CTDB_SERVICE_NMB" stop
90 # stop the winbind service
91 check_ctdb_manages_winbind
92 [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
93 service "$CTDB_SERVICE_WINBIND" stop
99 # set default samba cleanup period - in minutes
100 [ -z "$SAMBA_CLEANUP_PERIOD" ] && {
101 SAMBA_CLEANUP_PERIOD=10
104 # we keep a cached copy of smb.conf here
105 smbconf_cache="$CTDB_VARDIR/state/samba/smb.conf.cache"
108 #############################################
109 # update the smb.conf cache in the foreground
110 testparm_foreground_update() {
111 mkdir -p "$CTDB_VARDIR/state/samba" || exit 1
112 testparm -s 2> /dev/null | egrep -v 'registry.shares.=|include.=' > "$smbconf_cache"
115 #############################################
116 # update the smb.conf cache in the background
117 testparm_background_update() {
118 # if the cache doesn't exist, then update in the foreground
119 [ -f $smbconf_cache ] || {
120 testparm_foreground_update
122 # otherwise do a background update
124 tmpfile="${smbconf_cache}.$$"
125 testparm -s > $tmpfile 2> /dev/null &
126 # remember the pid of the teamparm process
128 # give it 10 seconds to run
130 while [ $timeleft -gt 0 ]; do
131 timeleft=$(($timeleft - 1))
132 # see if the process still exists
133 /bin/kill -0 $pid > /dev/null 2>&1 || {
134 # it doesn't exist, grab its exit status
137 echo "50.samba: smb.conf background update exited with status $?"
141 # put the new smb.conf contents in the cache (atomic rename)
142 # make sure we remove references to the registry while doing
143 # this to ensure that running testparm on the cache does
144 # not use the registry
145 egrep -v 'registry.shares.=|include.=' < "$tmpfile" > "${tmpfile}.2"
147 mv -f "${tmpfile}.2" "$smbconf_cache" || {
148 echo "50.samba: failed to update background cache"
154 # keep waiting for testparm to finish
157 # it took more than 10 seconds - kill it off
159 /bin/kill -9 "$pid" > /dev/null 2>&1
160 echo "50.samba: timed out updating smbconf cache in background"
165 ##################################################
166 # show the testparm output using a cached smb.conf
167 # to avoid registry access
169 [ -f $smbconf_cache ] || {
170 testparm_foreground_update
172 testparm -s "$smbconf_cache" "$@" 2>/dev/null
175 # function to see if ctdb manages winbind
176 check_ctdb_manages_winbind() {
177 [ -z "$CTDB_MANAGES_WINBIND" ] && {
178 secmode=`testparm_cat --parameter-name=security`
181 CTDB_MANAGES_WINBIND="yes";
184 CTDB_MANAGES_WINBIND="no";
193 sed -n -e 's@^[[:space:]]*path[[:space:]]*=[[:space:]]@@p' |
198 ###########################
199 # periodic cleanup function
201 # running smbstatus scrubs any dead entries from the connections
202 # and sessionid database
203 # echo "Running periodic cleanup of samba databases"
204 smbstatus -np > /dev/null 2>&1 &
207 ###########################
209 [ "$1" = "monitor" ] && {
210 ctdb_start_stop_service
211 ctdb_start_stop_service "winbind"
214 is_ctdb_managed_service || is_ctdb_managed_service "winbind" || exit 0
216 ###########################
228 # Create a dummy file to track when we need to do periodic cleanup
230 [ -f $CTDB_VARDIR/state/samba/periodic_cleanup ] || {
231 touch $CTDB_VARDIR/state/samba/periodic_cleanup
233 [ `/usr/bin/find $CTDB_VARDIR/state/samba/periodic_cleanup -mmin +$SAMBA_CLEANUP_PERIOD | wc -l` -eq 1 ] && {
234 # Cleanup the databases
236 touch $CTDB_VARDIR/state/samba/periodic_cleanup
239 [ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
240 [ "$CTDB_SAMBA_SKIP_SHARE_CHECK" = "yes" ] || {
241 testparm_background_update
243 testparm_cat | egrep '^WARNING|^ERROR|^Unknown' && {
244 testparm_foreground_update
245 testparm_cat | egrep '^WARNING|^ERROR|^Unknown' && {
246 echo "ERROR: testparm shows smb.conf is not clean"
252 ctdb_check_directories_probe || {
253 testparm_foreground_update
255 ctdb_check_directories
259 smb_ports="$CTDB_SAMBA_CHECK_PORTS"
260 [ -z "$smb_ports" ] && {
261 smb_ports=`testparm_cat --parameter-name="smb ports"`
263 ctdb_check_tcp_ports $smb_ports || exit $?
266 # check winbind is OK
267 check_ctdb_manages_winbind
268 [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
269 ctdb_check_command "winbind" "wbinfo -p"
278 smbcontrol winbindd ip-dropped $ip >/dev/null 2>/dev/null &
281 ctdb_standard_event_handler "$@"