From c1342ca136d6fdaff530fc729dd8cf5bc65eaaaa Mon Sep 17 00:00:00 2001 From: CanbiZ <47820557+MickLesk@users.noreply.github.com> Date: Wed, 25 Jun 2025 08:39:08 +0200 Subject: [PATCH] Update create_lxc.sh --- misc/create_lxc.sh | 63 +++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 26 deletions(-) diff --git a/misc/create_lxc.sh b/misc/create_lxc.sh index 1d0cdfaf..5f31fb38 100644 --- a/misc/create_lxc.sh +++ b/misc/create_lxc.sh @@ -37,12 +37,12 @@ function error_handler() { msg_info "Validating Storage" VALIDCT=$(pvesm status -content rootdir | awk 'NR>1') if [ -z "$VALIDCT" ]; then - echo -e "\n\n${CROSS}${RD}Unable to detect a valid Container Storage location." + msg_error "Unable to detect a valid Container Storage location." exit 1 fi VALIDTMP=$(pvesm status -content vztmpl | awk 'NR>1') if [ -z "$VALIDTMP" ]; then - echo -e "\n\n${CROSS}${RD}Unable to detect a valid Template Storage location." + msg_error "Unable to detect a valid Template Storage location." exit 1 fi @@ -119,24 +119,27 @@ function select_storage() { # Test if required variables are set [[ "${CTID:-}" ]] || { - echo -e "\n\n${CROSS}${RD}You need to set 'CTID' variable." + msg_error "You need to set 'CTID' variable." exit 203 } [[ "${PCT_OSTYPE:-}" ]] || { - echo -e "\n\n${CROSS}${RD}You need to set 'PCT_OSTYPE' variable." + msg_error "You need to set 'PCT_OSTYPE' variable." exit 204 } # Test if ID is valid [ "$CTID" -ge "100" ] || { - echo -e "\n\n${CROSS}${RD}ID cannot be less than 100." + msg_error "ID cannot be less than 100." exit 205 } -# # Get template storage -# TEMPLATE_STORAGE=$(select_storage template) -# CONTAINER_STORAGE=$(select_storage container) || exit -# msg_ok "Template Storage: ${BL}$TEMPLATE_STORAGE${CL} ${GN}Container Storage: ${BL}$CONTAINER_STORAGE${CL}." +# Test if ID is in use +if qm status "$CTID" &>/dev/null || pct status "$CTID" &>/dev/null; then + echo -e "ID '$CTID' is already in use." + unset CTID + msg_error "Cannot use ID that is already in use." + exit 206 +fi # Get template storage TEMPLATE_STORAGE=$(select_storage template) @@ -150,7 +153,7 @@ msg_ok "Using ${BL}$CONTAINER_STORAGE${CL} ${GN}for Container Storage." STORAGE_FREE=$(pvesm status | awk -v s="$CONTAINER_STORAGE" '$1 == s { print $6 }') REQUIRED_KB=$((${PCT_DISK_SIZE:-8} * 1024 * 1024)) if [ "$STORAGE_FREE" -lt "$REQUIRED_KB" ]; then - echo -e "\n\n${CROSS}${RD}Not enough space on '$CONTAINER_STORAGE'. Needed: ${PCT_DISK_SIZE:-8}G." + msg_error "Not enough space on '$CONTAINER_STORAGE'. Needed: ${PCT_DISK_SIZE:-8}G." exit 214 fi @@ -166,19 +169,26 @@ if [ -f /etc/pve/corosync.conf ]; then fi # Update LXC template list -$STD msg_info "Updating LXC Template List" -if ! timeout 10 pveam update >/dev/null 2>&1; then - echo -e "\n\n${CROSS}${RD}Failed to update LXC template list. Please check your Proxmox host's internet connection and DNS resolution." - exit 201 -fi -$STD msg_ok "LXC Template List Updated" +TEMPLATE_SEARCH="${PCT_OSTYPE}-${PCT_OSVERSION:-}" + +msg_info "Updating LXC Template List" +if ! timeout 15 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) if [ ${#TEMPLATES[@]} -eq 0 ]; then - echo -e "\n\n${CROSS}${RD}No matching LXC template found for '${TEMPLATE_SEARCH}'. Make sure your host can reach the Proxmox template repository." + msg_error "No matching LXC template found for '${TEMPLATE_SEARCH}'. Make sure your host can reach the Proxmox template repository." exit 207 fi @@ -200,7 +210,7 @@ if ! pveam list "$TEMPLATE_STORAGE" | grep -q "$TEMPLATE" || ! zstdcat "$TEMPLAT fi if [ $attempt -eq 3 ]; then - echo -e "\n\n${CROSS}${RD}Failed after 3 attempts. Please check your Proxmox host’s internet access or manually run:\n pveam download $TEMPLATE_STORAGE $TEMPLATE" + msg_error "Failed after 3 attempts. Please check your Proxmox host’s internet access or manually run:\n pveam download $TEMPLATE_STORAGE $TEMPLATE" exit 208 fi @@ -222,22 +232,22 @@ PCT_OPTIONS=(${PCT_OPTIONS[@]:-${DEFAULT_PCT_OPTIONS[@]}}) lockfile="/tmp/template.${TEMPLATE}.lock" exec 9>"$lockfile" flock -w 60 9 || { - echo -e "\n\n${CROSS}${RD}Timeout while waiting for template lock" + msg_error "Timeout while waiting for template lock" exit 211 } msg_info "Creating LXC Container" if ! pct create "$CTID" "${TEMPLATE_STORAGE}:vztmpl/${TEMPLATE}" "${PCT_OPTIONS[@]}" &>/dev/null; then - echo -e "\n\n${CROSS}${RD}Container creation failed. Checking if template is corrupted or incomplete." + msg_error "Container creation failed. Checking if template is corrupted or incomplete." if [[ ! -s "$TEMPLATE_PATH" || "$(stat -c%s "$TEMPLATE_PATH")" -lt 1000000 ]]; then - echo -e "\n\n${CROSS}${RD}Template file too small or missing – re-downloading." + msg_error "Template file too small or missing – re-downloading." rm -f "$TEMPLATE_PATH" elif ! zstdcat "$TEMPLATE_PATH" | tar -tf - &>/dev/null; then - echo -e "\n\n${CROSS}${RD}Template appears to be corrupted – re-downloading." + msg_error "Template appears to be corrupted – re-downloading." rm -f "$TEMPLATE_PATH" else - echo -e "\n\n${CROSS}${RD}Template is valid, but container creation still failed." + msg_error "Template is valid, but container creation still failed." exit 209 fi @@ -249,7 +259,7 @@ if ! pct create "$CTID" "${TEMPLATE_STORAGE}:vztmpl/${TEMPLATE}" "${PCT_OPTIONS[ break fi if [ "$attempt" -eq 3 ]; then - echo -e "\n\n${CROSS}${RD}Three failed attempts. Aborting." + msg_error "Three failed attempts. Aborting." exit 208 fi sleep $((attempt * 5)) @@ -257,14 +267,15 @@ if ! pct create "$CTID" "${TEMPLATE_STORAGE}:vztmpl/${TEMPLATE}" "${PCT_OPTIONS[ sleep 1 # I/O-Sync-Delay + msg_ok "Re-downloaded LXC Template" if ! pct create "$CTID" "${TEMPLATE_STORAGE}:vztmpl/${TEMPLATE}" "${PCT_OPTIONS[@]}" &>/dev/null; then - echo -e "\n\n${CROSS}${RD}Container creation failed after re-downloading template." + msg_error "Container creation failed after re-downloading template." exit 200 fi fi if ! pct status "$CTID" &>/dev/null; then - echo -e "\n\n${CROSS}${RD}Container not found after pct create – assuming failure." + msg_error "Container not found after pct create – assuming failure." exit 210 fi : "${UDHCPC_FIX:=}"