From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id AA5391FF09C for ; Mon, 21 Sep 2026 17:54:41 +0200 (CEST) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id ECFC4215EF; Mon, 21 Sep 2026 17:54:25 +0200 (CEST) From: =?UTF-8?q?Michael=20K=C3=B6ppl?= To: pve-devel@lists.proxmox.com Subject: [PATCH qemu-server v5 4/7] api: record VM ID as used on creation, cloning, and destruction Date: Mon, 21 Sep 2026 17:54:07 +0200 Message-ID: <20260921155410.938337-5-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: 1790006059382 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.568 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: UZG4TMHHL5WKLHKAMHAZMDARE2DAVFEW X-Message-ID-Hash: UZG4TMHHL5WKLHKAMHAZMDARE2DAVFEW 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 VM 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 VM 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/Qemu.pm | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/PVE/API2/Qemu.pm b/src/PVE/API2/Qemu.pm index 71247eec..c2571f0c 100644 --- a/src/PVE/API2/Qemu.pm +++ b/src/PVE/API2/Qemu.pm @@ -68,6 +68,7 @@ use PVE::ReplicationState; use PVE::StorageTunnel; use PVE::RESTEnvironment qw(log_warn); use PVE::Ticket; +use PVE::UsedGuestIDs; BEGIN { if (!$ENV{PVE_GENERATING_DOCS}) { @@ -1371,6 +1372,10 @@ __PACKAGE__->register_method({ my $emsg = $is_restore ? "unable to restore VM $vmid -" : "unable to create VM $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::QemuConfig->create_and_lock_config($vmid, $force) }; die "$emsg $@" if $@; @@ -2847,6 +2852,10 @@ __PACKAGE__->register_method({ $early_checks->(); + # record before destroying anything, so a failure here leaves the VM intact + eval { PVE::UsedGuestIDs::add_id($vmid) }; + die "unable to destroy VM $vmid - could not record VMID as used - $@" if $@; + my $realcmd = sub { my $upid = shift; @@ -4610,6 +4619,10 @@ __PACKAGE__->register_method({ $newconf->{description} = $param->{description}; } + # record before the config exists, so a failure here leaves nothing behind + eval { PVE::UsedGuestIDs::add_id($newid) }; + die "unable to clone VM $vmid - could not record VMID $newid as used - $@" if $@; + # create empty/temp config - this fails if VM already exists on other node # FIXME use PVE::QemuConfig->create_and_lock_config and adapt code PVE::Tools::file_set_contents($conffile, "# qmclone temporary file\nlock: clone\n"); @@ -6708,6 +6721,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 VM $vmid - could not record VMID as used - $@" if $@; + my $lock = 'create'; eval { PVE::QemuConfig->create_and_lock_config($vmid, 0, $lock); }; -- 2.47.3