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"
33 [ "$CTDB_MANAGES_SAMBA" = "yes" ] || [ "$CTDB_MANAGES_WINBIND" = "yes" ] || exit 0
36 # create the state directory for samba
37 /bin/mkdir -p $CTDB_BASE/state/samba
39 # make sure samba is not already started
40 [ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
41 service "$CTDB_SERVICE_SMB" stop > /dev/null 2>&1
42 service "$CTDB_SERVICE_NMB" stop > /dev/null 2>&1
43 killall -0 -q smbd && {
45 # make absolutely sure samba is dead
49 killall -0 -q nmbd && {
51 # make absolutely sure samba is dead
56 # restart the winbind service
57 check_ctdb_manages_winbind
58 [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
59 service "$CTDB_SERVICE_WINBIND" stop > /dev/null 2>&1
60 killall -0 -q winbindd && {
62 # make absolutely sure winbindd is dead
63 killall -q -9 winbindd
65 service "$CTDB_SERVICE_WINBIND" start
68 # start Samba service. Start it reniced, as under very heavy load
69 # the number of smbd processes will mean that it leaves few cycles for
71 [ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
72 nice_service "$CTDB_SERVICE_NMB" start
73 nice_service "$CTDB_SERVICE_SMB" start
78 # shutdown Samba when ctdb goes down
79 [ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
80 service "$CTDB_SERVICE_SMB" stop
81 service "$CTDB_SERVICE_NMB" stop
84 # stop the winbind service
85 check_ctdb_manages_winbind
86 [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
87 service "$CTDB_SERVICE_WINBIND" stop
91 # set default samba cleanup period - in minutes
92 [ -z "$SAMBA_CLEANUP_PERIOD" ] && {
93 SAMBA_CLEANUP_PERIOD=10
96 # we keep a cached copy of smb.conf here
97 smbconf_cache="$CTDB_BASE/state/samba/smb.conf.cache"
100 #############################################
101 # update the smb.conf cache in the foreground
102 testparm_foreground_update() {
103 mkdir -p "$CTDB_BASE/state/samba" || exit 1
104 testparm -s 2> /dev/null | egrep -v 'registry.shares.=|include.=' > "$smbconf_cache"
107 #############################################
108 # update the smb.conf cache in the background
109 testparm_background_update() {
110 # if the cache doesn't exist, then update in the foreground
111 [ -f $smbconf_cache ] || {
112 testparm_foreground_update
114 # otherwise do a background update
116 tmpfile="${smbconf_cache}.$$"
117 testparm -s > $tmpfile 2> /dev/null &
118 # remember the pid of the teamparm process
120 # give it 10 seconds to run
122 while [ $timeleft -gt 0 ]; do
123 timeleft=$(($timeleft - 1))
124 # see if the process still exists
125 /bin/kill -0 $pid > /dev/null 2>&1 || {
126 # it doesn't exist, grab its exit status
129 echo "50.samba: smb.conf background update exited with status $?"
133 # put the new smb.conf contents in the cache (atomic rename)
134 # make sure we remove references to the registry while doing
135 # this to ensure that running testparm on the cache does
136 # not use the registry
137 egrep -v 'registry.shares.=|include.=' < "$tmpfile" > "${tmpfile}.2"
139 mv -f "${tmpfile}.2" "$smbconf_cache" || {
140 echo "50.samba: failed to update background cache"
146 # keep waiting for testparm to finish
149 # it took more than 10 seconds - kill it off
151 /bin/kill -9 "$pid" > /dev/null 2>&1
152 echo "50.samba: timed out updating smbconf cache in background"
157 ##################################################
158 # show the testparm output using a cached smb.conf
159 # to avoid registry access
161 [ -f $smbconf_cache ] || {
162 testparm_foreground_update
164 testparm -s "$smbconf_cache" "$@" 2>/dev/null
167 # function to see if ctdb manages winbind
168 check_ctdb_manages_winbind() {
169 [ -z "$CTDB_MANAGES_WINBIND" ] && {
170 secmode=`testparm_cat --parameter-name=security`
173 CTDB_MANAGES_WINBIND="yes";
176 CTDB_MANAGES_WINBIND="no";
182 ###########################
183 # periodic cleanup function
185 # running smbstatus scrubs any dead entries from the connections
186 # and sessionid database
187 # echo "Running periodic cleanup of samba databases"
188 smbstatus -np > /dev/null 2>&1 &
201 # Create a dummy file to track when we need to do periodic cleanup
203 [ -f $CTDB_BASE/state/samba/periodic_cleanup ] || {
204 touch $CTDB_BASE/state/samba/periodic_cleanup
206 [ `/usr/bin/find $CTDB_BASE/state/samba/periodic_cleanup -mmin +$SAMBA_CLEANUP_PERIOD | wc -l` -eq 1 ] && {
207 # Cleanup the databases
209 touch $CTDB_BASE/state/samba/periodic_cleanup
212 [ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
213 [ "$CTDB_SAMBA_SKIP_SHARE_CHECK" = "yes" ] || {
214 testparm_background_update
216 testparm_cat | egrep '^WARNING|^ERROR|^Unknown' && {
217 testparm_foreground_update
218 testparm_cat | egrep '^WARNING|^ERROR|^Unknown' && {
219 echo "ERROR: testparm shows smb.conf is not clean"
224 smb_dirs=`testparm_cat | egrep '^[[:space:]]*path = ' | cut -d= -f2`
225 ctdb_check_directories_probe "Samba" $smb_dirs || {
226 testparm_foreground_update
227 smb_dirs=`testparm_cat | egrep '^[[:space:]]*path = ' | cut -d= -f2`
228 ctdb_check_directories "Samba" $smb_dirs
232 smb_ports="$CTDB_SAMBA_CHECK_PORTS"
233 [ -z "$smb_ports" ] && {
234 smb_ports=`testparm_cat --parameter-name="smb ports"`
236 ctdb_check_tcp_ports $smb_ports || exit $?
239 # check winbind is OK
240 check_ctdb_manages_winbind
241 [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
242 ctdb_check_command "winbind" "wbinfo -p"
247 ctdb_checkstatus || exit $?
251 # ignore unknown commands