diff --git a/vm/archlinux-vm.sh b/vm/archlinux-vm.sh index e749c49f3..223c5796a 100644 --- a/vm/archlinux-vm.sh +++ b/vm/archlinux-vm.sh @@ -26,7 +26,6 @@ NSAPP="arch-linux-vm" var_os="arch-linux" var_version=" " GEN_MAC=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") @@ -76,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 +177,7 @@ function exit-script() { } function default_settings() { - VMID="$NEXTID" + VMID=$(get_valid_nextid) FORMAT=",efitype=4m" MACHINE="" DISK_SIZE="4G" @@ -194,10 +210,11 @@ function default_settings() { function advanced_settings() { 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}" diff --git a/vm/debian-vm.sh b/vm/debian-vm.sh index 6242346f1..d99fe39e4 100644 --- a/vm/debian-vm.sh +++ b/vm/debian-vm.sh @@ -20,7 +20,6 @@ EOF header_info echo -e "\n Loading..." GEN_MAC=02:$(openssl rand -hex 5 | awk '{print toupper($0)}' | sed 's/\(..\)/\1:/g; s/.$//') -NEXTID=$(pvesh get /cluster/nextid) RANDOM_UUID="$(cat /proc/sys/kernel/random/uuid)" METHOD="" NSAPP="debian12vm" @@ -75,6 +74,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 +177,7 @@ function exit-script() { } function default_settings() { - VMID="$NEXTID" + VMID=$(get_valid_nextid) FORMAT=",efitype=4m" MACHINE="" DISK_SIZE="8G" @@ -194,10 +210,11 @@ function default_settings() { function advanced_settings() { 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}" diff --git a/vm/docker-vm.sh b/vm/docker-vm.sh index 3cfa2dfc8..92194d17a 100644 --- a/vm/docker-vm.sh +++ b/vm/docker-vm.sh @@ -20,7 +20,6 @@ EOF header_info echo -e "\n Loading..." GEN_MAC=02:$(openssl rand -hex 5 | awk '{print toupper($0)}' | sed 's/\(..\)/\1:/g; s/.$//') -NEXTID=$(pvesh get /cluster/nextid) RANDOM_UUID="$(cat /proc/sys/kernel/random/uuid)" METHOD="" NSAPP="debian12vm" @@ -76,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 @@ -162,7 +178,7 @@ function exit-script() { } function default_settings() { - VMID="$NEXTID" + VMID=$(get_valid_nextid) FORMAT=",efitype=4m" MACHINE="" DISK_CACHE="" @@ -195,10 +211,11 @@ function default_settings() { function advanced_settings() { 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}" @@ -244,7 +261,6 @@ function advanced_settings() { 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 \ diff --git a/vm/haos-vm.sh b/vm/haos-vm.sh index 5055591a4..a64147446 100644 --- a/vm/haos-vm.sh +++ b/vm/haos-vm.sh @@ -21,15 +21,13 @@ EOF header_info echo -e "\n Loading..." GEN_MAC=02:$(openssl rand -hex 5 | awk '{print toupper($0)}' | sed 's/\(..\)/\1:/g; s/.$//') -NEXTID=$(pvesh get /cluster/nextid) VERSIONS=(stable beta dev) -#API VARIABLES RANDOM_UUID="$(cat /proc/sys/kernel/random/uuid)" METHOD="" NSAPP="homeassistant-os" var_os="homeassistant" DISK_SIZE="32G" -# + for version in "${VERSIONS[@]}"; do eval "$version=$(curl -fsSL https://raw.githubusercontent.com/home-assistant/version/master/stable.json | grep '"ova"' | cut -d '"' -f 4)" done @@ -65,6 +63,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 @@ -166,7 +181,7 @@ function exit-script() { function default_settings() { BRANCH="$stable" - VMID="$NEXTID" + VMID=$(get_valid_nextid) FORMAT=",efitype=4m" MACHINE="" DISK_CACHE="cache=writethrough," @@ -210,10 +225,11 @@ function advanced_settings() { exit-script fi + [ -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="$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}" diff --git a/vm/mikrotik-routeros.sh b/vm/mikrotik-routeros.sh index 730a74c28..95ed5c82d 100644 --- a/vm/mikrotik-routeros.sh +++ b/vm/mikrotik-routeros.sh @@ -21,15 +21,12 @@ clear header_info echo -e "Loading..." GEN_MAC=$(echo '00 60 2f'$(od -An -N3 -t xC /dev/urandom) | sed -e 's/ /:/g' | tr '[:lower:]' '[:upper:]') -NEXTID=$(pvesh get /cluster/nextid) -#API VARIABLES RANDOM_UUID="$(cat /proc/sys/kernel/random/uuid)" METHOD="" NSAPP="mikrotik-router-os" var_os="mikrotik" var_version=" " DISK_SIZE="1G" -# YW=$(echo "\033[33m") BL=$(echo "\033[36m") HA=$(echo "\033[1;34m") @@ -60,6 +57,24 @@ function error_exit() { [ ! -z ${VMID-} ] && cleanup_vmid exit $EXIT } + +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 if [ "$(qm status $VMID | awk '{print $2}')" == "running" ]; then diff --git a/vm/nextcloud-vm.sh b/vm/nextcloud-vm.sh index 8bbbb5c5d..5ecec0b19 100644 --- a/vm/nextcloud-vm.sh +++ b/vm/nextcloud-vm.sh @@ -19,16 +19,13 @@ EOF } header_info echo -e "\n Loading..." -#API VARIABLES RANDOM_UUID="$(cat /proc/sys/kernel/random/uuid)" METHOD="" NSAPP="turnkey-nextcloud" var_os="turnkey-nextcloud" var_version=" " DISK_SIZE="12G" -# GEN_MAC=02:$(openssl rand -hex 5 | awk '{print toupper($0)}' | sed 's/\(..\)/\1:/g; s/.$//') -NEXTID=$(pvesh get /cluster/nextid) NAME="TurnKey Nexcloud VM" YW=$(echo "\033[33m") BL=$(echo "\033[36m") @@ -58,6 +55,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 @@ -142,7 +156,7 @@ function exit-script() { } function default_settings() { - VMID="$NEXTID" + VMID=$(get_valid_nextid) FORMAT=",efitype=4m" MACHINE="" DISK_CACHE="" @@ -173,10 +187,11 @@ function default_settings() { function advanced_settings() { 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}" diff --git a/vm/openwrt.sh b/vm/openwrt.sh index 26a747647..9c17c9625 100644 --- a/vm/openwrt.sh +++ b/vm/openwrt.sh @@ -23,17 +23,15 @@ EOF } header_info echo -e "Loading..." -#API VARIABLES RANDOM_UUID="$(cat /proc/sys/kernel/random/uuid)" METHOD="" NSAPP="openwrt-vm" var_os="openwrt" var_version=" " DISK_SIZE="0.5G" -# 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") @@ -61,6 +59,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 @@ -242,10 +257,11 @@ function default_settings() { function advanced_settings() { 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}" diff --git a/vm/opnsense-vm.sh b/vm/opnsense-vm.sh index 1f811da0e..ff2c2aac7 100644 --- a/vm/opnsense-vm.sh +++ b/vm/opnsense-vm.sh @@ -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}" diff --git a/vm/owncloud-vm.sh b/vm/owncloud-vm.sh index 29b0e34eb..dcc06fdfb 100644 --- a/vm/owncloud-vm.sh +++ b/vm/owncloud-vm.sh @@ -19,17 +19,15 @@ EOF } header_info echo -e "\n Loading..." -#API VARIABLES RANDOM_UUID="$(cat /proc/sys/kernel/random/uuid)" METHOD="" NSAPP="turnkey-owncloud-vm" var_os="owncloud" var_version="12" DISK_SIZE="12G" -# GEN_MAC=02:$(openssl rand -hex 5 | awk '{print toupper($0)}' | sed 's/\(..\)/\1:/g; s/.$//') -NEXTID=$(pvesh get /cluster/nextid) NAME="TurnKey ownCloud VM" + YW=$(echo "\033[33m") BL=$(echo "\033[36m") HA=$(echo "\033[1;34m") @@ -58,6 +56,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 @@ -142,7 +157,7 @@ function exit-script() { } function default_settings() { - VMID="$NEXTID" + VMID=$(get_valid_nextid) FORMAT=",efitype=4m" MACHINE="" DISK_CACHE="" @@ -173,10 +188,11 @@ function default_settings() { function advanced_settings() { 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}" diff --git a/vm/pimox-haos-vm.sh b/vm/pimox-haos-vm.sh index ab9a9e43b..7441e6b22 100644 --- a/vm/pimox-haos-vm.sh +++ b/vm/pimox-haos-vm.sh @@ -24,17 +24,14 @@ EOF clear header_info echo -e "Loading..." -#API VARIABLES RANDOM_UUID="$(cat /proc/sys/kernel/random/uuid)" METHOD="" NSAPP="pimox-haos-vm" var_os="pimox-haos" var_version=" " DISK_SIZE="32G" -# GEN_MAC=$(echo '00 60 2f'$(od -An -N3 -t xC /dev/urandom) | sed -e 's/ /:/g' | tr '[:lower:]' '[:upper:]') USEDID=$(pvesh get /cluster/resources --type vm --output-format yaml | egrep -i 'vmid' | awk '{print substr($2, 1, length($2)-0) }') -NEXTID=$(pvesh get /cluster/nextid) STABLE=$(curl -fsSL https://raw.githubusercontent.com/home-assistant/version/master/stable.json | grep "ova" | awk '{print substr($2, 2, length($2)-3) }') BETA=$(curl -fsSL https://raw.githubusercontent.com/home-assistant/version/master/beta.json | grep "ova" | awk '{print substr($2, 2, length($2)-3) }') DEV=$(curl -fsSL https://raw.githubusercontent.com/home-assistant/version/master/dev.json | grep "ova" | awk '{print substr($2, 2, length($2)-3) }') @@ -70,6 +67,24 @@ function error_exit() { [ ! -z ${VMID-} ] && cleanup_vmid exit $EXIT } + +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 if [ "$(qm status $VMID | awk '{print $2}')" == "running" ]; then diff --git a/vm/ubuntu2204-vm.sh b/vm/ubuntu2204-vm.sh index a71754ebf..a662efb3c 100644 --- a/vm/ubuntu2204-vm.sh +++ b/vm/ubuntu2204-vm.sh @@ -20,7 +20,6 @@ EOF header_info echo -e "\n Loading..." GEN_MAC=02:$(openssl rand -hex 5 | awk '{print toupper($0)}' | sed 's/\(..\)/\1:/g; s/.$//') -NEXTID=$(pvesh get /cluster/nextid) RANDOM_UUID="$(cat /proc/sys/kernel/random/uuid)" METHOD="" NSAPP="ubuntu-2204-vm" @@ -33,8 +32,6 @@ RD=$(echo "\033[01;31m") BGN=$(echo "\033[4;92m") GN=$(echo "\033[1;92m") DGN=$(echo "\033[32m") -CL=$(echo "\033[m") - CL=$(echo "\033[m") BOLD=$(echo "\033[1m") BFR="\\r\\033[K" @@ -75,6 +72,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 @@ -160,7 +174,7 @@ function exit-script() { } function default_settings() { - VMID="$NEXTID" + VMID=$(get_valid_nextid) FORMAT=",efitype=4m" MACHINE="" DISK_SIZE="5G" @@ -193,10 +207,11 @@ function default_settings() { function advanced_settings() { 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}" diff --git a/vm/ubuntu2404-vm.sh b/vm/ubuntu2404-vm.sh index ee42c6b92..28f1f4e20 100644 --- a/vm/ubuntu2404-vm.sh +++ b/vm/ubuntu2404-vm.sh @@ -21,7 +21,6 @@ EOF header_info echo -e "\n Loading..." GEN_MAC=02:$(openssl rand -hex 5 | awk '{print toupper($0)}' | sed 's/\(..\)/\1:/g; s/.$//') -NEXTID=$(pvesh get /cluster/nextid) RANDOM_UUID="$(cat /proc/sys/kernel/random/uuid)" METHOD="" NSAPP="ubuntu-2404-vm" @@ -76,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 +177,7 @@ function exit-script() { } function default_settings() { - VMID="$NEXTID" + VMID=$(get_valid_nextid) FORMAT=",efitype=4m" MACHINE="" DISK_SIZE="7G" @@ -194,10 +210,11 @@ function default_settings() { function advanced_settings() { 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}" diff --git a/vm/ubuntu2410-vm.sh b/vm/ubuntu2410-vm.sh index e02e92af7..1711430e8 100644 --- a/vm/ubuntu2410-vm.sh +++ b/vm/ubuntu2410-vm.sh @@ -20,7 +20,6 @@ EOF header_info echo -e "\n Loading..." GEN_MAC=02:$(openssl rand -hex 5 | awk '{print toupper($0)}' | sed 's/\(..\)/\1:/g; s/.$//') -NEXTID=$(pvesh get /cluster/nextid) RANDOM_UUID="$(cat /proc/sys/kernel/random/uuid)" METHOD="" NSAPP="ubuntu-2410-vm" @@ -75,6 +74,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 @@ -160,7 +176,7 @@ function exit-script() { } function default_settings() { - VMID="$NEXTID" + VMID=$(get_valid_nextid) FORMAT=",efitype=4m" MACHINE="" DISK_SIZE="8G" @@ -193,10 +209,11 @@ function default_settings() { function advanced_settings() { 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}"