summaryrefslogtreecommitdiff
path: root/contrib/exchange-bmc-os-info.init.redhat
blob: b7ec43f829e34a86c3772ad6de023f8e405f14eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/bin/sh
#############################################################################
#
# exchange-bmc-os-info: Set OS and BMC (Baseboard Management Controller)
#			 parameters during system startup.
#
# version:	0.72
#
# Authors:	Charles Rose <charles_rose@dell.com>
#		Jordan Hargrave <jordan_hargrave@dell.com>
#
# Description:  Script to set OS information in the BMC; fetch BMC IP/URL
#		and set in the OS for use by other scripts/user.
#
#		BMC IP and URL are made available in /var/run/bmc-info
#
#		Example to launch BMC web-interface:
#		# . /var/run/bmc-info
#		# xdg-open $BMC_URL
#
#		See here for details:
#		https://fedoraproject.org/wiki/Features/AgentFreeManagement
#
# OEM Specific: OEM specific ipmi commands go in:
#		'oem_set_os_version' and 'oem_get_bmc_url'
#############################################################################
#
# chkconfig: 345 99 00
# description: Set OS name, hostname in BMC; make BMC IP/URL available in OS
# processname: exchange-bmc-os-info
# config:      /etc/sysconfig/exchange-bmc-os-info
#
### BEGIN INIT INFO
# Provides:          exchange-bmc-os-info
# Required-Start:    ipmi
# Default-Start:     3 4 5
# Default-Stop:      0 1 2 6


#############################################################################
# GLOBALS
#############################################################################
CONFIGFILE=/etc/sysconfig/exchange-bmc-os-info
IPMI_TOOL=/usr/bin/ipmitool
BMC_INFO=/var/run/bmc-info

# BMC Manufacturer ID used in 'oem_set_os_version' and 'oem_get_bmc_url'
DELL="674"
#OTHER_OEM="123"

# Defaults for ${CONFIGFILE}
SET_OS_INFO="yes"
RESET_OS_INFO="no"
SET_BMC_INFO="yes"

# getsysinfo and setsysinfo commands
IPMI_SET_SYSINFO="${IPMI_TOOL} mc setsysinfo"
IPMI_GET_SYSINFO="${IPMI_TOOL} mc getsysinfo"
#############################################################################
SCRIPT_NAME=$(basename $0)

# source config
[ -r ${CONFIGFILE} ] && . ${CONFIGFILE}

RETVAL=0

if [ -f /bin/gettext.sh ]; then
	GETTEXT=1
	. /bin/gettext.sh
	OUTPUT="eval_gettext"
else
	GETTEXT=0
	OUTPUT="echo"
fi

#############################################################################
# Get Vendor ID of BMC for use in 'oem_set_os_version' and 'oem_get_bmc_url'
#
get_bmc_vendor_id()
{
	BMC_VENDOR=$(${IPMI_TOOL} mc info 2>/dev/null | \
		sed -n "s#^Manufacturer ID.*: ##p")
	[ -z "${BMC_VENDOR}" ] && RETVAL=4
}

check_ipmitool()
{
	if [ -x ${IPMI_TOOL} ]; then
	# v1.8.12 plus patches are required for set/getsysinfo support
	# http://sourceforge.net/mailarchive/message.php?msg_id=29647222
		[ ! ${IPMI_GET_SYSINFO} >/dev/null 2>&1 ] && \
			RETVAL=3
	else
		RETVAL=2
	fi
}

bmc_exists()
{
	check_ipmitool
	[ $RETVAL -eq 0 ] && get_bmc_vendor_id
	return $RETVAL
}
#############################################################################

get_os_info()
{
	OS_HOSTNAME=$(hostname)
	KERNEL_VERSION=$(uname -r -m)

	if  [ -e /etc/lsb-release ] ; then
		. /etc/lsb-release
		NAME=${DISTRIB_ID}
		VERSION="${DISTRIB_RELEASE} ${DISTRIB_CODENAME}"
	fi

	# we prefer systemd's /etc/os-release over other sources
	[ -e /etc/os-release ] && . /etc/os-release

	OS_NAME=${NAME}
	OS_VERSION="${VERSION} kernel ${KERNEL_VERSION}"
}

oem_set_os_version()
{
	# OS Version setting is not standard yet
	# we need per vendor oem commands
	case "${BMC_VENDOR}" in
		$DELL) ${IPMI_SET_SYSINFO} delloem_os_version \
				"${OS_VERSION}" > /dev/null 2>&1
			return $?
			;;
# Add OEM specific commands.
# Example:
#		$OTHER_OEM) ${IPMI_SET_SYSINFO} otheroem_os_version \
#				"${OS_VERSION}" > /dev/null 2>&1
#			return $?
#			;;
		*) 	return 0
			;;
	esac
}

set_os_info()
{
	# Set and reset OS info in the BMC
	if [ "$1" = "reset" ]; then
		OS_NAME=""
		OS_HOSTNAME=""
		OS_VERSION=""
	fi

	${IPMI_SET_SYSINFO} os_name "${OS_NAME}" >/dev/null 2>&1 \
		|| RETVAL=6
	${IPMI_SET_SYSINFO} primary_os_name "${OS_NAME}" >/dev/null 2>&1 \
		|| RETVAL=6
	${IPMI_SET_SYSINFO} system_name "${OS_HOSTNAME}" >/dev/null 2>&1 \
		|| RETVAL=6
	oem_set_os_version || RETVAL=6
}

#############################################################################
valid_url()
{
	url="(https?|http)://[a-z0-9-]+(\.[a-z0-9-]+)+([/?].*)?"
	printf -- "%s" "${TMP_URL}"| grep -Eq "^${url}"
	return $?
}

oem_get_bmc_url()
{
	# BMC URL is not standard yet
	# we need per vendor oem commands
	case "$BMC_VENDOR" in
		$DELL)	TMP_URL=$(${IPMI_GET_SYSINFO} delloem_url 2> /dev/null)
				;;
# Add OEM specific commands
# Example:
#		$OTHER_OEM)
#			TMP_URL=$(${IPMI_GET_SYSINFO} otheroem_url 2> /dev/null)
#				;;
		*)  TMP_URL=""	;;
	esac

	valid_url && BMC_URL=${TMP_URL} || BMC_URL=""
}

valid_ip()
{
	#Thanks to mkyong.com
	octet="([01]?[[:digit:]][[:digit:]]?|2[0-4][[:digit:]]|25[0-5])"

	printf -- "%s" "${TMP_IPv4}"| grep -Eq "^${octet}\\.${octet}\\.${octet}\\.${octet}$"
	return $?
}

get_bmc_ip()
{
	#Thanks to http://ingvar.blog.redpill-linpro.com
	for CHANNEL in `seq 1 14`
	do
		[ $(${IPMI_TOOL} lan print ${CHANNEL} 2>/dev/null \
			| grep -q "^Set") ] || break
	done

	# Get BMC_IPv4 and BMC_URL from BMC
	TMP_IPv4=$(${IPMI_TOOL} lan print ${CHANNEL} 2>/dev/null \
			| sed -n "s#^IP Address  .*: ##p")

	valid_ip && BMC_IPv4=${TMP_IPv4} || BMC_IPv4=""
}

get_bmc_info()
{
	get_bmc_ip
	if [ -z "${BMC_IPv4}" ] || [ "${BMC_IPv4}" = "0.0.0.0" ]; then
		BMC_IPv4=""
		RETVAL=5
	else
		# URL makes sense only if there is an IP
		oem_get_bmc_url
	fi
}

set_bmc_info()
{
	if [ ! $(touch "${BMC_INFO}" && chmod 600 "${BMC_INFO}") ]; then
		printf "BMC_IPv4=%s\n" "${BMC_IPv4}" > "${BMC_INFO}"
		[ -n "${BMC_URL}" ] && \
			printf "BMC_URL=%s\n" "${BMC_URL}" >> "${BMC_INFO}"
	else
		RETVAL=5
	fi
}

unset_bmc_info()
{
	[ -f ${BMC_INFO} ] && rm -f ${BMC_INFO} > /dev/null 2>&1
}

#############################################################################
start()
{
	if bmc_exists; then
		[ "${SET_OS_INFO}" = "yes" ] && \
			get_os_info && set_os_info

		if [ "${SET_BMC_INFO}" = "yes" ]; then
			get_bmc_info
			if [ ${RETVAL} -eq 0 ]; then
				set_bmc_info
			fi
		fi
	fi
}

#############################################################################
stop()
{
	if bmc_exists; then
		# reset OS info while system reboots
		# aids with debugging OS boot-up issues
		if [ "${RESET_OS_INFO}" = "yes" ]; then
			set_os_info reset
		fi
		unset_bmc_info
	fi
}

#############################################################################
restart()
{
	stop
	[ $RETVAL -eq 0 ] && start
}

#############################################################################
status()
{
	[ -r ${BMC_INFO} ] && \
		grep -q "BMC_IPv4" "${BMC_INFO}" >/dev/null 1>&2 && \
			BMC_STATUS="ok" || BMC_STATUS="inactive"
	${OUTPUT} "${SCRIPT_NAME}: ${BMC_STATUS}" 1>&2
	[ ${GETTEXT} -eq 1 ] && echo
}

#############################################################################
usage()
{
	${OUTPUT} "Usage: ${SCRIPT_NAME} {start|stop|restart|status}" 1>&2
	[ ${GETTEXT} -eq 1 ] && echo
	RETVAL=1
}

#############################################################################
# MAIN
#############################################################################
case "$1" in
	start) start ;;
	stop)  stop ;;
	restart) restart ;;
	status)	status ;;
	*) usage ;;
esac

case "$RETVAL" in
	0|1) ;;
	2) ${OUTPUT} "${SCRIPT_NAME}: ipmitool(1) not found." 1>&2 ;;
	3) ${OUTPUT} "${SCRIPT_NAME}: this version of ipmitool does not support getsysinfo." 1>&2 ;;
	4) ${OUTPUT} "${SCRIPT_NAME}: failed to communicate with BMC." 1>&2 ;;
	5) ${OUTPUT} "${SCRIPT_NAME}: failed to set OS information in BMC." 1>&2 ;;
	6) ${OUTPUT} "${SCRIPT_NAME}: failed to get BMC information." 1>&2 ;;
	*) ${OUTPUT} "${SCRIPT_NAME}: unexpected error." 1>&2 ;;
esac

if [ ${RETVAL} -gt 1 ]; then
	${OUTPUT} " Return code: ${RETVAL}" 1>&2
	[ ${GETTEXT} -eq 1 ] && echo
fi


exit ${RETVAL}

#############################################################################
# end of file
#############################################################################