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 09/10] restore: extract common helper for preparing new configuration
Date: Fri, 27 Nov 2026 14:45:33 +0100	[thread overview]
Message-ID: <20261127134637.140854-10-f.ebner@proxmox.com> (raw)
In-Reply-To: <20261127134637.140854-1-f.ebner@proxmox.com>

For restore_vma_archive(), the permission check and getting the
virtdev hash is now done before restoring the firewall configuration.

Note that having $options->{live} is only possible for PBS. Rejected
for file-based archives in the create_vm API endpoint and for external
archives in restore_external_archive().

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---
 src/PVE/QemuServer.pm | 110 +++++++++++++++---------------------------
 1 file changed, 40 insertions(+), 70 deletions(-)

diff --git a/src/PVE/QemuServer.pm b/src/PVE/QemuServer.pm
index baaa6ef0..9bfb6906 100644
--- a/src/PVE/QemuServer.pm
+++ b/src/PVE/QemuServer.pm
@@ -6748,6 +6748,39 @@ sub restore_firewall_config {
     PVE::GuestHelpers::set_firewall_config_raw($vmid, PVE::Tools::file_get_contents($path));
 }
 
+my sub restore_prepare_new_config {
+    my ($rpcenv, $user, $vmid, $cfgfn, $storecfg, $devinfo, $options) = @_;
+
+    my $fh = IO::File->new($cfgfn, "r") or die "unable to read qemu-server.conf - $!\n";
+
+    my $virtdev_hash = PVE::QemuServer::Restore::parse_backup_hints(
+        $rpcenv, $user, $storecfg, $fh, $devinfo, $options,
+    );
+
+    $fh->seek(0, 0) || die "seek failed - $!\n";
+
+    my $new_conf_raw = '';
+    my $cookie = { netcount => 0 };
+    while (defined(my $line = <$fh>)) {
+        $new_conf_raw .= restore_update_config_line(
+            $cookie, $virtdev_hash, $line, $options->{unique},
+        );
+    }
+
+    $fh->close();
+
+    if ($options->{live}) {
+        # keep lock during live-restore
+        $new_conf_raw .= "\nlock: create";
+    }
+
+    my $conffile = PVE::QemuConfig->config_file($vmid);
+    my $new_conf = restore_merge_config($conffile, $new_conf_raw, $options->{override_conf});
+    check_restore_permissions($rpcenv, $user, $new_conf);
+
+    return ($new_conf, $virtdev_hash);
+}
+
 sub restore_proxmox_backup_archive {
     my ($archive, $vmid, $user, $options) = @_;
 
@@ -6778,7 +6811,6 @@ sub restore_proxmox_backup_archive {
     rmtree $tmpdir;
     mkpath $tmpdir;
 
-    my $conffile = PVE::QemuConfig->config_file($vmid);
     # disable interrupts (always do cleanups)
     local $SIG{INT} = local $SIG{TERM} = local $SIG{QUIT} = local $SIG{HUP} =
         sub { print STDERR "got interrupt - ignored\n"; };
@@ -6844,33 +6876,10 @@ sub restore_proxmox_backup_archive {
             restore_firewall_config($vmid, $firewall_config_fn);
         }
 
-        my $fh = IO::File->new($cfgfn, "r")
-            || die "unable to read qemu-server.conf - $!\n";
-
-        $virtdev_hash = PVE::QemuServer::Restore::parse_backup_hints(
-            $rpcenv, $user, $storecfg, $fh, $devinfo, $options,
+        ($new_conf, $virtdev_hash) = restore_prepare_new_config(
+            $rpcenv, $user, $vmid, $cfgfn, $storecfg, $devinfo, $options,
         );
 
-        $fh->seek(0, 0) || die "seek failed - $!\n";
-
-        my $new_conf_raw = '';
-        my $cookie = { netcount => 0 };
-        while (defined(my $line = <$fh>)) {
-            $new_conf_raw .= restore_update_config_line(
-                $cookie, $virtdev_hash, $line, $options->{unique},
-            );
-        }
-
-        $fh->close();
-
-        if ($options->{live}) {
-            # keep lock during live-restore
-            $new_conf_raw .= "\nlock: create";
-        }
-
-        $new_conf = restore_merge_config($conffile, $new_conf_raw, $options->{override_conf});
-        check_restore_permissions($rpcenv, $user, $new_conf);
-
         # fixme: rate limit?
 
         # create empty/temp config
@@ -6989,7 +6998,6 @@ sub restore_external_archive {
     rmtree($tmpdir);
     mkpath($tmpdir) or die "unable to create $tmpdir\n";
 
-    my $conffile = PVE::QemuConfig->config_file($vmid);
     # disable interrupts (always do cleanups)
     local $SIG{INT} = local $SIG{TERM} = local $SIG{QUIT} = local $SIG{HUP} =
         sub { print STDERR "got interrupt - ignored\n"; };
@@ -7034,27 +7042,10 @@ sub restore_external_archive {
             restore_firewall_config($vmid, $firewall_config_fn);
         }
 
-        my $fh = IO::File->new($cfgfn, "r") or die "unable to read qemu-server.conf - $!\n";
-
-        $virtdev_hash = PVE::QemuServer::Restore::parse_backup_hints(
-            $rpcenv, $user, $storecfg, $fh, $devinfo, $options,
+        ($new_conf, $virtdev_hash) = restore_prepare_new_config(
+            $rpcenv, $user, $vmid, $cfgfn, $storecfg, $devinfo, $options,
         );
 
-        $fh->seek(0, 0) || die "seek failed - $!\n";
-
-        my $new_conf_raw = '';
-        my $cookie = { netcount => 0 };
-        while (defined(my $line = <$fh>)) {
-            $new_conf_raw .= restore_update_config_line(
-                $cookie, $virtdev_hash, $line, $options->{unique},
-            );
-        }
-
-        $fh->close();
-
-        $new_conf = restore_merge_config($conffile, $new_conf_raw, $options->{override_conf});
-        check_restore_permissions($rpcenv, $user, $new_conf);
-
         # create empty/temp config
         PVE::QemuServer::Restore::write_config(
             $vmid, { memory => 128, lock => 'create' }, $options,
@@ -7434,8 +7425,6 @@ sub restore_vma_archive {
 
     my $rpcenv = PVE::RPCEnvironment::get();
 
-    my $conffile = PVE::QemuConfig->config_file($vmid);
-
     my $cfs_path = PVE::QemuConfig->cfs_config_path($vmid);
     my $oldconf = PVE::Cluster::cfs_read_file($cfs_path);
     my $new_conf;
@@ -7452,32 +7441,13 @@ sub restore_vma_archive {
         $opts->{progress}->{'target-changed'} = 1;
         PVE::ReplicationState::delete_guest_states($vmid);
 
-        # we can read the config - that is already extracted
-        my $fh = IO::File->new($cfgfn, "r")
-            || die "unable to read qemu-server.conf - $!\n";
+        ($new_conf, $virtdev_hash) = restore_prepare_new_config(
+            $rpcenv, $user, $vmid, $cfgfn, $storecfg, $devinfo, $opts,
+        );
 
         my $fwcfgfn = "$tmpdir/qemu-server.fw";
         restore_firewall_config($vmid, $fwcfgfn) if -f $fwcfgfn;
 
-        $virtdev_hash = PVE::QemuServer::Restore::parse_backup_hints(
-            $rpcenv, $user, $storecfg, $fh, $devinfo, $opts,
-        );
-
-        $fh->seek(0, 0) || die "seek failed - $!\n";
-
-        my $new_conf_raw = '';
-        my $cookie = { netcount => 0 };
-        while (defined(my $line = <$fh>)) {
-            $new_conf_raw .= restore_update_config_line(
-                $cookie, $virtdev_hash, $line, $opts->{unique},
-            );
-        }
-
-        $fh->close();
-
-        $new_conf = restore_merge_config($conffile, $new_conf_raw, $opts->{override_conf});
-        check_restore_permissions($rpcenv, $user, $new_conf);
-
         foreach my $info (values %{$virtdev_hash}) {
             my $storeid = $info->{storeid};
             next if defined($storage_limits{$storeid});
-- 
2.47.3





  parent reply	other threads:[~2026-11-27 13:47 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 ` [PATCH qemu-server 07/10] restore: move helpers to check identity and write config " Fiona Ebner
2026-11-27 13:45 ` [PATCH qemu-server 08/10] restore: check permissions up-front Fiona Ebner
2026-11-27 13:45 ` Fiona Ebner [this message]
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-10-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