Blame SOURCES/exchange-bmc-os-info

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