#!/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===" # --- 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" [ -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==="