From 6c648f4b89e002c731bb52901d1fce3b3d1e5df0 Mon Sep 17 00:00:00 2001 From: Michel Roegl-Brunner <73236783+michelroegl-brunner@users.noreply.github.com> Date: Mon, 5 May 2025 16:49:22 +0200 Subject: [PATCH] Docker-VM: Add Disk Size choice (#4241) * Add Disk Size Choice to Advanced * Add Disk Size Choice to Advanced --- vm/docker-vm.sh | 82 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 53 insertions(+), 29 deletions(-) diff --git a/vm/docker-vm.sh b/vm/docker-vm.sh index bbac79ebd..157439619 100644 --- a/vm/docker-vm.sh +++ b/vm/docker-vm.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash # Copyright (c) 2021-2025 community-scripts ORG -# Author: thost96 (thost96) +# Author: thost96 (thost96) | Co-Author: michelroegl-brunner # License: MIT | https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE source /dev/stdin <<<$(curl -fsSL https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/api.func) @@ -26,7 +26,7 @@ METHOD="" NSAPP="debian12vm" var_os="debian" var_version="12" -DISK_SIZE="8G" +DISK_SIZE="4G" YW=$(echo "\033[33m") BL=$(echo "\033[36m") @@ -57,19 +57,19 @@ function error_handler() { } function cleanup_vmid() { - if qm status $VMID &>/dev/null; then - qm stop $VMID &>/dev/null - qm destroy $VMID &>/dev/null + if qm status "$VMID" &>/dev/null; then + qm stop "$VMID" &>/dev/null + qm destroy "$VMID" &>/dev/null fi } function cleanup() { popd >/dev/null - rm -rf $TEMP_DIR + rm -rf "$TEMP_DIR" } TEMP_DIR=$(mktemp -d) -pushd $TEMP_DIR >/dev/null +pushd "$TEMP_DIR" >/dev/null if whiptail --backtitle "Proxmox VE Helper Scripts" --title "Docker VM" --yesno "This will create a New Docker VM. Proceed?" 10 58; then : else @@ -146,6 +146,7 @@ function default_settings() { FORMAT=",efitype=4m" MACHINE="" DISK_CACHE="" + DISK_SIZE="4G" HN="docker" CPU_TYPE="" CORE_COUNT="2" @@ -159,6 +160,7 @@ function default_settings() { echo -e "${DGN}Using Virtual Machine ID: ${BGN}${VMID}${CL}" echo -e "${DGN}Using Machine Type: ${BGN}i440fx${CL}" echo -e "${DGN}Using Disk Cache: ${BGN}None${CL}" + echo -e "${DISKSIZE}${BOLD}${DGN}Disk Size: ${BGN}$DISK_SIZE${CL}" echo -e "${DGN}Using Hostname: ${BGN}${HN}${CL}" echo -e "${DGN}Using CPU Model: ${BGN}KVM64${CL}" echo -e "${DGN}Allocated Cores: ${BGN}${CORE_COUNT}${CL}" @@ -174,7 +176,7 @@ function default_settings() { function advanced_settings() { METHOD="advanced" while true; do - if VMID=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set Virtual Machine ID" 8 58 $NEXTID --title "VIRTUAL MACHINE ID" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then + if VMID=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set Virtual Machine ID" 8 58 "$NEXTID" --title "VIRTUAL MACHINE ID" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then if [ -z "$VMID" ]; then VMID="$NEXTID" fi @@ -194,7 +196,7 @@ function advanced_settings() { "i440fx" "Machine i440fx" ON \ "q35" "Machine q35" OFF \ 3>&1 1>&2 2>&3); then - if [ $MACH = q35 ]; then + if [ "$MACH" = q35 ]; then echo -e "${DGN}Using Machine Type: ${BGN}$MACH${CL}" FORMAT="" MACHINE=" -machine q35" @@ -207,11 +209,26 @@ function advanced_settings() { exit-script fi + if DISK_SIZE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set Disk Size in GiB (e.g., 10, 20)" 8 58 "$DISK_SIZE" --title "DISK SIZE" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then + DISK_SIZE=$(echo "$DISK_SIZE" | tr -d ' ') + if [[ "$DISK_SIZE" =~ ^[0-9]+$ ]]; then + DISK_SIZE="${DISK_SIZE}G" + echo -e "${DISKSIZE}${BOLD}${DGN}Disk Size: ${BGN}$DISK_SIZE${CL}" + elif [[ "$DISK_SIZE" =~ ^[0-9]+G$ ]]; then + echo -e "${DISKSIZE}${BOLD}${DGN}Disk Size: ${BGN}$DISK_SIZE${CL}" + else + echo -e "${DISKSIZE}${BOLD}${RD}Invalid Disk Size. Please use a number (e.g., 10 or 10G).${CL}" + exit-script + fi + else + exit-script + fi + if DISK_CACHE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "DISK CACHE" --radiolist "Choose" --cancel-button Exit-Script 10 58 2 \ "0" "None (Default)" ON \ "1" "Write Through" OFF \ 3>&1 1>&2 2>&3); then - if [ $DISK_CACHE = "1" ]; then + if [ "$DISK_CACHE" = "1" ]; then echo -e "${DGN}Using Disk Cache: ${BGN}Write Through${CL}" DISK_CACHE="cache=writethrough," else @@ -223,11 +240,11 @@ function advanced_settings() { fi if VM_NAME=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set Hostname" 8 58 docker --title "HOSTNAME" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then - if [ -z $VM_NAME ]; then + if [ -z "$VM_NAME" ]; then HN="docker" echo -e "${DGN}Using Hostname: ${BGN}$HN${CL}" else - HN=$(echo ${VM_NAME,,} | tr -d ' ') + HN=$(echo "${VM_NAME,,}" | tr -d ' ') echo -e "${DGN}Using Hostname: ${BGN}$HN${CL}" fi else @@ -238,7 +255,7 @@ function advanced_settings() { "0" "KVM64 (Default)" ON \ "1" "Host" OFF \ 3>&1 1>&2 2>&3); then - if [ $CPU_TYPE1 = "1" ]; then + if [ "$CPU_TYPE1" = "1" ]; then echo -e "${DGN}Using CPU Model: ${BGN}Host${CL}" CPU_TYPE=" -cpu host" else @@ -250,7 +267,7 @@ function advanced_settings() { fi if CORE_COUNT=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Allocate CPU Cores" 8 58 2 --title "CORE COUNT" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then - if [ -z $CORE_COUNT ]; then + if [ -z "$CORE_COUNT" ]; then CORE_COUNT="2" echo -e "${DGN}Allocated Cores: ${BGN}$CORE_COUNT${CL}" else @@ -261,7 +278,7 @@ function advanced_settings() { fi if RAM_SIZE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Allocate RAM in MiB" 8 58 4096 --title "RAM" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then - if [ -z $RAM_SIZE ]; then + if [ -z "$RAM_SIZE" ]; then RAM_SIZE="4096" echo -e "${DGN}Allocated RAM: ${BGN}$RAM_SIZE${CL}" else @@ -272,7 +289,7 @@ function advanced_settings() { fi if BRG=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a Bridge" 8 58 vmbr0 --title "BRIDGE" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then - if [ -z $BRG ]; then + if [ -z "$BRG" ]; then BRG="vmbr0" echo -e "${DGN}Using Bridge: ${BGN}$BRG${CL}" else @@ -282,8 +299,8 @@ function advanced_settings() { exit-script fi - if MAC1=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a MAC Address" 8 58 $GEN_MAC --title "MAC ADDRESS" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then - if [ -z $MAC1 ]; then + if MAC1=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a MAC Address" 8 58 "$GEN_MAC" --title "MAC ADDRESS" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then + if [ -z "$MAC1" ]; then MAC="$GEN_MAC" echo -e "${DGN}Using MAC Address: ${BGN}$MAC${CL}" else @@ -295,7 +312,7 @@ function advanced_settings() { fi if VLAN1=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a Vlan(leave blank for default)" 8 58 --title "VLAN" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then - if [ -z $VLAN1 ]; then + if [ -z "$VLAN1" ]; then VLAN1="Default" VLAN="" echo -e "${DGN}Using Vlan: ${BGN}$VLAN1${CL}" @@ -308,7 +325,7 @@ function advanced_settings() { fi if MTU1=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set Interface MTU Size (leave blank for default)" 8 58 --title "MTU SIZE" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then - if [ -z $MTU1 ]; then + if [ -z "$MTU1" ]; then MTU1="Default" MTU="" echo -e "${DGN}Using Interface MTU Size: ${BGN}$MTU1${CL}" @@ -358,9 +375,9 @@ post_to_api_vm msg_info "Validating Storage" while read -r line; do - TAG=$(echo $line | awk '{print $1}') - TYPE=$(echo $line | awk '{printf "%-10s", $2}') - FREE=$(echo $line | numfmt --field 4-6 --from-unit=K --to=iec --format %.2f | awk '{printf( "%9sB", $6)}') + TAG=$(echo "$line" | awk '{print $1}') + TYPE=$(echo "$line" | awk '{printf "%-10s", $2}') + FREE=$(echo "$line" | numfmt --field 4-6 --from-unit=K --to=iec --format %.2f | awk '{printf( "%9sB", $6)}') ITEM=" Type: $TYPE Free: $FREE " OFFSET=2 if [[ $((${#ITEM} + $OFFSET)) -gt ${MSG_MAX_LENGTH:-} ]]; then @@ -390,10 +407,10 @@ sleep 2 msg_ok "${CL}${BL}${URL}${CL}" curl -f#SL -o "$(basename "$URL")" "$URL" echo -en "\e[1A\e[0K" -FILE=$(basename $URL) +FILE=$(basename "$URL") msg_ok "Downloaded ${CL}${BL}${FILE}${CL}" -STORAGE_TYPE=$(pvesm status -storage $STORAGE | awk 'NR>1 {print $2}') +STORAGE_TYPE=$(pvesm status -storage "$STORAGE" | awk 'NR>1 {print $2}') case $STORAGE_TYPE in nfs | dir) DISK_EXT=".qcow2" @@ -411,8 +428,8 @@ btrfs) esac for i in {0,1}; do disk="DISK$i" - eval DISK${i}=vm-${VMID}-disk-${i}${DISK_EXT:-} - eval DISK${i}_REF=${STORAGE}:${DISK_REF:-}${!disk} + eval DISK"${i}"=vm-"${VMID}"-disk-"${i}"""${DISK_EXT:-"}" + eval DISK"${i}"_REF="${STORAGE}":"${DISK_REF:-}"""${!disk"}" done msg_info "Installing Pre-Requisite libguestfs-tools onto Host" @@ -435,10 +452,9 @@ pvesm alloc $STORAGE $VMID $DISK0 4M 1>&/dev/null qm importdisk $VMID ${FILE} $STORAGE ${DISK_IMPORT:-} 1>&/dev/null qm set $VMID \ -efidisk0 ${DISK0_REF}${FORMAT} \ - -scsi0 ${DISK1_REF},${DISK_CACHE}${THIN}size=2G \ + -scsi0 ${DISK1_REF},${DISK_CACHE}${THIN}size=${DISK_SIZE} \ -boot order=scsi0 \ -serial0 socket >/dev/null -qm resize $VMID scsi0 8G >/dev/null qm set $VMID --agent enabled=1 >/dev/null DESCRIPTION=$( @@ -473,6 +489,14 @@ EOF ) qm set "$VMID" -description "$DESCRIPTION" >/dev/null +if [ -n "$DISK_SIZE" ]; then + msg_info "Resizing disk to $DISK_SIZE GB" + qm resize $VMID scsi0 ${DISK_SIZE} >/dev/null +else + msg_info "Using default disk size of $DEFAULT_DISK_SIZE GB" + qm resize $VMID scsi0 ${DEFAULT_DISK_SIZE} >/dev/null +fi + msg_ok "Created a Docker VM ${CL}${BL}(${HN})" if [ "$START_VM" == "yes" ]; then msg_info "Starting Docker VM"