Blame SOURCES/openipmi-helper

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