confd: simplify gen-hostname, defer MAC expansion to confd

gen-hostname baked the last three MAC octets straight into the generated
hostname, so the actual MAC ended up stored in the configuration.  Copy
that config to another device and its hostname no longer matched the
hardware.

Emit the %h and %m format specifiers and let confd expand them when the
config is applied, so the MAC is resolved per device and never written
to a saved config.  This also drops the /run/system.json lookup.

Closes #1554

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2026-06-29 19:35:04 +02:00
parent f8c68a38af
commit 208f7c9f08
+7 -13
View File
@@ -2,26 +2,20 @@
# Generate ietf-system.yang hostname based on:
# 1. Command line argument + MAC suffix
# 2. Specific hostname from qemu_fw_cfg, from Qeneth
# 3. /etc/hostname from squashfs + MAC suffix
# 3. Default hostname + MAC suffix
#
# The MAC suffix is emitted as the %m format specifier, expanded by confd
# at apply time, so the actual MAC never ends up baked into a saved
# configuration (issue #1554).
#
set -e
gen()
{
base_mac=$(jq -r '.["mac-address"]' /run/system.json)
if [ -z "$base_mac" ] || [ "$base_mac" = "null" ]; then
base_mac=00:00:00:00:00:00
fi
echo "$1-$(echo $base_mac | tail -c 9 | tr ':' '-')"
}
if [ "$1" ]; then
sysname=$(gen "$1")
sysname="$1-%m"
elif [ -f "/sys/firmware/qemu_fw_cfg/by_name/opt/hostname/raw" ]; then
sysname="$(cat /sys/firmware/qemu_fw_cfg/by_name/opt/hostname/raw)"
else
sysname=$(gen "$(cat /etc/hostname)")
sysname="%h-%m"
fi
cat <<EOF