Blame SOURCES/openipmi-helper

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