2 # ctdb event script for Samba
4 PATH=/bin:/usr/bin:$PATH
12 case $CTDB_INIT_STYLE in
14 CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-smb}
15 CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-nmb}
16 CTDB_SERVICE_WINBIND=${CTDB_SERVICE_WINBIND:-winbind}
19 CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-samba}
20 CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-""}
21 CTDB_SERVICE_WINBIND=${CTDB_SERVICE_WINBIND:-winbind}
24 CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-smb}
25 CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-""}
26 CTDB_SERVICE_WINBIND=${CTDB_SERVICE_WINBIND:-winbind}
29 # should not happen, but for now use redhat style as default:
30 CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-smb}
31 CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-""}
32 CTDB_SERVICE_WINBIND=${CTDB_SERVICE_WINBIND:-winbind}
39 [ "$CTDB_MANAGES_SAMBA" = "yes" ] || [ "$CTDB_MANAGES_WINBIND" = "yes" ] || exit 0
42 # set default samba cleanup period - in minutes
43 [ -z "$SAMBA_CLEANUP_PERIOD" ] && {
44 SAMBA_CLEANUP_PERIOD=10
47 # we keep a cached copy of smb.conf here
48 smbconf_cache="$CTDB_BASE/state/samba/smb.conf.cache"
51 #############################################
52 # update the smb.conf cache in the foreground
53 testparm_foreground_update() {
54 mkdir -p "$CTDB_BASE/state/samba" || exit 1
55 testparm -s 2> /dev/null | egrep -v 'registry.shares.=|include.=' > "$smbconf_cache"
58 #############################################
59 # update the smb.conf cache in the background
60 testparm_background_update() {
61 # if the cache doesn't exist, then update in the foreground
62 [ -f $smbconf_cache ] || {
63 testparm_foreground_update
65 # otherwise do a background update
67 tmpfile="${smbconf_cache}.$$"
68 testparm -s > $tmpfile 2> /dev/null &
69 # remember the pid of the teamparm process
71 # give it 10 seconds to run
73 while [ $timeleft -gt 0 ]; do
74 timeleft=$(($timeleft - 1))
75 # see if the process still exists
76 /bin/kill -0 $pid > /dev/null 2>&1 || {
77 # it doesn't exist, grab its exit status
80 echo "50.samba: smb.conf background update exited with status $?"
84 # put the new smb.conf contents in the cache (atomic rename)
85 # make sure we remove references to the registry while doing
86 # this to ensure that running testparm on the cache does
87 # not use the registry
88 egrep -v 'registry.shares.=|include.=' < "$tmpfile" > "${tmpfile}.2"
90 mv -f "${tmpfile}.2" "$smbconf_cache" || {
91 echo "50.samba: failed to update background cache"
97 # keep waiting for testparm to finish
100 # it took more than 10 seconds - kill it off
102 /bin/kill -9 "$pid" > /dev/null 2>&1
103 echo "50.samba: timed out updating smbconf cache in background"
108 ##################################################
109 # show the testparm output using a cached smb.conf
110 # to avoid registry access
112 [ -f $smbconf_cache ] || {
113 testparm_foreground_update
115 testparm -s "$smbconf_cache" "$@" 2>/dev/null
118 # function to see if ctdb manages winbind
119 check_ctdb_manages_winbind() {
120 [ -z "$CTDB_MANAGES_WINBIND" ] && {
121 secmode=`testparm_cat --parameter-name=security`
124 CTDB_MANAGES_WINBIND="yes";
127 CTDB_MANAGES_WINBIND="no";
133 ###########################
134 # periodic cleanup function
136 # running smbstatus scrubs any dead entries from the connections
137 # and sessionid database
138 # echo "Running periodic cleanup of samba databases"
139 smbstatus -np > /dev/null 2>&1 &
144 # create the state directory for samba
145 /bin/mkdir -p $CTDB_BASE/state/samba
147 # make sure samba is not already started
148 [ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
149 service "$CTDB_SERVICE_SMB" stop > /dev/null 2>&1
150 service "$CTDB_SERVICE_NMB" stop > /dev/null 2>&1
151 killall -0 -q smbd && {
153 # make absolutely sure samba is dead
157 killall -0 -q nmbd && {
159 # make absolutely sure samba is dead
164 # restart the winbind service
165 check_ctdb_manages_winbind
166 [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
167 service "$CTDB_SERVICE_WINBIND" stop > /dev/null 2>&1
168 killall -0 -q winbindd && {
170 # make absolutely sure winbindd is dead
171 killall -q -9 winbindd
173 service "$CTDB_SERVICE_WINBIND" start
175 echo "Startup failed. wbinfo -t returned error."
180 # start Samba service. Start it reniced, as under very heavy load
181 # the number of smbd processes will mean that it leaves few cycles for
183 [ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
184 nice_service "$CTDB_SERVICE_NMB" start
185 nice_service "$CTDB_SERVICE_SMB" start
190 # nothing special for Samba
194 # nothing special for Samba
198 # nothing special for Samba
203 # shutdown Samba when ctdb goes down
204 [ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
205 service "$CTDB_SERVICE_SMB" stop
206 service "$CTDB_SERVICE_NMB" stop
209 # stop the winbind service
210 check_ctdb_manages_winbind
211 [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
212 service "$CTDB_SERVICE_WINBIND" stop
217 # Create a dummy file to track when we need to do periodic cleanup
219 [ -f $CTDB_BASE/state/samba/periodic_cleanup ] || {
220 touch $CTDB_BASE/state/samba/periodic_cleanup
222 [ `/usr/bin/find $CTDB_BASE/state/samba/periodic_cleanup -mmin +$SAMBA_CLEANUP_PERIOD | wc -l` -eq 1 ] && {
223 # Cleanup the databases
225 touch $CTDB_BASE/state/samba/periodic_cleanup
228 [ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
229 [ "$CTDB_SAMBA_SKIP_SHARE_CHECK" = "yes" ] || {
230 testparm_background_update
232 testparm_cat | egrep '^WARNING|^ERROR|^Unknown' && {
233 testparm_foreground_update
234 testparm_cat | egrep '^WARNING|^ERROR|^Unknown' && {
235 echo "ERROR: testparm shows smb.conf is not clean"
240 smb_dirs=`testparm_cat | egrep '^[[:space:]]*path = ' | cut -d= -f2`
241 ctdb_check_directories_probe "Samba" $smb_dirs || {
242 testparm_foreground_update
243 smb_dirs=`testparm_cat | egrep '^[[:space:]]*path = ' | cut -d= -f2`
244 ctdb_check_directories "Samba" $smb_dirs
248 smb_ports="$CTDB_SAMBA_CHECK_PORTS"
249 [ -z "$smb_ports" ] && {
250 smb_ports=`testparm_cat --parameter-name="smb ports"`
252 ctdb_check_tcp_ports "Samba" $smb_ports
255 # check winbind is OK
256 check_ctdb_manages_winbind
257 [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
258 ctdb_check_command "winbind" "wbinfo -p"
264 # ignore unknown commands