From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id 225CA1FF09C for ; Mon, 21 Sep 2026 17:54:57 +0200 (CEST) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id ADD032163D; Mon, 21 Sep 2026 17:54:26 +0200 (CEST) From: =?UTF-8?q?Michael=20K=C3=B6ppl?= 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 Message-ID: <20260921155410.938337-6-m.koeppl@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260921155410.938337-1-m.koeppl@proxmox.com> References: <20260921155410.938337-1-m.koeppl@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1790006059495 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.559 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: U2ANHQAUHVBLTERRD2BCSS545KIK7RSH X-Message-ID-Hash: U2ANHQAUHVBLTERRD2BCSS545KIK7RSH X-MailFrom: m.koeppl@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 Originally-by: Severen Redwood Signed-off-by: Michael Köppl --- 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