[core]: create_lxc: better handling, fix lock handling, improve template validation & storage selection UX (#6296)
* Update create_lxc.sh * Update create_lxc.sh * Update create_lxc.sh
This commit is contained in:
parent
48c63aaebb
commit
7bf69cbd07
@ -32,7 +32,6 @@ function on_exit() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function error_handler() {
|
function error_handler() {
|
||||||
|
|
||||||
local exit_code="$?"
|
local exit_code="$?"
|
||||||
local line_number="$1"
|
local line_number="$1"
|
||||||
local command="$2"
|
local command="$2"
|
||||||
@ -51,6 +50,14 @@ function on_terminate() {
|
|||||||
exit 143
|
exit 143
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function exit_script() {
|
||||||
|
clear
|
||||||
|
printf "\e[?25h"
|
||||||
|
echo -e "\n${CROSS}${RD}User exited script${CL}\n"
|
||||||
|
kill 0
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
function check_storage_support() {
|
function check_storage_support() {
|
||||||
local CONTENT="$1"
|
local CONTENT="$1"
|
||||||
local -a VALID_STORAGES=()
|
local -a VALID_STORAGES=()
|
||||||
@ -64,21 +71,7 @@ function check_storage_support() {
|
|||||||
[[ ${#VALID_STORAGES[@]} -gt 0 ]]
|
[[ ${#VALID_STORAGES[@]} -gt 0 ]]
|
||||||
}
|
}
|
||||||
|
|
||||||
# This checks for the presence of valid Container Storage and Template Storage locations
|
# This function selects a storage pool for a given content type (e.g., rootdir, vztmpl).
|
||||||
msg_info "Validating Storage"
|
|
||||||
if ! check_storage_support "rootdir"; then
|
|
||||||
|
|
||||||
msg_error "No valid storage found for 'rootdir' (Container)."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
if ! check_storage_support "vztmpl"; then
|
|
||||||
|
|
||||||
msg_error "No valid storage found for 'vztmpl' (Template)."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
msg_ok "Validated Storage (rootdir / vztmpl)."
|
|
||||||
|
|
||||||
# This function is used to select the storage class and determine the corresponding storage content type and label.
|
|
||||||
function select_storage() {
|
function select_storage() {
|
||||||
local CLASS=$1 CONTENT CONTENT_LABEL
|
local CLASS=$1 CONTENT CONTENT_LABEL
|
||||||
|
|
||||||
@ -113,8 +106,20 @@ function select_storage() {
|
|||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
local -a MENU
|
# Check for preset STORAGE variable
|
||||||
|
if [ "$CONTENT" = "rootdir" ] && [ -n "${STORAGE:-}" ]; then
|
||||||
|
if pvesm status -content "$CONTENT" | awk 'NR>1 {print $1}' | grep -qx "$STORAGE"; then
|
||||||
|
STORAGE_RESULT="$STORAGE"
|
||||||
|
msg_info "Using preset storage: $STORAGE_RESULT for $CONTENT_LABEL"
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
msg_error "Preset storage '$STORAGE' is not valid for content type '$CONTENT'."
|
||||||
|
return 2
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
local -A STORAGE_MAP
|
local -A STORAGE_MAP
|
||||||
|
local -a MENU
|
||||||
local COL_WIDTH=0
|
local COL_WIDTH=0
|
||||||
|
|
||||||
while read -r TAG TYPE _ TOTAL USED FREE _; do
|
while read -r TAG TYPE _ TOTAL USED FREE _; do
|
||||||
@ -135,17 +140,23 @@ local -a MENU
|
|||||||
|
|
||||||
if [ $((${#MENU[@]} / 3)) -eq 1 ]; then
|
if [ $((${#MENU[@]} / 3)) -eq 1 ]; then
|
||||||
STORAGE_RESULT="${STORAGE_MAP[${MENU[0]}]}"
|
STORAGE_RESULT="${STORAGE_MAP[${MENU[0]}]}"
|
||||||
|
STORAGE_INFO="${MENU[1]}"
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local WIDTH=$((COL_WIDTH + 42))
|
local WIDTH=$((COL_WIDTH + 42))
|
||||||
while true; do
|
while true; do
|
||||||
local DISPLAY_SELECTED=$(whiptail --backtitle "Proxmox VE Helper Scripts" \
|
local DISPLAY_SELECTED
|
||||||
|
DISPLAY_SELECTED=$(whiptail --backtitle "Proxmox VE Helper Scripts" \
|
||||||
--title "Storage Pools" \
|
--title "Storage Pools" \
|
||||||
--radiolist "Which storage pool for ${CONTENT_LABEL,,}?\n(Spacebar to select)" \
|
--radiolist "Which storage pool for ${CONTENT_LABEL,,}?\n(Spacebar to select)" \
|
||||||
16 "$WIDTH" 6 "${MENU[@]}" 3>&1 1>&2 2>&3)
|
16 "$WIDTH" 6 "${MENU[@]}" 3>&1 1>&2 2>&3)
|
||||||
|
|
||||||
[[ $? -ne 0 ]] && return 3
|
# Cancel or ESC
|
||||||
|
[[ $? -ne 0 ]] && exit_script
|
||||||
|
|
||||||
|
# Strip trailing whitespace or newline (important for storages like "storage (dir)")
|
||||||
|
DISPLAY_SELECTED=$(sed 's/[[:space:]]*$//' <<<"$DISPLAY_SELECTED")
|
||||||
|
|
||||||
if [[ -z "$DISPLAY_SELECTED" || -z "${STORAGE_MAP[$DISPLAY_SELECTED]+_}" ]]; then
|
if [[ -z "$DISPLAY_SELECTED" || -z "${STORAGE_MAP[$DISPLAY_SELECTED]+_}" ]]; then
|
||||||
whiptail --msgbox "No valid storage selected. Please try again." 8 58
|
whiptail --msgbox "No valid storage selected. Please try again." 8 58
|
||||||
@ -153,6 +164,12 @@ local -a MENU
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
STORAGE_RESULT="${STORAGE_MAP[$DISPLAY_SELECTED]}"
|
STORAGE_RESULT="${STORAGE_MAP[$DISPLAY_SELECTED]}"
|
||||||
|
for ((i = 0; i < ${#MENU[@]}; i += 3)); do
|
||||||
|
if [[ "${MENU[$i]}" == "$DISPLAY_SELECTED" ]]; then
|
||||||
|
STORAGE_INFO="${MENU[$i + 1]}"
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
done
|
||||||
return 0
|
return 0
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
@ -181,45 +198,22 @@ if qm status "$CTID" &>/dev/null || pct status "$CTID" &>/dev/null; then
|
|||||||
exit 206
|
exit 206
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# DEFAULT_FILE="/usr/local/community-scripts/default_storage"
|
# This checks for the presence of valid Container Storage and Template Storage locations
|
||||||
# if [[ -f "$DEFAULT_FILE" ]]; then
|
msg_info "Validating Storage"
|
||||||
# source "$DEFAULT_FILE"
|
if ! check_storage_support "rootdir"; then
|
||||||
# if [[ -n "$TEMPLATE_STORAGE" && -n "$CONTAINER_STORAGE" ]]; then
|
msg_error "No valid storage found for 'rootdir' (Container)."
|
||||||
# msg_info "Using default storage configuration from: $DEFAULT_FILE"
|
exit 1
|
||||||
# msg_ok "Template Storage: ${BL}$TEMPLATE_STORAGE${CL} ${GN}|${CL} Container Storage: ${BL}$CONTAINER_STORAGE${CL}"
|
fi
|
||||||
# else
|
if ! check_storage_support "vztmpl"; then
|
||||||
# msg_warn "Default storage file exists but is incomplete – falling back to manual selection"
|
msg_error "No valid storage found for 'vztmpl' (Template)."
|
||||||
# TEMPLATE_STORAGE=$(select_storage template)
|
exit 1
|
||||||
# msg_ok "Using ${BL}$TEMPLATE_STORAGE${CL} ${GN}for Template Storage."
|
fi
|
||||||
# CONTAINER_STORAGE=$(select_storage container)
|
msg_ok "Valid Storage Found"
|
||||||
# msg_ok "Using ${BL}$CONTAINER_STORAGE${CL} ${GN}for Container Storage."
|
|
||||||
# fi
|
|
||||||
# else
|
|
||||||
# # TEMPLATE STORAGE SELECTION
|
|
||||||
# # Template Storage
|
|
||||||
# while true; do
|
|
||||||
# TEMPLATE_STORAGE=$(select_storage template)
|
|
||||||
# if [[ -n "$TEMPLATE_STORAGE" ]]; then
|
|
||||||
# msg_ok "Using ${BL}$TEMPLATE_STORAGE${CL} ${GN}for Template Storage."
|
|
||||||
# break
|
|
||||||
# fi
|
|
||||||
# msg_warn "No valid template storage selected. Please try again."
|
|
||||||
# done
|
|
||||||
|
|
||||||
# while true; do
|
|
||||||
# CONTAINER_STORAGE=$(select_storage container)
|
|
||||||
# if [[ -n "$CONTAINER_STORAGE" ]]; then
|
|
||||||
# msg_ok "Using ${BL}$CONTAINER_STORAGE${CL} ${GN}for Container Storage."
|
|
||||||
# break
|
|
||||||
# fi
|
|
||||||
# msg_warn "No valid container storage selected. Please try again."
|
|
||||||
# done
|
|
||||||
|
|
||||||
# fi
|
|
||||||
|
|
||||||
while true; do
|
while true; do
|
||||||
if select_storage template; then
|
if select_storage template; then
|
||||||
TEMPLATE_STORAGE="$STORAGE_RESULT"
|
TEMPLATE_STORAGE="$STORAGE_RESULT"
|
||||||
|
TEMPLATE_STORAGE_INFO="$STORAGE_INFO"
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
@ -227,9 +221,11 @@ done
|
|||||||
while true; do
|
while true; do
|
||||||
if select_storage container; then
|
if select_storage container; then
|
||||||
CONTAINER_STORAGE="$STORAGE_RESULT"
|
CONTAINER_STORAGE="$STORAGE_RESULT"
|
||||||
|
CONTAINER_STORAGE_INFO="$STORAGE_INFO"
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
msg_ok "Validated Storage | Container: ${BL}$CONTAINER_STORAGE${CL} ($CONTAINER_STORAGE_INFO)"
|
||||||
|
|
||||||
# Check free space on selected container storage
|
# Check free space on selected 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 }')
|
||||||
@ -276,28 +272,34 @@ fi
|
|||||||
TEMPLATE="${TEMPLATES[-1]}"
|
TEMPLATE="${TEMPLATES[-1]}"
|
||||||
TEMPLATE_PATH="$(pvesm path $TEMPLATE_STORAGE:vztmpl/$TEMPLATE 2>/dev/null || echo "/var/lib/vz/template/cache/$TEMPLATE")"
|
TEMPLATE_PATH="$(pvesm path $TEMPLATE_STORAGE:vztmpl/$TEMPLATE 2>/dev/null || echo "/var/lib/vz/template/cache/$TEMPLATE")"
|
||||||
|
|
||||||
# Check if template exists and is valid
|
TEMPLATE_VALID=1
|
||||||
if ! pveam list "$TEMPLATE_STORAGE" | grep -q "$TEMPLATE" || ! zstdcat "$TEMPLATE_PATH" | tar -tf - >/dev/null 2>&1; then
|
if ! pveam list "$TEMPLATE_STORAGE" | grep -q "$TEMPLATE"; then
|
||||||
msg_warn "Template $TEMPLATE not found or appears to be corrupted. Re-downloading."
|
TEMPLATE_VALID=0
|
||||||
|
elif [ ! -s "$TEMPLATE_PATH" ]; then
|
||||||
|
TEMPLATE_VALID=0
|
||||||
|
elif ! tar --use-compress-program=zstdcat -tf "$TEMPLATE_PATH" >/dev/null 2>&1; then
|
||||||
|
TEMPLATE_VALID=0
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "$TEMPLATE_VALID" -eq 0 ]; then
|
||||||
|
msg_warn "Template $TEMPLATE not found or appears to be corrupted. Re-downloading."
|
||||||
[[ -f "$TEMPLATE_PATH" ]] && rm -f "$TEMPLATE_PATH"
|
[[ -f "$TEMPLATE_PATH" ]] && rm -f "$TEMPLATE_PATH"
|
||||||
for attempt in {1..3}; do
|
for attempt in {1..3}; do
|
||||||
msg_info "Attempt $attempt: Downloading LXC template..."
|
msg_info "Attempt $attempt: Downloading LXC template..."
|
||||||
|
|
||||||
if pveam download "$TEMPLATE_STORAGE" "$TEMPLATE" >/dev/null 2>&1; then
|
if pveam download "$TEMPLATE_STORAGE" "$TEMPLATE" >/dev/null 2>&1; then
|
||||||
msg_ok "Template download successful."
|
msg_ok "Template download successful."
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ $attempt -eq 3 ]; then
|
if [ $attempt -eq 3 ]; then
|
||||||
msg_error "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 network access or manually run:\n pveam download $TEMPLATE_STORAGE $TEMPLATE"
|
||||||
exit 208
|
exit 208
|
||||||
fi
|
fi
|
||||||
|
|
||||||
sleep $((attempt * 5))
|
sleep $((attempt * 5))
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
msg_ok "LXC Template '$TEMPLATE' is ready to use."
|
||||||
|
|
||||||
msg_info "Creating LXC Container"
|
msg_info "Creating LXC Container"
|
||||||
# Check and fix subuid/subgid
|
# Check and fix subuid/subgid
|
||||||
grep -q "root:100000:65536" /etc/subuid || echo "root:100000:65536" >>/etc/subuid
|
grep -q "root:100000:65536" /etc/subuid || echo "root:100000:65536" >>/etc/subuid
|
||||||
@ -309,7 +311,7 @@ PCT_OPTIONS=(${PCT_OPTIONS[@]:-${DEFAULT_PCT_OPTIONS[@]}})
|
|||||||
|
|
||||||
# Secure creation of the LXC container with lock and template check
|
# Secure creation of the LXC container with lock and template check
|
||||||
lockfile="/tmp/template.${TEMPLATE}.lock"
|
lockfile="/tmp/template.${TEMPLATE}.lock"
|
||||||
exec 9>"$lockfile" >/dev/null 2>&1 || {
|
exec 9>"$lockfile" || {
|
||||||
msg_error "Failed to create lock file '$lockfile'."
|
msg_error "Failed to create lock file '$lockfile'."
|
||||||
exit 200
|
exit 200
|
||||||
}
|
}
|
||||||
@ -347,7 +349,6 @@ if ! pct create "$CTID" "${TEMPLATE_STORAGE}:vztmpl/${TEMPLATE}" "${PCT_OPTIONS[
|
|||||||
done
|
done
|
||||||
|
|
||||||
sleep 1 # I/O-Sync-Delay
|
sleep 1 # I/O-Sync-Delay
|
||||||
|
|
||||||
msg_ok "Re-downloaded LXC Template"
|
msg_ok "Re-downloaded LXC Template"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user