create_lxc: better handling of storage names & exit script

This commit is contained in:
CanbiZ 2025-07-28 09:14:10 +02:00
parent 143bdcfd9b
commit 6c0599e30b
2 changed files with 16 additions and 69 deletions

View File

@ -415,70 +415,11 @@ msg_custom() {
echo -e "${BFR:-} ${symbol} ${color}${msg}${CL:-\e[0m}" echo -e "${BFR:-} ${symbol} ${color}${msg}${CL:-\e[0m}"
} }
# msg_ok() { exit_script() {
# local msg="$1" clear
# [[ -z "$msg" ]] && return echo -e "\n${CROSS}${RD}User exited script${CL}\n"
# stop_spinner exit
# printf "\r\e[2K%s %b\n" "$CM" "${GN}${msg}${CL}" >&2 }
# if declare -p MSG_INFO_SHOWN &>/dev/null && [[ "$(declare -p MSG_INFO_SHOWN 2>/dev/null)" =~ "declare -A" ]]; then
# unset MSG_INFO_SHOWN["$msg"]
# fi
# }
# msg_error() {
# local msg="$1"
# [[ -z "$msg" ]] && return
# stop_spinner
# printf "\r\e[2K%s %b\n" "$CROSS" "${RD}${msg}${CL}" >&2
# }
# msg_warn() {
# local msg="$1"
# [[ -z "$msg" ]] && return
# stop_spinner
# printf "\r\e[2K%s %b\n" "$INFO" "${YWB}${msg}${CL}" >&2
# if declare -p MSG_INFO_SHOWN &>/dev/null && [[ "$(declare -p MSG_INFO_SHOWN 2>/dev/null)" =~ "declare -A" ]]; then
# unset MSG_INFO_SHOWN["$msg"]
# fi
# }
# msg_custom() {
# local symbol="${1:-"[*]"}"
# local color="${2:-"\e[36m"}" # Default: Cyan
# local msg="${3:-}"
# [[ -z "$msg" ]] && return
# stop_spinner 2>/dev/null || true
# printf "\r\e[2K%s %b\n" "$symbol" "${color}${msg}${CL:-\e[0m}" >&2
# }
# msg_progress() {
# local current="$1"
# local total="$2"
# local label="$3"
# local width=40
# local filled percent bar empty
# local fill_char="#"
# local empty_char="-"
# if ! [[ "$current" =~ ^[0-9]+$ ]] || ! [[ "$total" =~ ^[0-9]+$ ]] || [[ "$total" -eq 0 ]]; then
# printf "\r\e[2K%s %b\n" "$CROSS" "${RD}Invalid progress input${CL}" >&2
# return
# fi
# percent=$(((current * 100) / total))
# filled=$(((current * width) / total))
# empty=$((width - filled))
# bar=$(printf "%${filled}s" | tr ' ' "$fill_char")
# bar+=$(printf "%${empty}s" | tr ' ' "$empty_char")
# printf "\r\e[2K%s [%s] %3d%% %s" "${TAB}" "$bar" "$percent" "$label" >&2
# if [[ "$current" -eq "$total" ]]; then
# printf "\n" >&2
# fi
# }
run_container_safe() { run_container_safe() {
local ct="$1" local ct="$1"

View File

@ -75,7 +75,7 @@ if ! check_storage_support "vztmpl"; then
fi fi
msg_ok "Validated Storage (rootdir / vztmpl)." msg_ok "Validated Storage (rootdir / vztmpl)."
# This function is used to select the storage class and determine the corresponding storage content type and label. # This function selects a storage pool for a given content type (e.g., rootdir, vztmpl).
function select_storage() { function select_storage() {
local CLASS=$1 CONTENT CONTENT_LABEL local CLASS=$1 CONTENT CONTENT_LABEL
@ -110,7 +110,7 @@ function select_storage() {
;; ;;
esac esac
# >>> NEW: support STORAGE preset <<< # Check for preset STORAGE variable
if [ "$CONTENT" = "rootdir" ] && [ -n "${STORAGE:-}" ]; then if [ "$CONTENT" = "rootdir" ] && [ -n "${STORAGE:-}" ]; then
if pvesm status -content "$CONTENT" | awk 'NR>1 {print $1}' | grep -qx "$STORAGE"; then if pvesm status -content "$CONTENT" | awk 'NR>1 {print $1}' | grep -qx "$STORAGE"; then
STORAGE_RESULT="$STORAGE" STORAGE_RESULT="$STORAGE"
@ -121,8 +121,9 @@ function select_storage() {
return 2 return 2
fi fi
fi fi
local -a MENU
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
@ -148,12 +149,17 @@ function select_storage() {
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