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