Blame SOURCES/exchange-bmc-os-info

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