2 # ctdb event script for Samba
4 PATH=/bin:/usr/bin:$PATH
13 [ "$CTDB_MANAGES_SAMBA" = "yes" ] || exit 0
15 # set default samba cleanup period - in minutes
16 [ -z "$SAMBA_CLEANUP_PERIOD" ] && {
17 SAMBA_CLEANUP_PERIOD=10
20 # we keep a cached copy of smb.conf here
21 smbconf_cache="$CTDB_BASE/state/samba/smb.conf.cache"
24 #############################################
25 # update the smb.conf cache in the foreground
26 testparm_foreground_update() {
27 mkdir -p "$CTDB_BASE/state/samba" || exit 1
28 testparm -s 2> /dev/null | egrep -v 'registry.shares.=|include.=' > "$smbconf_cache"
31 #############################################
32 # update the smb.conf cache in the background
33 testparm_background_update() {
34 # if the cache doesn't exist, then update in the foreground
35 [ -f $smbconf_cache ] || {
36 testparm_foreground_update
38 # otherwise do a background update
40 tmpfile="${smbconf_cache}.$$"
41 testparm -s > $tmpfile 2> /dev/null &
42 # remember the pid of the teamparm process
44 # give it 10 seconds to run
46 while [ $timeleft -gt 0 ]; do
47 timeleft=$(($timeleft - 1))
48 # see if the process still exists
49 kill -0 $pid > /dev/null 2>&1 || {
50 # it doesn't exist, grab its exit status
53 echo "50.samba: smb.conf background update exited with status $?"
57 # put the new smb.conf contents in the cache (atomic rename)
58 # make sure we remove references to the registry while doing
59 # this to ensure that running testparm on the cache does
60 # not use the registry
61 egrep -v 'registry.shares.=|include.=' < "$tmpfile" > "${tmpfile}.2"
63 mv -f "${tmpfile}.2" "$smbconf_cache" || {
64 echo "50.samba: failed to update background cache"
70 # keep waiting for testparm to finish
73 # it took more than 10 seconds - kill it off
75 kill -9 "$pid" > /dev/null 2>&1
76 echo "50.samba: timed out updating smbconf cache in background"
81 ##################################################
82 # show the testparm output using a cached smb.conf
83 # to avoid registry access
85 [ -f $smbconf_cache ] || {
86 testparm_foreground_update
88 testparm -s "$smbconf_cache" "$@" 2>/dev/null
91 # function to see if ctdb manages winbind
92 check_ctdb_manages_winbind() {
93 [ -z "$CTDB_MANAGES_WINBIND" ] && {
94 secmode=`testparm_cat --parameter-name=security`
97 CTDB_MANAGES_WINBIND="yes";
100 CTDB_MANAGES_WINBIND="no";
106 ###########################
107 # periodic cleanup function
109 # running smbstatus scrubs any dead entries from the connections
110 # and sessionid database
111 echo "Running periodic cleanup of samba databases"
112 smbstatus -n > /dev/null 2>&1 &
117 # create the state directory for samba
118 /bin/mkdir -p $CTDB_BASE/state/samba
120 # make sure samba is not already started
121 service smb stop > /dev/null 2>&1
122 killall -0 -q smbd && {
124 # make absolutely sure samba is dead
128 # restart the winbind service
129 check_ctdb_manages_winbind
130 [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
131 service winbind stop > /dev/null 2>&1
132 killall -0 -q winbindd && {
134 # make absolutely sure winbindd is dead
135 killall -q -9 winbindd
137 service winbind start
140 # start Samba service. Start it reniced, as under very heavy load
141 # the number of smbd processes will mean that it leaves few cycles for
143 nice_service smb start
147 # nothing special for Samba
151 # nothing special for Samba
155 # nothing special for Samba
160 # shutdown Samba when ctdb goes down
163 # stop the winbind service
164 check_ctdb_manages_winbind
165 [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
171 # Create a dummy file to track when we need to do periodic cleanup
173 [ -f $CTDB_BASE/state/samba/periodic_cleanup ] || {
174 touch $CTDB_BASE/state/samba/periodic_cleanup
176 [ `/usr/bin/find $CTDB_BASE/state/samba/periodic_cleanup -mmin +$SAMBA_CLEANUP_PERIOD | wc -l` -eq 1 ] && {
177 # Cleanup the databases
179 touch $CTDB_BASE/state/samba/periodic_cleanup
182 testparm_background_update
184 testparm_cat | egrep '^WARNING|^ERROR|^Unknown' && {
185 testparm_foreground_update
186 testparm_cat | egrep '^WARNING|^ERROR|^Unknown' && {
187 echo "ERROR: testparm shows smb.conf is not clean"
192 smb_dirs=`testparm_cat | egrep '^[[:space:]]*path = ' | cut -d= -f2`
193 ctdb_check_directories_probe "Samba" $smb_dirs || {
194 testparm_foreground_update
195 smb_dirs=`testparm_cat | egrep '^[[:space:]]*path = ' | cut -d= -f2`
196 ctdb_check_directories "Samba" $smb_dirs
199 smb_ports="$CTDB_SAMBA_CHECK_PORTS"
200 [ -z "$smb_ports" ] && {
201 smb_ports=`testparm_cat --parameter-name="smb ports"`
203 ctdb_check_tcp_ports "Samba" $smb_ports
205 # check winbind is OK
206 check_ctdb_manages_winbind
207 [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
208 ctdb_check_command "winbind" "wbinfo -p"
214 # ignore unknown commands