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