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}
14 CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-samba}
15 CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-""}
18 # Use redhat style as default:
19 CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-smb}
20 CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-""}
28 ctdb_setup_service_state_dir
32 # make sure samba is not already started
33 service "$CTDB_SERVICE_SMB" stop > /dev/null 2>&1
34 if [ -n "$CTDB_SERVICE_NMB" ] ; then
35 service "$CTDB_SERVICE_NMB" stop > /dev/null 2>&1
37 killall -0 -q smbd && {
39 # make absolutely sure samba is dead
42 killall -0 -q nmbd && {
44 # make absolutely sure samba is dead
48 # start Samba service. Start it reniced, as under very heavy load
49 # the number of smbd processes will mean that it leaves few cycles
53 if [ -n "$CTDB_SERVICE_NMB" ] ; then
54 nice_service "$CTDB_SERVICE_NMB" start || die "Failed to start nmbd"
57 nice_service "$CTDB_SERVICE_SMB" start || die "Failed to start samba"
62 service "$CTDB_SERVICE_SMB" stop
63 if [ -n "$CTDB_SERVICE_NMB" ] ; then
64 service "$CTDB_SERVICE_NMB" stop
68 # set default samba cleanup period - in minutes
69 [ -z "$SAMBA_CLEANUP_PERIOD" ] && {
70 SAMBA_CLEANUP_PERIOD=10
73 # we keep a cached copy of smb.conf here
74 smbconf_cache="$service_state_dir/smb.conf.cache"
77 #############################################
78 # update the smb.conf cache in the foreground
79 testparm_foreground_update() {
80 testparm -s 2> /dev/null | egrep -v 'registry.shares.=|include.=' > "$smbconf_cache"
83 #############################################
84 # update the smb.conf cache in the background
85 testparm_background_update() {
86 # if the cache doesn't exist, then update in the foreground
87 [ -f $smbconf_cache ] || {
88 testparm_foreground_update
90 # otherwise do a background update
92 tmpfile="${smbconf_cache}.$$"
93 testparm -s > $tmpfile 2> /dev/null &
94 # remember the pid of the teamparm process
96 # give it 10 seconds to run
98 while [ $timeleft -gt 0 ]; do
99 timeleft=$(($timeleft - 1))
100 # see if the process still exists
101 kill -0 $pid > /dev/null 2>&1 || {
102 # it doesn't exist, grab its exit status
105 echo "50.samba: smb.conf background update exited with status $?"
109 # put the new smb.conf contents in the cache (atomic rename)
110 # make sure we remove references to the registry while doing
111 # this to ensure that running testparm on the cache does
112 # not use the registry
113 egrep -v 'registry.shares.=|include.=' < "$tmpfile" > "${tmpfile}.2"
115 mv -f "${tmpfile}.2" "$smbconf_cache" || {
116 echo "50.samba: failed to update background cache"
122 # keep waiting for testparm to finish
125 # it took more than 10 seconds - kill it off
127 kill -9 "$pid" > /dev/null 2>&1
128 echo "50.samba: timed out updating smbconf cache in background"
133 ##################################################
134 # show the testparm output using a cached smb.conf
135 # to avoid registry access
137 [ -f $smbconf_cache ] || {
138 testparm_foreground_update
140 testparm -v -s "$smbconf_cache" "$@" 2>/dev/null
146 sed -n -e 's@^[[:space:]]*path[[:space:]]*=[[:space:]]@@p' |
151 ###########################
152 # periodic cleanup function
154 # running smbstatus scrubs any dead entries from the connections
155 # and sessionid database
156 # echo "Running periodic cleanup of samba databases"
157 smbstatus -np > /dev/null 2>&1 &
160 ###########################
162 ctdb_start_stop_service
164 is_ctdb_managed_service || exit 0
166 ctdb_service_check_reconfigure
168 ###########################
180 # Create a dummy file to track when we need to do periodic cleanup
182 periodic_cleanup_file="$service_state_dir/periodic_cleanup"
183 [ -f "$periodic_cleanup_file" ] || {
184 touch "$periodic_cleanup_file"
186 [ `find "$periodic_cleanup_file" -mmin +$SAMBA_CLEANUP_PERIOD | wc -l` -eq 1 ] && {
187 # Cleanup the databases
189 touch "$periodic_cleanup_file"
192 if [ "$CTDB_SAMBA_SKIP_SHARE_CHECK" != "yes" ] ; then
193 testparm_background_update
195 testparm_cat | egrep '^WARNING|^ERROR|^Unknown' && {
196 testparm_foreground_update
197 testparm_cat | egrep '^WARNING|^ERROR|^Unknown' && \
198 die "ERROR: testparm shows smb.conf is not clean"
201 list_samba_shares | ctdb_check_directories_probe || {
202 testparm_foreground_update
204 ctdb_check_directories
208 smb_ports="$CTDB_SAMBA_CHECK_PORTS"
209 if [ -z "$smb_ports" ] ; then
210 smb_ports=`testparm_cat --parameter-name="smb ports"`
212 ctdb_check_tcp_ports $smb_ports || exit $?
216 ctdb_standard_event_handler "$@"