Project

General

Profile

Download (3.39 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/bin/sh
2
#
3
# Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
4
# Copyright (c) 2015 Xin LI <delphij@FreeBSD.org>
5
# All rights reserved.
6
#
7
# Redistribution and use in source and binary forms, with or without
8
# modification, are permitted provided that the following conditions
9
# are met:
10
# 1. Redistributions of source code must retain the above copyright
11
#    notice, this list of conditions and the following disclaimer.
12
# 2. Redistributions in binary form must reproduce the above copyright
13
#    notice, this list of conditions and the following disclaimer in the
14
#    documentation and/or other materials provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
# SUCH DAMAGE.
27
#
28
# $FreeBSD$
29
#
30

    
31
# PROVIDE: hostid
32
# REQUIRE: sysctl
33
# KEYWORD: nojail
34

    
35
. /etc/rc.subr
36

    
37
name="hostid"
38
start_cmd="hostid_start"
39
stop_cmd=":"
40
reset_cmd="hostid_reset"
41
extra_commands="reset"
42
rcvar="hostid_enable"
43

    
44
hostid_set()
45
{
46
	uuid=$1
47
	# Generate hostid based on hostuuid - take first four bytes from md5(uuid).
48
	id=`echo -n $uuid | /sbin/md5`
49
	id="0x${id%????????????????????????}"
50

    
51
	# Set both kern.hostuuid and kern.hostid.
52
	#
53
	check_startmsgs && echo "Setting hostuuid: ${uuid}."
54
	${SYSCTL} kern.hostuuid="${uuid}" >/dev/null
55
	check_startmsgs && echo "Setting hostid: ${id}."
56
	${SYSCTL} kern.hostid=${id} >/dev/null
57
}
58

    
59
valid_hostid()
60
{
61
	uuid=$1
62

    
63
	x="[0-9a-f]"
64
	y=$x$x$x$x
65

    
66
	# Check against a blacklist before
67
	# accepting the UUID.
68
	case "${uuid}" in
69
	00000000-0000-0000-0000-000000000000)
70
		;;
71
	00020003-0004-0005-0006-000700080009)
72
		;;
73
	03000200-0400-0500-0006-000700080009)
74
		;;
75
	07090201-0103-0301-0807-060504030201)
76
		;;
77
	11111111-1111-1111-1111-111111111111)
78
		;;
79
	11111111-2222-3333-4444-555555555555)
80
		;;
81
	4c4c4544-0000-2010-8020-80c04f202020)
82
		;;
83
	58585858-5858-5858-5858-585858585858)
84
		;;
85
	890e2d14-cacd-45d1-ae66-bc80e8bfeb0f)
86
		;;
87
	8e275844-178f-44a8-aceb-a7d7e5178c63)
88
		;;
89
	dc698397-fa54-4cf2-82c8-b1b5307a6a7f)
90
		;;
91
	fefefefe-fefe-fefe-fefe-fefefefefefe)
92
		;;
93
	*-ffff-ffff-ffff-ffffffffffff)
94
		;;
95
	$y$y-$y-$y-$y-$y$y$y)
96
		return 0
97
		;;
98
	esac
99

    
100
	return 1
101
}
102

    
103
hostid_generate()
104
{
105
	# First look for UUID in hardware.
106
	# If not found, fall back to software-generated UUID.
107
	uuid=`uuidgen`
108
	hostid_set $uuid
109
}
110

    
111
hostid_reset()
112
{
113
	hostid_generate
114
	# Store newly generated UUID in ${hostid_file}.
115
	echo $uuid > ${hostid_file}
116
	if [ $? -ne 0 ]; then
117
		warn "could not store hostuuid in ${hostid_file}."
118
	fi
119
}
120

    
121
hostid_start()
122
{
123
	# If ${hostid_file} already exists, we take UUID from there.
124
	if [ -r ${hostid_file} ]; then
125
		read saved_hostid < ${hostid_file}
126
		if valid_hostid ${saved_hostid}; then
127
			hostid_set `cat ${hostid_file}`
128
			exit 0
129
		fi
130
	fi
131

    
132
	# No hostid file, generate UUID.
133
	hostid_generate
134
}
135

    
136
load_rc_config $name
137
run_rc_command "$1"
(2-2/3)