Blame SOURCES/exchange-bmc-os-info

a51664
#!/bin/sh
a51664
#############################################################################
a51664
#
a51664
# exchange-bmc-os-info: Set OS and BMC (Baseboard Management Controller)
a51664
#			 parameters during system startup.
a51664
#
a51664
# version:	0.72
a51664
#
a51664
# Authors:	Charles Rose <charles_rose@dell.com>
a51664
#		Jordan Hargrave <jordan_hargrave@dell.com>
a51664
#
a51664
# Description:  Script to set OS information in the BMC; fetch BMC IP/URL
a51664
#		and set in the OS for use by other scripts/user.
a51664
#
a51664
#		BMC IP and URL are made available in /var/run/bmc-info
a51664
#
a51664
#		Example to launch BMC web-interface:
a51664
#		# . /var/run/bmc-info
a51664
#		# xdg-open $BMC_URL
a51664
#
a51664
#		See here for details:
a51664
#		https://fedoraproject.org/wiki/Features/AgentFreeManagement
a51664
#
a51664
# OEM Specific: OEM specific ipmi commands go in:
a51664
#		'oem_set_os_version' and 'oem_get_bmc_url'
a51664
#############################################################################
a51664
#
a51664
# chkconfig: 345 99 00
a51664
# description: Set OS name, hostname in BMC; make BMC IP/URL available in OS
a51664
# processname: exchange-bmc-os-info
a51664
# config:      /etc/sysconfig/exchange-bmc-os-info
a51664
#
a51664
### BEGIN INIT INFO
a51664
# Provides:          exchange-bmc-os-info
a51664
# Required-Start:    ipmi
a51664
# Default-Start:     3 4 5
a51664
# Default-Stop:      0 1 2 6
a51664
a51664
a51664
#############################################################################
a51664
# GLOBALS
a51664
#############################################################################
a51664
CONFIGFILE=/etc/sysconfig/exchange-bmc-os-info
a51664
IPMI_TOOL=/usr/bin/ipmitool
a51664
BMC_INFO=/var/run/bmc-info
a51664
a51664
# BMC Manufacturer ID used in 'oem_set_os_version' and 'oem_get_bmc_url'
a51664
DELL="674"
a51664
#OTHER_OEM="123"
a51664
a51664
# Defaults for ${CONFIGFILE}
a51664
SET_OS_INFO="yes"
a51664
RESET_OS_INFO="no"
a51664
SET_BMC_INFO="yes"
a51664
a51664
# getsysinfo and setsysinfo commands
a51664
IPMI_SET_SYSINFO="${IPMI_TOOL} mc setsysinfo"
a51664
IPMI_GET_SYSINFO="${IPMI_TOOL} mc getsysinfo"
a51664
#############################################################################
a51664
SCRIPT_NAME=$(basename $0)
a51664
a51664
# source config
a51664
[ -r ${CONFIGFILE} ] && . ${CONFIGFILE}
a51664
a51664
RETVAL=0
a51664
a51664
if [ -f /bin/gettext.sh ]; then
a51664
	GETTEXT=1
a51664
	. /bin/gettext.sh
a51664
	OUTPUT="eval_gettext"
a51664
else
a51664
	GETTEXT=0
a51664
	OUTPUT="echo"
a51664
fi
a51664
a51664
#############################################################################
a51664
# Get Vendor ID of BMC for use in 'oem_set_os_version' and 'oem_get_bmc_url'
a51664
#
a51664
get_bmc_vendor_id()
a51664
{
a51664
	BMC_VENDOR=$(${IPMI_TOOL} mc info 2>/dev/null | \
a51664
		sed -n "s#^Manufacturer ID.*: ##p")
a51664
	[ -z "${BMC_VENDOR}" ] && RETVAL=4
a51664
}
a51664
a51664
# set/getsysinfo support was added to ipmitool post v1.8.12 via this patch
a51664
# http://sourceforge.net/mailarchive/message.php?msg_id=29647222
a51664
check_ipmitool()
a51664
{
a51664
	if [ -x ${IPMI_TOOL} ]; then
a51664
		[ ! ${IPMI_GET_SYSINFO} >/dev/null 2>&1 ] && \
a51664
			RETVAL=3
a51664
	else
a51664
		RETVAL=2
a51664
	fi
a51664
}
a51664
a51664
bmc_exists()
a51664
{
a51664
	check_ipmitool
a51664
	[ $RETVAL -eq 0 ] && get_bmc_vendor_id
a51664
	return $RETVAL
a51664
}
a51664
#############################################################################
a51664
a51664
get_os_info()
a51664
{
a51664
	OS_HOSTNAME=$(hostname)
a51664
	KERNEL_VERSION=$(uname -r -m)
a51664
a51664
	if  [ -e /etc/lsb-release ] ; then
a51664
		. /etc/lsb-release
a51664
		NAME=${DISTRIB_ID}
a51664
		VERSION="${DISTRIB_RELEASE} ${DISTRIB_CODENAME}"
a51664
	fi
a51664
a51664
	# we prefer systemd's /etc/os-release over other sources
a51664
	[ -e /etc/os-release ] && . /etc/os-release
a51664
a51664
	OS_NAME=${NAME}
a51664
	OS_VERSION="${VERSION} kernel ${KERNEL_VERSION}"
a51664
}
a51664
a51664
oem_set_os_version()
a51664
{
a51664
	# OS Version setting is not standard yet
a51664
	# we need per vendor oem commands
a51664
	case "${BMC_VENDOR}" in
a51664
		$DELL) ${IPMI_SET_SYSINFO} delloem_os_version \
a51664
				"${OS_VERSION}" > /dev/null 2>&1
a51664
			return $?
a51664
			;;
a51664
# Add OEM specific commands.
a51664
# Example:
a51664
#		$OTHER_OEM) ${IPMI_SET_SYSINFO} otheroem_os_version \
a51664
#				"${OS_VERSION}" > /dev/null 2>&1
a51664
#			return $?
a51664
#			;;
a51664
		*) 	return 0
a51664
			;;
a51664
	esac
a51664
}
a51664
a51664
set_os_info()
a51664
{
a51664
	# Set and reset OS info in the BMC
a51664
	if [ "$1" = "reset" ]; then
a51664
		OS_NAME=""
a51664
		OS_HOSTNAME=""
a51664
		OS_VERSION=""
a51664
	fi
a51664
a51664
	${IPMI_SET_SYSINFO} os_name "${OS_NAME}" >/dev/null 2>&1 \
a51664
		|| RETVAL=6
a51664
	${IPMI_SET_SYSINFO} primary_os_name "${OS_NAME}" >/dev/null 2>&1 \
a51664
		|| RETVAL=6
a51664
	${IPMI_SET_SYSINFO} system_name "${OS_HOSTNAME}" >/dev/null 2>&1 \
a51664
		|| RETVAL=6
a51664
	oem_set_os_version || RETVAL=6
a51664
}
a51664
a51664
#############################################################################
a51664
valid_url()
a51664
{
a51664
	url="(https?|http)://[a-z0-9-]+(\.[a-z0-9-]+)+([/?].*)?"
a51664
	printf -- "%s" "${TMP_URL}"| grep -Eq "^${url}"
a51664
	return $?
a51664
}
a51664
a51664
oem_get_bmc_url()
a51664
{
a51664
	# BMC URL is not standard yet
a51664
	# we need per vendor oem commands
a51664
	case "$BMC_VENDOR" in
a51664
		$DELL)	TMP_URL=$(${IPMI_GET_SYSINFO} delloem_url 2> /dev/null)
a51664
				;;
a51664
# Add OEM specific commands
a51664
# Example:
a51664
#		$OTHER_OEM)
a51664
#			TMP_URL=$(${IPMI_GET_SYSINFO} otheroem_url 2> /dev/null)
a51664
#				;;
a51664
		*)  TMP_URL=""	;;
a51664
	esac
a51664
a51664
	valid_url && BMC_URL=${TMP_URL} || BMC_URL=""
a51664
}
a51664
a51664
valid_ip()
a51664
{
a51664
	#Thanks to mkyong.com
a51664
	octet="([01]?[[:digit:]][[:digit:]]?|2[0-4][[:digit:]]|25[0-5])"
a51664
a51664
	printf -- "%s" "${TMP_IPv4}"| grep -Eq "^${octet}\\.${octet}\\.${octet}\\.${octet}$"
a51664
	return $?
a51664
}
a51664
a51664
get_bmc_ip()
a51664
{
a51664
	#Thanks to http://ingvar.blog.redpill-linpro.com
a51664
	for CHANNEL in `seq 1 14`
a51664
	do
a51664
		[ $(${IPMI_TOOL} lan print ${CHANNEL} 2>/dev/null \
a51664
			| grep -q "^Set") ] || break
a51664
	done
a51664
a51664
	# Get BMC_IPv4 and BMC_URL from BMC
a51664
	TMP_IPv4=$(${IPMI_TOOL} lan print ${CHANNEL} 2>/dev/null \
a51664
			| sed -n "s#^IP Address  .*: ##p")
a51664
a51664
	valid_ip && BMC_IPv4=${TMP_IPv4} || BMC_IPv4=""
a51664
}
a51664
a51664
get_bmc_info()
a51664
{
a51664
	get_bmc_ip
a51664
	if [ -z "${BMC_IPv4}" ] || [ "${BMC_IPv4}" = "0.0.0.0" ]; then
a51664
		BMC_IPv4=""
a51664
		RETVAL=5
a51664
	else
a51664
		# URL makes sense only if there is an IP
a51664
		oem_get_bmc_url
a51664
	fi
a51664
}
a51664
a51664
set_bmc_info()
a51664
{
a51664
	if [ ! $(touch "${BMC_INFO}" && chmod 600 "${BMC_INFO}") ]; then
a51664
		printf "BMC_IPv4=%s\n" "${BMC_IPv4}" > "${BMC_INFO}"
a51664
		[ -n "${BMC_URL}" ] && \
a51664
			printf "BMC_URL=%s\n" "${BMC_URL}" >> "${BMC_INFO}"
a51664
	else
a51664
		RETVAL=5
a51664
	fi
a51664
}
a51664
a51664
unset_bmc_info()
a51664
{
a51664
	[ -f ${BMC_INFO} ] && rm -f ${BMC_INFO} > /dev/null 2>&1
a51664
}
a51664
a51664
#############################################################################
a51664
start()
a51664
{
a51664
	if bmc_exists; then
a51664
		[ "${SET_OS_INFO}" = "yes" ] && \
a51664
			get_os_info && set_os_info
a51664
a51664
		if [ "${SET_BMC_INFO}" = "yes" ]; then
a51664
			get_bmc_info
a51664
			if [ ${RETVAL} -eq 0 ]; then
a51664
				set_bmc_info
a51664
			fi
a51664
		fi
a51664
	fi
a51664
}
a51664
a51664
#############################################################################
a51664
stop()
a51664
{
a51664
	if bmc_exists; then
a51664
		# reset OS info while system reboots
a51664
		# aids with debugging OS boot-up issues
a51664
		if [ "${RESET_OS_INFO}" = "yes" ]; then
a51664
			set_os_info reset
a51664
		fi
a51664
		unset_bmc_info
a51664
	fi
a51664
}
a51664
a51664
#############################################################################
a51664
restart()
a51664
{
a51664
	stop
a51664
	[ $RETVAL -eq 0 ] && start
a51664
}
a51664
a51664
#############################################################################
a51664
status()
a51664
{
a51664
	[ -r ${BMC_INFO} ] && \
a51664
		grep -q "BMC_IPv4" "${BMC_INFO}" >/dev/null 1>&2 && \
a51664
			BMC_STATUS="ok" || BMC_STATUS="inactive"
a51664
	${OUTPUT} "${SCRIPT_NAME}: ${BMC_STATUS}" 1>&2
a51664
	[ ${GETTEXT} -eq 1 ] && echo
a51664
}
a51664
a51664
#############################################################################
a51664
usage()
a51664
{
a51664
	${OUTPUT} "Usage: ${SCRIPT_NAME} {start|stop|restart|status}" 1>&2
a51664
	[ ${GETTEXT} -eq 1 ] && echo
a51664
	RETVAL=1
a51664
}
a51664
a51664
#############################################################################
a51664
# MAIN
a51664
#############################################################################
a51664
case "$1" in
a51664
	start) start ;;
a51664
	stop)  stop ;;
a51664
	restart) restart ;;
a51664
	status)	status ;;
a51664
	*) usage ;;
a51664
esac
a51664
a51664
case "$RETVAL" in
a51664
	0|1) ;;
a51664
	2) ${OUTPUT} "${SCRIPT_NAME}: ipmitool(1) not found." 1>&2 ;;
a51664
	3) ${OUTPUT} "${SCRIPT_NAME}: this version of ipmitool does not support getsysinfo." 1>&2 ;;
a51664
	4) ${OUTPUT} "${SCRIPT_NAME}: failed to communicate with BMC." 1>&2 ;;
a51664
	5) ${OUTPUT} "${SCRIPT_NAME}: failed to set OS information in BMC." 1>&2 ;;
a51664
	6) ${OUTPUT} "${SCRIPT_NAME}: failed to get BMC information." 1>&2 ;;
a51664
	*) ${OUTPUT} "${SCRIPT_NAME}: unexpected error." 1>&2 ;;
a51664
esac
a51664
a51664
if [ ${RETVAL} -gt 1 ]; then
a51664
	${OUTPUT} " Return code: ${RETVAL}" 1>&2
a51664
	[ ${GETTEXT} -eq 1 ] && echo
a51664
fi
a51664
a51664
a51664
exit ${RETVAL}
a51664
a51664
#############################################################################
a51664
# end of file
a51664
#############################################################################