all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Fiona Ebner <f.ebner@proxmox.com>
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	[thread overview]
Message-ID: <20261127134637.140854-8-f.ebner@proxmox.com> (raw)
In-Reply-To: <20261127134637.140854-1-f.ebner@proxmox.com>

No functional change intended.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---
 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





  parent reply	other threads:[~2026-11-27 13:48 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27 13:45 [PATCH-SERIES qemu-server 00/10] restore: check permissions up-front Fiona Ebner
2026-11-27 13:45 ` [PATCH qemu-server 01/10] restore: vma: rename variable for storage configuration Fiona Ebner
2026-11-27 13:45 ` [PATCH qemu-server 02/10] restore: skip non-main config sections Fiona Ebner
2026-11-27 13:45 ` [PATCH qemu-server 03/10] restore: introduce dedicated module Fiona Ebner
2026-11-27 13:45 ` [PATCH qemu-server 04/10] restore: move helper for cleaning up target to module Fiona Ebner
2026-11-27 13:45 ` [PATCH qemu-server 05/10] restore: move helper for parsing backup device hints " Fiona Ebner
2026-11-27 13:45 ` [PATCH qemu-server 06/10] restore: move helpers to deactivate and destroy volumes " Fiona Ebner
2026-11-27 13:45 ` Fiona Ebner [this message]
2026-11-27 13:45 ` [PATCH qemu-server 08/10] restore: check permissions up-front Fiona Ebner
2026-11-27 13:45 ` [PATCH qemu-server 09/10] restore: extract common helper for preparing new configuration Fiona Ebner
2026-11-27 13:45 ` [PATCH qemu-server 10/10] restore: prepare new config before modifying the target Fiona Ebner

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=20261127134637.140854-8-f.ebner@proxmox.com \
    --to=f.ebner@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal