public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: "Michael Köppl" <m.koeppl@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH pve-container v5 5/7] api: record CT ID as used on creation, cloning, and destruction
Date: Mon, 21 Sep 2026 17:54:08 +0200	[thread overview]
Message-ID: <20260921155410.938337-6-m.koeppl@proxmox.com> (raw)
In-Reply-To: <20260921155410.938337-1-m.koeppl@proxmox.com>

Record an ID as used when a container is created, cloned, destroyed, or
brought in by a remote migration, so that the `/cluster/nextid` endpoint
can later optionally avoid suggesting previously used IDs.

Recording is done on creation as well such that the ID stays recorded
even when a user removes the config by hand, which bypasses the destroy
API.

Each call happens before the first persistent change and a failure to
record aborts the operation. That way a CT is never created or destroyed
without its ID being recorded and a failure leaves nothing half-done
behind.

Originally-by: Daniel Krambrock <krambrock@hrz.uni-marburg.de>
Originally-by: Severen Redwood <severen.redwood@sitehost.co.nz>
Signed-off-by: Michael Köppl <m.koeppl@proxmox.com>
---
 src/PVE/API2/LXC.pm | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/src/PVE/API2/LXC.pm b/src/PVE/API2/LXC.pm
index 5f94d5a1..73c06fbd 100644
--- a/src/PVE/API2/LXC.pm
+++ b/src/PVE/API2/LXC.pm
@@ -26,6 +26,7 @@ use PVE::SafeSyslog;
 use PVE::Storage;
 use PVE::Ticket;
 use PVE::Tools qw(extract_param run_command);
+use PVE::UsedGuestIDs;
 use PVE::VZDump::Plugin;
 
 use PVE::LXC;
@@ -422,6 +423,10 @@ __PACKAGE__->register_method({
 
         my $emsg = $restore ? "unable to restore CT $vmid -" : "unable to create CT $vmid -";
 
+        # record before the config exists, so a failure here leaves nothing behind
+        eval { PVE::UsedGuestIDs::add_id($vmid) };
+        die "$emsg could not record VMID as used - $@" if $@;
+
         eval { PVE::LXC::Config->create_and_lock_config($vmid, $force) };
         die "$emsg $@" if $@;
 
@@ -882,6 +887,10 @@ __PACKAGE__->register_method({
 
         $early_checks->($conf);
 
+        # record before destroying anything, so a failure here leaves the CT intact
+        eval { PVE::UsedGuestIDs::add_id($vmid) };
+        die "unable to destroy CT $vmid - could not record VMID as used - $@" if $@;
+
         my $running_error_msg = "unable to destroy CT $vmid - container is running\n";
         die $running_error_msg if !$param->{force} && PVE::LXC::check_running($vmid); # check early
 
@@ -2038,6 +2047,10 @@ __PACKAGE__->register_method({
             );
         };
 
+        # record before the config exists, so a failure here leaves nothing behind
+        eval { PVE::UsedGuestIDs::add_id($newid) };
+        die "unable to clone CT $vmid - could not record VMID $newid as used - $@" if $@;
+
         my $src_conf = PVE::LXC::Config->set_lock($vmid, 'disk');
 
         eval { PVE::LXC::Config->create_and_lock_config($newid, 0); };
@@ -3148,6 +3161,11 @@ __PACKAGE__->register_method({
 
         PVE::Cluster::check_cfs_quorum();
 
+        # remote migration brings in a VMID this cluster has not seen,
+        # so record it here
+        eval { PVE::UsedGuestIDs::add_id($vmid) };
+        die "unable to create CT $vmid - could not record VMID as used - $@" if $@;
+
         my $socket_addr = "/run/pve/ct-$vmid.mtunnel";
 
         my $lock = 'create';
-- 
2.47.3





  parent reply	other threads:[~2026-09-21 15:54 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-21 15:54 [PATCH many v5 0/7] add option to prevent suggesting previously used VMIDs Michael Köppl
2026-09-21 15:54 ` [PATCH pve-cluster v5 1/7] cluster files: add used-guest-ids Michael Köppl
2026-09-21 15:54 ` [PATCH pve-cluster v5 2/7] datacenter config: add unique-next-id to schema Michael Köppl
2026-09-21 15:54 ` [PATCH pve-guest-common v5 3/7] add module to track previously used guest IDs Michael Köppl
2026-09-21 15:54 ` [PATCH qemu-server v5 4/7] api: record VM ID as used on creation, cloning, and destruction Michael Köppl
2026-09-21 15:54 ` Michael Köppl [this message]
2026-09-21 15:54 ` [PATCH pve-manager v5 6/7] close #4369: api: optionally only suggest unique IDs Michael Köppl
2026-09-21 15:54 ` [PATCH pve-manager v5 7/7] close #4369: ui: add datacenter option for unique VM/CT IDs 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=20260921155410.938337-6-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
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal