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