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