check if vmid already exists in cluster

macleod

Well-Known Member
Aug 3, 2017
66
8
48
47
Hello

I'm playing with some scripting stuff, but got into some issues.
I'm trying to find out if a virtual machine with id $vmid already exists or not IN THE CLUSTER. The web interface stops me from creating virtual machines with duplicate ids, so the check is surely built in.
Ideas:
I could check for $vmid.conf in /etc/pve/nodes/*/qemu-server (also ctx dir), but seems to be too 'bloaty'
qm status $vmid does not work if the vm is located on another machine from cluster
also I would like that the check will be universal (against VMs & also containers)

btw: the qm create will return errors if that id is already used by some vm/ctx ?

ps: I'm not interested in getting the next valid id in cluster, but checking that the desired vmid is not currently used by another vm or container in cluster

thank you
 
well, I asked, I will answer :-P
by doing some reverse engineering in web interface, it's easy to answer

# pvesh get /cluster/nextid -vmid 9999
200 OK
"9999"
# pvesh get /cluster/nextid -vmid 193
400 Parameter verification failed.
vmid: VM 193 already exists
get cluster/nextid [OPTIONS]
 
check /etc/pve/.vmlist

thank you, that is even better that what I've found (although the api interface is probably just using the same file)
can I make the assumption that the vmid will be forever (at least for version 5) at the beginning of the line ?
or better parse the file as json, the safest and cleanest way (even if a grep it's easier to do)
 
I see it's JSON, but in particularly the vmids are written one per line, at the very beginning of the line (and between ")
but even if a grep it's simpler, for the sake of good programming JSON parsing will do the trick, as the file format could change in the future (i.e. all vmids on the same line, same JSON data, but another way of formatting it)
thank you