mirror of
				https://github.com/community-scripts/ProxmoxVE.git
				synced 2025-11-04 10:22:50 +00:00 
			
		
		
		
	Feature: get correct next VMID (#4292)
fix(vm-creation): ensure whiptail VMID inputbox is pre-filled with a valid and available ID - Replaced hardcoded NEXTID usage with a new get_valid_nextid() function - Ensures no collision with existing VMs, LXCs or orphaned LVM volumes - Improves user experience by properly pre-filling whiptail inputbox - Automatically skips invalid IDs instead of failing on alloc
This commit is contained in:
		@@ -28,7 +28,7 @@ var_version="25.1"
 | 
			
		||||
#
 | 
			
		||||
GEN_MAC=02:$(openssl rand -hex 5 | awk '{print toupper($0)}' | sed 's/\(..\)/\1:/g; s/.$//')
 | 
			
		||||
GEN_MAC_LAN=02:$(openssl rand -hex 5 | awk '{print toupper($0)}' | sed 's/\(..\)/\1:/g; s/.$//')
 | 
			
		||||
NEXTID=$(pvesh get /cluster/nextid)
 | 
			
		||||
 | 
			
		||||
YW=$(echo "\033[33m")
 | 
			
		||||
BL=$(echo "\033[36m")
 | 
			
		||||
HA=$(echo "\033[1;34m")
 | 
			
		||||
@@ -54,6 +54,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
 | 
			
		||||
@@ -202,7 +219,7 @@ function exit-script() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function default_settings() {
 | 
			
		||||
  VMID="$NEXTID"
 | 
			
		||||
  VMID=$(get_valid_nextid)
 | 
			
		||||
  FORMAT=",efitype=4m"
 | 
			
		||||
  MACHINE=""
 | 
			
		||||
  DISK_CACHE=""
 | 
			
		||||
@@ -252,10 +269,11 @@ function default_settings() {
 | 
			
		||||
function advanced_settings() {
 | 
			
		||||
  local ip_regex='^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$'
 | 
			
		||||
  METHOD="advanced"
 | 
			
		||||
  [ -z "${VMID:-}" ] && VMID=$(get_valid_nextid)
 | 
			
		||||
  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=$(get_valid_nextid)
 | 
			
		||||
      fi
 | 
			
		||||
      if pct status "$VMID" &>/dev/null || qm status "$VMID" &>/dev/null; then
 | 
			
		||||
        echo -e "${CROSS}${RD} ID $VMID is already in use${CL}"
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user