Update create_lxc.sh

This commit is contained in:
CanbiZ 2025-06-25 08:39:08 +02:00
parent eeeb19509e
commit c1342ca136

View File

@ -37,12 +37,12 @@ function error_handler() {
msg_info "Validating Storage" msg_info "Validating Storage"
VALIDCT=$(pvesm status -content rootdir | awk 'NR>1') VALIDCT=$(pvesm status -content rootdir | awk 'NR>1')
if [ -z "$VALIDCT" ]; then 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 exit 1
fi fi
VALIDTMP=$(pvesm status -content vztmpl | awk 'NR>1') VALIDTMP=$(pvesm status -content vztmpl | awk 'NR>1')
if [ -z "$VALIDTMP" ]; then 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 exit 1
fi fi
@ -119,24 +119,27 @@ function select_storage() {
# Test if required variables are set # Test if required variables are set
[[ "${CTID:-}" ]] || { [[ "${CTID:-}" ]] || {
echo -e "\n\n${CROSS}${RD}You need to set 'CTID' variable." msg_error "You need to set 'CTID' variable."
exit 203 exit 203
} }
[[ "${PCT_OSTYPE:-}" ]] || { [[ "${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 exit 204
} }
# Test if ID is valid # Test if ID is valid
[ "$CTID" -ge "100" ] || { [ "$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 exit 205
} }
# # Get template storage # Test if ID is in use
# TEMPLATE_STORAGE=$(select_storage template) if qm status "$CTID" &>/dev/null || pct status "$CTID" &>/dev/null; then
# CONTAINER_STORAGE=$(select_storage container) || exit echo -e "ID '$CTID' is already in use."
# msg_ok "Template Storage: ${BL}$TEMPLATE_STORAGE${CL} ${GN}Container Storage: ${BL}$CONTAINER_STORAGE${CL}." unset CTID
msg_error "Cannot use ID that is already in use."
exit 206
fi
# Get template storage # Get template storage
TEMPLATE_STORAGE=$(select_storage template) 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 }') STORAGE_FREE=$(pvesm status | awk -v s="$CONTAINER_STORAGE" '$1 == s { print $6 }')
REQUIRED_KB=$((${PCT_DISK_SIZE:-8} * 1024 * 1024)) REQUIRED_KB=$((${PCT_DISK_SIZE:-8} * 1024 * 1024))
if [ "$STORAGE_FREE" -lt "$REQUIRED_KB" ]; then 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 exit 214
fi fi
@ -166,19 +169,26 @@ if [ -f /etc/pve/corosync.conf ]; then
fi fi
# Update LXC template list # Update LXC template list
$STD msg_info "Updating LXC Template List" TEMPLATE_SEARCH="${PCT_OSTYPE}-${PCT_OSVERSION:-}"
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." msg_info "Updating LXC Template List"
exit 201 if ! timeout 15 pveam update >/dev/null 2>&1; then
fi TEMPLATE_FALLBACK=$(pveam list "$TEMPLATE_STORAGE" | awk "/$TEMPLATE_SEARCH/ {print \$2}" | sort -t - -k 2 -V | tail -n1)
$STD msg_ok "LXC Template List Updated" 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 # Get LXC template string
TEMPLATE_SEARCH="${PCT_OSTYPE}-${PCT_OSVERSION:-}" 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) mapfile -t TEMPLATES < <(pveam available -section system | sed -n "s/.*\($TEMPLATE_SEARCH.*\)/\1/p" | sort -t - -k 2 -V)
if [ ${#TEMPLATES[@]} -eq 0 ]; then 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 exit 207
fi fi
@ -200,7 +210,7 @@ if ! pveam list "$TEMPLATE_STORAGE" | grep -q "$TEMPLATE" || ! zstdcat "$TEMPLAT
fi fi
if [ $attempt -eq 3 ]; then if [ $attempt -eq 3 ]; then
echo -e "\n\n${CROSS}${RD}Failed after 3 attempts. Please check your Proxmox hosts internet access or manually run:\n pveam download $TEMPLATE_STORAGE $TEMPLATE" msg_error "Failed after 3 attempts. Please check your Proxmox hosts internet access or manually run:\n pveam download $TEMPLATE_STORAGE $TEMPLATE"
exit 208 exit 208
fi fi
@ -222,22 +232,22 @@ PCT_OPTIONS=(${PCT_OPTIONS[@]:-${DEFAULT_PCT_OPTIONS[@]}})
lockfile="/tmp/template.${TEMPLATE}.lock" lockfile="/tmp/template.${TEMPLATE}.lock"
exec 9>"$lockfile" exec 9>"$lockfile"
flock -w 60 9 || { 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 exit 211
} }
msg_info "Creating LXC Container" msg_info "Creating LXC Container"
if ! pct create "$CTID" "${TEMPLATE_STORAGE}:vztmpl/${TEMPLATE}" "${PCT_OPTIONS[@]}" &>/dev/null; then 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 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" rm -f "$TEMPLATE_PATH"
elif ! zstdcat "$TEMPLATE_PATH" | tar -tf - &>/dev/null; then 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" rm -f "$TEMPLATE_PATH"
else 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 exit 209
fi fi
@ -249,7 +259,7 @@ if ! pct create "$CTID" "${TEMPLATE_STORAGE}:vztmpl/${TEMPLATE}" "${PCT_OPTIONS[
break break
fi fi
if [ "$attempt" -eq 3 ]; then if [ "$attempt" -eq 3 ]; then
echo -e "\n\n${CROSS}${RD}Three failed attempts. Aborting." msg_error "Three failed attempts. Aborting."
exit 208 exit 208
fi fi
sleep $((attempt * 5)) sleep $((attempt * 5))
@ -257,14 +267,15 @@ if ! pct create "$CTID" "${TEMPLATE_STORAGE}:vztmpl/${TEMPLATE}" "${PCT_OPTIONS[
sleep 1 # I/O-Sync-Delay 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 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 exit 200
fi fi
fi fi
if ! pct status "$CTID" &>/dev/null; then 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 exit 210
fi fi
: "${UDHCPC_FIX:=}" : "${UDHCPC_FIX:=}"