Skip to content

Commit c9aad3a

Browse files
authored
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
1 parent ebc17e1 commit c9aad3a

13 files changed

+270
-60
lines changed

vm/archlinux-vm.sh

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ NSAPP="arch-linux-vm"
2626
var_os="arch-linux"
2727
var_version=" "
2828
GEN_MAC=02:$(openssl rand -hex 5 | awk '{print toupper($0)}' | sed 's/\(..\)/\1:/g; s/.$//')
29-
NEXTID=$(pvesh get /cluster/nextid)
3029

3130
YW=$(echo "\033[33m")
3231
BL=$(echo "\033[36m")
@@ -76,6 +75,23 @@ function error_handler() {
7675
cleanup_vmid
7776
}
7877

78+
function get_valid_nextid() {
79+
local try_id
80+
try_id=$(pvesh get /cluster/nextid)
81+
while true; do
82+
if [ -f "/etc/pve/qemu-server/${try_id}.conf" ] || [ -f "/etc/pve/lxc/${try_id}.conf" ]; then
83+
try_id=$((try_id + 1))
84+
continue
85+
fi
86+
if lvs --noheadings -o lv_name | grep -qE "(^|[-_])${try_id}($|[-_])"; then
87+
try_id=$((try_id + 1))
88+
continue
89+
fi
90+
break
91+
done
92+
echo "$try_id"
93+
}
94+
7995
function cleanup_vmid() {
8096
if qm status $VMID &>/dev/null; then
8197
qm stop $VMID &>/dev/null
@@ -161,7 +177,7 @@ function exit-script() {
161177
}
162178

163179
function default_settings() {
164-
VMID="$NEXTID"
180+
VMID=$(get_valid_nextid)
165181
FORMAT=",efitype=4m"
166182
MACHINE=""
167183
DISK_SIZE="4G"
@@ -194,10 +210,11 @@ function default_settings() {
194210

195211
function advanced_settings() {
196212
METHOD="advanced"
213+
[ -z "${VMID:-}" ] && VMID=$(get_valid_nextid)
197214
while true; do
198-
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
215+
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
199216
if [ -z "$VMID" ]; then
200-
VMID="$NEXTID"
217+
VMID=$(get_valid_nextid)
201218
fi
202219
if pct status "$VMID" &>/dev/null || qm status "$VMID" &>/dev/null; then
203220
echo -e "${CROSS}${RD} ID $VMID is already in use${CL}"

vm/debian-vm.sh

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ EOF
2020
header_info
2121
echo -e "\n Loading..."
2222
GEN_MAC=02:$(openssl rand -hex 5 | awk '{print toupper($0)}' | sed 's/\(..\)/\1:/g; s/.$//')
23-
NEXTID=$(pvesh get /cluster/nextid)
2423
RANDOM_UUID="$(cat /proc/sys/kernel/random/uuid)"
2524
METHOD=""
2625
NSAPP="debian12vm"
@@ -75,6 +74,23 @@ function error_handler() {
7574
cleanup_vmid
7675
}
7776

77+
function get_valid_nextid() {
78+
local try_id
79+
try_id=$(pvesh get /cluster/nextid)
80+
while true; do
81+
if [ -f "/etc/pve/qemu-server/${try_id}.conf" ] || [ -f "/etc/pve/lxc/${try_id}.conf" ]; then
82+
try_id=$((try_id + 1))
83+
continue
84+
fi
85+
if lvs --noheadings -o lv_name | grep -qE "(^|[-_])${try_id}($|[-_])"; then
86+
try_id=$((try_id + 1))
87+
continue
88+
fi
89+
break
90+
done
91+
echo "$try_id"
92+
}
93+
7894
function cleanup_vmid() {
7995
if qm status $VMID &>/dev/null; then
8096
qm stop $VMID &>/dev/null
@@ -161,7 +177,7 @@ function exit-script() {
161177
}
162178

163179
function default_settings() {
164-
VMID="$NEXTID"
180+
VMID=$(get_valid_nextid)
165181
FORMAT=",efitype=4m"
166182
MACHINE=""
167183
DISK_SIZE="8G"
@@ -194,10 +210,11 @@ function default_settings() {
194210

195211
function advanced_settings() {
196212
METHOD="advanced"
213+
[ -z "${VMID:-}" ] && VMID=$(get_valid_nextid)
197214
while true; do
198-
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
215+
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
199216
if [ -z "$VMID" ]; then
200-
VMID="$NEXTID"
217+
VMID=$(get_valid_nextid)
201218
fi
202219
if pct status "$VMID" &>/dev/null || qm status "$VMID" &>/dev/null; then
203220
echo -e "${CROSS}${RD} ID $VMID is already in use${CL}"

vm/docker-vm.sh

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ EOF
2020
header_info
2121
echo -e "\n Loading..."
2222
GEN_MAC=02:$(openssl rand -hex 5 | awk '{print toupper($0)}' | sed 's/\(..\)/\1:/g; s/.$//')
23-
NEXTID=$(pvesh get /cluster/nextid)
2423
RANDOM_UUID="$(cat /proc/sys/kernel/random/uuid)"
2524
METHOD=""
2625
NSAPP="debian12vm"
@@ -76,6 +75,23 @@ function error_handler() {
7675
cleanup_vmid
7776
}
7877

78+
function get_valid_nextid() {
79+
local try_id
80+
try_id=$(pvesh get /cluster/nextid)
81+
while true; do
82+
if [ -f "/etc/pve/qemu-server/${try_id}.conf" ] || [ -f "/etc/pve/lxc/${try_id}.conf" ]; then
83+
try_id=$((try_id + 1))
84+
continue
85+
fi
86+
if lvs --noheadings -o lv_name | grep -qE "(^|[-_])${try_id}($|[-_])"; then
87+
try_id=$((try_id + 1))
88+
continue
89+
fi
90+
break
91+
done
92+
echo "$try_id"
93+
}
94+
7995
function cleanup_vmid() {
8096
if qm status $VMID &>/dev/null; then
8197
qm stop $VMID &>/dev/null
@@ -162,7 +178,7 @@ function exit-script() {
162178
}
163179

164180
function default_settings() {
165-
VMID="$NEXTID"
181+
VMID=$(get_valid_nextid)
166182
FORMAT=",efitype=4m"
167183
MACHINE=""
168184
DISK_CACHE=""
@@ -195,10 +211,11 @@ function default_settings() {
195211

196212
function advanced_settings() {
197213
METHOD="advanced"
214+
[ -z "${VMID:-}" ] && VMID=$(get_valid_nextid)
198215
while true; do
199-
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
216+
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
200217
if [ -z "$VMID" ]; then
201-
VMID="$NEXTID"
218+
VMID=$(get_valid_nextid)
202219
fi
203220
if pct status "$VMID" &>/dev/null || qm status "$VMID" &>/dev/null; then
204221
echo -e "${CROSS}${RD} ID $VMID is already in use${CL}"
@@ -244,7 +261,6 @@ function advanced_settings() {
244261
exit-script
245262
fi
246263

247-
248264
if DISK_CACHE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "DISK CACHE" --radiolist "Choose" --cancel-button Exit-Script 10 58 2 \
249265
"0" "None (Default)" ON \
250266
"1" "Write Through" OFF \

vm/haos-vm.sh

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,13 @@ EOF
2121
header_info
2222
echo -e "\n Loading..."
2323
GEN_MAC=02:$(openssl rand -hex 5 | awk '{print toupper($0)}' | sed 's/\(..\)/\1:/g; s/.$//')
24-
NEXTID=$(pvesh get /cluster/nextid)
2524
VERSIONS=(stable beta dev)
26-
#API VARIABLES
2725
RANDOM_UUID="$(cat /proc/sys/kernel/random/uuid)"
2826
METHOD=""
2927
NSAPP="homeassistant-os"
3028
var_os="homeassistant"
3129
DISK_SIZE="32G"
32-
#
30+
3331
for version in "${VERSIONS[@]}"; do
3432
eval "$version=$(curl -fsSL https://raw.githubusercontent.com/home-assistant/version/master/stable.json | grep '"ova"' | cut -d '"' -f 4)"
3533
done
@@ -65,6 +63,23 @@ function error_handler() {
6563
cleanup_vmid
6664
}
6765

66+
function get_valid_nextid() {
67+
local try_id
68+
try_id=$(pvesh get /cluster/nextid)
69+
while true; do
70+
if [ -f "/etc/pve/qemu-server/${try_id}.conf" ] || [ -f "/etc/pve/lxc/${try_id}.conf" ]; then
71+
try_id=$((try_id + 1))
72+
continue
73+
fi
74+
if lvs --noheadings -o lv_name | grep -qE "(^|[-_])${try_id}($|[-_])"; then
75+
try_id=$((try_id + 1))
76+
continue
77+
fi
78+
break
79+
done
80+
echo "$try_id"
81+
}
82+
6883
function cleanup_vmid() {
6984
if qm status $VMID &>/dev/null; then
7085
qm stop $VMID &>/dev/null
@@ -166,7 +181,7 @@ function exit-script() {
166181

167182
function default_settings() {
168183
BRANCH="$stable"
169-
VMID="$NEXTID"
184+
VMID=$(get_valid_nextid)
170185
FORMAT=",efitype=4m"
171186
MACHINE=""
172187
DISK_CACHE="cache=writethrough,"
@@ -210,10 +225,11 @@ function advanced_settings() {
210225
exit-script
211226
fi
212227

228+
[ -z "${VMID:-}" ] && VMID=$(get_valid_nextid)
213229
while true; do
214-
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
230+
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
215231
if [ -z "$VMID" ]; then
216-
VMID="$NEXTID"
232+
VMID="$VMID"
217233
fi
218234
if pct status "$VMID" &>/dev/null || qm status "$VMID" &>/dev/null; then
219235
echo -e "${CROSS}${RD} ID $VMID is already in use${CL}"

vm/mikrotik-routeros.sh

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,12 @@ clear
2121
header_info
2222
echo -e "Loading..."
2323
GEN_MAC=$(echo '00 60 2f'$(od -An -N3 -t xC /dev/urandom) | sed -e 's/ /:/g' | tr '[:lower:]' '[:upper:]')
24-
NEXTID=$(pvesh get /cluster/nextid)
25-
#API VARIABLES
2624
RANDOM_UUID="$(cat /proc/sys/kernel/random/uuid)"
2725
METHOD=""
2826
NSAPP="mikrotik-router-os"
2927
var_os="mikrotik"
3028
var_version=" "
3129
DISK_SIZE="1G"
32-
#
3330
YW=$(echo "\033[33m")
3431
BL=$(echo "\033[36m")
3532
HA=$(echo "\033[1;34m")
@@ -60,6 +57,24 @@ function error_exit() {
6057
[ ! -z ${VMID-} ] && cleanup_vmid
6158
exit $EXIT
6259
}
60+
61+
function get_valid_nextid() {
62+
local try_id
63+
try_id=$(pvesh get /cluster/nextid)
64+
while true; do
65+
if [ -f "/etc/pve/qemu-server/${try_id}.conf" ] || [ -f "/etc/pve/lxc/${try_id}.conf" ]; then
66+
try_id=$((try_id + 1))
67+
continue
68+
fi
69+
if lvs --noheadings -o lv_name | grep -qE "(^|[-_])${try_id}($|[-_])"; then
70+
try_id=$((try_id + 1))
71+
continue
72+
fi
73+
break
74+
done
75+
echo "$try_id"
76+
}
77+
6378
function cleanup_vmid() {
6479
if $(qm status $VMID &>/dev/null); then
6580
if [ "$(qm status $VMID | awk '{print $2}')" == "running" ]; then

vm/nextcloud-vm.sh

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,13 @@ EOF
1919
}
2020
header_info
2121
echo -e "\n Loading..."
22-
#API VARIABLES
2322
RANDOM_UUID="$(cat /proc/sys/kernel/random/uuid)"
2423
METHOD=""
2524
NSAPP="turnkey-nextcloud"
2625
var_os="turnkey-nextcloud"
2726
var_version=" "
2827
DISK_SIZE="12G"
29-
#
3028
GEN_MAC=02:$(openssl rand -hex 5 | awk '{print toupper($0)}' | sed 's/\(..\)/\1:/g; s/.$//')
31-
NEXTID=$(pvesh get /cluster/nextid)
3229
NAME="TurnKey Nexcloud VM"
3330
YW=$(echo "\033[33m")
3431
BL=$(echo "\033[36m")
@@ -58,6 +55,23 @@ function error_handler() {
5855
cleanup_vmid
5956
}
6057

58+
function get_valid_nextid() {
59+
local try_id
60+
try_id=$(pvesh get /cluster/nextid)
61+
while true; do
62+
if [ -f "/etc/pve/qemu-server/${try_id}.conf" ] || [ -f "/etc/pve/lxc/${try_id}.conf" ]; then
63+
try_id=$((try_id + 1))
64+
continue
65+
fi
66+
if lvs --noheadings -o lv_name | grep -qE "(^|[-_])${try_id}($|[-_])"; then
67+
try_id=$((try_id + 1))
68+
continue
69+
fi
70+
break
71+
done
72+
echo "$try_id"
73+
}
74+
6175
function cleanup_vmid() {
6276
if qm status $VMID &>/dev/null; then
6377
qm stop $VMID &>/dev/null
@@ -142,7 +156,7 @@ function exit-script() {
142156
}
143157

144158
function default_settings() {
145-
VMID="$NEXTID"
159+
VMID=$(get_valid_nextid)
146160
FORMAT=",efitype=4m"
147161
MACHINE=""
148162
DISK_CACHE=""
@@ -173,10 +187,11 @@ function default_settings() {
173187

174188
function advanced_settings() {
175189
METHOD="advanced"
190+
[ -z "${VMID:-}" ] && VMID=$(get_valid_nextid)
176191
while true; do
177-
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
192+
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
178193
if [ -z "$VMID" ]; then
179-
VMID="$NEXTID"
194+
VMID=$(get_valid_nextid)
180195
fi
181196
if pct status "$VMID" &>/dev/null || qm status "$VMID" &>/dev/null; then
182197
echo -e "${CROSS}${RD} ID $VMID is already in use${CL}"

vm/openwrt.sh

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,15 @@ EOF
2323
}
2424
header_info
2525
echo -e "Loading..."
26-
#API VARIABLES
2726
RANDOM_UUID="$(cat /proc/sys/kernel/random/uuid)"
2827
METHOD=""
2928
NSAPP="openwrt-vm"
3029
var_os="openwrt"
3130
var_version=" "
3231
DISK_SIZE="0.5G"
33-
#
3432
GEN_MAC=02:$(openssl rand -hex 5 | awk '{print toupper($0)}' | sed 's/\(..\)/\1:/g; s/.$//')
3533
GEN_MAC_LAN=02:$(openssl rand -hex 5 | awk '{print toupper($0)}' | sed 's/\(..\)/\1:/g; s/.$//')
36-
NEXTID=$(pvesh get /cluster/nextid)
34+
3735
YW=$(echo "\033[33m")
3836
BL=$(echo "\033[36m")
3937
HA=$(echo "\033[1;34m")
@@ -61,6 +59,23 @@ function error_handler() {
6159
cleanup_vmid
6260
}
6361

62+
function get_valid_nextid() {
63+
local try_id
64+
try_id=$(pvesh get /cluster/nextid)
65+
while true; do
66+
if [ -f "/etc/pve/qemu-server/${try_id}.conf" ] || [ -f "/etc/pve/lxc/${try_id}.conf" ]; then
67+
try_id=$((try_id + 1))
68+
continue
69+
fi
70+
if lvs --noheadings -o lv_name | grep -qE "(^|[-_])${try_id}($|[-_])"; then
71+
try_id=$((try_id + 1))
72+
continue
73+
fi
74+
break
75+
done
76+
echo "$try_id"
77+
}
78+
6479
function cleanup_vmid() {
6580
if qm status $VMID &>/dev/null; then
6681
qm stop $VMID &>/dev/null
@@ -242,10 +257,11 @@ function default_settings() {
242257

243258
function advanced_settings() {
244259
METHOD="advanced"
260+
[ -z "${VMID:-}" ] && VMID=$(get_valid_nextid)
245261
while true; do
246-
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
262+
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
247263
if [ -z "$VMID" ]; then
248-
VMID="$NEXTID"
264+
VMID=$(get_valid_nextid)
249265
fi
250266
if pct status "$VMID" &>/dev/null || qm status "$VMID" &>/dev/null; then
251267
echo -e "${CROSS}${RD} ID $VMID is already in use${CL}"

0 commit comments

Comments
 (0)