Update create_lxc.sh

This commit is contained in:
CanbiZ 2025-06-16 12:47:28 +02:00
parent fa64db7ffb
commit 223c0af3a7

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
msg_error "Unable to detect a valid Container Storage location." echo -e "\n\n${CROSS}${RD}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
msg_error "Unable to detect a valid Template Storage location." echo -e "\n\n${CROSS}${RD}Unable to detect a valid Template Storage location."
exit 1 exit 1
fi fi
@ -62,7 +62,7 @@ function select_storage() {
CONTENT_LABEL='Container Template' CONTENT_LABEL='Container Template'
;; ;;
*) *)
msg_error "Invalid storage class: $CLASS" echo -e "\n\n${CROSS}${RD}Invalid storage class: $CLASS"
exit 201 exit 201
;; ;;
esac esac
@ -98,7 +98,7 @@ function select_storage() {
"Select the storage pool to use for the ${CONTENT_LABEL,,}.\nUse the spacebar to make a selection.\n" \ "Select the storage pool to use for the ${CONTENT_LABEL,,}.\nUse the spacebar to make a selection.\n" \
16 $((MSG_MAX_LENGTH + 23)) 6 \ 16 $((MSG_MAX_LENGTH + 23)) 6 \
"${MENU[@]}" 3>&1 1>&2 2>&3) || { "${MENU[@]}" 3>&1 1>&2 2>&3) || {
msg_error "Storage selection cancelled." echo -e "\n\n${CROSS}${RD}Storage selection cancelled."
exit 202 exit 202
} }
done done
@ -108,17 +108,17 @@ function select_storage() {
# Test if required variables are set # Test if required variables are set
[[ "${CTID:-}" ]] || { [[ "${CTID:-}" ]] || {
msg_error "You need to set 'CTID' variable." echo -e "\n\n${CROSS}${RD}You need to set 'CTID' variable."
exit 203 exit 203
} }
[[ "${PCT_OSTYPE:-}" ]] || { [[ "${PCT_OSTYPE:-}" ]] || {
msg_error "You need to set 'PCT_OSTYPE' variable." echo -e "\n\n${CROSS}${RD}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" ] || {
msg_error "ID cannot be less than 100." echo -e "\n\n${CROSS}${RD}ID cannot be less than 100."
exit 205 exit 205
} }
@ -139,22 +139,21 @@ 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
msg_error "Not enough space on '$CONTAINER_STORAGE'. Needed: ${PCT_DISK_SIZE:-8}G." echo -e "\n\n${CROSS}${RD}Not enough space on '$CONTAINER_STORAGE'. Needed: ${PCT_DISK_SIZE:-8}G."
exit 214 exit 214
fi fi
# Check Cluster Quorum if in Cluster # Check Cluster Quorum if in Cluster
if [ -f /etc/pve/corosync.conf ]; then if ! pvecm status | grep -q "Quorate: Yes"; then
if ! pvecm status | grep -q "Quorate: Yes"; then printf "\e[?25h" # Cursor sichtbar machen
msg_error "Cluster is not quorate. Start all nodes or configure quorum device (QDevice)." echo -e "\n\n${CROSS}${RD}Cluster is not quorate. Start all nodes or configure quorum device (QDevice).${CL}\n"
exit 210 exit 210
fi
fi fi
# Update LXC template list # Update LXC template list
$STD msg_info "Updating LXC Template List" $STD msg_info "Updating LXC Template List"
if ! timeout 10 pveam update >/dev/null 2>&1; then if ! timeout 10 pveam update >/dev/null 2>&1; then
msg_error "Failed to update LXC template list. Please check your Proxmox host's internet connection and DNS resolution." 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 exit 201
fi fi
$STD msg_ok "LXC Template List Updated" $STD msg_ok "LXC Template List Updated"
@ -164,7 +163,7 @@ 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
msg_error "No matching LXC template found for '${TEMPLATE_SEARCH}'. Make sure your host can reach the Proxmox template repository." echo -e "\n\n${CROSS}${RD}No matching LXC template found for '${TEMPLATE_SEARCH}'. Make sure your host can reach the Proxmox template repository."
exit 207 exit 207
fi fi
@ -186,7 +185,7 @@ if ! pveam list "$TEMPLATE_STORAGE" | grep -q "$TEMPLATE" || ! zstdcat "$TEMPLAT
fi fi
if [ $attempt -eq 3 ]; then if [ $attempt -eq 3 ]; then
msg_error "Failed after 3 attempts. Please check your Proxmox hosts internet access or manually run:\n pveam download $TEMPLATE_STORAGE $TEMPLATE" 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"
exit 208 exit 208
fi fi
@ -208,22 +207,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 || {
msg_error "Timeout while waiting for template lock" echo -e "\n\n${CROSS}${RD}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
msg_error "Container creation failed. Checking if template is corrupted or incomplete." echo -e "\n\n${CROSS}${RD}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
msg_error "Template file too small or missing re-downloading." echo -e "\n\n${CROSS}${RD}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
msg_error "Template appears to be corrupted re-downloading." echo -e "\n\n${CROSS}${RD}Template appears to be corrupted re-downloading."
rm -f "$TEMPLATE_PATH" rm -f "$TEMPLATE_PATH"
else else
msg_error "Template is valid, but container creation still failed." echo -e "\n\n${CROSS}${RD}Template is valid, but container creation still failed."
exit 209 exit 209
fi fi
@ -235,7 +234,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
msg_error "Three failed attempts. Aborting." echo -e "\n\n${CROSS}${RD}Three failed attempts. Aborting."
exit 208 exit 208
fi fi
sleep $((attempt * 5)) sleep $((attempt * 5))
@ -244,13 +243,13 @@ if ! pct create "$CTID" "${TEMPLATE_STORAGE}:vztmpl/${TEMPLATE}" "${PCT_OPTIONS[
sleep 1 # I/O-Sync-Delay sleep 1 # I/O-Sync-Delay
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
msg_error "Container creation failed after re-downloading template." echo -e "\n\n${CROSS}${RD}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
msg_error "Container not found after pct create assuming failure." echo -e "\n\n${CROSS}${RD}Container not found after pct create assuming failure."
exit 210 exit 210
fi fi
: "${UDHCPC_FIX:=}" : "${UDHCPC_FIX:=}"
@ -283,7 +282,7 @@ if [ "$UDHCPC_FIX" == "yes" ]; then
' "$CONFIG_FILE" >"${CONFIG_FILE}.tmp" && mv "${CONFIG_FILE}.tmp" "$CONFIG_FILE" ' "$CONFIG_FILE" >"${CONFIG_FILE}.tmp" && mv "${CONFIG_FILE}.tmp" "$CONFIG_FILE"
msg_ok "Patched udhcpc.conf (RESOLV_CONF=\"no\")" msg_ok "Patched udhcpc.conf (RESOLV_CONF=\"no\")"
else else
msg_error "udhcpc.conf not found in $CONFIG_FILE after waiting" echo -e "\n\n${CROSS}${RD}udhcpc.conf not found in $CONFIG_FILE after waiting"
fi fi
# Clean up: only unmount if we mounted it here # Clean up: only unmount if we mounted it here