From: "Michael Köppl" <m.koeppl@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH guest-common v8 10/22] guest id: optionally enforce the next-id range and uniqueness
Date: Mon, 12 Oct 2026 17:45:48 +0200 [thread overview]
Message-ID: <20261012154600.877830-11-m.koeppl@proxmox.com> (raw)
In-Reply-To: <20261012154600.877830-1-m.koeppl@proxmox.com>
If the 'enforce' subproperty of next-id is enabled, ensure that only
guest IDs from the configured range and, if unique is enabled as well,
only previously unused IDs can be used. Actions on existing guests, such
as destroy, remote migration with --delete, and restore over an existing
guest, are always exempt from enforcement. A guest counts as existing if
its config is present in the cluster.
IDs are recorded before a guest exists and stay recorded if its
creation fails later. With both 'enforce' and 'unique' set, retrying a
failed creation with the same ID is therefore rejected.
Signed-off-by: Michael Köppl <m.koeppl@proxmox.com>
Reviewed-by: Fiona Ebner <f.ebner@proxmox.com>
Tested-by: Fiona Ebner <f.ebner@proxmox.com>
---
src/PVE/GuestID.pm | 32 +++++++++++++++++++++++++++-----
1 file changed, 27 insertions(+), 5 deletions(-)
diff --git a/src/PVE/GuestID.pm b/src/PVE/GuestID.pm
index 0494913..83876db 100644
--- a/src/PVE/GuestID.pm
+++ b/src/PVE/GuestID.pm
@@ -192,25 +192,47 @@ my sub insert_id($ranges, $id) {
return 1;
}
+my sub assert_id_in_next_id_range($next_id_opts, $id) {
+ my $lower = $next_id_opts->{lower};
+ my $upper = $next_id_opts->{upper};
+
+ die "guest ID $id is below the lower boundary $lower of the next-id range\n"
+ if defined($lower) && $id < $lower;
+ die "guest ID $id is not below the upper boundary $upper of the next-id range\n"
+ if defined($upper) && $id >= $upper;
+}
+
+# Records $id as used. If the next-id datacenter option enforces its
+# range or uniqueness, IDs a new guest must not use are rejected.
+# IDs of currently existing guests are never rejected.
sub register_used_id($id) {
- cfs_lock_file(
+ my $next_id_opts = cfs_read_file('datacenter.cfg')->{'next-id'} // {};
+ my $enforce = $next_id_opts->{enforce} && PVE::Cluster::check_vmid_unused($id, 1);
+
+ assert_id_in_next_id_range($next_id_opts, $id) if $enforce;
+
+ my $recorded = cfs_lock_file(
$FILENAME,
10,
sub {
my $ranges = cfs_read_file($FILENAME);
- return if !insert_id($ranges, $id);
+ return 0 if !insert_id($ranges, $id);
cfs_write_file($FILENAME, $ranges);
+ return 1;
},
);
if (my $err = $@) {
- my $dc_conf = cfs_read_file('datacenter.cfg');
-
my $emsg = "unable to record guest ID $id as used";
- die "$emsg - $err" if $dc_conf->{'next-id'}->{unique};
+ die "$emsg - $err" if $next_id_opts->{unique};
warn "$emsg - $err";
+
+ return;
}
+
+ die "guest ID $id was used before\n"
+ if $enforce && $next_id_opts->{unique} && !$recorded;
}
cfs_register_file($FILENAME, \&parse_id_list, \&write_id_list);
--
2.47.3
next prev parent reply other threads:[~2026-10-12 15:46 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-12 15:45 [PATCH many v8 00/22] add option to prevent suggesting previously used VMIDs Michael Köppl
2026-10-12 15:45 ` [PATCH cluster v8 01/22] cluster files: add virtual-guest/used-guest-ids Michael Köppl
2026-10-12 15:45 ` [PATCH cluster v8 02/22] datacenter config: add unique subproperty to next-id Michael Köppl
2026-10-12 15:45 ` [PATCH cluster v8 03/22] datacenter config: next-id: add enforce subproperty Michael Köppl
2026-10-12 15:45 ` [PATCH guest-common v8 04/22] src/makefile: order files alphabetically Michael Köppl
2026-10-12 15:45 ` [PATCH guest-common v8 05/22] add module to track previously used guest IDs Michael Köppl
2026-10-12 15:45 ` [PATCH guest-common v8 06/22] tests: add tests for used guest ID tracking Michael Köppl
2026-10-12 15:45 ` [PATCH guest-common v8 07/22] abstract config: register used guest ID when creating config Michael Köppl
2026-10-12 15:45 ` [PATCH guest-common v8 08/22] guest id: keep used ID list below the pmxcfs file size limit Michael Köppl
2026-10-12 15:45 ` [PATCH guest-common v8 09/22] tests: add tests for used-guest-ids max file size handling Michael Köppl
2026-10-12 15:45 ` Michael Köppl [this message]
2026-10-12 15:45 ` [PATCH guest-common v8 11/22] guest id: add helper to check guest IDs against next-id enforcement Michael Köppl
2026-10-12 15:45 ` [PATCH qemu-server v8 12/22] api: record VM ID as used on destruction and remote migration Michael Köppl
2026-10-12 15:45 ` [PATCH container v8 13/22] api: record CT " Michael Köppl
2026-10-12 15:45 ` [PATCH manager v8 14/22] ui: guest ID selector: rename exists flag to rejected Michael Köppl
2026-10-12 15:45 ` [PATCH manager v8 15/22] ui: guest ID selector: show the error returned by nextid Michael Köppl
2026-10-12 15:45 ` [PATCH manager v8 16/22] fix #4369: api: optionally only suggest unique IDs Michael Köppl
2026-10-12 15:45 ` [PATCH manager v8 17/22] ui: dc options: rename VMID to guest ID Michael Köppl
2026-10-12 15:45 ` [PATCH manager v8 18/22] fix #4369: ui: dc options: add option for unique VM/CT IDs Michael Köppl
2026-10-12 15:45 ` [PATCH manager v8 19/22] api: nextid: reject IDs forbidden by next-id enforcement Michael Köppl
2026-10-12 15:45 ` [PATCH manager v8 20/22] ui: dc options: add option to enforce next free guest ID settings Michael Köppl
2026-10-12 15:45 ` [PATCH docs v8 21/22] pmxcfs: files: add virtual-guest/used-guest-ids Michael Köppl
2026-10-12 15:46 ` [PATCH docs v8 22/22] pvecm: next-id: document unique and enforce options Michael Köppl
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261012154600.877830-11-m.koeppl@proxmox.com \
--to=m.koeppl@proxmox.com \
--cc=pve-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox