Merge pull request #2037 from weaveworks/2021-fallback-to-hypervisor-uuid

fall back to hypervisor uuid for mac derivation
This commit is contained in:
Adam Harrison committed 2016-03-14 16:56:00 +00:00
commit 115162fbfd
1 file changed
+18 -14
+18 -14
View File
@@ -458,6 +458,14 @@ mac_from_hex() {
read a b c d e f && printf "%02x:$b:$c:$d:$e:$f" $((0x$a & ~1 | 2)) read a b c d e f && printf "%02x:$b:$c:$d:$e:$f" $((0x$a & ~1 | 2))
} }
mac_from_file() {
UUID=$(cat $1 2>/dev/null) || return 1
# We salt the input just as a precaution to avoid clashes with
# other applications who might have had the bright idea of
# generating MACs in the same way.
echo "9oBJ0Jmip-$UUID" | sha256sum | sed 's/\(..\)/\1 /g' | cut -c1-17 | mac_from_hex
}
# Generate a random MAC value # Generate a random MAC value
random_mac() { random_mac() {
od -txC -An -N6 /dev/urandom | mac_from_hex od -txC -An -N6 /dev/urandom | mac_from_hex
@@ -627,21 +635,17 @@ init_bridge() {
ip link add name $BRIDGE type bridge ip link add name $BRIDGE type bridge
# Derive the bridge MAC from the system (aka bios) UUID, if # Derive the bridge MAC from the system (aka bios) UUID, or,
# present. Elsewhere we in turn derive the peer name from that, # failing that, the hypervisor UUID. Elsewhere we in turn derive
# which we want to be stable across reboots but otherwise # the peer name from that, which we want to be stable across
# unique. The system UUID fits that bill, unlike, say, # reboots but otherwise unique. The system/hypervisor UUID fits
# /etc/machine-id, which is often identical on VMs created from # that bill, unlike, say, /etc/machine-id, which is often
# cloned filesystems. If we cannot determine the system UUID we # identical on VMs created from cloned filesystems. If we cannot
# just generate a random MAC. # determine the system/hypervisor UUID we just generate a random MAC.
if SYSTEM_UUID=$(cat /sys/class/dmi/id/product_uuid 2>/dev/null) ; then MAC=$(mac_from_file /sys/class/dmi/id/product_uuid) || \
# We salt the input just as a precaution to avoid clashes with MAC=$(mac_from_file /sys/hypervisor/uuid) || \
# other applications who might have had the bright idea of
# generating MACs in the same way.
MAC=$(echo "9oBJ0Jmip-$SYSTEM_UUID" | sha256sum | sed 's/\(..\)/\1 /g' | cut -c1-17 | mac_from_hex)
else
MAC=$(random_mac) MAC=$(random_mac)
fi
ip link set dev $BRIDGE address $MAC ip link set dev $BRIDGE address $MAC
# Attempting to set the bridge MTU to a high value directly # Attempting to set the bridge MTU to a high value directly