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 E2CAD1FF0B4 for ; Fri, 27 Nov 2026 14:48:05 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id E7A1921899; Fri, 27 Nov 2026 14:46:49 +0100 (CET) From: Fiona Ebner To: pve-devel@lists.proxmox.com Subject: [PATCH qemu-server 07/10] restore: move helpers to check identity and write config to module Date: Fri, 27 Nov 2026 14:45:31 +0100 Message-ID: <20261127134637.140854-8-f.ebner@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261127134637.140854-1-f.ebner@proxmox.com> References: <20261127134637.140854-1-f.ebner@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1795787200938 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.242 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: B7AML7663MRZYGZZ4V332HYZU6NU4FOG X-Message-ID-Hash: B7AML7663MRZYGZZ4V332HYZU6NU4FOG X-MailFrom: f.ebner@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: No functional change intended. Signed-off-by: Fiona Ebner --- src/PVE/API2/Qemu.pm | 3 +- src/PVE/QemuServer.pm | 73 ++++++++++------------------------- src/PVE/QemuServer/Restore.pm | 38 ++++++++++++++++++ 3 files changed, 60 insertions(+), 54 deletions(-) diff --git a/src/PVE/API2/Qemu.pm b/src/PVE/API2/Qemu.pm index 2119e69e..05ed7997 100644 --- a/src/PVE/API2/Qemu.pm +++ b/src/PVE/API2/Qemu.pm @@ -52,6 +52,7 @@ use PVE::QemuServer::Network; use PVE::QemuServer::OVMF; use PVE::QemuServer::PCI; use PVE::QemuServer::QMPHelpers; +use PVE::QemuServer::Restore; use PVE::QemuServer::RNG; use PVE::QemuServer::RunState; use PVE::QemuServer::USB; @@ -1599,7 +1600,7 @@ __PACKAGE__->register_method({ PVE::QemuServer::Network::create_ifaces_ipams_ips($restored_conf, $vmid) if $unique; - PVE::QemuServer::restore_adopt_identity($vmid, $restore_options); + PVE::QemuServer::Restore::adopt_identity($vmid, $restore_options); }; my $restore_locked = sub { diff --git a/src/PVE/QemuServer.pm b/src/PVE/QemuServer.pm index 8415bb62..cc573cfd 100644 --- a/src/PVE/QemuServer.pm +++ b/src/PVE/QemuServer.pm @@ -4845,7 +4845,7 @@ sub vmconfig_apply_pending { return if !scalar(keys %{ $conf->{pending} }); # a restore keeps its pending uid while it holds the 'create' lock, also when it starts the VM - # for a live restore, and removes it itself, see restore_check_identity + # for a live restore, and removes it itself, see PVE::QemuServer::Restore::check_identity my $restore_uid = ($conf->{lock} // '') eq 'create' ? PVE::GuestIdentity::get_pending_uid($conf) : undef; PVE::GuestIdentity::set_pending_uid($conf, undef) if defined($restore_uid); @@ -6586,49 +6586,6 @@ sub restore_merge_config { return $backup_conf; } -# Checks the guest identity of the backup against the one of the target VM and prepares adopting its -# uid, see PVE::GuestIdentity::prepare_restore. Must run before the restore resets the replication -# state, restores the firewall config or replaces data of the target VM. -# -# A uid that the restore could adopt at its end becomes the pending uid of the VM until then, so -# that a VM left behind by a failure or an abort of the restore still has it for a retry, see -# restore_write_config. That holds with 'unique' too, which only keeps the restore itself from -# adopting the uid. -my sub restore_check_identity { - my ($vmid, $backup_conf_raw, $opts) = @_; - - my $backup = PVE::GuestIdentity::parse_identity_from_raw_config($backup_conf_raw); - $opts->{'backup-uid'} = $backup ? $backup->{uid} : undef; - $opts->{'pending-uid'} = PVE::GuestIdentity::prepare_restore( - 'PVE::QemuConfig', - $vmid, - $opts->{reservation}, - $opts->{'backup-uid'}, - $opts->{'allow-identity-mismatch'}, - ); -} - -# Writes $conf, a config that the restore assembled itself, like a temporary or the restored one, -# with the pending uid of the restore, see restore_check_identity. -my sub restore_write_config { - my ($vmid, $conf, $opts) = @_; - - PVE::GuestIdentity::set_pending_uid($conf, $opts->{'pending-uid'}); - PVE::QemuConfig->write_config($vmid, $conf); -} - -# Adopts the uid of the backup for the restored VM where the identity rules allow it, see -# PVE::GuestIdentity::adopt, which also removes the pending uid. Must be the last step of a -# successful restore, with the config lock still held, so that a failure before leaves the pending -# uid behind. A failure only warns, as the restored VM keeps the identity of its reservation. -sub restore_adopt_identity { - my ($vmid, $opts) = @_; - - my $uid = $opts->{unique} ? undef : $opts->{'backup-uid'}; - - PVE::GuestIdentity::adopt_from_backup('PVE::QemuConfig', $vmid, $uid, $opts->{reservation}); -} - sub scan_volids { my ($cfg, $vmid) = @_; @@ -6865,7 +6822,9 @@ sub restore_proxmox_backup_archive { $param = [$pbs_backup_name, "qemu-server.conf", $cfgfn]; PVE::Storage::PBSPlugin::run_raw_client_cmd($scfg, $storeid, $cmd, $param); - restore_check_identity($vmid, PVE::Tools::file_get_contents($cfgfn), $options); + PVE::QemuServer::Restore::check_identity( + $vmid, PVE::Tools::file_get_contents($cfgfn), $options, + ); $options->{progress}->{'target-changed'} = 1; PVE::ReplicationState::delete_guest_states($vmid); @@ -6887,7 +6846,9 @@ sub restore_proxmox_backup_archive { # fixme: rate limit? # create empty/temp config - restore_write_config($vmid, { memory => 128, lock => 'create' }, $options); + PVE::QemuServer::Restore::write_config( + $vmid, { memory => 128, lock => 'create' }, $options, + ); PVE::QemuServer::Restore::cleanup_old_volumes($storecfg, $vmid, $oldconf, $virtdev_hash); @@ -6973,7 +6934,7 @@ sub restore_proxmox_backup_archive { my $new_conf = restore_merge_config($conffile, $new_conf_raw, $options->{override_conf}); check_restore_permissions($rpcenv, $user, $new_conf); - restore_write_config($vmid, $new_conf, $options); + PVE::QemuServer::Restore::write_config($vmid, $new_conf, $options); eval { rescan($vmid, 1); }; warn $@ if $@; @@ -7051,7 +7012,7 @@ sub restore_external_archive { my $data = $backup_provider->archive_get_guest_config($volname) or die "backup provider failed to extract guest configuration\n"; - restore_check_identity($vmid, $data, $options); + PVE::QemuServer::Restore::check_identity($vmid, $data, $options); $options->{progress}->{'target-changed'} = 1; PVE::ReplicationState::delete_guest_states($vmid); @@ -7070,7 +7031,9 @@ sub restore_external_archive { ); # create empty/temp config - restore_write_config($vmid, { memory => 128, lock => 'create' }, $options); + PVE::QemuServer::Restore::write_config( + $vmid, { memory => 128, lock => 'create' }, $options, + ); PVE::QemuServer::Restore::cleanup_old_volumes($storecfg, $vmid, $oldconf, $virtdev_hash); @@ -7149,7 +7112,7 @@ sub restore_external_archive { my $new_conf = restore_merge_config($conffile, $new_conf_raw, $options->{override_conf}); check_restore_permissions($rpcenv, $user, $new_conf); - restore_write_config($vmid, $new_conf, $options); + PVE::QemuServer::Restore::write_config($vmid, $new_conf, $options); eval { rescan($vmid, 1); }; warn $@ if $@; @@ -7470,7 +7433,9 @@ sub restore_vma_archive { my $print_devmap = sub { my $cfgfn = "$tmpdir/qemu-server.conf"; - restore_check_identity($vmid, PVE::Tools::file_get_contents($cfgfn), $opts); + PVE::QemuServer::Restore::check_identity( + $vmid, PVE::Tools::file_get_contents($cfgfn), $opts, + ); $opts->{progress}->{'target-changed'} = 1; PVE::ReplicationState::delete_guest_states($vmid); @@ -7501,7 +7466,9 @@ sub restore_vma_archive { } # create empty/temp config - restore_write_config($vmid, { memory => 128, lock => 'create' }, $opts); + PVE::QemuServer::Restore::write_config( + $vmid, { memory => 128, lock => 'create' }, $opts, + ); PVE::QemuServer::Restore::cleanup_old_volumes($storecfg, $vmid, $oldconf, $virtdev_hash); @@ -7621,7 +7588,7 @@ sub restore_vma_archive { my $new_conf = restore_merge_config($conffile, $new_conf_raw, $opts->{override_conf}); check_restore_permissions($rpcenv, $user, $new_conf); - restore_write_config($vmid, $new_conf, $opts); + PVE::QemuServer::Restore::write_config($vmid, $new_conf, $opts); eval { rescan($vmid, 1); }; warn $@ if $@; diff --git a/src/PVE/QemuServer/Restore.pm b/src/PVE/QemuServer/Restore.pm index 928fb811..583f7581 100644 --- a/src/PVE/QemuServer/Restore.pm +++ b/src/PVE/QemuServer/Restore.pm @@ -2,6 +2,7 @@ package PVE::QemuServer::Restore; use v5.36; +use PVE::GuestIdentity; use PVE::Storage; use PVE::Storage::Common; @@ -196,4 +197,41 @@ sub destroy_volumes($storecfg, $virtdev_hash) { return $left; } +# Checks the guest identity of the backup against the one of the target VM and prepares adopting its +# uid, see PVE::GuestIdentity::prepare_restore. Must run before the restore resets the replication +# state, restores the firewall config or replaces data of the target VM. +# +# A uid that the restore could adopt at its end becomes the pending uid of the VM until then, so +# that a VM left behind by a failure or an abort of the restore still has it for a retry, see +# write_config. That holds with 'unique' too, which only keeps the restore itself from adopting the +# uid. +sub check_identity($vmid, $backup_conf_raw, $opts) { + my $backup = PVE::GuestIdentity::parse_identity_from_raw_config($backup_conf_raw); + $opts->{'backup-uid'} = $backup ? $backup->{uid} : undef; + $opts->{'pending-uid'} = PVE::GuestIdentity::prepare_restore( + 'PVE::QemuConfig', + $vmid, + $opts->{reservation}, + $opts->{'backup-uid'}, + $opts->{'allow-identity-mismatch'}, + ); +} + +# Writes $conf, a config that the restore assembled itself, like a temporary or the restored one, +# with the pending uid of the restore, see check_identity. +sub write_config($vmid, $conf, $opts) { + PVE::GuestIdentity::set_pending_uid($conf, $opts->{'pending-uid'}); + PVE::QemuConfig->write_config($vmid, $conf); +} + +# Adopts the uid of the backup for the restored VM where the identity rules allow it, see +# PVE::GuestIdentity::adopt, which also removes the pending uid. Must be the last step of a +# successful restore, with the config lock still held, so that a failure before leaves the pending +# uid behind. A failure only warns, as the restored VM keeps the identity of its reservation. +sub adopt_identity($vmid, $opts) { + my $uid = $opts->{unique} ? undef : $opts->{'backup-uid'}; + + PVE::GuestIdentity::adopt_from_backup('PVE::QemuConfig', $vmid, $uid, $opts->{reservation}); +} + 1; -- 2.47.3