diff --git a/scripts/hostname-helper.sh b/scripts/hostname-helper.sh index b51e0424..7b7f46f3 100755 --- a/scripts/hostname-helper.sh +++ b/scripts/hostname-helper.sh @@ -1,5 +1,5 @@ #!/bin/bash -# Give each board a hostname derived from its CPU serial, before the network comes up. +# Give each board a hostname from its CPU serial and, early in boot, its own machine identity. # Pass --install-only to refresh the unit without renaming the running system. set -eu @@ -99,6 +99,95 @@ set_hostname() { ensure_hosts_entry "$want" } +new_machine_id() { + local root="$1" id tmp + id="$(tr -d '-' /dev/null || return 1 + as_root chmod 0444 "$tmp" || return 1 + as_root mv -f "$tmp" "$root/etc/machine-id" || return 1 + # A regular-file copy would keep the old id for dbus. + if [ -d "$root/var/lib/dbus" ]; then + as_root ln -sfn /etc/machine-id "$root/var/lib/dbus/machine-id" || return 1 + fi +} + +stage_ssh_host_keys() { + local stage="$1" + as_root rm -rf "$stage" + if ! as_root mkdir -p "$stage/etc/ssh" || ! as_root ssh-keygen -A -f "$stage" >/dev/null; then + as_root rm -rf "$stage" + echo_error "ssh-keygen failed, keeping the current SSH host keys" + return 1 + fi +} + +install_ssh_host_keys() { + local root="$1" stage="$2" key stale=() + # Old types ssh-keygen -A no longer makes stay shared, and host certificates no longer match the new keys. + for key in "$root"/etc/ssh/ssh_host_*; do + if [ -e "$key" ] && [ ! -e "$stage/etc/ssh/${key##*/}" ]; then + stale+=("$key") + fi + done + # Install before deleting, so a failed rename never leaves a key type missing. + as_root mv -f "$stage"/etc/ssh/ssh_host_* "$root/etc/ssh/" || return 1 + if [ "${#stale[@]}" -gt 0 ]; then + as_root rm -f "${stale[@]}" || echo_error "Could not remove stale SSH host keys" + fi + as_root rm -rf "$stage" || true +} + +# Only v2 keys ("nm-v2:") are hashed with the machine-id; NM writes a fresh v2 key when none exists. +drop_v1_nm_secret_key() { + local key="$1/var/lib/NetworkManager/secret_key" + as_root test -f "$key" || return 0 + [ "$(as_root head -c 6 "$key" | tr -d '\0')" = "nm-v2:" ] && return 0 + as_root rm -f "$key" +} + +# Before basic.target nothing has read the identity; as root, systemctl reaches PID 1 without dbus. +early_boot() { + [ "$(systemctl is-system-running 2>/dev/null || true)" = "initializing" ] +} + +# Flushes the step's changes first, so the marker never claims work still in the page cache. +mark_done() { + local marker="$1" serial="$2" + sync + as_root mkdir -p "$(dirname "$marker")" || return 1 + printf '%s\n' "$serial" | as_root tee "$marker" >/dev/null || return 1 + sync +} + +# One marker per step: a later failure retries only that step, never re-rolling an earlier one. +ensure_identity() { + local serial="$1" root="${2:-}" state + state="$root/var/lib/blocksscreen" + # Cloned images share machine-id (NetworkManager's IPv6 address seed) and SSH host keys. + if [ "$(cat "$state/identity-serial" 2>/dev/null || true)" != "$serial" ]; then + echo_info "First boot on this board, regenerating machine-id..." + new_machine_id "$root" || return 0 + drop_v1_nm_secret_key "$root" || echo_error "Could not remove the v1 NetworkManager secret_key" + mark_done "$state/identity-serial" "$serial" || return 0 + echo_ok "machine-id regenerated" + fi + if command -v ssh-keygen >/dev/null && + [ "$(cat "$state/hostkeys-serial" 2>/dev/null || true)" != "$serial" ]; then + echo_info "Regenerating SSH host keys..." + stage_ssh_host_keys "$root/etc/ssh/.hostkeys-new" || return 0 + install_ssh_host_keys "$root" "$root/etc/ssh/.hostkeys-new" || return 0 + mark_done "$state/hostkeys-serial" "$serial" || return 0 + echo_ok "SSH host keys regenerated" + fi +} + main() { echo_info "Script running on $SCRIPT_PATH..." install_service @@ -114,6 +203,8 @@ main() { esac set_hostname "$BRAND_PREFIX-$MODEL-$SERIAL" + early_boot && ensure_identity "$SERIAL" + return 0 } # Sourcing (tests) only defines the functions. diff --git a/tests/scripts/test_hostname_helper.py b/tests/scripts/test_hostname_helper.py index 622d026b..82ce6fe9 100644 --- a/tests/scripts/test_hostname_helper.py +++ b/tests/scripts/test_hostname_helper.py @@ -1,7 +1,10 @@ -"""Guard: hostname-helper.sh leaves exactly one 127.0.1.1 line in /etc/hosts.""" +"""Guard: hostname-helper.sh keeps /etc/hosts tidy and regenerates cloned identity.""" from __future__ import annotations +import os +import re +import shutil import subprocess from pathlib import Path @@ -50,3 +53,128 @@ def test_correct_file_is_left_alone(tmp_path: Path) -> None: before = hosts.stat().st_mtime_ns assert "Rewriting" not in _ensure(hosts) assert hosts.stat().st_mtime_ns == before + + +_MACHINE_ID = Path("etc/machine-id") +_DBUS_MACHINE_ID = Path("var/lib/dbus/machine-id") +_HOST_KEY = Path("etc/ssh/ssh_host_ed25519_key.pub") +_STALE_KEY = Path("etc/ssh/ssh_host_dsa_key") +_NM_SECRET = Path("var/lib/NetworkManager/secret_key") +_ID_MARKER = Path("var/lib/blocksscreen/identity-serial") +_KEYS_MARKER = Path("var/lib/blocksscreen/hostkeys-serial") +_IMAGE_ID = "0" * 31 + "1\n" +_IMAGE_KEY = "ssh-ed25519 IMAGE-KEY root@image\n" +_SERIAL = "1000000000000001" +_OTHER_SERIAL = "1000000000000002" + + +@pytest.fixture +def cloned_root(tmp_path: Path) -> Path: + """Fake root filesystem carrying the identity shared by a cloned image.""" + root = tmp_path / "root" + for rel, content in ( + (_MACHINE_ID, _IMAGE_ID), + (_DBUS_MACHINE_ID, _IMAGE_ID), + (_HOST_KEY, _IMAGE_KEY), + (_STALE_KEY, _IMAGE_KEY), + ): + (root / rel).parent.mkdir(parents=True, exist_ok=True) + (root / rel).write_text(content) + return root + + +def _ensure_identity(root: Path, serial: str, path: str | None = None) -> None: + """Source the helper with root writes stubbed out and run ensure_identity.""" + script = f'source "{_HELPER}"; as_root() {{ "$@"; }}; ensure_identity "$1" "$2"' + subprocess.run( + ["bash", "-c", script, "bash", serial, str(root)], + capture_output=True, + text=True, + timeout=60, + check=True, + env={**os.environ, "PATH": path or os.environ["PATH"]}, + ) + + +_NEEDS_KEYGEN = pytest.mark.skipif( + shutil.which("ssh-keygen") is None, reason="needs ssh-keygen" +) + + +@_NEEDS_KEYGEN +def test_identity_regenerates_once_per_serial(cloned_root: Path) -> None: + _ensure_identity(cloned_root, _SERIAL) + machine_id = (cloned_root / _MACHINE_ID).read_text() + assert re.fullmatch(r"[0-9a-f]{32}\n", machine_id) and machine_id != _IMAGE_ID + assert os.readlink(cloned_root / _DBUS_MACHINE_ID) == "/etc/machine-id" + host_key = (cloned_root / _HOST_KEY).read_text() + assert host_key.startswith("ssh-ed25519 ") and host_key != _IMAGE_KEY + assert not (cloned_root / "etc/ssh/.hostkeys-new").exists() + assert not (cloned_root / _STALE_KEY).exists() + assert (cloned_root / _ID_MARKER).read_text() == f"{_SERIAL}\n" + assert (cloned_root / _KEYS_MARKER).read_text() == f"{_SERIAL}\n" + + _ensure_identity(cloned_root, _SERIAL) + assert (cloned_root / _MACHINE_ID).read_text() == machine_id + + # Same SD card moved to another board. + _ensure_identity(cloned_root, _OTHER_SERIAL) + assert (cloned_root / _MACHINE_ID).read_text() != machine_id + + +@_NEEDS_KEYGEN +def test_identity_keygen_failure_retries_only_the_keys( + cloned_root: Path, tmp_path: Path +) -> None: + stubs = tmp_path / "bin" + stubs.mkdir() + keygen = stubs / "ssh-keygen" + keygen.write_text("#!/bin/sh\nexit 1\n") + keygen.chmod(0o755) + failing = f"{stubs}:{os.environ['PATH']}" + _ensure_identity(cloned_root, _SERIAL, path=failing) + machine_id = (cloned_root / _MACHINE_ID).read_text() + assert machine_id != _IMAGE_ID + + _ensure_identity(cloned_root, _SERIAL, path=failing) + assert (cloned_root / _MACHINE_ID).read_text() == machine_id + assert (cloned_root / _HOST_KEY).read_text() == _IMAGE_KEY + assert not (cloned_root / _KEYS_MARKER).exists() + + _ensure_identity(cloned_root, _SERIAL) + assert (cloned_root / _MACHINE_ID).read_text() == machine_id + assert (cloned_root / _HOST_KEY).read_text() != _IMAGE_KEY + + +@pytest.mark.parametrize( + ("key", "kept"), + [(b"nm-v2:" + b"\xa5" * 32, True), (b"\0\xa5" * 16, False)], + ids=["v2", "v1-binary"], +) +def test_identity_drops_only_v1_nm_secret_key( + cloned_root: Path, key: bytes, kept: bool +) -> None: + secret = cloned_root / _NM_SECRET + secret.parent.mkdir(parents=True) + secret.write_bytes(key) + _ensure_identity(cloned_root, _SERIAL) + assert secret.exists() == kept + + +@pytest.mark.parametrize( + ("state", "expected"), + [("initializing", 0), ("starting", 1), ("running", 1), ("degraded", 1)], +) +def test_identity_only_before_basic_target( + tmp_path: Path, state: str, expected: int +) -> None: + systemctl = tmp_path / "systemctl" + systemctl.write_text(f"#!/bin/sh\necho {state}\nexit 1\n") + systemctl.chmod(0o755) + res = subprocess.run( + ["bash", "-c", f'source "{_HELPER}"; early_boot'], + capture_output=True, + timeout=10, + env={**os.environ, "PATH": f"{tmp_path}:{os.environ['PATH']}"}, + ) + assert res.returncode == expected