From aaf1b4988d6aa2c696035ce5283379ffe272232e Mon Sep 17 00:00:00 2001 From: Gilles Soulier Date: Sat, 6 Jun 2026 18:47:44 +0200 Subject: [PATCH] =?UTF-8?q?fix(post-install):=20identity=5Fnetwork=20?= =?UTF-8?q?=C3=A9crit=20r=C3=A9ellement=20hostname=20+=20/etc/hosts=20+=20?= =?UTF-8?q?IP=20statique?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Le template SJ-8 était un stub (echo STATIC_TARGET sans écriture). Désormais : - /etc/hostname + hostnamectl, ligne 127.0.1.1 dans /etc/hosts - IP statique via drop-in /etc/network/interfaces.d/.cfg (ifupdown), neutralisation awk de la strophe DHCP existante + source interfaces.d - sauvegardes horodatées avant écriture ; réseau appliqué AU REBOOT (ne coupe jamais SSH en live) ; FILE_MODIFIED émis après écriture réelle Cible Debian/ifupdown (netinstall). syntaxe sh -n validée sur rendu. Co-Authored-By: Claude Opus 4.8 --- templates/custom/identity-network.sh.tpl | 72 ++++++++++++++++++++---- 1 file changed, 62 insertions(+), 10 deletions(-) diff --git a/templates/custom/identity-network.sh.tpl b/templates/custom/identity-network.sh.tpl index ad1c2e6..2340467 100644 --- a/templates/custom/identity-network.sh.tpl +++ b/templates/custom/identity-network.sh.tpl @@ -1,24 +1,76 @@ #!/bin/sh -# Identité + réseau : hostname, domaine, IP statique. Sauvegarde avant modif. -# Ne coupe jamais la connexion sans stratégie de reconnexion planifiée côté webapp. +# Identité + réseau : hostname, /etc/hosts, IP statique (ifupdown drop-in). +# Le changement d'IP s'applique AU REBOOT (on ne coupe jamais SSH en live). +# Sauvegardes horodatées avant toute écriture. Échec contrôlé. export LC_ALL=C +HOST="{{newHostname}}" +DOMAIN="{{domain}}" +IFACE="{{interfaceName}}" +ADDR="{{staticAddress}}" +GW="{{gateway}}" +DNS="{{dnsNameservers}}" echo "===SU:CUSTOM_IDENTITY===" -cp -a /etc/hosts "/etc/hosts.su.bak.$(date +%s)" 2>/dev/null && echo "FILE_MODIFIED=/etc/hosts" -cp -a /etc/network/interfaces "/etc/network/interfaces.su.bak.$(date +%s)" 2>/dev/null && echo "FILE_MODIFIED=/etc/network/interfaces" + +# --- Sauvegardes --- +TS=$(date +%s) +cp -a /etc/hosts "/etc/hosts.su.bak.${TS}" 2>/dev/null +[ -f /etc/network/interfaces ] && cp -a /etc/network/interfaces "/etc/network/interfaces.su.bak.${TS}" 2>/dev/null +[ -f /etc/hostname ] && cp -a /etc/hostname "/etc/hostname.su.bak.${TS}" 2>/dev/null + echo "OLD_ENDPOINT={{dhcpEndpoint}}" -if hostnamectl set-hostname "{{newHostname}}" 2>&1; then - echo "HOSTNAME_SET={{newHostname}}" + +# --- Hostname (immédiat, ne coupe pas SSH) --- +if hostnamectl set-hostname "$HOST" 2>/dev/null || { printf '%s\n' "$HOST" > /etc/hostname; }; then + printf '%s\n' "$HOST" > /etc/hostname + echo "HOSTNAME_SET=$HOST" + echo "FILE_MODIFIED=/etc/hostname" else echo "ERR=hostname_failed" fi -if ip link show "{{interfaceName}}" >/dev/null 2>&1; then - echo "IFACE_OK={{interfaceName}}" - # Rendu détaillé de /etc/network/interfaces renvoyé à la tâche 4 ; ici on valide la cible. - echo "STATIC_TARGET={{staticAddress}} gw {{gateway}} dns {{dnsNameservers}}" + +# --- /etc/hosts : ligne 127.0.1.1 --- +FQDN="$HOST" +[ -n "$DOMAIN" ] && FQDN="$HOST.$DOMAIN" +if grep -qE '^127\.0\.1\.1' /etc/hosts 2>/dev/null; then + sed -i -E "s|^127\.0\.1\.1.*|127.0.1.1\t${FQDN} ${HOST}|" /etc/hosts && echo "FILE_MODIFIED=/etc/hosts" +else + printf '127.0.1.1\t%s %s\n' "$FQDN" "$HOST" >> /etc/hosts && echo "FILE_MODIFIED=/etc/hosts" +fi + +# --- IP statique (ifupdown drop-in, appliqué au reboot) --- +if ip link show "$IFACE" >/dev/null 2>&1; then + echo "IFACE_OK=$IFACE" + mkdir -p /etc/network/interfaces.d + # S'assure que le fichier principal source le répertoire interfaces.d. + if [ -f /etc/network/interfaces ] && ! grep -qE '^[[:space:]]*source(-directory)?[[:space:]]+/etc/network/interfaces\.d' /etc/network/interfaces; then + printf '\nsource /etc/network/interfaces.d/*\n' >> /etc/network/interfaces + fi + # Neutralise (commente) toute strophe existante de l'interface dans le fichier principal. + if [ -f /etc/network/interfaces ]; then + awk -v IFACE="$IFACE" ' + $0 ~ "^[[:space:]]*(auto|allow-hotplug)[[:space:]]+" IFACE "([[:space:]]|$)" { print "#SU# " $0; next } + $0 ~ "^[[:space:]]*iface[[:space:]]+" IFACE "([[:space:]]|$)" { inblk=1; print "#SU# " $0; next } + inblk==1 && $0 ~ /^[[:space:]]+[^[:space:]]/ { print "#SU# " $0; next } + inblk==1 { inblk=0 } + { print } + ' /etc/network/interfaces > /etc/network/interfaces.su.tmp && cat /etc/network/interfaces.su.tmp > /etc/network/interfaces && rm -f /etc/network/interfaces.su.tmp + fi + # Écrit la configuration statique en drop-in. + { + echo "auto $IFACE" + echo "iface $IFACE inet static" + echo " address $ADDR" + echo " gateway $GW" + [ -n "$DNS" ] && echo " dns-nameservers $DNS" + } > "/etc/network/interfaces.d/${IFACE}.cfg" + echo "FILE_MODIFIED=/etc/network/interfaces.d/${IFACE}.cfg" + echo "STATIC_TARGET=$ADDR gw $GW dns $DNS" else echo "ERR=interface_not_found" fi + echo "NEW_ENDPOINT={{reconnectHost}}" echo "RECONNECT_REQUIRED=1" +echo "NETWORK_APPLIES_ON=reboot" {{#rebootAfterInstall}}echo "REBOOT_REQUESTED=1"{{/rebootAfterInstall}} echo "===SU:EXIT=0==="