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 717291FF09B for ; Mon, 12 Oct 2026 17:48:07 +0200 (CEST) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 4BB40217D0; Mon, 12 Oct 2026 17:46:22 +0200 (CEST) From: =?UTF-8?q?Michael=20K=C3=B6ppl?= To: pve-devel@lists.proxmox.com Subject: [PATCH qemu-server v8 12/22] api: record VM ID as used on destruction and remote migration Date: Mon, 12 Oct 2026 17:45:50 +0200 Message-ID: <20261012154600.877830-13-m.koeppl@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261012154600.877830-1-m.koeppl@proxmox.com> References: <20261012154600.877830-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: 1791819968855 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.268 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: LTVVNJA7P2ESK27O3PRZ5RK5HVQ2XQPF X-Message-ID-Hash: LTVVNJA7P2ESK27O3PRZ5RK5HVQ2XQPF 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 the ID of a VM as used when it is destroyed, cloned or removed after a remote migration with --delete, so that the `/cluster/nextid` endpoint can later optionally avoid suggesting previously used IDs. For creation, restore and incoming remote migrations the ID is recorded by create_and_lock_config(). VM clones write the new config directly instead, so they record it explicitly. Recording on destruction as well covers guests that were created before IDs were tracked. The same holds for a remote migration with --delete, which removes the source guest without going through the destroy API. This call explicitly only warns on failure, since the guest has already moved. With 'unique' set in the next-id datacenter option, a failure to record aborts create, clone and destroy before anything is changed. Otherwise it only warns. Originally-by: Daniel Krambrock Originally-by: Severen Redwood Signed-off-by: Michael Köppl Reviewed-by: Fiona Ebner Tested-by: Fiona Ebner --- src/PVE/API2/Qemu.pm | 9 +++++++++ src/PVE/QemuMigrate.pm | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/src/PVE/API2/Qemu.pm b/src/PVE/API2/Qemu.pm index cfbc1f51..5c64dcc6 100644 --- a/src/PVE/API2/Qemu.pm +++ b/src/PVE/API2/Qemu.pm @@ -25,6 +25,7 @@ use PVE::JSONSchema qw(get_standard_option); use PVE::RESTHandler; use PVE::ReplicationConfig; use PVE::GuestHelpers qw(assert_tag_permissions); +use PVE::GuestID; use PVE::GuestImport; use PVE::QemuConfig; use PVE::QemuServer; @@ -2878,6 +2879,10 @@ __PACKAGE__->register_method({ $early_checks->(); + # record before destroying anything, so a failure here leaves the VM intact + eval { PVE::GuestID::register_used_id($vmid) }; + die "unable to destroy VM $vmid - $@" if $@; + my $realcmd = sub { my $upid = shift; @@ -4641,6 +4646,10 @@ __PACKAGE__->register_method({ $newconf->{description} = $param->{description}; } + # record before the config exists, so a failure here leaves nothing behind + eval { PVE::GuestID::register_used_id($newid) }; + die "unable to clone VM $vmid - $@" 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"); diff --git a/src/PVE/QemuMigrate.pm b/src/PVE/QemuMigrate.pm index 6605f5d8..c083a20a 100644 --- a/src/PVE/QemuMigrate.pm +++ b/src/PVE/QemuMigrate.pm @@ -13,6 +13,7 @@ use PVE::Cluster; use PVE::Format qw(render_bytes); use PVE::Firewall::Helpers; use PVE::GuestHelpers qw(safe_boolean_ne safe_string_ne); +use PVE::GuestID; use PVE::INotify; use PVE::JSONSchema; use PVE::RPCEnvironment; @@ -1905,6 +1906,9 @@ sub phase3_cleanup { } if ($self->{opts}->{remote} && $self->{opts}->{delete}) { + eval { PVE::GuestID::register_used_id($vmid) }; + warn $@ if $@; + eval { PVE::QemuServer::destroy_vm($self->{storecfg}, $vmid, 1, undef, 0) }; warn "Failed to remove source VM - $@\n" if $@; } -- 2.47.3