Blame SOURCES/openipmi-helper

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