2007-06-01 14:54:26 +04:00
#!/bin/sh
# ctdb event script for Samba
PATH=/bin:/usr/bin:$PATH
2007-09-14 08:14:03 +04:00
. $CTDB_BASE/functions
2007-06-03 16:07:07 +04:00
loadconfig ctdb
2008-05-27 02:21:18 +04:00
loadconfig samba
2007-06-01 14:54:26 +04:00
2009-01-16 15:33:13 +03:00
detect_init_style
case $CTDB_INIT_STYLE in
suse)
2009-03-09 02:20:30 +03: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 15:33:13 +03:00
;;
ubuntu)
2009-03-09 02:20:30 +03:00
CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-samba}
CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-""}
CTDB_SERVICE_WINBIND=${CTDB_SERVICE_WINBIND:-winbind}
2009-01-16 15:33:13 +03:00
;;
redhat)
2009-03-09 02:20:30 +03:00
CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-smb}
CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-""}
CTDB_SERVICE_WINBIND=${CTDB_SERVICE_WINBIND:-winbind}
2009-01-16 15:33:13 +03:00
;;
*)
# should not happen, but for now use redhat style as default:
2009-03-09 02:20:30 +03:00
CTDB_SERVICE_SMB=${CTDB_SERVICE_SMB:-smb}
CTDB_SERVICE_NMB=${CTDB_SERVICE_NMB:-""}
CTDB_SERVICE_WINBIND=${CTDB_SERVICE_WINBIND:-winbind}
2009-01-16 15:33:13 +03:00
;;
esac
2007-06-01 14:54:26 +04:00
cmd="$1"
shift
2007-06-02 12:51:05 +04:00
[ "$CTDB_MANAGES_SAMBA" = "yes" ] || exit 0
2007-06-17 21:15:08 +04:00
# set default samba cleanup period - in minutes
[ -z "$SAMBA_CLEANUP_PERIOD" ] && {
SAMBA_CLEANUP_PERIOD=10
}
2008-07-23 09:36:23 +04:00
# we keep a cached copy of smb.conf here
smbconf_cache="$CTDB_BASE/state/samba/smb.conf.cache"
#############################################
# update the smb.conf cache in the foreground
testparm_foreground_update() {
mkdir -p "$CTDB_BASE/state/samba" || exit 1
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
kill -0 $pid > /dev/null 2>&1 || {
# 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}"
kill -9 "$pid" > /dev/null 2>&1
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 14:57:04 +04:00
# function to see if ctdb manages winbind
check_ctdb_manages_winbind() {
[ -z "$CTDB_MANAGES_WINBIND" ] && {
2008-07-23 09:36:23 +04:00
secmode=`testparm_cat --parameter-name=security`
2007-11-18 07:14:54 +03:00
case $secmode in
ADS|DOMAIN)
CTDB_MANAGES_WINBIND="yes";
;;
*)
CTDB_MANAGES_WINBIND="no";
;;
esac
2008-05-14 14:57:04 +04:00
}
2007-11-18 07:14:54 +03:00
}
2007-06-17 21:15:08 +04:00
###########################
# periodic cleanup function
periodic_cleanup() {
# running smbstatus scrubs any dead entries from the connections
# and sessionid database
2008-10-20 02:45:15 +04:00
# echo "Running periodic cleanup of samba databases"
2009-05-06 02:18:21 +04:00
smbstatus -np > /dev/null 2>&1 &
2007-06-17 21:15:08 +04:00
}
2007-06-01 14:54:26 +04:00
case $cmd in
startup)
2007-06-17 20:34:29 +04:00
# create the state directory for samba
2007-09-14 08:14:03 +04:00
/bin/mkdir -p $CTDB_BASE/state/samba
2007-06-17 20:34:29 +04:00
2007-06-05 09:18:37 +04:00
# make sure samba is not already started
2009-01-19 23:22:58 +03:00
service "$CTDB_SERVICE_SMB" stop > /dev/null 2>&1
service "$CTDB_SERVICE_NMB" stop > /dev/null 2>&1
2008-01-07 06:31:13 +03:00
killall -0 -q smbd && {
sleep 1
# make absolutely sure samba is dead
killall -q -9 smbd
}
2007-11-14 08:17:52 +03:00
2009-01-16 15:33:13 +03:00
killall -0 -q nmbd && {
sleep 1
# make absolutely sure samba is dead
killall -q -9 nmbd
}
2007-11-14 08:17:52 +03:00
# restart the winbind service
2008-05-14 14:57:04 +04:00
check_ctdb_manages_winbind
2007-11-14 08:17:52 +03:00
[ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
2009-01-19 23:22:58 +03:00
service "$CTDB_SERVICE_WINBIND" stop > /dev/null 2>&1
2008-01-07 06:31:13 +03:00
killall -0 -q winbindd && {
2007-11-14 08:17:52 +03:00
sleep 1
2008-05-14 14:57:04 +04:00
# make absolutely sure winbindd is dead
2008-01-07 06:31:13 +03:00
killall -q -9 winbindd
2007-11-14 08:17:52 +03:00
}
2009-01-19 23:22:58 +03:00
service "$CTDB_SERVICE_WINBIND" start
2007-09-13 08:36:23 +04:00
}
2007-06-05 09:18:37 +04:00
2007-06-05 11:43:19 +04: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
2009-01-19 23:22:58 +03:00
nice_service "$CTDB_SERVICE_NMB" start
nice_service "$CTDB_SERVICE_SMB" start
2007-06-01 14:54:26 +04:00
;;
takeip)
# nothing special for Samba
;;
releaseip)
# nothing special for Samba
;;
recovered)
# nothing special for Samba
exit 0
;;
shutdown)
2007-06-01 18:10:22 +04:00
# shutdown Samba when ctdb goes down
2009-01-19 23:22:58 +03:00
service "$CTDB_SERVICE_SMB" stop
service "$CTDB_SERVICE_NMB" stop
2007-11-14 08:17:52 +03:00
# stop the winbind service
2008-05-14 14:57:04 +04:00
check_ctdb_manages_winbind
2007-11-14 08:17:52 +03:00
[ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
2009-01-19 23:22:58 +03:00
service "$CTDB_SERVICE_WINBIND" stop
2007-11-14 08:17:52 +03:00
}
2007-06-01 14:54:26 +04:00
;;
2007-06-06 06:08:42 +04:00
monitor)
2007-06-17 20:34:29 +04:00
# Create a dummy file to track when we need to do periodic cleanup
# of samba databases
2007-09-14 08:14:03 +04:00
[ -f $CTDB_BASE/state/samba/periodic_cleanup ] || {
touch $CTDB_BASE/state/samba/periodic_cleanup
2007-06-17 20:34:29 +04:00
}
2007-09-14 08:14:03 +04:00
[ `/usr/bin/find $CTDB_BASE/state/samba/periodic_cleanup -mmin +$SAMBA_CLEANUP_PERIOD | wc -l` -eq 1 ] && {
2007-06-17 20:34:29 +04:00
# Cleanup the databases
2007-06-17 21:15:08 +04:00
periodic_cleanup
2007-09-14 08:14:03 +04:00
touch $CTDB_BASE/state/samba/periodic_cleanup
2007-06-17 20:34:29 +04:00
}
2009-02-20 02:58:34 +03:00
[ "$CTDB_SAMBA_SKIP_SHARE_CHECK" = "yes" ] || {
testparm_background_update
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
}
}
2007-06-06 13:46:25 +04:00
2009-02-20 02:58:34 +03:00
smb_dirs=`testparm_cat | egrep '^[[:space:]]*path = ' | cut -d= -f2`
ctdb_check_directories_probe "Samba" $smb_dirs || {
testparm_foreground_update
smb_dirs=`testparm_cat | egrep '^[[:space:]]*path = ' | cut -d= -f2`
ctdb_check_directories "Samba" $smb_dirs
}
2008-07-10 02:56:33 +04:00
}
2007-06-06 06:08:42 +04:00
2008-07-15 05:03:35 +04:00
smb_ports="$CTDB_SAMBA_CHECK_PORTS"
[ -z "$smb_ports" ] && {
2008-07-23 09:36:23 +04:00
smb_ports=`testparm_cat --parameter-name="smb ports"`
2008-07-15 05:03:35 +04:00
}
2007-06-06 06:08:42 +04:00
ctdb_check_tcp_ports "Samba" $smb_ports
2007-06-17 06:05:29 +04:00
# check winbind is OK
2008-05-14 14:57:04 +04:00
check_ctdb_manages_winbind
2007-11-14 08:17:52 +03:00
[ "$CTDB_MANAGES_WINBIND" = "yes" ] && {
ctdb_check_command "winbind" "wbinfo -p"
}
2007-06-06 06:08:42 +04:00
;;
2007-06-01 14:54:26 +04:00
esac
# ignore unknown commands
exit 0