2007-06-01 20:54:26 +10:00
#!/bin/sh
# ctdb event script for Samba
2007-09-14 14:14:03 +10:00
. $CTDB_BASE/functions
2007-06-01 20:54:26 +10:00
2009-01-16 13:33:13 +01:00
detect_init_style
case $CTDB_INIT_STYLE in
suse)
2009-03-09 00:20:30 +01:00
CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-smb}
CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-nmb}
CTDB_SERVICE_WINBIND=${CTDB_SERVICE_WINBIND:-winbind}
2009-01-16 13:33:13 +01:00
;;
2009-09-15 19:33:35 +10:00
debian)
2009-03-09 00:20:30 +01:00
CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-samba}
CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-""}
CTDB_SERVICE_WINBIND=${CTDB_SERVICE_WINBIND:-winbind}
2009-01-16 13:33:13 +01:00
;;
*)
# should not happen, but for now use redhat style as default:
2009-03-09 00:20:30 +01:00
CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-smb}
CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-""}
CTDB_SERVICE_WINBIND=${CTDB_SERVICE_WINBIND:-winbind}
2009-01-16 13:33:13 +01:00
;;
esac
2009-11-19 16:48:19 +11:00
service_name="samba"
service_start="start_samba"
service_stop="stop_samba"
loadconfig
2007-06-01 20:54:26 +10:00
2009-11-19 16:48:19 +11:00
start_samba() {
# create the state directory for samba
2011-08-04 17:14:11 +10:00
mkdir -p $CTDB_VARDIR/state/samba
2009-11-19 16:48:19 +11:00
# make sure samba is not already started
[ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
service "$CTDB_SERVICE_SMB" stop > /dev/null 2>&1
service "$CTDB_SERVICE_NMB" stop > /dev/null 2>&1
killall -0 -q smbd && {
sleep 1
# make absolutely sure samba is dead
killall -q -9 smbd
}
killall -0 -q nmbd && {
sleep 1
# make absolutely sure samba is dead
killall -q -9 nmbd
}
}
2010-03-26 17:33:51 +01:00
# make sure winbind is not already started
2009-11-19 16:48:19 +11:00
check_ctdb_manages_winbind
[ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
service "$CTDB_SERVICE_WINBIND" stop > /dev/null 2>&1
killall -0 -q winbindd && {
sleep 1
# make absolutely sure winbindd is dead
killall -q -9 winbindd
}
2010-03-26 17:33:51 +01:00
}
# start the winbind service
[ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
2011-05-10 08:25:27 +10:00
service "$CTDB_SERVICE_WINBIND" start || {
echo failed to start winbind
exit 1
}
2009-11-19 16:48:19 +11:00
}
# start Samba service. Start it reniced, as under very heavy load
# the number of smbd processes will mean that it leaves few cycles for
# anything else
[ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
2011-04-13 12:08:09 +10:00
net serverid wipe
2010-12-08 11:08:19 +11:00
2011-05-10 08:25:27 +10:00
nice_service "$CTDB_SERVICE_NMB" start || {
echo failed to start nmbd
exit 1
}
nice_service "$CTDB_SERVICE_SMB" start || {
echo failed to start samba
exit 1
}
2009-11-19 16:48:19 +11:00
}
2010-11-18 15:40:19 +11:00
return 0
2009-11-19 16:48:19 +11:00
}
stop_samba() {
# shutdown Samba when ctdb goes down
[ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
service "$CTDB_SERVICE_SMB" stop
service "$CTDB_SERVICE_NMB" stop
}
# stop the winbind service
check_ctdb_manages_winbind
[ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
service "$CTDB_SERVICE_WINBIND" stop
}
2010-11-18 15:40:19 +11:00
return 0
2009-11-19 16:48:19 +11:00
}
2007-06-02 18:51:05 +10:00
2007-06-18 03:15:08 +10:00
# set default samba cleanup period - in minutes
[ -z "$SAMBA_CLEANUP_PERIOD" ] && {
SAMBA_CLEANUP_PERIOD=10
}
2008-07-23 15:36:23 +10:00
# we keep a cached copy of smb.conf here
2010-09-03 12:35:25 +10:00
smbconf_cache="$CTDB_VARDIR/state/samba/smb.conf.cache"
2008-07-23 15:36:23 +10:00
#############################################
# update the smb.conf cache in the foreground
testparm_foreground_update() {
2010-09-03 12:35:25 +10:00
mkdir -p "$CTDB_VARDIR/state/samba" || exit 1
2008-07-23 15:36:23 +10:00
testparm -s 2> /dev/null | egrep -v 'registry.shares.=|include.=' > "$smbconf_cache"
}
#############################################
# update the smb.conf cache in the background
testparm_background_update() {
# if the cache doesn't exist, then update in the foreground
[ -f $smbconf_cache ] || {
testparm_foreground_update
}
# otherwise do a background update
(
tmpfile="${smbconf_cache}.$$"
testparm -s > $tmpfile 2> /dev/null &
# remember the pid of the teamparm process
pid="$!"
# give it 10 seconds to run
timeleft=10
while [ $timeleft -gt 0 ]; do
timeleft=$(($timeleft - 1))
# see if the process still exists
2011-04-13 12:08:09 +10:00
kill -0 $pid > /dev/null 2>&1 || {
2008-07-23 15:36:23 +10:00
# it doesn't exist, grab its exit status
wait $pid
[ $? = 0 ] || {
echo "50.samba: smb.conf background update exited with status $?"
rm -f "${tmpfile}"
exit 1
}
# put the new smb.conf contents in the cache (atomic rename)
# make sure we remove references to the registry while doing
# this to ensure that running testparm on the cache does
# not use the registry
egrep -v 'registry.shares.=|include.=' < "$tmpfile" > "${tmpfile}.2"
rm -f "$tmpfile"
mv -f "${tmpfile}.2" "$smbconf_cache" || {
echo "50.samba: failed to update background cache"
rm -f "${tmpfile}.2"
exit 1
}
exit 0
}
# keep waiting for testparm to finish
sleep 1
done
# it took more than 10 seconds - kill it off
rm -f "${tmpfile}"
2011-04-13 12:08:09 +10:00
kill -9 "$pid" > /dev/null 2>&1
2008-07-23 15:36:23 +10:00
echo "50.samba: timed out updating smbconf cache in background"
exit 1
) &
}
##################################################
# show the testparm output using a cached smb.conf
# to avoid registry access
testparm_cat() {
[ -f $smbconf_cache ] || {
testparm_foreground_update
}
testparm -s "$smbconf_cache" "$@" 2>/dev/null
}
2008-05-14 20:57:04 +10:00
# function to see if ctdb manages winbind
check_ctdb_manages_winbind() {
[ -z "$CTDB_MANAGES_WINBIND" ] && {
2008-07-23 15:36:23 +10:00
secmode=`testparm_cat --parameter-name=security`
2007-11-18 15:14:54 +11:00
case $secmode in
ADS|DOMAIN)
CTDB_MANAGES_WINBIND="yes";
;;
*)
CTDB_MANAGES_WINBIND="no";
;;
esac
2008-05-14 20:57:04 +10:00
}
2007-11-18 15:14:54 +11:00
}
2009-11-20 16:45:36 +11:00
list_samba_shares ()
{
testparm_cat |
sed -n -e 's@^[[:space:]]*path[[:space:]]*=[[:space:]]@@p' |
sed -e 's/"//g'
}
2007-06-18 03:15:08 +10:00
###########################
# periodic cleanup function
periodic_cleanup() {
# running smbstatus scrubs any dead entries from the connections
# and sessionid database
2008-10-20 09:45:15 +11:00
# echo "Running periodic cleanup of samba databases"
2009-05-06 08:18:21 +10:00
smbstatus -np > /dev/null 2>&1 &
2007-06-18 03:15:08 +10:00
}
2010-11-18 11:04:52 +11:00
###########################
2010-12-14 21:17:14 +11:00
[ "$1" = "monitor" ] && {
2010-11-18 11:04:52 +11:00
ctdb_start_stop_service
2010-11-18 15:40:19 +11:00
ctdb_start_stop_service "winbind"
2010-12-03 06:07:03 +11:00
}
2010-11-18 11:04:52 +11:00
is_ctdb_managed_service || is_ctdb_managed_service "winbind" || exit 0
###########################
2009-12-01 17:43:47 +11:00
case "$1" in
2007-06-01 20:54:26 +10:00
startup)
2009-11-19 16:48:19 +11:00
ctdb_service_start
2007-06-01 20:54:26 +10:00
;;
shutdown)
2009-11-19 16:48:19 +11:00
ctdb_service_stop
2007-06-01 20:54:26 +10:00
;;
2007-06-06 12:08:42 +10:00
monitor)
2007-06-18 02:34:29 +10:00
# Create a dummy file to track when we need to do periodic cleanup
# of samba databases
2010-09-03 12:35:25 +10:00
[ -f $CTDB_VARDIR/state/samba/periodic_cleanup ] || {
touch $CTDB_VARDIR/state/samba/periodic_cleanup
2007-06-18 02:34:29 +10:00
}
2011-04-13 12:08:09 +10:00
[ `find $CTDB_VARDIR/state/samba/periodic_cleanup -mmin +$SAMBA_CLEANUP_PERIOD | wc -l` -eq 1 ] && {
2007-06-18 02:34:29 +10:00
# Cleanup the databases
2007-06-18 03:15:08 +10:00
periodic_cleanup
2010-09-03 12:35:25 +10:00
touch $CTDB_VARDIR/state/samba/periodic_cleanup
2007-06-18 02:34:29 +10:00
}
2009-09-04 02:59:24 +10:00
[ "$CTDB_MANAGES_SAMBA" = "yes" ] && {
[ "$CTDB_SAMBA_SKIP_SHARE_CHECK" = "yes" ] || {
testparm_background_update
2009-02-20 10:58:34 +11:00
2009-09-04 02:59:24 +10:00
testparm_cat | egrep '^WARNING|^ERROR|^Unknown' && {
testparm_foreground_update
testparm_cat | egrep '^WARNING|^ERROR|^Unknown' && {
echo "ERROR: testparm shows smb.conf is not clean"
exit 1
}
}
2009-11-20 16:45:36 +11:00
list_samba_shares |
ctdb_check_directories_probe || {
2009-09-04 02:59:24 +10:00
testparm_foreground_update
2009-11-20 16:45:36 +11:00
list_samba_shares |
ctdb_check_directories
} || exit $?
2009-02-20 10:58:34 +11:00
}
2007-06-06 12:08:42 +10:00
2009-09-04 02:59:24 +10:00
smb_ports="$CTDB_SAMBA_CHECK_PORTS"
[ -z "$smb_ports" ] && {
smb_ports=`testparm_cat --parameter-name="smb ports"`
}
2009-11-13 18:28:25 +11:00
ctdb_check_tcp_ports $smb_ports || exit $?
2008-07-15 11:03:35 +10:00
}
2007-06-17 12:05:29 +10:00
# check winbind is OK
2008-05-14 20:57:04 +10:00
check_ctdb_manages_winbind
2007-11-14 16:17:52 +11:00
[ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
2010-01-12 21:02:44 +11:00
ctdb_check_command "winbind" "wbinfo -p"
2007-11-14 16:17:52 +11:00
}
2007-06-06 12:08:42 +10:00
;;
2011-02-18 10:44:55 +11:00
takeip|releaseip)
iface=$2
ip=$3
maskbits=$4
smbcontrol winbindd ip-dropped $ip >/dev/null 2>/dev/null
;;
2009-12-01 17:43:47 +11:00
*)
ctdb_standard_event_handler "$@"
2009-11-19 16:48:19 +11:00
;;
2007-06-01 20:54:26 +10:00
esac
exit 0