1168546Spjd#!/bin/sh
2168546Spjd#
3168546Spjd# Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
4168546Spjd# All rights reserved.
5168546Spjd#
6168546Spjd# Redistribution and use in source and binary forms, with or without
7168546Spjd# modification, are permitted provided that the following conditions
8168546Spjd# are met:
9168546Spjd# 1. Redistributions of source code must retain the above copyright
10168546Spjd#    notice, this list of conditions and the following disclaimer.
11168546Spjd# 2. Redistributions in binary form must reproduce the above copyright
12168546Spjd#    notice, this list of conditions and the following disclaimer in the
13168546Spjd#    documentation and/or other materials provided with the distribution.
14168546Spjd#
15168546Spjd# THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16168546Spjd# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17168546Spjd# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18168546Spjd# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19168546Spjd# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20168546Spjd# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21168546Spjd# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22168546Spjd# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23168546Spjd# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24168546Spjd# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25168546Spjd# SUCH DAMAGE.
26168546Spjd#
27168546Spjd# $FreeBSD$
28168546Spjd#
29168546Spjd
30168546Spjd# PROVIDE: hostid
31208307Sdougb# REQUIRE: sysctl
32168546Spjd# KEYWORD: nojail
33168546Spjd
34168546Spjd. /etc/rc.subr
35168546Spjd
36168546Spjdname="hostid"
37168546Spjdstart_cmd="hostid_start"
38168546Spjdstop_cmd=":"
39168546Spjdreset_cmd="hostid_reset"
40168546Spjdextra_commands="reset"
41168546Spjdrcvar="hostid_enable"
42168546Spjd
43168546Spjdhostid_set()
44168546Spjd{
45168546Spjd	uuid=$1
46168546Spjd	# Generate hostid based on hostuuid - take first four bytes from md5(uuid).
47178809Smtm	id=`echo -n $uuid | /sbin/md5`
48168546Spjd	id="0x${id%????????????????????????}"
49179945Smtm
50168546Spjd	# Set both kern.hostuuid and kern.hostid.
51179945Smtm	#
52197947Sdougb	check_startmsgs && echo "Setting hostuuid: ${uuid}."
53220153Semaste	${SYSCTL} kern.hostuuid="${uuid}" >/dev/null
54197947Sdougb	check_startmsgs && echo "Setting hostid: ${id}."
55220153Semaste	${SYSCTL} kern.hostid=${id} >/dev/null
56168546Spjd}
57168546Spjd
58168607Spjdhostid_hardware()
59168607Spjd{
60175618Sru	uuid=`kenv -q smbios.system.uuid`
61169818Srse	x="[0-9a-f]"
62168607Spjd	y=$x$x$x$x
63168607Spjd	case "${uuid}" in
64168607Spjd	$y$y-$y-$y-$y-$y$y$y)
65168607Spjd		echo "${uuid}"
66168607Spjd		;;
67168607Spjd	esac
68168607Spjd}
69168607Spjd
70195938Spjdhostid_generate()
71168546Spjd{
72168607Spjd	# First look for UUID in hardware.
73168607Spjd	uuid=`hostid_hardware`
74168607Spjd	if [ -z ${uuid} ]; then
75168607Spjd		# If not found, fall back to software-generated UUID.
76168607Spjd		uuid=`uuidgen`
77168607Spjd	fi
78195938Spjd	hostid_set $uuid
79195938Spjd}
80195938Spjd
81195938Spjdhostid_reset()
82195938Spjd{
83195938Spjd	hostid_generate
84168546Spjd	# Store newly generated UUID in ${hostid_file}.
85168546Spjd	echo $uuid > ${hostid_file}
86168546Spjd	if [ $? -ne 0 ]; then
87168546Spjd		warn "could not store hostuuid in ${hostid_file}."
88168546Spjd	fi
89168546Spjd}
90168546Spjd
91168546Spjdhostid_start()
92168546Spjd{
93168546Spjd	# If ${hostid_file} already exists, we take UUID from there.
94168546Spjd	if [ -r ${hostid_file} ]; then
95168546Spjd		hostid_set `cat ${hostid_file}`
96168546Spjd	else
97168546Spjd		# No hostid file, generate UUID.
98195938Spjd		hostid_generate
99168546Spjd	fi
100168546Spjd}
101168546Spjd
102168546Spjdload_rc_config $name
103168546Spjdrun_rc_command "$1"
104