Files
2026-06-19 09:59:49 +00:00

46 lines
2.3 KiB
Bash
Executable File

#!/bin/sh
# fleet-namer — dnsmasq --dhcp-script that assigns a stable, creative cyber
# sci-fi hostname per host, SERVER-SIDE, so the workstation image stays neutral.
#
# dnsmasq calls this on every lease event with: $1=action(add|old|del)
# $2=MAC $3=IP [$4=hostname]. For a MAC we haven't named yet, we derive a name
# DETERMINISTICALLY from the MAC (sha256 -> curated wordlists), append a
# reservation (MAC,name) to the dhcp-hostsfile, and SIGHUP dnsmasq so it re-reads
# it. dnsmasq then hands the name out as DHCP option 12 + registers DNS A/PTR;
# NetworkManager on the host sets it as the hostname.
#
# Same MAC -> same name forever (idempotent). The host picks up the name on its
# next DHCP transaction (renewal at ~T1, or reboot) — see the lease-time note in
# docker-compose.yml. Manual reservations in the hostsfile always win (we skip a
# MAC that's already listed).
set -eu
action="$1"; mac="$2"
case "$action" in add|old) ;; *) exit 0 ;; esac
[ -n "$mac" ] || exit 0
HOSTSFILE="${FLEET_HOSTSFILE:-/etc/dnsmasq-hosts}"
# Already named (auto or manual)? leave it.
grep -qi "^$mac," "$HOSTSFILE" 2>/dev/null && exit 0
# Curated DNS-safe wordlists (single line, single-spaced — indexed with cut).
PREFIX="nova nebula quasar pulsar comet vega rigel orion lyra cygnus draco hydra phoenix titan atlas helios aurora zenith vortex ion plasma photon quantum warp aether astral stellar lunar solar cobalt obsidian halcyon"
NOUN="cruiser frigate vanguard sentinel ranger voyager pioneer horizon beacon spectre nomad drifter wraith talon raptor falcon meridian apex helm reactor relay array lance aegis ember corsair seeker harbinger paragon warden monolith"
np=$(echo "$PREFIX" | wc -w)
nn=$(echo "$NOUN" | wc -w)
# Deterministic indices from the MAC's sha256 (hex chunks mod list length).
h=$(printf '%s' "$mac" | sha256sum | cut -c1-12)
pi=$(( 0x$(echo "$h" | cut -c1-4) % np + 1 ))
ni=$(( 0x$(echo "$h" | cut -c5-8) % nn + 1 ))
num=$(( 0x$(echo "$h" | cut -c9-12) % 100 ))
name=$(printf '%s-%s-%02d' "$(echo "$PREFIX" | cut -d' ' -f"$pi")" "$(echo "$NOUN" | cut -d' ' -f"$ni")" "$num")
# Register + reload dnsmasq (re-reads --dhcp-hostsfile on SIGHUP). dnsmasq is
# PID 1 in this container; the script runs as root (--user=root in compose).
echo "$mac,$name" >> "$HOSTSFILE"
kill -HUP 1 2>/dev/null || true
echo "fleet-namer: $mac -> $name" >&2
exit 0