diff --git a/vm/debian-vm.sh b/vm/debian-vm.sh index 3512f7d..6df528c 100644 --- a/vm/debian-vm.sh +++ b/vm/debian-vm.sh @@ -75,6 +75,23 @@ function error_handler() { cleanup_vmid } +function get_valid_nextid() { + local try_id + try_id=$(pvesh get /cluster/nextid) + while true; do + if [ -f "/etc/pve/qemu-server/${try_id}.conf" ] || [ -f "/etc/pve/lxc/${try_id}.conf" ]; then + try_id=$((try_id + 1)) + continue + fi + if lvs --noheadings -o lv_name | grep -qE "(^|[-_])${try_id}($|[-_])"; then + try_id=$((try_id + 1)) + continue + fi + break + done + echo "$try_id" +} + function cleanup_vmid() { if qm status $VMID &>/dev/null; then qm stop $VMID &>/dev/null @@ -161,7 +178,7 @@ function exit-script() { } function default_settings() { - VMID="$NEXTID" + VMID=$(get_valid_nextid) FORMAT=",efitype=4m" MACHINE="" DISK_SIZE="8G" @@ -195,9 +212,9 @@ 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 $VMID --title "VIRTUAL MACHINE ID" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then if [ -z "$VMID" ]; then - VMID="$NEXTID" + VMID="$$VMID" fi if pct status "$VMID" &>/dev/null || qm status "$VMID" &>/dev/null; then echo -e "${CROSS}${RD} ID $VMID is already in use${CL}"