Is there an atomic way to get the next free VM_ID and reserve it?

Cylindric

Member
Mar 5, 2021
7
1
6
47
Hi all. I'm using automation to deploy VMs onto PVE, and I've come across a problem. In order to create a VM, I think I have to provide a VMID, for example in the API or with `qm create`. I can get the next free ID with `/cluster/nextid`, however if two processes try to build a VM at the same time, they'll fail if they both check `nextid` at the same time before they create a VM.
I need a way that's similar to how IPAM does "next free IP address" - it needs to reserve the one it returns so two things don't get the same number, or I need a way of call create that doesn't need a VMID.
 
Why do you need the next free ID? Why don't you just pick one at random and check if it's free? You will - of course - still have a non-zero probability of a race condition, but it may occur a lot less frequent.
 
Well, because asking if one exists and using it is better than assuming it doesn't and cobbling something together that isn't necessary.
 
  • Like
Reactions: anda42
Hi, there is currently no way to atomically lookup and reserve the next free VMID. See [1] for some related discussions. For your automation usecase, the best course of action would be the one suggested by LnxBil: Just generate a random VMID (e.g. between 100 and 1_000_000) and try to create a VM with that VMID using the POST /nodes/{node}/qemu [2] endpoint. Note that this request will fail if the VMID is already taken -- in that case, the caller should just generate a new random VMID and try again. With this, it should be impossible to run into a situation where two callers assume they "own" the same VMID.

[1] https://bugzilla.proxmox.com/show_bug.cgi?id=4369
[2] https://pve.proxmox.com/pve-docs/api-viewer/index.html#/nodes/{node}/qemu
 
Last edited:
  • Like
Reactions: LnxBil

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!