42d98b
#!/bin/sh
42d98b
#############################################################################
42d98b
#
42d98b
# ipmi:		OpenIPMI Driver helper script
42d98b
#
42d98b
# Authors:	Jan Safranek <jsafrane@redhat.com>
42d98b
#
42d98b
# Based on IPMI init script by:
42d98b
#               Matt Domsch <Matt_Domsch@dell.com>
42d98b
#               Chris Poblete <Chris_Poblete@dell.com>
42d98b
#
42d98b
# Status return code bits
42d98b
# no bits set = no errors
42d98b
# bit 0 set = minimum modules aren't loaded
42d98b
# bit 1 set = requested feature module isn't loaded
42d98b
# bit 2 set = /dev/ipmi0 (or /dev/imb if using that instead) doesn't exist
42d98b
# bit 3 set = /dev/watchdog doesn't exist
42d98b
# bit 4 set = lockfile doesn't exist
42d98b
# bit 5 set = modules are loaded even when asked to be unloaded
42d98b
42d98b
CONFIGFILE=/etc/sysconfig/ipmi
42d98b
# source config info
42d98b
[ -r ${CONFIGFILE} ] && . ${CONFIGFILE}
42d98b
42d98b
#############################################################################
42d98b
# GLOBALS
42d98b
#############################################################################
42d98b
MODULE_NAME="ipmi"
42d98b
INTF_NUM=0
42d98b
42d98b
IPMI_SMB_MODULE_NAME="ipmi_smb"
42d98b
IPMI_SI_MODULE_NAME="ipmi_si"
42d98b
kernel=`uname -r | cut -d. -f1-2`
42d98b
if [ "${kernel}" == "2.4" ]; then
42d98b
    IPMI_SMB_MODULE_NAME="ipmi_smb_intf"
42d98b
    IPMI_SI_MODULE_NAME="ipmi_si_drv"
42d98b
fi
42d98b
42d98b
MODULES_INTERFACES=""
42d98b
[ "${DEV_IPMI}" = "yes" ] && MODULES_INTERFACES="${MODULES_INTERFACES} ipmi_devintf"
42d98b
[ "${IPMI_IMB}" = "yes" ] && MODULES_INTERFACES="${MODULES_INTERFACES} ipmi_imb"
42d98b
42d98b
MODULES_FEATURES=""
42d98b
[ "${IPMI_WATCHDOG}" = "yes" ] && MODULES_FEATURES="${MODULES_FEATURES} ipmi_watchdog"
42d98b
[ "${IPMI_POWEROFF}" = "yes" ] && MODULES_FEATURES="${MODULES_FEATURES} ipmi_poweroff"
42d98b
42d98b
MODULES_HW=""
42d98b
[ "${IPMI_SI}"  = "yes" ] && MODULES_HW="${MODULES_HW} ${IPMI_SI_MODULE_NAME}"
42d98b
[ "${IPMI_SMB}" = "yes" ] && MODULES_HW="${MODULES_HW} ${IPMI_SMB_MODULE_NAME}"
42d98b
42d98b
MODULES_BASE="ipmi_msghandler"
42d98b
MODULES="${MODULES_INTERFACES} ${MODULES_FEATURES} ${MODULES_HW} ${MODULES_BASE}"
42d98b
42d98b
RETVAL=0
42d98b
LOCKFILE=/var/lock/subsys/ipmi
42d98b
DEV_IPMI_TIMEOUT=150
42d98b
42d98b
UDEV_EXISTS=0
42d98b
if [ -e /sbin/udev -o -e /sbin/udevd ]; then
42d98b
    UDEV_EXISTS=1
42d98b
fi
42d98b
42d98b
#############################################################################
42d98b
# NOTES:
42d98b
# * /dev/ipmi0 is unconditionally deleted here on ipmi_devintf unload,
42d98b
#   because SLES9 and RHEL4 kernels don't send a message for udev to delete
42d98b
#   it for us.
42d98b
# 
42d98b
#############################################################################
42d98b
42d98b
modules_loaded_verbose()
42d98b
{
42d98b
	OnePlusLoaded=0
42d98b
	OnePlusUnloaded=0
42d98b
	for m in $@; do
42d98b
		if /sbin/lsmod | grep $m >/dev/null 2>&1 ; then
42d98b
			echo "$m module loaded."
42d98b
			OnePlusLoaded=1
42d98b
		else
42d98b
			echo "$m module not loaded."
42d98b
			OnePlusUnloaded=1
42d98b
		fi
42d98b
	done
42d98b
}
42d98b
42d98b
modules_loaded()
42d98b
{
42d98b
	OnePlusLoaded=0
42d98b
	OnePlusUnloaded=0
42d98b
	for m in $@; do
42d98b
		if /sbin/lsmod | grep $m >/dev/null 2>&1 ; then
42d98b
			OnePlusLoaded=1
42d98b
		else
42d98b
			OnePlusUnloaded=1
42d98b
		fi
42d98b
	done
42d98b
}
42d98b
42d98b
device_node_exists ()
42d98b
{
42d98b
	if [ -e "$1" ]; then
42d98b
		echo "$1 exists."
42d98b
		return 1
42d98b
	fi
42d98b
	echo "$1 does not exist."
42d98b
	return 0
42d98b
}
42d98b
42d98b
minimum_modules_loaded()
42d98b
{
42d98b
        rc_base=1
42d98b
	rc_hw=1
42d98b
        modules_loaded_verbose "${MODULES_BASE}"
42d98b
	[ ${OnePlusLoaded} -eq 0 ] && rc_base=0
42d98b
	    
42d98b
	modules_loaded_verbose "${MODULES_HW}"
42d98b
	[ ${OnePlusLoaded} -eq 0 ] && rc_hw=0
42d98b
	
42d98b
	return $((rc_base && rc_hw))
42d98b
}
42d98b
42d98b
#############################################################################
42d98b
42d98b
load_si()
42d98b
{
42d98b
	if [ "${IPMI_SI}" = "yes" ]; then
42d98b
		modprobe ${IPMI_SI_MODULE_NAME} > /dev/null 2>&1
42d98b
		modules_loaded ${IPMI_SI_MODULE_NAME}
42d98b
		[ ${OnePlusLoaded} -ne 1 ] && RETVAL=$((RETVAL | 1))
42d98b
	fi
42d98b
}
42d98b
42d98b
load_smb()
42d98b
{
42d98b
	if [ "${IPMI_SMB}" = "yes" ]; then
42d98b
		modprobe ${IPMI_SMB_MODULE_NAME} > /dev/null 2>&1
42d98b
		modules_loaded ${IPMI_SMB_MODULE_NAME}
42d98b
		[ ${OnePlusLoaded} -ne 1 ] && RETVAL=$((RETVAL | 1))
42d98b
	fi
42d98b
}
42d98b
42d98b
load_hw_modules()
42d98b
{
42d98b
	load_si
42d98b
	load_smb
42d98b
}
42d98b
42d98b
start_watchdog_common()
42d98b
{
42d98b
	load_hw_modules
42d98b
	modprobe ipmi_watchdog ${IPMI_WATCHDOG_OPTIONS} > /dev/null 2>&1
42d98b
	modules_loaded ipmi_watchdog
42d98b
	[ ${OnePlusUnloaded} -ne 0 ] &&
42d98b
		RETVAL=$((RETVAL | 2)) &&
42d98b
		echo "Watchdog startup failed: cannot load ipmi_watchdog module" &&
42d98b
		return
42d98b
	if [ ${UDEV_EXISTS} -eq 0 -a ! -e /dev/watchdog ]; then
42d98b
		mknod -m 0600 /dev/watchdog c 10 130
42d98b
		[ $? -ne 0 ] &&
42d98b
			RETVAL=$((RETVAL | 8)) &&
42d98b
			echo "Watchdog startup failed: cannot create /dev/watchdog" &&
42d98b
			return
42d98b
	fi
42d98b
}
42d98b
42d98b
start_watchdog_quiet()
42d98b
{
42d98b
	[ "${IPMI_WATCHDOG}" != "yes" ] &&
42d98b
		return
42d98b
	start_watchdog_common
42d98b
}
42d98b
42d98b
start_watchdog()
42d98b
{
42d98b
	[ "${IPMI_WATCHDOG}" != "yes" ] &&
42d98b
		RETVAL=$((RETVAL | 2)) &&
42d98b
		echo "Watchdog not configured" &&
42d98b
		return
42d98b
	start_watchdog_common
42d98b
}
42d98b
42d98b
stop_watchdog()
42d98b
{
42d98b
	modprobe -q -r ipmi_watchdog > /dev/null 2>&1
42d98b
	modules_loaded ipmi_watchdog
42d98b
	if [ ${OnePlusLoaded} -ne 0 ]; then
42d98b
	    RETVAL=$((RETVAL | 32))
42d98b
	    echo "Watchog shutdown failed: cannot unload ipmi_watchdog module"
42d98b
	else
42d98b
	    if [ "${IPMI_WATCHDOG}" = "yes" ]; then
42d98b
		[ ${UDEV_EXISTS} -eq 0 ] && rm -f /dev/watchdog
42d98b
	    fi
42d98b
	fi
42d98b
}
42d98b
42d98b
stop_watchdog_quiet()
42d98b
{
42d98b
	modprobe -q -r ipmi_watchdog > /dev/null 2>&1
42d98b
	modules_loaded ipmi_watchdog
42d98b
	if [ ${OnePlusLoaded} -ne 0 ]; then
42d98b
	    RETVAL=$((RETVAL | 32))
42d98b
	else
42d98b
	    if [ "${IPMI_WATCHDOG}" = "yes" ]; then
42d98b
		[ ${UDEV_EXISTS} -eq 0 ] && rm -f /dev/watchdog
42d98b
	    fi
42d98b
	fi
42d98b
}
42d98b
42d98b
start_powercontrol_common()
42d98b
{
42d98b
	local poweroff_opts=""
42d98b
	load_hw_modules
42d98b
	if [ "${IPMI_POWERCYCLE}" == "yes" ]; then
42d98b
	    modinfo ipmi_poweroff 2>/dev/null | grep poweroff_control > /dev/null 2>&1 && \
42d98b
		poweroff_opts="poweroff_control=2"
42d98b
	    modinfo ipmi_poweroff 2>/dev/null | grep poweroff_powercycle > /dev/null 2>&1 && \
42d98b
		poweroff_opts="poweroff_powercycle=1"
42d98b
	fi
42d98b
	modprobe ipmi_poweroff "${poweroff_opts}" > /dev/null 2>&1
42d98b
	modules_loaded ipmi_poweroff
42d98b
	[ ${OnePlusUnloaded} -ne 0 ] &&
42d98b
		RETVAL=$((RETVAL | 2)) &&
42d98b
		echo "Powercontroll startup failed: cannot load ipmi_poweroff module" &&
42d98b
		return
42d98b
}
42d98b
42d98b
start_powercontrol_quiet()
42d98b
{
42d98b
	[ "${IPMI_POWEROFF}" != "yes" ] &&
42d98b
		return
42d98b
	start_powercontrol_common
42d98b
}
42d98b
42d98b
start_powercontrol()
42d98b
{
42d98b
	[ "${IPMI_POWEROFF}" != "yes" ] &&
42d98b
		RETVAL=$((RETVAL | 2)) &&
42d98b
		echo "Powercontroll not configured" &&
42d98b
		return
42d98b
	start_powercontrol_common
42d98b
}
42d98b
42d98b
stop_powercontrol()
42d98b
{
42d98b
	modprobe -q -r ipmi_poweroff > /dev/null 2>&1
42d98b
	modules_loaded ipmi_poweroff
42d98b
	if [ ${OnePlusLoaded} -ne 0 ]; then
42d98b
	    RETVAL=$((RETVAL | 32))
42d98b
	    echo "Powercontroll shutdown failed: cannot unload ipmi_poweroff module"
42d98b
	fi
42d98b
}
42d98b
42d98b
stop_powercontrol_quiet()
42d98b
{
42d98b
	modprobe -q -r ipmi_poweroff > /dev/null 2>&1
42d98b
	modules_loaded ipmi_poweroff
42d98b
	[ ${OnePlusLoaded} -ne 0 ] && RETVAL=$((RETVAL | 32))
42d98b
}
42d98b
42d98b
#############################################################################
42d98b
unload_all_ipmi_modules()
42d98b
{
42d98b
	stop_watchdog_quiet
42d98b
	stop_powercontrol_quiet
42d98b
	for m in ${MODULES}; do
42d98b
		modprobe -q -r ${m} > /dev/null 2>&1
42d98b
	done
42d98b
        # delete interface node ONLY if ipmi_devintf is unloaded
42d98b
        [ `lsmod | grep -c "ipmi_devintf"` -eq 0 ] &&
42d98b
	        rm -f "/dev/ipmi${INTF_NUM}"
42d98b
}
42d98b
42d98b
unload_ipmi_modules_leave_features()
42d98b
{
42d98b
	for m in ${MODULES_INTERFACES}; do
42d98b
		modprobe -q -r ${m} > /dev/null 2>&1
42d98b
	done
42d98b
        # delete interface node ONLY if ipmi_devintf is unloaded
42d98b
        [ `lsmod | grep -c "ipmi_devintf"` -eq 0 ] &&
42d98b
	        rm -f "/dev/ipmi${INTF_NUM}"
42d98b
	lsmod | egrep -q "ipmi_(poweroff|watchdog)" > /dev/null 2>&1
42d98b
	if [ "$?" -ne "0" ]; then
42d98b
		stop_watchdog_quiet
42d98b
		stop_powercontrol_quiet
42d98b
		for m in ${MODULES}; do
42d98b
			modprobe -q -r ${m} > /dev/null 2>&1
42d98b
		done
42d98b
	fi
42d98b
}
42d98b
42d98b
#############################################################################
42d98b
load_ipmi_modules ()
42d98b
{
42d98b
	local locdelay
42d98b
	modprobe ipmi_msghandler > /dev/null 2>&1
42d98b
	modules_loaded ipmi_msghandler
42d98b
	[ ${OnePlusLoaded} -ne 1 ] && unload_all_ipmi_modules && RETVAL=$((RETVAL | 1)) && return
42d98b
	load_hw_modules
42d98b
	[ $((RETVAL & 1)) -eq 1 ] && unload_all_ipmi_modules && RETVAL=$((RETVAL | 1)) && return
42d98b
42d98b
	if [ "${DEV_IPMI}" = "yes" ]; then
42d98b
		modprobe ipmi_devintf > /dev/null 2>&1
42d98b
		modules_loaded ipmi_devintf
42d98b
		RETVAL=$((RETVAL & ~2))
42d98b
		[ ${OnePlusLoaded} -eq 0 ] && RETVAL=$((RETVAL | 2))
42d98b
		if [ ${OnePlusLoaded} -eq 1 ]; then
42d98b
			if [ ${UDEV_EXISTS} -eq 0 ]; then
42d98b
				DEVMAJOR=`cat /proc/devices | awk '/ipmidev/{print $1}'`
42d98b
				rm -f /dev/ipmi${INTF_NUM}
42d98b
				mknod -m 0600 /dev/ipmi${INTF_NUM} c ${DEVMAJOR} 0 || RETVAL=$((RETVAL | 4))
42d98b
			fi
42d98b
42d98b
			# udev can take several seconds to create /dev/ipmi0, 
42d98b
			# but it happens asynchronously, so delay here
42d98b
			locdelay=${DEV_IPMI_TIMEOUT}
42d98b
			while [ ! -e /dev/ipmi${INTF_NUM} -a ${locdelay} -gt 0 ]; do
42d98b
				locdelay=$((locdelay - 1))
42d98b
				sleep 0.1
42d98b
			done
42d98b
		fi
42d98b
	fi
42d98b
42d98b
	if [ "${IPMI_IMB}" = "yes" ]; then
42d98b
		modprobe ipmi_imb > /dev/null 2>&1
42d98b
		modules_loaded ipmi_imb
42d98b
		RETVAL=$((RETVAL & ~2))
42d98b
		[ ${OnePlusLoaded} -eq 0 ] && RETVAL=$((RETVAL | 2))
42d98b
		if [ ${OnePlusLoaded} -eq 1 ]; then
42d98b
			DEVMAJOR=`cat /proc/devices | awk '/imb/{print $1}'`
42d98b
			rm -f /dev/imb
42d98b
			mknod -m 0600 /dev/imb c ${DEVMAJOR} 0 || RETVAL=$((RETVAL | 4))
42d98b
		fi
42d98b
	fi
42d98b
42d98b
	# Per Corey Minyard, essentially no one uses ipmi_radisys
42d98b
	# and we don't want to encourage its further use
42d98b
	# so it won't be handled here.
42d98b
	return
42d98b
}
42d98b
42d98b
#############################################################################
42d98b
start()
42d98b
{
42d98b
	load_ipmi_modules
42d98b
	if [ ${RETVAL} -eq 0 ]; then
42d98b
		touch ${LOCKFILE}
42d98b
	else
42d98b
		if [ $((RETVAL & 1)) -eq 1 ]; then
42d98b
			echo "Startup failed."
42d98b
		else
42d98b
			touch ${LOCKFILE} && echo "Warning!?"
42d98b
		fi
42d98b
	fi
42d98b
	start_watchdog_quiet
42d98b
	start_powercontrol_quiet
42d98b
}
42d98b
42d98b
#############################################################################
42d98b
stop()
42d98b
{
42d98b
	unload_ipmi_modules_leave_features
42d98b
	modules_loaded ${MODULES_INTERFACES}
42d98b
	if [ ${OnePlusLoaded} -ne 0 ]; then
42d98b
		RETVAL=$((RETVAL | 32))
42d98b
		echo "Shutdown failed, something may be in use"
42d98b
	else
42d98b
		rm -f ${LOCKFILE}
42d98b
	fi
42d98b
}
42d98b
42d98b
stop_all()
42d98b
{
42d98b
	unload_all_ipmi_modules
42d98b
	modules_loaded ${MODULES}
42d98b
	if [ ${OnePlusLoaded} -ne 0 ]; then
42d98b
		RETVAL=$((RETVAL | 32))
42d98b
		echo "Shutdown failed, something may be in use"
42d98b
	else
42d98b
		rm -f ${LOCKFILE}
42d98b
	fi
42d98b
}
42d98b
42d98b
#############################################################################
42d98b
restart()
42d98b
{
42d98b
	stop_all
42d98b
	RETVAL=0
42d98b
	start
42d98b
}
42d98b
42d98b
#############################################################################
42d98b
42d98b
reload()
42d98b
{
42d98b
	stop_all
42d98b
	RETVAL=0
42d98b
	start
42d98b
}
42d98b
42d98b
#############################################################################
42d98b
42d98b
status_all()
42d98b
{
42d98b
	minimum_modules_loaded
42d98b
	[ $? -eq 0 ] && RETVAL=$((RETVAL | 1))
42d98b
	
42d98b
	modules_loaded_verbose ${MODULES_FEATURES} ${MODULES_INTERFACES}
42d98b
	[ ${OnePlusUnloaded} -ne 0 ] && RETVAL=$((RETVAL | 2))
42d98b
42d98b
	if [ "${DEV_IPMI}" = "yes" ]; then 
42d98b
	    device_node_exists /dev/ipmi${INTF_NUM}
42d98b
	    [ $? -eq 0 ] && RETVAL=$((RETVAL | 4))
42d98b
	fi
42d98b
42d98b
	if [ "${IPMI_IMB}" = "yes" ]; then
42d98b
	    device_node_exists /dev/imb
42d98b
	    [ $? -eq 0 ] && RETVAL=$((RETVAL | 4))
42d98b
	fi
42d98b
42d98b
	if [ "${IPMI_WATCHDOG}" = "yes" ]; then
42d98b
	    device_node_exists /dev/watchdog
42d98b
	    [ $? -eq 0 ] && RETVAL=$((RETVAL | 8))
42d98b
	fi
42d98b
42d98b
	[ ! -e ${LOCKFILE} ] && RETVAL=$((RETVAL | 16))
42d98b
}
42d98b
42d98b
status()
42d98b
{
42d98b
	minimum_modules_loaded
42d98b
	[ $? -eq 0 ] && RETVAL=$((RETVAL | 1))
42d98b
	
42d98b
	if [ "${DEV_IPMI}" = "yes" ]; then 
42d98b
	    modules_loaded_verbose ipmi_devintf
42d98b
	    [ ${OnePlusLoaded} -eq 0 ] && RETVAL=$((RETVAL | 2))
42d98b
42d98b
	    device_node_exists /dev/ipmi${INTF_NUM}
42d98b
	    [ $? -eq 0 ] && RETVAL=$((RETVAL | 4))
42d98b
	fi
42d98b
42d98b
	if [ "${IPMI_IMB}" = "yes" ]; then
42d98b
	    device_node_exists /dev/imb
42d98b
	    [ $? -eq 0 ] && RETVAL=$((RETVAL | 4))
42d98b
	fi
42d98b
}
42d98b
42d98b
status_watchdog()
42d98b
{
42d98b
	minimum_modules_loaded
42d98b
	[ $? -eq 0 ] && RETVAL=$((RETVAL | 1))
42d98b
42d98b
	modules_loaded_verbose ipmi_watchdog
42d98b
	[ ${OnePlusLoaded} -eq 0 ] && RETVAL=$((RETVAL | 2))
42d98b
42d98b
	device_node_exists /dev/watchdog
42d98b
	[ $? -eq 0 ] && RETVAL=$((RETVAL | 8))
42d98b
}
42d98b
42d98b
status_powercontrol()
42d98b
{
42d98b
	minimum_modules_loaded
42d98b
	[ $? -eq 0 ] && RETVAL=$((RETVAL | 1))
42d98b
42d98b
	modules_loaded_verbose ipmi_powercontrol
42d98b
	[ ${OnePlusLoaded} -eq 0 ] && RETVAL=$((RETVAL | 2))
42d98b
}
42d98b
42d98b
#############################################################################
42d98b
usage ()
42d98b
{
42d98b
	echo $"Usage: $0 {start|stop|status" 1>&2
42d98b
	echo $"          restart|condrestart|try-restart|reload|force-reload" 1>&2
42d98b
	echo $"          start-watchdog|stop-watchdog|status-watchdog" 1>&2
42d98b
	echo $"          start-powercontrol|stop-powercontrol|status-powercontrol" 1>&2
42d98b
	echo $"          stop-all|status-all}" 1>&2
42d98b
	RETVAL=2
42d98b
}
42d98b
42d98b
condrestart ()
42d98b
{
42d98b
	[ -e ${LOCKFILE} ] && restart
42d98b
}
42d98b
42d98b
#############################################################################
42d98b
# MAIN
42d98b
#############################################################################
42d98b
case "$1" in
42d98b
	start) start ;;
42d98b
	stop)  stop ;;
42d98b
	restart) restart ;;
42d98b
	force-reload) reload ;;
42d98b
	reload) reload ;;
42d98b
	status)	status ;;
42d98b
	status-all) status_all ;;
42d98b
	condrestart) condrestart ;;
42d98b
	try-restart) condrestart ;;
42d98b
	start-watchdog) start_watchdog ;;
42d98b
	stop-watchdog) stop_watchdog ;;
42d98b
	status-watchdog) status_watchdog ;;
42d98b
	start-powercontrol) start_powercontrol ;;
42d98b
	stop-powercontrol) stop_powercontrol ;;
42d98b
	status-powercontrol) status_powercontrol ;;
42d98b
	stop-all) stop_all ;;
42d98b
	*) usage ;;
42d98b
esac
42d98b
42d98b
exit ${RETVAL}
42d98b
42d98b
#############################################################################
42d98b
# end of file
42d98b
#############################################################################
42d98b