xref: /freebsd/libexec/rc/rc.d/hostid (revision bf847ea31ae25953d115a57c77ff556e9641cd26)
10696600cSBjoern A. Zeeb#!/bin/sh
20696600cSBjoern A. Zeeb#
30696600cSBjoern A. Zeeb# Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
40696600cSBjoern A. Zeeb# Copyright (c) 2015 Xin LI <delphij@FreeBSD.org>
50696600cSBjoern A. Zeeb#
60696600cSBjoern A. Zeeb# Redistribution and use in source and binary forms, with or without
70696600cSBjoern A. Zeeb# modification, are permitted provided that the following conditions
80696600cSBjoern A. Zeeb# are met:
90696600cSBjoern A. Zeeb# 1. Redistributions of source code must retain the above copyright
100696600cSBjoern A. Zeeb#    notice, this list of conditions and the following disclaimer.
110696600cSBjoern A. Zeeb# 2. Redistributions in binary form must reproduce the above copyright
120696600cSBjoern A. Zeeb#    notice, this list of conditions and the following disclaimer in the
130696600cSBjoern A. Zeeb#    documentation and/or other materials provided with the distribution.
140696600cSBjoern A. Zeeb#
150696600cSBjoern A. Zeeb# THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
160696600cSBjoern A. Zeeb# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
170696600cSBjoern A. Zeeb# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
180696600cSBjoern A. Zeeb# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
190696600cSBjoern A. Zeeb# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
200696600cSBjoern A. Zeeb# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
210696600cSBjoern A. Zeeb# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
220696600cSBjoern A. Zeeb# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
230696600cSBjoern A. Zeeb# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
240696600cSBjoern A. Zeeb# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
250696600cSBjoern A. Zeeb# SUCH DAMAGE.
260696600cSBjoern A. Zeeb#
270696600cSBjoern A. Zeeb#
280696600cSBjoern A. Zeeb
290696600cSBjoern A. Zeeb# PROVIDE: hostid
300696600cSBjoern A. Zeeb# REQUIRE: sysctl
310696600cSBjoern A. Zeeb# KEYWORD: nojail
320696600cSBjoern A. Zeeb
330696600cSBjoern A. Zeeb. /etc/rc.subr
340696600cSBjoern A. Zeeb
350696600cSBjoern A. Zeebname="hostid"
360696600cSBjoern A. Zeebdesc="Generate a unique host ID"
370696600cSBjoern A. Zeebstart_cmd="hostid_start"
380696600cSBjoern A. Zeebstop_cmd=":"
390696600cSBjoern A. Zeebreset_cmd="hostid_reset"
400696600cSBjoern A. Zeebextra_commands="reset"
410696600cSBjoern A. Zeebrcvar="hostid_enable"
420696600cSBjoern A. Zeeb
430696600cSBjoern A. Zeebhostid_set()
440696600cSBjoern A. Zeeb{
450696600cSBjoern A. Zeeb	uuid=$1
460696600cSBjoern A. Zeeb	# Generate hostid based on hostuuid - take first four bytes from md5(uuid).
470696600cSBjoern A. Zeeb	id=`echo -n $uuid | /sbin/md5`
480696600cSBjoern A. Zeeb	id="0x${id%????????????????????????}"
490696600cSBjoern A. Zeeb
500696600cSBjoern A. Zeeb	# Set both kern.hostuuid and kern.hostid.
510696600cSBjoern A. Zeeb	#
52325ebf37SJose Luis Duran	startmsg "Setting hostuuid: ${uuid}."
530696600cSBjoern A. Zeeb	${SYSCTL} kern.hostuuid="${uuid}" >/dev/null
54325ebf37SJose Luis Duran	startmsg "Setting hostid: ${id}."
550696600cSBjoern A. Zeeb	${SYSCTL} kern.hostid=${id} >/dev/null
560696600cSBjoern A. Zeeb}
570696600cSBjoern A. Zeeb
580696600cSBjoern A. Zeebvalid_hostid()
590696600cSBjoern A. Zeeb{
600696600cSBjoern A. Zeeb	uuid=$1
610696600cSBjoern A. Zeeb
620696600cSBjoern A. Zeeb	x="[0-9a-f]"
630696600cSBjoern A. Zeeb	y=$x$x$x$x
640696600cSBjoern A. Zeeb
650696600cSBjoern A. Zeeb	# Check against a blacklist before
660696600cSBjoern A. Zeeb	# accepting the UUID.
670696600cSBjoern A. Zeeb	case "${uuid}" in
680696600cSBjoern A. Zeeb	00000000-0000-0000-0000-000000000000)
690696600cSBjoern A. Zeeb		;;
700696600cSBjoern A. Zeeb	00020003-0004-0005-0006-000700080009)
710696600cSBjoern A. Zeeb		;;
720696600cSBjoern A. Zeeb	03000200-0400-0500-0006-000700080009)
730696600cSBjoern A. Zeeb		;;
740696600cSBjoern A. Zeeb	07090201-0103-0301-0807-060504030201)
750696600cSBjoern A. Zeeb		;;
760696600cSBjoern A. Zeeb	11111111-1111-1111-1111-111111111111)
770696600cSBjoern A. Zeeb		;;
780696600cSBjoern A. Zeeb	11111111-2222-3333-4444-555555555555)
790696600cSBjoern A. Zeeb		;;
80*bf847ea3SFranco Fichtner	12345678-1234-5678-90ab-cddeefaabbcc)
81*bf847ea3SFranco Fichtner		;;
820696600cSBjoern A. Zeeb	4c4c4544-0000-2010-8020-80c04f202020)
830696600cSBjoern A. Zeeb		;;
840696600cSBjoern A. Zeeb	58585858-5858-5858-5858-585858585858)
850696600cSBjoern A. Zeeb		;;
860696600cSBjoern A. Zeeb	890e2d14-cacd-45d1-ae66-bc80e8bfeb0f)
870696600cSBjoern A. Zeeb		;;
880696600cSBjoern A. Zeeb	8e275844-178f-44a8-aceb-a7d7e5178c63)
890696600cSBjoern A. Zeeb		;;
900696600cSBjoern A. Zeeb	dc698397-fa54-4cf2-82c8-b1b5307a6a7f)
910696600cSBjoern A. Zeeb		;;
920696600cSBjoern A. Zeeb	fefefefe-fefe-fefe-fefe-fefefefefefe)
930696600cSBjoern A. Zeeb		;;
940696600cSBjoern A. Zeeb	*-ffff-ffff-ffff-ffffffffffff)
950696600cSBjoern A. Zeeb		;;
960696600cSBjoern A. Zeeb	$y$y-$y-$y-$y-$y$y$y)
970696600cSBjoern A. Zeeb		return 0
980696600cSBjoern A. Zeeb		;;
990696600cSBjoern A. Zeeb	esac
1000696600cSBjoern A. Zeeb
1010696600cSBjoern A. Zeeb	return 1
1020696600cSBjoern A. Zeeb}
1030696600cSBjoern A. Zeeb
1040696600cSBjoern A. Zeebhostid_hardware()
1050696600cSBjoern A. Zeeb{
1060696600cSBjoern A. Zeeb	uuid=`kenv -q smbios.system.uuid`
1070696600cSBjoern A. Zeeb
1080696600cSBjoern A. Zeeb	if valid_hostid $uuid; then
1090696600cSBjoern A. Zeeb		echo "${uuid}"
110cc495d3bSColin Percival	elif [ "$uuid" ]; then
111cc495d3bSColin Percival		echo "INVALID"
1120696600cSBjoern A. Zeeb	fi
1130696600cSBjoern A. Zeeb}
1140696600cSBjoern A. Zeeb
1150696600cSBjoern A. Zeebhostid_generate()
1160696600cSBjoern A. Zeeb{
1170696600cSBjoern A. Zeeb	# First look for UUID in hardware.
1180696600cSBjoern A. Zeeb	uuid=`hostid_hardware`
119cc495d3bSColin Percival
120cc495d3bSColin Percival	# Warn about invalid UUIDs
121cc495d3bSColin Percival	if [ "${uuid}" = "INVALID" ]; then
1220696600cSBjoern A. Zeeb		warn "hostid: unable to figure out a UUID from DMI data, generating a new one"
1230696600cSBjoern A. Zeeb		sleep 2
124cc495d3bSColin Percival		uuid=""
125cc495d3bSColin Percival	fi
126cc495d3bSColin Percival
127cc495d3bSColin Percival	# Generate a random UUID if invalid or not found
128cc495d3bSColin Percival	if [ -z "${uuid}" ]; then
1290696600cSBjoern A. Zeeb		# If not found, fall back to software-generated UUID.
130862aab62STijl Coosemans		uuid=`uuidgen ${hostid_uuidgen_flags}`
1310696600cSBjoern A. Zeeb	fi
1320696600cSBjoern A. Zeeb	hostid_set $uuid
1330696600cSBjoern A. Zeeb}
1340696600cSBjoern A. Zeeb
1350696600cSBjoern A. Zeebhostid_reset()
1360696600cSBjoern A. Zeeb{
1370696600cSBjoern A. Zeeb	hostid_generate
1380696600cSBjoern A. Zeeb	# Store newly generated UUID in ${hostid_file}.
1390696600cSBjoern A. Zeeb	echo $uuid > ${hostid_file}
1400696600cSBjoern A. Zeeb	if [ $? -ne 0 ]; then
1410696600cSBjoern A. Zeeb		warn "could not store hostuuid in ${hostid_file}."
1420696600cSBjoern A. Zeeb	fi
1430696600cSBjoern A. Zeeb}
1440696600cSBjoern A. Zeeb
1450696600cSBjoern A. Zeebhostid_start()
1460696600cSBjoern A. Zeeb{
1470696600cSBjoern A. Zeeb	# If ${hostid_file} already exists, we take UUID from there.
1480696600cSBjoern A. Zeeb	if [ -r ${hostid_file} ]; then
1490696600cSBjoern A. Zeeb		read saved_hostid < ${hostid_file}
1500696600cSBjoern A. Zeeb		if valid_hostid ${saved_hostid}; then
15111bd40d0Sнаб			hostid_set ${saved_hostid}
1520696600cSBjoern A. Zeeb			exit 0
1530696600cSBjoern A. Zeeb		fi
1540696600cSBjoern A. Zeeb	fi
1550696600cSBjoern A. Zeeb
1560696600cSBjoern A. Zeeb	# No hostid file, generate UUID.
1570696600cSBjoern A. Zeeb	hostid_generate
1580696600cSBjoern A. Zeeb}
1590696600cSBjoern A. Zeeb
1600696600cSBjoern A. Zeebload_rc_config $name
161f99f0ee1SAlexander Leidinger
162f99f0ee1SAlexander Leidinger# doesn't make sense to run in a svcj: config setting
163f99f0ee1SAlexander Leidingerhostid_svcj="NO"
164f99f0ee1SAlexander Leidinger
1650696600cSBjoern A. Zeebrun_rc_command "$1"
166