From 2db707079b7b92a339d8fe370ab1ab4a338a2b73 Mon Sep 17 00:00:00 2001 From: CanbiZ <47820557+MickLesk@users.noreply.github.com> Date: Mon, 22 Sep 2025 11:31:19 +0200 Subject: [PATCH] Update build.func --- misc/build.func | 70 ++++++++++++++++++++++--------------------------- 1 file changed, 32 insertions(+), 38 deletions(-) diff --git a/misc/build.func b/misc/build.func index 8ad9ec13..b00eac9c 100644 --- a/misc/build.func +++ b/misc/build.func @@ -2371,46 +2371,40 @@ EOF if [ "$var_os" != "alpine" ]; then msg_info "Waiting for network in LXC container" - sleep 2 - for i in {1..10}; do - # --- Check 1: Has CT an IP? --- - ip_in_lxc=$(pct exec "$CTID" -- ip -4 addr show dev eth0 | awk '/inet / {print $2}' | cut -d/ -f1) - if [ -z "$ip_in_lxc" ]; then - msg_warn "No IP in LXC yet (try $i/10) – waiting..." - sleep 3 - continue - fi - - # --- Check 2: Can reach gateway? --- - if ! pct exec "$CTID" -- ping -c1 -W1 "$GATEWAY" >/dev/null 2>&1; then - msg_warn "CT $CTID has IP $ip_in_lxc but cannot reach gateway $GATEWAY (try $i/10)" - sleep 3 - continue - fi - - # --- Check 3: Can resolve DNS? --- - if pct exec "$CTID" -- getent hosts deb.debian.org >/dev/null 2>&1; then - msg_ok "Network in LXC is reachable (DNS OK, IP $ip_in_lxc)" - break - else - msg_warn "CT $CTID has IP $ip_in_lxc, gateway OK, but DNS failed (try $i/10)" - sleep 3 - fi - - # --- End of loop fallback --- - if [ "$i" -eq 10 ]; then - msg_warn "DNS still failing after 10 attempts. Applying fallback resolv.conf..." - pct exec "$CTID" -- bash -c 'echo "nameserver 1.1.1.1" > /etc/resolv.conf && echo "nameserver 8.8.8.8" >> /etc/resolv.conf' - - if pct exec "$CTID" -- getent hosts deb.debian.org >/dev/null 2>&1; then - msg_ok "Network reachable after DNS fallback" - else - msg_error "Still no DNS/network in LXC! Aborting customization." - exit_script - fi - fi + # Global timeout 20s, aborts early as soon as the network is ready + timeout 20 bash -c ' + # --- Wait until the container has an IP --- + until ip_in_lxc=$(pct exec '"$CTID"' -- ip -4 addr show dev eth0 | awk "/inet / {print \$2}" | cut -d/ -f1) && [ -n "$ip_in_lxc" ]; do + echo " ⏳ No IP in LXC yet – waiting..." + sleep 1 done + + # --- Optional: trigger ARP to speed up gateway availability --- + if pct exec '"$CTID"' -- command -v arping >/dev/null 2>&1; then + pct exec '"$CTID"' -- arping -c1 -I eth0 '"$GATEWAY"' >/dev/null 2>&1 || true + fi + + # --- Wait until the gateway is reachable --- + until pct exec '"$CTID"' -- ping -c1 -W1 '"$GATEWAY"' >/dev/null 2>&1; do + echo " ⏳ CT '"$CTID"' has IP $ip_in_lxc but cannot reach gateway '"$GATEWAY"' – retry..." + sleep 1 + done + ' + + # --- DNS check --- + if pct exec "$CTID" -- getent hosts deb.debian.org >/dev/null 2>&1; then + msg_ok "Network in LXC is reachable (DNS OK, IP $ip_in_lxc)" + else + msg_warn "Network reachable, but DNS failed – applying fallback resolv.conf" + pct exec "$CTID" -- bash -c 'echo "nameserver 1.1.1.1" > /etc/resolv.conf && echo "nameserver 8.8.8.8" >> /etc/resolv.conf' + if pct exec "$CTID" -- getent hosts deb.debian.org >/dev/null 2>&1; then + msg_ok "Network reachable after DNS fallback" + else + msg_error "Still no DNS/network in LXC! Aborting customization." + exit_script + fi + fi fi msg_info "Customizing LXC Container"