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