#!/bin/sh # 60-hostname.sh — Set system hostname # If cloud-init (stage 45) already set the hostname, skip this stage. # Cloud-init writes /etc/hostname and saves to data partition if [ "$CLOUDINIT_APPLIED" = "1" ] && [ -f /etc/hostname ]; then HOSTNAME="$(cat /etc/hostname)" if [ -n "$HOSTNAME" ]; then log "Hostname already set by cloud-init: $HOSTNAME" return 0 fi fi if [ -f "$DATA_MOUNT/etc-kubesolo/hostname" ]; then HOSTNAME="$(cat "$DATA_MOUNT/etc-kubesolo/hostname")" elif [ -f /etc/kubesolo/hostname ]; then HOSTNAME="$(cat /etc/kubesolo/hostname)" else # Generate hostname from MAC address of primary interface MAC_SUFFIX="" for iface in /sys/class/net/*; do iface="$(basename "$iface")" case "$iface" in lo|docker*|veth*|br*|cni*) continue ;; esac MAC_SUFFIX="$(cat "/sys/class/net/$iface/address" 2>/dev/null | tr -d ':' | tail -c 7)" break done HOSTNAME="kubesolo-${MAC_SUFFIX:-unknown}" fi hostname "$HOSTNAME" echo "$HOSTNAME" > /etc/hostname echo "127.0.0.1 $HOSTNAME" >> /etc/hosts # Generate /etc/machine-id if missing (kubelet requires it) if [ ! -f /etc/machine-id ]; then if [ -f "$DATA_MOUNT/etc-kubesolo/machine-id" ]; then cp "$DATA_MOUNT/etc-kubesolo/machine-id" /etc/machine-id else # Generate from hostname hash (deterministic across reboots) printf '%s' "$HOSTNAME" | md5sum 2>/dev/null | cut -d' ' -f1 > /etc/machine-id || \ cat /proc/sys/kernel/random/uuid 2>/dev/null | tr -d '-' > /etc/machine-id || true # Persist for next boot cp /etc/machine-id "$DATA_MOUNT/etc-kubesolo/machine-id" 2>/dev/null || true fi fi log_ok "Hostname set to: $HOSTNAME"