Files
system_update/templates/custom/identity-network.sh.tpl
T
gilles 3ea2e66359 fix(post-install): identity_network cadré Debian/ifupdown (VM) avec précheck
- précheck en tête : refuse proprement si OS != debian (os_not_supported),
  si netplan présent (unsupported_network_manager) ou si /etc/network/interfaces
  absent (ifupdown_not_found) — au lieu d'écrire une conf inopérante
- manifeste : label « (Debian/VM) » + description précisant la cible ifupdown
  et l'application au reboot

Validé en réel sur Debian VM (ens18) : strophe DHCP commentée + drop-in statique.
sh -n OK · tsc 0 · 113 tests.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-06 19:03:28 +02:00

98 lines
3.7 KiB
Smarty

#!/bin/sh
# 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==="
# --- Précheck : Debian + ifupdown uniquement (MVP, cible VM netinstall) ---
. /etc/os-release 2>/dev/null
if [ "$ID" != "debian" ]; then
echo "ERR=os_not_supported"
echo "DETAIL=identity_network ne gère que Debian (ID=$ID)"
echo "===SU:EXIT=2==="
exit 2
fi
if ls /etc/netplan/*.yaml >/dev/null 2>&1; then
echo "ERR=unsupported_network_manager"
echo "DETAIL=netplan détecté ; ce profil cible ifupdown"
echo "===SU:EXIT=2==="
exit 2
fi
if [ ! -f /etc/network/interfaces ]; then
echo "ERR=ifupdown_not_found"
echo "DETAIL=/etc/network/interfaces absent"
echo "===SU:EXIT=2==="
exit 2
fi
# --- 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}}"
# --- 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
# --- /etc/hosts : ligne 127.0.1.1 <fqdn> <host> ---
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==="