storage preselection

This commit is contained in:
CanbiZ 2025-09-02 11:10:00 +02:00
parent 6525f097b3
commit d044aaa785

View File

@ -58,6 +58,55 @@ function exit_script() {
exit 1 exit 1
} }
# Resolve and validate a preselected storage for a given class.
# class: "template" -> requires content=vztmpl
# "container" -> requires content=rootdir
resolve_storage_preselect() {
local class="$1"
local preselect="$2"
local required_content=""
case "$class" in
template) required_content="vztmpl" ;;
container) required_content="rootdir" ;;
*) return 1 ;;
esac
# No preselect provided
[ -z "$preselect" ] && return 1
# Check storage exists and supports required content
if ! pvesm status -content "$required_content" | awk 'NR>1{print $1}' | grep -qx -- "$preselect"; then
msg_warn "Preselected storage '${preselect}' does not support content '${required_content}' (or not found)"
return 1
fi
# Build human-readable info string from pvesm status
# Expected columns: Name Type Status Total Used Free ...
local line total used free
line="$(pvesm status | awk -v s="$preselect" 'NR>1 && $1==s {print $0}')"
if [ -z "$line" ]; then
STORAGE_INFO="n/a"
else
total="$(echo "$line" | awk '{print $4}')"
used="$(echo "$line" | awk '{print $5}')"
free="$(echo "$line" | awk '{print $6}')"
# Format bytes to IEC
local total_h used_h free_h
if command -v numfmt >/dev/null 2>&1; then
total_h="$(numfmt --to=iec --suffix=B --format %.1f "$total" 2>/dev/null || echo "$total")"
used_h="$(numfmt --to=iec --suffix=B --format %.1f "$used" 2>/dev/null || echo "$used")"
free_h="$(numfmt --to=iec --suffix=B --format %.1f "$free" 2>/dev/null || echo "$free")"
STORAGE_INFO="Free: ${free_h} Used: ${used_h}"
else
STORAGE_INFO="Free: ${free} Used: ${used}"
fi
fi
# Set outputs expected by your callers
STORAGE_RESULT="$preselect"
return 0
}
function check_storage_support() { function check_storage_support() {
local CONTENT="$1" local CONTENT="$1"
local -a VALID_STORAGES=() local -a VALID_STORAGES=()
@ -214,23 +263,37 @@ if ! check_storage_support "vztmpl"; then
exit 1 exit 1
fi fi
while true; do # Template storage selection
if select_storage template; then if resolve_storage_preselect template "${TEMPLATE_STORAGE}"; 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]" msg_ok "Storage ${BL}${TEMPLATE_STORAGE}${CL} (${TEMPLATE_STORAGE_INFO}) [Template]"
break else
fi while true; do
done if select_storage template; then
TEMPLATE_STORAGE="$STORAGE_RESULT"
TEMPLATE_STORAGE_INFO="$STORAGE_INFO"
msg_ok "Storage ${BL}${TEMPLATE_STORAGE}${CL} (${TEMPLATE_STORAGE_INFO}) [Template]"
break
fi
done
fi
while true; do # Container storage selection
if select_storage container; then if resolve_storage_preselect container "${CONTAINER_STORAGE}"; 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]" msg_ok "Storage ${BL}${CONTAINER_STORAGE}${CL} (${CONTAINER_STORAGE_INFO}) [Container]"
break else
fi while true; do
done if select_storage container; then
CONTAINER_STORAGE="$STORAGE_RESULT"
CONTAINER_STORAGE_INFO="$STORAGE_INFO"
msg_ok "Storage ${BL}${CONTAINER_STORAGE}${CL} (${CONTAINER_STORAGE_INFO}) [Container]"
break
fi
done
fi
# Storage Content Validation # Storage Content Validation
msg_info "Validating content types of storage '$CONTAINER_STORAGE'" msg_info "Validating content types of storage '$CONTAINER_STORAGE'"