Files
pvedhcp/install-pve-netconfig.sh
T
SDEInfoandCopilot 3a823ef2ff Ajouter les fichiers manquants (unités systemd, hooks DHCP) et corriger install-pve-netconfig.sh
- Les fichiers pve-netconfig.service, pve-netconfig-watch.service,
  pve-netconfig.timer, dhclient-hook et dhcpcd-hook n'avaient jamais ete
  ajoutes au depot git : un git clone frais ne les contenait pas, ce qui
  faisait echouer install-pve-netconfig.sh (install: cannot stat ...).
- install-pve-netconfig.sh applique desormais automatiquement la
  configuration DHCP (apply-interfaces + check + demarrage des services)
  au lieu de se limiter a des instructions manuelles, avec une option
  SKIP_AUTO_APPLY=1 pour desactiver ce comportement.
- Bit executable ajoute sur install-pve-netconfig.sh, dhclient-hook et
  dhcpcd-hook.
- README.md mis a jour avec l'URL reelle du depot (https://git.sdeinfo.app/sde/pvedhcp.git)
  et le nouveau comportement automatique.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-09-10 15:55:53 +02:00

107 lines
4.5 KiB
Bash
Executable File

#!/bin/bash
#===============================================================================
# Installe pve-netconfig sur un hôte Proxmox VE 9 fraîchement installé.
# À exécuter en root immédiatement après l'installation du serveur.
#===============================================================================
set -euo pipefail
[[ $EUID -eq 0 ]] || { echo "Exécuter en root." >&2; exit 1; }
SRC_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
echo "==> Vérification de l'environnement"
if ! command -v pveversion >/dev/null 2>&1; then
echo "AVERTISSEMENT : pveversion introuvable, est-ce bien un hôte Proxmox VE ?" >&2
fi
echo " Debian : $(cat /etc/debian_version)"
echo "==> Installation des dépendances"
export DEBIAN_FRONTEND=noninteractive
apt-get update -qq
apt-get install -y --no-install-recommends jq iproute2
# Client DHCP : vérifier qu'au moins un est présent
if ! command -v dhclient >/dev/null 2>&1 && ! command -v dhcpcd >/dev/null 2>&1; then
echo "==> Aucun client DHCP détecté, installation d'isc-dhcp-client"
apt-get install -y --no-install-recommends isc-dhcp-client
fi
echo "==> Sauvegarde de la configuration réseau initiale"
mkdir -p /var/backups/pve-netconfig
cp -a /etc/network/interfaces "/var/backups/pve-netconfig/interfaces.pre-install.$(date +%Y%m%d-%H%M%S)"
cp -a /etc/hosts "/var/backups/pve-netconfig/hosts.pre-install.$(date +%Y%m%d-%H%M%S)"
echo "==> Copie des scripts"
install -m 0750 -o root -g root "${SRC_DIR}/pve-netconfig.sh" /usr/local/sbin/pve-netconfig.sh
install -m 0750 -o root -g root "${SRC_DIR}/pve-netconfig-watch.sh" /usr/local/sbin/pve-netconfig-watch.sh
echo "==> Installation des unités systemd"
install -m 0644 "${SRC_DIR}/pve-netconfig.service" /etc/systemd/system/
install -m 0644 "${SRC_DIR}/pve-netconfig.timer" /etc/systemd/system/
install -m 0644 "${SRC_DIR}/pve-netconfig-watch.service" /etc/systemd/system/
echo "==> Installation du hook DHCP"
if command -v dhclient >/dev/null 2>&1; then
mkdir -p /etc/dhcp/dhclient-exit-hooks.d
install -m 0755 "${SRC_DIR}/dhclient-hook" /etc/dhcp/dhclient-exit-hooks.d/pve-netconfig
echo " Hook dhclient installé."
fi
if command -v dhcpcd >/dev/null 2>&1; then
mkdir -p /usr/lib/dhcpcd/dhcpcd-hooks
install -m 0755 "${SRC_DIR}/dhcpcd-hook" /usr/lib/dhcpcd/dhcpcd-hooks/90-pve-netconfig
echo " Hook dhcpcd installé."
fi
echo "==> Activation des services"
systemctl daemon-reload
systemctl enable pve-netconfig.service
systemctl enable pve-netconfig.timer
systemctl enable pve-netconfig-watch.service
# --- 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 <<EOF
===============================================================================
Installation terminée. Le DHCP a été activé automatiquement sur ${BRIDGE_MAIN:-vmbr0} / ${BRIDGE_SECONDARY:-vmbr1}.
Pour personnaliser la configuration (IP de secours, ports, DNS...) :
nano /usr/local/sbin/pve-netconfig.sh
-> FALLBACK_IP, FALLBACK_CIDR, FALLBACK_GATEWAY, FALLBACK_DNS
-> PORT_MAIN / PORT_SECONDARY si la détection automatique échoue
Puis réappliquer :
pve-netconfig.sh apply-interfaces
pve-netconfig.sh check
Vérifier l'état à tout moment :
pve-netconfig.sh status
Astuce : pour ignorer l'application automatique lors de l'installation,
relancer avec : SKIP_AUTO_APPLY=1 ./install-pve-netconfig.sh
Il est recommandé de redémarrer pour valider le comportement au boot :
reboot
===============================================================================
EOF