QM Create using the next available VMID? (CLI)

nmiller0113

Active Member
Aug 23, 2012
16
1
43
I do most of my creation/cloning of VMs from the CLI and with Ansible. I was wondering if there was a way to I don't have to explicitly define the new vmid. When working in the WebUI it will always suggest the next vmid to use, so I was wondering if there was a way to take advantage of that logic in the CLI and not have to define it each time I create/clone a VM? I'm hoping I'm just missing something and there is a way that to make it automatically use the next available from the CLI. Thanks!
 
  • Like
Reactions: nmiller0113
I use the following snippet to calculate the next available id:

# List all the existing VM IDs in-use
head="VMID"
idlist=$(qm list | awk '{print $1}' | awk -v val="$head" '$1 != val {print}' | sort)

# Query the last ID among these
lastid=$(echo ${idlist} | awk '{print $NF}')

# Increment it to get the next available ID
newid=$(expr ${lastid} \+ 1)

You may additionally check for boundaries (like $newid > 999999999).
 
Last edited:
# List all the existing VM IDs in-use
idlist=$(qm list | awk '{print $1}' | awk -v val="$head" '$1 != val {print}' | sort)
Keep in mind that qm will list VMs on the server you run the command on. Will not list VMs on other nodes of your cluster. IMHO, it's a lot easier to just use newId="$(pvesh get /cluster/nextid)" as it will check all IDs in the cluster.
 
  • Like
Reactions: bhaskaraba
Keep in mind that qm will list VMs on the server you run the command on. Will not list VMs on other nodes of your cluster. IMHO, it's a lot easier to just use newId="$(pvesh get /cluster/nextid)" as it will check all IDs in the cluster.
Thanks for suggesting this @VictorSTS.
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!