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 /usr/bin/net serverid wipe
68 # start the winbind service
69 [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
70 service "$CTDB_SERVICE_WINBIND" start
73 # start Samba service. Start it reniced, as under very heavy load
74 # the number of smbd processes will mean that it leaves few cycles for
76 [ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
77 nice_service "$CTDB_SERVICE_NMB" start
78 nice_service "$CTDB_SERVICE_SMB" start
83 # shutdown Samba when ctdb goes down
84 [ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
85 service "$CTDB_SERVICE_SMB" stop
86 service "$CTDB_SERVICE_NMB" stop
89 # stop the winbind service
90 check_ctdb_manages_winbind
91 [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
92 service "$CTDB_SERVICE_WINBIND" stop
96 # set default samba cleanup period - in minutes
97 [ -z "$SAMBA_CLEANUP_PERIOD" ] && {
98 SAMBA_CLEANUP_PERIOD=10
101 # we keep a cached copy of smb.conf here
102 smbconf_cache="$CTDB_VARDIR/state/samba/smb.conf.cache"
105 #############################################
106 # update the smb.conf cache in the foreground
107 testparm_foreground_update() {
108 mkdir -p "$CTDB_VARDIR/state/samba" || exit 1
109 testparm -s 2> /dev/null | egrep -v 'registry.shares.=|include.=' > "$smbconf_cache"
112 #############################################
113 # update the smb.conf cache in the background
114 testparm_background_update() {
115 # if the cache doesn't exist, then update in the foreground
116 [ -f $smbconf_cache ] || {
117 testparm_foreground_update
119 # otherwise do a background update
121 tmpfile="${smbconf_cache}.$$"
122 testparm -s > $tmpfile 2> /dev/null &
123 # remember the pid of the teamparm process
125 # give it 10 seconds to run
127 while [ $timeleft -gt 0 ]; do
128 timeleft=$(($timeleft - 1))
129 # see if the process still exists
130 /bin/kill -0 $pid > /dev/null 2>&1 || {
131 # it doesn't exist, grab its exit status
134 echo "50.samba: smb.conf background update exited with status $?"
138 # put the new smb.conf contents in the cache (atomic rename)
139 # make sure we remove references to the registry while doing
140 # this to ensure that running testparm on the cache does
141 # not use the registry
142 egrep -v 'registry.shares.=|include.=' < "$tmpfile" > "${tmpfile}.2"
144 mv -f "${tmpfile}.2" "$smbconf_cache" || {
145 echo "50.samba: failed to update background cache"
151 # keep waiting for testparm to finish
154 # it took more than 10 seconds - kill it off
156 /bin/kill -9 "$pid" > /dev/null 2>&1
157 echo "50.samba: timed out updating smbconf cache in background"
162 ##################################################
163 # show the testparm output using a cached smb.conf
164 # to avoid registry access
166 [ -f $smbconf_cache ] || {
167 testparm_foreground_update
169 testparm -s "$smbconf_cache" "$@" 2>/dev/null
172 # function to see if ctdb manages winbind
173 check_ctdb_manages_winbind() {
174 [ -z "$CTDB_MANAGES_WINBIND" ] && {
175 secmode=`testparm_cat --parameter-name=security`
178 CTDB_MANAGES_WINBIND="yes";
181 CTDB_MANAGES_WINBIND="no";
190 sed -n -e 's@^[[:space:]]*path[[:space:]]*=[[:space:]]@@p' |
195 ###########################
196 # periodic cleanup function
198 # running smbstatus scrubs any dead entries from the connections
199 # and sessionid database
200 # echo "Running periodic cleanup of samba databases"
201 smbstatus -np > /dev/null 2>&1 &
204 ###########################
206 ctdb_start_stop_service
207 ctdb_start_stop_service "windbind"
209 is_ctdb_managed_service || is_ctdb_managed_service "winbind" || exit 0
211 ###########################
223 # Create a dummy file to track when we need to do periodic cleanup
225 [ -f $CTDB_VARDIR/state/samba/periodic_cleanup ] || {
226 touch $CTDB_VARDIR/state/samba/periodic_cleanup
228 [ `/usr/bin/find $CTDB_VARDIR/state/samba/periodic_cleanup -mmin +$SAMBA_CLEANUP_PERIOD | wc -l` -eq 1 ] && {
229 # Cleanup the databases
231 touch $CTDB_VARDIR/state/samba/periodic_cleanup
234 [ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
235 [ "$CTDB_SAMBA_SKIP_SHARE_CHECK" = "yes" ] || {
236 testparm_background_update
238 testparm_cat | egrep '^WARNING|^ERROR|^Unknown' && {
239 testparm_foreground_update
240 testparm_cat | egrep '^WARNING|^ERROR|^Unknown' && {
241 echo "ERROR: testparm shows smb.conf is not clean"
247 ctdb_check_directories_probe || {
248 testparm_foreground_update
250 ctdb_check_directories
254 smb_ports="$CTDB_SAMBA_CHECK_PORTS"
255 [ -z "$smb_ports" ] && {
256 smb_ports=`testparm_cat --parameter-name="smb ports"`
258 ctdb_check_tcp_ports $smb_ports || exit $?
261 # check winbind is OK
262 check_ctdb_manages_winbind
263 [ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
264 ctdb_check_command "winbind" "wbinfo -p"
269 ctdb_standard_event_handler "$@"