hostid revision 282745
1168546Spjd#!/bin/sh 2168546Spjd# 3168546Spjd# Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org> 4282114Sdelphij# Copyright (c) 2015 Xin LI <delphij@FreeBSD.org> 5168546Spjd# All rights reserved. 6168546Spjd# 7168546Spjd# Redistribution and use in source and binary forms, with or without 8168546Spjd# modification, are permitted provided that the following conditions 9168546Spjd# are met: 10168546Spjd# 1. Redistributions of source code must retain the above copyright 11168546Spjd# notice, this list of conditions and the following disclaimer. 12168546Spjd# 2. Redistributions in binary form must reproduce the above copyright 13168546Spjd# notice, this list of conditions and the following disclaimer in the 14168546Spjd# documentation and/or other materials provided with the distribution. 15168546Spjd# 16168546Spjd# THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 17168546Spjd# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18168546Spjd# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19168546Spjd# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 20168546Spjd# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21168546Spjd# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22168546Spjd# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23168546Spjd# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24168546Spjd# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25168546Spjd# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26168546Spjd# SUCH DAMAGE. 27168546Spjd# 28168546Spjd# $FreeBSD: head/etc/rc.d/hostid 282745 2015-05-11 00:16:32Z delphij $ 29168546Spjd# 30168546Spjd 31168546Spjd# PROVIDE: hostid 32208307Sdougb# REQUIRE: sysctl 33168546Spjd# KEYWORD: nojail 34168546Spjd 35168546Spjd. /etc/rc.subr 36168546Spjd 37168546Spjdname="hostid" 38168546Spjdstart_cmd="hostid_start" 39168546Spjdstop_cmd=":" 40168546Spjdreset_cmd="hostid_reset" 41168546Spjdextra_commands="reset" 42168546Spjdrcvar="hostid_enable" 43168546Spjd 44168546Spjdhostid_set() 45168546Spjd{ 46168546Spjd uuid=$1 47168546Spjd # Generate hostid based on hostuuid - take first four bytes from md5(uuid). 48178809Smtm id=`echo -n $uuid | /sbin/md5` 49168546Spjd id="0x${id%????????????????????????}" 50179945Smtm 51168546Spjd # Set both kern.hostuuid and kern.hostid. 52179945Smtm # 53197947Sdougb check_startmsgs && echo "Setting hostuuid: ${uuid}." 54220153Semaste ${SYSCTL} kern.hostuuid="${uuid}" >/dev/null 55197947Sdougb check_startmsgs && echo "Setting hostid: ${id}." 56220153Semaste ${SYSCTL} kern.hostid=${id} >/dev/null 57168546Spjd} 58168546Spjd 59282114Sdelphijvalid_hostid() 60168607Spjd{ 61282745Sdelphij uuid=$1 62282114Sdelphij 63169818Srse x="[0-9a-f]" 64168607Spjd y=$x$x$x$x 65282114Sdelphij 66282114Sdelphij # Check against a blacklist before 67282114Sdelphij # accepting the UUID. 68168607Spjd case "${uuid}" in 69282114Sdelphij 00000000-0000-0000-0000-000000000000) 70282114Sdelphij ;; 71282114Sdelphij 00020003-0004-0005-0006-000700080009) 72282114Sdelphij ;; 73282114Sdelphij 03000200-0400-0500-0006-000700080009) 74282114Sdelphij ;; 75282114Sdelphij 07090201-0103-0301-0807-060504030201) 76282114Sdelphij ;; 77282114Sdelphij 11111111-1111-1111-1111-111111111111) 78282114Sdelphij ;; 79282114Sdelphij 11111111-2222-3333-4444-555555555555) 80282114Sdelphij ;; 81282114Sdelphij 4c4c4544-0000-2010-8020-80c04f202020) 82282114Sdelphij ;; 83282114Sdelphij 58585858-5858-5858-5858-585858585858) 84282114Sdelphij ;; 85282114Sdelphij 890e2d14-cacd-45d1-ae66-bc80e8bfeb0f) 86282114Sdelphij ;; 87282114Sdelphij 8e275844-178f-44a8-aceb-a7d7e5178c63) 88282114Sdelphij ;; 89282114Sdelphij dc698397-fa54-4cf2-82c8-b1b5307a6a7f) 90282114Sdelphij ;; 91282114Sdelphij fefefefe-fefe-fefe-fefe-fefefefefefe) 92282114Sdelphij ;; 93282114Sdelphij *-ffff-ffff-ffff-ffffffffffff) 94282114Sdelphij ;; 95168607Spjd $y$y-$y-$y-$y-$y$y$y) 96282114Sdelphij return 0 97168607Spjd ;; 98168607Spjd esac 99282114Sdelphij 100282114Sdelphij return 1 101168607Spjd} 102168607Spjd 103282114Sdelphijhostid_hardware() 104282114Sdelphij{ 105282114Sdelphij uuid=`kenv -q smbios.system.uuid` 106282114Sdelphij 107282114Sdelphij if valid_hostid $uuid; then 108282114Sdelphij echo "${uuid}" 109282114Sdelphij fi 110282114Sdelphij} 111282114Sdelphij 112195938Spjdhostid_generate() 113168546Spjd{ 114168607Spjd # First look for UUID in hardware. 115168607Spjd uuid=`hostid_hardware` 116282114Sdelphij if [ -z "${uuid}" ]; then 117282114Sdelphij warn "hostid: unable to figure out a UUID from DMI data, generating a new one" 118282114Sdelphij sleep 2 119168607Spjd # If not found, fall back to software-generated UUID. 120168607Spjd uuid=`uuidgen` 121168607Spjd fi 122195938Spjd hostid_set $uuid 123195938Spjd} 124195938Spjd 125195938Spjdhostid_reset() 126195938Spjd{ 127195938Spjd hostid_generate 128168546Spjd # Store newly generated UUID in ${hostid_file}. 129168546Spjd echo $uuid > ${hostid_file} 130168546Spjd if [ $? -ne 0 ]; then 131168546Spjd warn "could not store hostuuid in ${hostid_file}." 132168546Spjd fi 133168546Spjd} 134168546Spjd 135168546Spjdhostid_start() 136168546Spjd{ 137168546Spjd # If ${hostid_file} already exists, we take UUID from there. 138168546Spjd if [ -r ${hostid_file} ]; then 139282114Sdelphij read saved_hostid < ${hostid_file} 140282114Sdelphij if valid_hostid ${saved_hostid}; then 141282114Sdelphij hostid_set `cat ${hostid_file}` 142282114Sdelphij exit 0 143282114Sdelphij fi 144168546Spjd fi 145282114Sdelphij 146282114Sdelphij # No hostid file, generate UUID. 147282114Sdelphij hostid_generate 148168546Spjd} 149168546Spjd 150168546Spjdload_rc_config $name 151168546Spjdrun_rc_command "$1" 152