Improve apt mirror selection and fallback

Refactor mirror logic in misc/build.func and misc/install.func to prefer regional Debian mirrors (detected from /etc/timezone), shuffle mirror lists, and include additional AP region hosts. Add quick TCP reachability checks, limit consecutive mirror failures, and surface clearer status messages. On repeated failures, prompt interactively for a custom mirror (with validation) and handle pct exec exit codes to present more informative errors and retry behavior.
This commit is contained in:
CanbiZ (MickLesk)
2026-03-26 14:29:20 +01:00
parent 76c79872e0
commit 947a536351
2 changed files with 121 additions and 58 deletions

View File

@@ -4604,48 +4604,73 @@ EOF'
msg_warn "apt-get update failed, trying alternate mirrors..."
pct exec "$CTID" -- bash -c '
APT_BASE="sudo curl mc gnupg2 jq"
MIRRORS="
ftp.debian.org
ftp.us.debian.org
ftp.de.debian.org
ftp.fr.debian.org
ftp.nl.debian.org
ftp.uk.debian.org
ftp.ch.debian.org
ftp.se.debian.org
ftp.it.debian.org
ftp.au.debian.org
ftp.jp.debian.org
ftp.ca.debian.org
debian.csail.mit.edu
mirrors.ocf.berkeley.edu
mirrors.wikimedia.org
debian.osuosl.org
mirror.cogentco.com
ftp.fau.de
ftp.halifax.rwth-aachen.de
debian.mirror.lrz.de
mirror.init7.net
debian.ethz.ch
mirrors.dotsrc.org
debian.mirrors.ovh.net
mirror.aarnet.edu.au
"
EU_MIRRORS="ftp.de.debian.org ftp.fr.debian.org ftp.nl.debian.org ftp.uk.debian.org ftp.ch.debian.org ftp.se.debian.org ftp.it.debian.org ftp.fau.de ftp.halifax.rwth-aachen.de debian.mirror.lrz.de mirror.init7.net debian.ethz.ch mirrors.dotsrc.org debian.mirrors.ovh.net"
US_MIRRORS="ftp.us.debian.org ftp.ca.debian.org debian.csail.mit.edu mirrors.ocf.berkeley.edu mirrors.wikimedia.org debian.osuosl.org mirror.cogentco.com"
AP_MIRRORS="ftp.au.debian.org ftp.jp.debian.org ftp.tw.debian.org ftp.kr.debian.org ftp.in.debian.org ftp.hk.debian.org ftp.sg.debian.org mirror.aarnet.edu.au mirror.nitc.ac.in"
TZ=$(cat /etc/timezone 2>/dev/null || echo "UTC")
case "$TZ" in
Europe/*|Arctic/*) REGIONAL="$EU_MIRRORS"; OTHERS="$US_MIRRORS $AP_MIRRORS" ;;
America/*) REGIONAL="$US_MIRRORS"; OTHERS="$EU_MIRRORS $AP_MIRRORS" ;;
Asia/*|Australia/*|Pacific/*) REGIONAL="$AP_MIRRORS"; OTHERS="$EU_MIRRORS $US_MIRRORS" ;;
*) REGIONAL=""; OTHERS="$EU_MIRRORS $US_MIRRORS $AP_MIRRORS" ;;
esac
MIRRORS="$(printf "%s\n" $REGIONAL | shuf) ftp.debian.org $(printf "%s\n" $OTHERS | shuf)"
echo "Acquire::By-Hash \"no\";" >/etc/apt/apt.conf.d/99no-by-hash
FAIL_COUNT=0
for mirror in $MIRRORS; do
timeout 2 bash -c "echo >/dev/tcp/$mirror/80" 2>/dev/null || { echo " [skip] $mirror (unreachable)"; continue; }
echo " [try] $mirror ..."
for src in /etc/apt/sources.list.d/debian.sources /etc/apt/sources.list; do
[ -f "$src" ] && sed -i "s|URIs: http[s]*://[^/]*/|URIs: https://${mirror}/|g; s|deb http[s]*://[^/]*/|deb https://${mirror}/|g" "$src"
done
rm -rf /var/lib/apt/lists/*
if apt-get update >/dev/null 2>&1 && apt-get install -y $APT_BASE >/dev/null 2>&1; then
APT_OUT=$(apt-get update 2>&1)
if echo "$APT_OUT" | grep -qi "hashsum\|hash sum"; then
echo " [fail] $mirror (hash mismatch)"
echo "$APT_OUT" | grep -i "hash" | head -3 | sed "s/^/ /"
elif apt-get install -y $APT_BASE >/dev/null 2>&1; then
echo " [ok] $mirror"
exit 0
else
echo " [fail] $mirror"
fi
FAIL_COUNT=$((FAIL_COUNT + 1))
[ "$FAIL_COUNT" -ge 3 ] && exit 2
done
exit 1
' || {
'
mirror_exit=$?
if [[ $mirror_exit -eq 2 ]]; then
msg_warn "Multiple mirrors failed (possible CDN synchronization issue)."
msg_info "Find Debian mirrors at: https://www.debian.org/mirror/list"
local custom_mirror=""
while true; do
read -rp " Enter a Debian mirror hostname (or 'skip' to abort): " custom_mirror </dev/tty
[[ -z "$custom_mirror" ]] && continue
[[ "$custom_mirror" == "skip" ]] && break
[[ ! "$custom_mirror" =~ ^[a-zA-Z0-9._-]+$ ]] && {
msg_warn "Invalid hostname format."
continue
}
pct exec "$CTID" -- bash -c "
for src in /etc/apt/sources.list.d/debian.sources /etc/apt/sources.list; do
[ -f \"\$src\" ] && sed -i \"s|URIs: http[s]*://[^/]*/|URIs: https://${custom_mirror}/|g; s|deb http[s]*://[^/]*/|deb https://${custom_mirror}/|g\" \"\$src\"
done
rm -rf /var/lib/apt/lists/*
apt-get update >/dev/null 2>&1 && apt-get install -y sudo curl mc gnupg2 jq >/dev/null 2>&1
" && break
msg_warn "Mirror '${custom_mirror}' also failed. Try another or type 'skip'."
done
if [[ "$custom_mirror" == "skip" ]]; then
msg_error "apt-get base packages installation failed"
exit 1
fi
elif [[ $mirror_exit -ne 0 ]]; then
msg_error "apt-get base packages installation failed"
exit 1
}
fi
}
fi