summaryrefslogtreecommitdiffstats
path: root/modules.d/slx-uuid/hooks/s3-get-system-uuid.sh
blob: 3669ff4e872dbcf92e19dabef221a6baafca54cb (plain) (blame)
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
#!/bin/ash

# Slighty changed version of:
# http://git.openslx.org/openslx-ng/mltk.git/plain/core/modules/system-uuid/data/bin/get-uuid

UUID=$(dmidecode -q -s system-uuid | grep -v '^#' | head -n 1 | tr 'a-z' 'A-Z')
if [ "${#UUID}" -ne "36" ]; then
	echo "Determined UUID ${UUID} has not expected length of 36, falling back to MAC..."
	UUID=
else
	# Got UUID, check blacklist
	DIR="/etc/bad-uuid.d"
	TMPLIST=$(mktemp)
	BADLIST=$(mktemp)
	for file in "$DIR"/*; do
		[ -f "$file" ] || continue
		# 11111111-2222-3333-4444-555555555555
		< "$file" tr 'a-z' 'A-Z' | grep -Eo '[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}'
	done | tee "$TMPLIST" > "$BADLIST"
	# Also add flipped version of bad uuids. Found some of them through googling and discovered that sometimes
	# users report them in a different order. UUIDs use different endianness for the first three blocks than
	# the remaining two, but some tools seem to ignore that fact.
	< "$BADLIST" sed -r 's/^(..)(..)(..)(..)\-(..)(..)\-(..)(..)\-([0-9A-F]{4}\-[0-9A-F]{12})$/\4\3\2\1-\6\5-\8\7-\9/' >> "$TMPLIST"
	# Finally make unique
	sort -u "$TMPLIST" > "$BADLIST"
	if grep -Fxq "$UUID" "$BADLIST"; then
		echo "WARNING: UUID $UUID is blacklisted as potentially not being unique, using MAC fallback"
		UUID=
	fi
	rm -f -- "$TMPLIST" "$BADLIST"
fi

if [ -z "$UUID" ]; then
	# No UUID, or was bogus - derive from bootif MAC address
	if [ -e /run/openslx/network.conf ]; then
		. /run/openslx/network.conf
	else
		echo "Don't have /run/openslx/network.conf"
	fi

	if [ "${#SLX_PXE_MAC}" != 17 ]; then
		echo "Getting MAC from /run/openslx/network.conf failed, looking up in sysfs"
		for iface in "${SLX_BRIDGE:-br0}" "${SLX_PXE_NETIF:-boot0}" eth0; do
			SLX_PXE_MAC=$( cat "/sys/class/net/$iface/address" )
			[ "${#SLX_PXE_MAC}" = "17" ] && break
		done
		if [ "${#SLX_PXE_MAC}" != "17" ]; then
			echo "Getting MAC from sysfs failed, using bogus value..."
			SLX_PXE_MAC="88:77:66:55:44:33"
		fi
	fi
	UUID="$( echo "$SLX_PXE_MAC" | sed -r 's/[^0-9A-Fa-f]//g' )"
	if [ "${#UUID}" = 12 ]; then
		UUID="baad1d00-9491-4716-b98b-$UUID"
	else
		echo "Could not derive UUID from MAC (got '$UUID'), using bogus fallback"
		UUID="baad1d00-9491-4716-b98b-000000000000"
	fi
fi
UUID=$( echo "$UUID" | tr 'a-z' 'A-Z' )
echo "Final UUID is $UUID"
echo "$UUID" > "/run/system-uuid"