From 3fd51d7a40248e5889f571c9ad79475348b27d1b Mon Sep 17 00:00:00 2001 From: CanbiZ <47820557+MickLesk@users.noreply.github.com> Date: Tue, 19 Aug 2025 11:32:44 +0200 Subject: [PATCH] fetch local templates --- misc/create_lxc.sh | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/misc/create_lxc.sh b/misc/create_lxc.sh index ea1749d2..72e0583a 100644 --- a/misc/create_lxc.sh +++ b/misc/create_lxc.sh @@ -258,34 +258,38 @@ fi # Update LXC template list TEMPLATE_SEARCH="${PCT_OSTYPE}-${PCT_OSVERSION:-}" -msg_info "Updating LXC Template List" -if ! pveam update >/dev/null 2>&1; then - TEMPLATE_FALLBACK=$(pveam list "$TEMPLATE_STORAGE" | awk "/$TEMPLATE_SEARCH/ {print \$2}" | sort -t - -k 2 -V | tail -n1) - if [[ -z "$TEMPLATE_FALLBACK" ]]; then - msg_error "Failed to update LXC template list and no local template matching '$TEMPLATE_SEARCH' found." - exit 201 - fi - msg_info "Skipping template update – using local fallback: $TEMPLATE_FALLBACK" -else - msg_ok "LXC Template List Updated" -fi - -# Get LXC template string -TEMPLATE_SEARCH="${PCT_OSTYPE}-${PCT_OSVERSION:-}" -mapfile -t TEMPLATES < <(pveam available -section system | sed -n "s/.*\($TEMPLATE_SEARCH.*\)/\1/p" | sort -t - -k 2 -V) +msg_info "Searching for online LXC template for '$TEMPLATE_SEARCH'..." +mapfile -t TEMPLATES < <( + pveam update >/dev/null 2>&1 && + pveam available -section system | sed -n "s/.*\($TEMPLATE_SEARCH.*\)/\1/p" | sort -t - -k 2 -V +) +# If nothing found online, search local templates if [ ${#TEMPLATES[@]} -eq 0 ]; then - msg_error "No matching LXC template found for '${TEMPLATE_SEARCH}'. Make sure your host can reach the Proxmox template repository." - exit 207 + msg_info "Online search failed or no template found. Checking for local fallbacks..." + mapfile -t TEMPLATES < <( + pveam list "$TEMPLATE_STORAGE" | awk "/$TEMPLATE_SEARCH/ {print \$2}" | sort -t - -k 2 -V + ) + + if [ ${#TEMPLATES[@]} -eq 0 ]; then + msg_error "No online or local LXC template found for '${TEMPLATE_SEARCH}'. Please check network or install a template manually." + exit 207 + fi + msg_ok "Found local fallback template." +else + msg_ok "Found online template." fi +# Use the newest available template (last in sorted list) TEMPLATE="${TEMPLATES[-1]}" TEMPLATE_PATH="$(pvesm path $TEMPLATE_STORAGE:vztmpl/$TEMPLATE 2>/dev/null || echo "/var/lib/vz/template/cache/$TEMPLATE")" + msg_debug "TEMPLATE_SEARCH=$TEMPLATE_SEARCH" msg_debug "TEMPLATES=(${TEMPLATES[*]})" msg_debug "Selected TEMPLATE=$TEMPLATE" msg_debug "TEMPLATE_PATH=$TEMPLATE_PATH" +# Validate that template file exists and is not corrupted TEMPLATE_VALID=1 if ! pveam list "$TEMPLATE_STORAGE" | grep -q "$TEMPLATE"; then TEMPLATE_VALID=0