Blame SOURCES/exchange-bmc-os-info

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