[core]: add new features to create_lxc (#6979)
This commit is contained in:
parent
206751f517
commit
e46648ecdc
@ -61,11 +61,11 @@ function exit_script() {
|
|||||||
function check_storage_support() {
|
function check_storage_support() {
|
||||||
local CONTENT="$1"
|
local CONTENT="$1"
|
||||||
local -a VALID_STORAGES=()
|
local -a VALID_STORAGES=()
|
||||||
|
|
||||||
while IFS= read -r line; do
|
while IFS= read -r line; do
|
||||||
local STORAGE=$(awk '{print $1}' <<<"$line")
|
local STORAGE_NAME
|
||||||
[[ "$STORAGE" == "storage" || -z "$STORAGE" ]] && continue
|
STORAGE_NAME=$(awk '{print $1}' <<<"$line")
|
||||||
VALID_STORAGES+=("$STORAGE")
|
[[ -z "$STORAGE_NAME" ]] && continue
|
||||||
|
VALID_STORAGES+=("$STORAGE_NAME")
|
||||||
done < <(pvesm status -content "$CONTENT" 2>/dev/null | awk 'NR>1')
|
done < <(pvesm status -content "$CONTENT" 2>/dev/null | awk 'NR>1')
|
||||||
|
|
||||||
[[ ${#VALID_STORAGES[@]} -gt 0 ]]
|
[[ ${#VALID_STORAGES[@]} -gt 0 ]]
|
||||||
@ -124,11 +124,12 @@ function select_storage() {
|
|||||||
|
|
||||||
while read -r TAG TYPE _ TOTAL USED FREE _; do
|
while read -r TAG TYPE _ TOTAL USED FREE _; do
|
||||||
[[ -n "$TAG" && -n "$TYPE" ]] || continue
|
[[ -n "$TAG" && -n "$TYPE" ]] || continue
|
||||||
local DISPLAY="${TAG} (${TYPE})"
|
local STORAGE_NAME="$TAG"
|
||||||
|
local DISPLAY="${STORAGE_NAME} (${TYPE})"
|
||||||
local USED_FMT=$(numfmt --to=iec --from-unit=K --format %.1f <<<"$USED")
|
local USED_FMT=$(numfmt --to=iec --from-unit=K --format %.1f <<<"$USED")
|
||||||
local FREE_FMT=$(numfmt --to=iec --from-unit=K --format %.1f <<<"$FREE")
|
local FREE_FMT=$(numfmt --to=iec --from-unit=K --format %.1f <<<"$FREE")
|
||||||
local INFO="Free: ${FREE_FMT}B Used: ${USED_FMT}B"
|
local INFO="Free: ${FREE_FMT}B Used: ${USED_FMT}B"
|
||||||
STORAGE_MAP["$DISPLAY"]="$TAG"
|
STORAGE_MAP["$DISPLAY"]="$STORAGE_NAME"
|
||||||
MENU+=("$DISPLAY" "$INFO" "OFF")
|
MENU+=("$DISPLAY" "$INFO" "OFF")
|
||||||
((${#DISPLAY} > COL_WIDTH)) && COL_WIDTH=${#DISPLAY}
|
((${#DISPLAY} > COL_WIDTH)) && COL_WIDTH=${#DISPLAY}
|
||||||
done < <(pvesm status -content "$CONTENT" | awk 'NR>1')
|
done < <(pvesm status -content "$CONTENT" | awk 'NR>1')
|
||||||
@ -199,21 +200,22 @@ if qm status "$CTID" &>/dev/null || pct status "$CTID" &>/dev/null; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# This checks for the presence of valid Container Storage and Template Storage locations
|
# This checks for the presence of valid Container Storage and Template Storage locations
|
||||||
msg_info "Validating Storage"
|
msg_info "Validating storage"
|
||||||
if ! check_storage_support "rootdir"; then
|
if ! check_storage_support "rootdir"; then
|
||||||
msg_error "No valid storage found for 'rootdir' (Container)."
|
msg_error "No valid storage found for 'rootdir' [Container]"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
if ! check_storage_support "vztmpl"; then
|
if ! check_storage_support "vztmpl"; then
|
||||||
msg_error "No valid storage found for 'vztmpl' (Template)."
|
msg_error "No valid storage found for 'vztmpl' [Template]"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
msg_ok "Valid Storage Found"
|
|
||||||
|
|
||||||
|
msg_info "Checking template storage"
|
||||||
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"
|
TEMPLATE_STORAGE_INFO="$STORAGE_INFO"
|
||||||
|
msg_ok "Storage ${BL}$TEMPLATE_STORAGE${CL} ($TEMPLATE_STORAGE_INFO) [Template]"
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
@ -222,10 +224,10 @@ 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"
|
CONTAINER_STORAGE_INFO="$STORAGE_INFO"
|
||||||
|
msg_ok "Storage ${BL}$CONTAINER_STORAGE${CL} ($CONTAINER_STORAGE_INFO) [Container]"
|
||||||
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 }')
|
||||||
@ -234,11 +236,12 @@ if [ "$STORAGE_FREE" -lt "$REQUIRED_KB" ]; then
|
|||||||
msg_error "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
|
||||||
|
|
||||||
# Check Cluster Quorum if in Cluster
|
# Check Cluster Quorum if in Cluster
|
||||||
if [ -f /etc/pve/corosync.conf ]; then
|
if [ -f /etc/pve/corosync.conf ]; then
|
||||||
msg_info "Checking Proxmox cluster quorum status"
|
msg_info "Checking cluster quorum"
|
||||||
if ! pvecm status | awk -F':' '/^Quorate/ { exit ($2 ~ /Yes/) ? 0 : 1 }'; then
|
if ! pvecm status | awk -F':' '/^Quorate/ { exit ($2 ~ /Yes/) ? 0 : 1 }'; then
|
||||||
printf "\e[?25h"
|
|
||||||
msg_error "Cluster is not quorate. Start all nodes or configure quorum device (QDevice)."
|
msg_error "Cluster is not quorate. Start all nodes or configure quorum device (QDevice)."
|
||||||
exit 210
|
exit 210
|
||||||
fi
|
fi
|
||||||
@ -248,41 +251,41 @@ fi
|
|||||||
# Update LXC template list
|
# Update LXC template list
|
||||||
TEMPLATE_SEARCH="${PCT_OSTYPE}-${PCT_OSVERSION:-}"
|
TEMPLATE_SEARCH="${PCT_OSTYPE}-${PCT_OSVERSION:-}"
|
||||||
|
|
||||||
msg_info "Updating LXC Template List"
|
# 1. Check local templates first
|
||||||
if ! pveam update >/dev/null 2>&1; then
|
msg_info "Searching for template '$TEMPLATE_SEARCH'"
|
||||||
TEMPLATE_FALLBACK=$(pveam list "$TEMPLATE_STORAGE" | awk "/$TEMPLATE_SEARCH/ {print \$2}" | sort -t - -k 2 -V | tail -n1)
|
mapfile -t TEMPLATES < <(
|
||||||
if [[ -z "$TEMPLATE_FALLBACK" ]]; then
|
pveam list "$TEMPLATE_STORAGE" | awk -v s="$TEMPLATE_SEARCH" '$1 ~ s {print $1}' |
|
||||||
msg_error "Failed to update LXC template list and no local template matching '$TEMPLATE_SEARCH' found."
|
sed 's/.*\///' | sort -t - -k 2 -V
|
||||||
exit 201
|
)
|
||||||
fi
|
|
||||||
msg_info "Skipping template update – using local fallback: $TEMPLATE_FALLBACK"
|
if [ ${#TEMPLATES[@]} -gt 0 ]; then
|
||||||
|
TEMPLATE_SOURCE="local"
|
||||||
else
|
else
|
||||||
msg_ok "LXC Template List Updated"
|
msg_info "No local template found, checking online repository"
|
||||||
fi
|
pveam update >/dev/null 2>&1
|
||||||
|
mapfile -t TEMPLATES < <(
|
||||||
# Get LXC template string
|
pveam available -section system | sed -n "s/.*\($TEMPLATE_SEARCH.*\)/\1/p" |
|
||||||
TEMPLATE_SEARCH="${PCT_OSTYPE}-${PCT_OSVERSION:-}"
|
sort -t - -k 2 -V
|
||||||
mapfile -t TEMPLATES < <(pveam available -section system | sed -n "s/.*\($TEMPLATE_SEARCH.*\)/\1/p" | sort -t - -k 2 -V)
|
)
|
||||||
|
TEMPLATE_SOURCE="online"
|
||||||
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
|
|
||||||
fi
|
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")"
|
||||||
|
msg_ok "Template ${BL}$TEMPLATE${CL} [$TEMPLATE_SOURCE]"
|
||||||
|
|
||||||
|
# 4. Validate template (exists & not corrupted)
|
||||||
TEMPLATE_VALID=1
|
TEMPLATE_VALID=1
|
||||||
if ! pveam list "$TEMPLATE_STORAGE" | grep -q "$TEMPLATE"; then
|
|
||||||
TEMPLATE_VALID=0
|
if [ ! -s "$TEMPLATE_PATH" ]; then
|
||||||
elif [ ! -s "$TEMPLATE_PATH" ]; then
|
|
||||||
TEMPLATE_VALID=0
|
TEMPLATE_VALID=0
|
||||||
elif ! tar --use-compress-program=zstdcat -tf "$TEMPLATE_PATH" >/dev/null 2>&1; then
|
elif ! tar --use-compress-program=zstdcat -tf "$TEMPLATE_PATH" >/dev/null 2>&1; then
|
||||||
TEMPLATE_VALID=0
|
TEMPLATE_VALID=0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "$TEMPLATE_VALID" -eq 0 ]; then
|
if [ "$TEMPLATE_VALID" -eq 0 ]; then
|
||||||
msg_warn "Template $TEMPLATE not found or appears to be corrupted. Re-downloading."
|
msg_warn "Template $TEMPLATE is missing or 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..."
|
||||||
@ -298,8 +301,6 @@ if [ "$TEMPLATE_VALID" -eq 0 ]; then
|
|||||||
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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user