diff --git a/README.md b/README.md index a0a5388..d0826f2 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,34 @@ # pvedhcp -Configuration du DHCP sur un serveur Proxmox 9 \ No newline at end of file +Configuration du DHCP sur un serveur Proxmox 9 + +## Utilisation + +Sur un hôte Proxmox VE 9 (Debian 13 "trixie") fraîchement installé, en root : + +```bash +git clone https://git.sdeinfo.app/sde/pvedhcp.git +cd pvedhcp +chmod +x install-pve-netconfig.sh +./install-pve-netconfig.sh +``` + +Le script installe les dépendances (`jq`, `iproute2`, un client DHCP si +nécessaire), copie les scripts et unités systemd, installe les hooks DHCP, +**puis active automatiquement le DHCP** sur `vmbr0`/`vmbr1` (génération de +`/etc/network/interfaces`, tentative de bail DHCP avec bascule automatique +sur une IP statique de secours en cas d'échec, démarrage des services de +surveillance). + +⚠️ Une rupture réseau temporaire est possible pendant l'application de la +configuration : prévoir un accès console (iDRAC/IPMI/KVM) par précaution. + +Pour ne pas appliquer automatiquement la configuration réseau (installation +des fichiers uniquement, activation manuelle ensuite) : + +```bash +SKIP_AUTO_APPLY=1 ./install-pve-netconfig.sh +``` + +Personnalisation (IP de secours, ports réseau, DNS...) : éditer +`/usr/local/sbin/pve-netconfig.sh` puis relancer `pve-netconfig.sh apply-interfaces`. \ No newline at end of file diff --git a/dhclient-hook b/dhclient-hook new file mode 100755 index 0000000..5c6654a --- /dev/null +++ b/dhclient-hook @@ -0,0 +1,24 @@ +#!/bin/bash +#=============================================================================== +# Hook de sortie dhclient (isc-dhcp-client). +# Installe dans /etc/dhcp/dhclient-exit-hooks.d/pve-netconfig +# +# Declenche a chaque evenement du client DHCP (BOUND, RENEW, REBIND, EXPIRE...). +# Sur obtention/renouvellement de bail : synchronise /etc/hosts et leve un +# eventuel fallback statique. dhclient exporte $interface et $reason. +#=============================================================================== + +SCRIPT="/usr/local/sbin/pve-netconfig.sh" + +[[ -x "$SCRIPT" ]] || exit 0 + +case "${reason:-}" in + BOUND|RENEW|REBIND|REBOOT|STATIC) + "$SCRIPT" sync-hosts >/dev/null 2>&1 & + ;; + EXPIRE|NOCARRIER|FAIL) + "$SCRIPT" check >/dev/null 2>&1 & + ;; +esac + +exit 0 diff --git a/dhcpcd-hook b/dhcpcd-hook new file mode 100755 index 0000000..5d91823 --- /dev/null +++ b/dhcpcd-hook @@ -0,0 +1,19 @@ +#!/bin/bash +#=============================================================================== +# Hook dhcpcd. +# Installe dans /usr/lib/dhcpcd/dhcpcd-hooks/90-pve-netconfig +# +# dhcpcd execute ce hook avec les fonctions/variables $interface et $reason +# deja en environnement (BOUND, RENEW, REBIND, ...). On se contente de +# resynchroniser /etc/hosts et de lever un eventuel fallback statique. +#=============================================================================== + +SCRIPT="/usr/local/sbin/pve-netconfig.sh" + +[[ -x "$SCRIPT" ]] || return 0 2>/dev/null || exit 0 + +case "${reason:-}" in + BOUND|RENEW|REBIND|REBOOT) + "$SCRIPT" check >/dev/null 2>&1 & + ;; +esac diff --git a/install-pve-netconfig.sh b/install-pve-netconfig.sh old mode 100644 new mode 100755 index d52f323..a9bc3ea --- a/install-pve-netconfig.sh +++ b/install-pve-netconfig.sh @@ -58,35 +58,50 @@ systemctl enable pve-netconfig.service systemctl enable pve-netconfig.timer systemctl enable pve-netconfig-watch.service -cat <<'EOF' +# --- Activation automatique du DHCP ----------------------------------------- +# But du script : qu'un simple "git clone + ./install-pve-netconfig.sh" suffise +# à activer le DHCP. On applique donc la configuration réseau et on démarre +# les services immédiatement, sauf si l'utilisateur désactive ce comportement. +SKIP_AUTO_APPLY="${SKIP_AUTO_APPLY:-0}" + +if [[ "$SKIP_AUTO_APPLY" != "1" ]]; then + echo "==> Génération de /etc/network/interfaces (DHCP)" + /usr/local/sbin/pve-netconfig.sh apply-interfaces || \ + echo "AVERTISSEMENT : apply-interfaces a échoué, voir les messages ci-dessus." >&2 + + echo "==> Premier contrôle DHCP / fallback" + /usr/local/sbin/pve-netconfig.sh check || \ + echo "AVERTISSEMENT : le contrôle initial a échoué, voir les messages ci-dessus." >&2 + + echo "==> Démarrage des services" + systemctl start pve-netconfig.timer + systemctl start pve-netconfig-watch.service + + /usr/local/sbin/pve-netconfig.sh status || true +else + echo "==> SKIP_AUTO_APPLY=1 : application automatique ignorée (mode manuel)." +fi + +cat < FALLBACK_IP, FALLBACK_CIDR, FALLBACK_GATEWAY, FALLBACK_DNS -> PORT_MAIN / PORT_SECONDARY si la détection automatique échoue - - 2. Vérifier la détection des ports : - pve-netconfig.sh status - ip -br link - - 3. Générer /etc/network/interfaces : +Puis réappliquer : pve-netconfig.sh apply-interfaces - ATTENTION : rupture réseau possible. Prévoir un accès console (iDRAC/IPMI/KVM). - - 4. Lancer un premier contrôle complet : pve-netconfig.sh check + +Vérifier l'état à tout moment : pve-netconfig.sh status - 5. Démarrer les services : - systemctl start pve-netconfig.timer - systemctl start pve-netconfig-watch.service +Astuce : pour ignorer l'application automatique lors de l'installation, +relancer avec : SKIP_AUTO_APPLY=1 ./install-pve-netconfig.sh - 6. Redémarrer pour valider le comportement au boot : +Il est recommandé de redémarrer pour valider le comportement au boot : reboot =============================================================================== EOF \ No newline at end of file diff --git a/pve-netconfig-watch.service b/pve-netconfig-watch.service new file mode 100644 index 0000000..0304912 --- /dev/null +++ b/pve-netconfig-watch.service @@ -0,0 +1,13 @@ +[Unit] +Description=Surveillance des changements d'adresse IP (netlink) pour Proxmox +After=network.target pve-netconfig.service +Wants=pve-netconfig.service + +[Service] +Type=simple +ExecStart=/usr/local/sbin/pve-netconfig-watch.sh +Restart=always +RestartSec=10 + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/pve-netconfig.service b/pve-netconfig.service new file mode 100644 index 0000000..3d4d7b6 --- /dev/null +++ b/pve-netconfig.service @@ -0,0 +1,16 @@ +[Unit] +Description=Proxmox : configuration DHCP/fallback et synchronisation /etc/hosts +Documentation=file:/usr/local/sbin/pve-netconfig.sh +After=network-online.target networking.service +Wants=network-online.target +# Doit tourner AVANT que les services PVE ne lisent /etc/hosts +Before=pve-guests.service pveproxy.service pvedaemon.service pve-cluster.service + +[Service] +Type=oneshot +RemainAfterExit=yes +ExecStart=/usr/local/sbin/pve-netconfig.sh check +TimeoutStartSec=120 + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/pve-netconfig.timer b/pve-netconfig.timer new file mode 100644 index 0000000..e0c7ede --- /dev/null +++ b/pve-netconfig.timer @@ -0,0 +1,11 @@ +[Unit] +Description=Vérification périodique DHCP/fallback Proxmox + +[Timer] +OnBootSec=2min +OnUnitActiveSec=5min +AccuracySec=30s +Unit=pve-netconfig.service + +[Install] +WantedBy=timers.target \ No newline at end of file