2 # ctdb event script for Samba
4 PATH=/bin:/usr/bin:$PATH
14 case $CTDB_INIT_STYLE in
16 CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-smb}
17 CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-nmb}
18 CTDB_SERVICE_WINBIND=${CTDB_SERVICE_WINBIND:-winbind}
21 CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-samba}
22 CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-""}
23 CTDB_SERVICE_WINBIND=${CTDB_SERVICE_WINBIND:-winbind}
26 CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-smb}
27 CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-""}
28 CTDB_SERVICE_WINBIND=${CTDB_SERVICE_WINBIND:-winbind}
31 # should not happen, but for now use redhat style as default:
32 CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-smb}
33 CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-""}
34 CTDB_SERVICE_WINBIND=${CTDB_SERVICE_WINBIND:-winbind}
41 [ "$CTDB_MANAGES_SAMBA" = "yes" ] || [ "$CTDB_MANAGES_WINBIND" = "yes" ] || exit 0
44 # set default samba cleanup period - in minutes
45 [ -z "$SAMBA_CLEANUP_PERIOD" ] && {
46 SAMBA_CLEANUP_PERIOD=10
49 # we keep a cached copy of smb.conf here
50 smbconf_cache="$CTDB_BASE/state/samba/smb.conf.cache"
53 #############################################
54 # update the smb.conf cache in the foreground
55 testparm_foreground_update() {
56 mkdir -p "$CTDB_BASE/state/samba" || exit 1
57 testparm -s 2> /dev/null | egrep -v 'registry.shares.=|include.=' > "$smbconf_cache"
60 #############################################
61 # update the smb.conf cache in the background
62 testparm_background_update() {
63 # if the cache doesn't exist, then update in the foreground
64 [ -f $smbconf_cache ] || {
65 testparm_foreground_update
67 # otherwise do a background update
69 tmpfile="${smbconf_cache}.$$"
70 testparm -s > $tmpfile 2> /dev/null &
71 # remember the pid of the teamparm process
73 # give it 10 seconds to run
75 while [ $timeleft -gt 0 ]; do
76 timeleft=$(($timeleft - 1))
77 # see if the process still exists
78 /bin/kill -0 $pid > /dev/null 2>&1 || {
79 # it doesn't exist, grab its exit status
82 echo "50.samba: smb.conf background update exited with status $?"
86 # put the new smb.conf contents in the cache (atomic rename)
87 # make sure we remove references to the registry while doing
88 # this to ensure that running testparm on the cache does
89 # not use the registry
90 egrep -v 'registry.shares.=|include.=' < "$tmpfile" > "${tmpfile}.2"
92 mv -f "${tmpfile}.2" "$smbconf_cache" || {
93 echo "50.samba: failed to update background cache"
99 # keep waiting for testparm to finish
102 # it took more than 10 seconds - kill it off
104 /bin/kill -9 "$pid" > /dev/null 2>&1
105 echo "50.samba: timed out updating smbconf cache in background"
110 ##################################################
111 # show the testparm output using a cached smb.conf
112 # to avoid registry access
114 [ -f $smbconf_cache ] || {
115 testparm_foreground_update
117 testparm -s "$smbconf_cache" "$@" 2>/dev/null
120 # function to see if ctdb manages winbind
121 check_ctdb_manages_winbind() {
122 [ -z "$CTDB_MANAGES_WINBIND" ] && {
123 secmode=`testparm_cat --parameter-name=security`
126 CTDB_MANAGES_WINBIND="yes";
129 CTDB_MANAGES_WINBIND="no";
135 ###########################
136 # periodic cleanup function
138 # running smbstatus scrubs any dead entries from the connections
139 # and sessionid database
140 # echo "Running periodic cleanup of samba databases"
141 smbstatus -np > /dev/null 2>&1 &
146 # create the state directory for samba
147 /bin/mkdir -p $CTDB_BASE/state/samba
149 # make sure samba is not already started
150 [ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
151 service "$CTDB_SERVICE_SMB" stop > /dev/null 2>&1
152 service "$CTDB_SERVICE_NMB" stop > /dev/null 2>&1
153 killall -0 -q smbd && {
155 # make absolutely sure samba is dead
159 killall -0 -q nmbd && {
161 # make absolutely sure samba is dead
166 # restart the winbind service
167 check_ctdb_manages_winbind
168 [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
169 service "$CTDB_SERVICE_WINBIND" stop > /dev/null 2>&1
170 killall -0 -q winbindd && {
172 # make absolutely sure winbindd is dead
173 killall -q -9 winbindd
175 service "$CTDB_SERVICE_WINBIND" start
178 # start Samba service. Start it reniced, as under very heavy load
179 # the number of smbd processes will mean that it leaves few cycles for
181 [ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
182 nice_service "$CTDB_SERVICE_NMB" start
183 nice_service "$CTDB_SERVICE_SMB" start
188 # nothing special for Samba
192 # nothing special for Samba
196 # nothing special for Samba
201 # shutdown Samba when ctdb goes down
202 [ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
203 service "$CTDB_SERVICE_SMB" stop
204 service "$CTDB_SERVICE_NMB" stop
207 # stop the winbind service
208 check_ctdb_manages_winbind
209 [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
210 service "$CTDB_SERVICE_WINBIND" stop
215 # Create a dummy file to track when we need to do periodic cleanup
217 [ -f $CTDB_BASE/state/samba/periodic_cleanup ] || {
218 touch $CTDB_BASE/state/samba/periodic_cleanup
220 [ `/usr/bin/find $CTDB_BASE/state/samba/periodic_cleanup -mmin +$SAMBA_CLEANUP_PERIOD | wc -l` -eq 1 ] && {
221 # Cleanup the databases
223 touch $CTDB_BASE/state/samba/periodic_cleanup
226 [ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
227 [ "$CTDB_SAMBA_SKIP_SHARE_CHECK" = "yes" ] || {
228 testparm_background_update
230 testparm_cat | egrep '^WARNING|^ERROR|^Unknown' && {
231 testparm_foreground_update
232 testparm_cat | egrep '^WARNING|^ERROR|^Unknown' && {
233 echo "ERROR: testparm shows smb.conf is not clean"
238 smb_dirs=`testparm_cat | egrep '^[[:space:]]*path = ' | cut -d= -f2`
239 ctdb_check_directories_probe "Samba" $smb_dirs || {
240 testparm_foreground_update
241 smb_dirs=`testparm_cat | egrep '^[[:space:]]*path = ' | cut -d= -f2`
242 ctdb_check_directories "Samba" $smb_dirs
246 smb_ports="$CTDB_SAMBA_CHECK_PORTS"
247 [ -z "$smb_ports" ] && {
248 smb_ports=`testparm_cat --parameter-name="smb ports"`
250 ctdb_check_tcp_ports $smb_ports || exit $?
253 # check winbind is OK
254 check_ctdb_manages_winbind
255 [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
256 ctdb_check_command "winbind" "wbinfo -p"
262 # ignore unknown commands