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