public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [PATCH-SERIES qemu-server 00/10] restore: check permissions up-front
@ 2026-11-27 13:45 Fiona Ebner
  2026-11-27 13:45 ` [PATCH qemu-server 01/10] restore: vma: rename variable for storage configuration Fiona Ebner
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: Fiona Ebner @ 2026-11-27 13:45 UTC (permalink / raw)
  To: pve-devel

Patch 01/10 is a code cleanup.
Patch 02/10 is a small improvement, skipping all non-main config
sections.
Patches 03/10 to 07/10 create the restore module and move functions
to it.
Patches 08/10 to 10/10 move the permission checks up to the beginning
of the restore.

There is already an early permission check with the extracted
configuration, but that is marked as "best-effort" and can race with
something modifies the configuration in the meantime.

Check for the configuration actually used by restore if the user has
the necessary permissions before destroying the old configuration and
restoring all drives which might take a long time.

I had forgotten that we also have the early check, so this is less
relevant than I initially thought, but I still think it's nice to
have. Improves modularity and shaves off 230 lines of QemuServer.pm.

Allows extracting a common helper for preparing the new configuration
to avoid some duplication. It cannot be moved into the new Restore
module, since it depends on parse_vm_config().

qemu-server:

Fiona Ebner (10):
  restore: vma: rename variable for storage configuration
  restore: skip non-main config sections
  restore: introduce dedicated module
  restore: move helper for cleaning up target to module
  restore: move helper for parsing backup device hints to module
  restore: move helpers to deactivate and destroy volumes to module
  restore: move helpers to check identity and write config to module
  restore: check permissions up-front
  restore: extract common helper for preparing new configuration
  restore: prepare new config before modifying the target

 src/PVE/API2/Qemu.pm                      |   5 +-
 src/PVE/QemuServer.pm                     | 442 ++++++----------------
 src/PVE/QemuServer/Makefile               |   1 +
 src/PVE/QemuServer/Restore.pm             | 247 ++++++++++++
 src/test/restore-config-expected/160.conf |   6 +-
 src/test/restore-config-input/160.conf    |   2 +-
 src/test/run_qemu_restore_config_tests.pl |  70 +++-
 7 files changed, 415 insertions(+), 358 deletions(-)
 create mode 100644 src/PVE/QemuServer/Restore.pm


Summary over all repositories:
  7 files changed, 415 insertions(+), 358 deletions(-)

-- 
Generated by git-murpp 0.5.0




^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH qemu-server 01/10] restore: vma: rename variable for storage configuration
  2026-11-27 13:45 [PATCH-SERIES qemu-server 00/10] restore: check permissions up-front Fiona Ebner
@ 2026-11-27 13:45 ` Fiona Ebner
  2026-11-27 13:45 ` [PATCH qemu-server 02/10] restore: skip non-main config sections Fiona Ebner
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Fiona Ebner @ 2026-11-27 13:45 UTC (permalink / raw)
  To: pve-devel

The name $cfg is too generic, use $storecfg like for the restore
implementations for the other archive types.

No functional change intended.

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

diff --git a/src/PVE/QemuServer.pm b/src/PVE/QemuServer.pm
index 1d665877..a26faa3d 100644
--- a/src/PVE/QemuServer.pm
+++ b/src/PVE/QemuServer.pm
@@ -7580,7 +7580,7 @@ sub restore_vma_archive {
 
     my $readfrom = $archive;
 
-    my $cfg = PVE::Storage::config();
+    my $storecfg = PVE::Storage::config();
     my $commands = [];
     my $bwlimit = $opts->{bwlimit};
 
@@ -7599,7 +7599,7 @@ sub restore_vma_archive {
     } else {
         # If we use a backup from a PVE defined storage we also consider that
         # storage's rate limit:
-        my (undef, $volid) = PVE::Storage::path_to_volume_id($cfg, $archive);
+        my (undef, $volid) = PVE::Storage::path_to_volume_id($storecfg, $archive);
         if (defined($volid)) {
             my ($sid, undef) = PVE::Storage::parse_volume_id($volid);
             my $readlimit = PVE::Storage::get_bandwidth_limit('restore', [$sid], $bwlimit);
@@ -7665,7 +7665,7 @@ sub restore_vma_archive {
         my $fwcfgfn = "$tmpdir/qemu-server.fw";
         restore_firewall_config($vmid, $fwcfgfn) if -f $fwcfgfn;
 
-        $virtdev_hash = $parse_backup_hints->($rpcenv, $user, $cfg, $fh, $devinfo, $opts);
+        $virtdev_hash = $parse_backup_hints->($rpcenv, $user, $storecfg, $fh, $devinfo, $opts);
 
         foreach my $info (values %{$virtdev_hash}) {
             my $storeid = $info->{storeid};
@@ -7683,10 +7683,10 @@ sub restore_vma_archive {
 
         # create empty/temp config
         restore_write_config($vmid, { memory => 128, lock => 'create' }, $opts);
-        $restore_cleanup_oldconf->($cfg, $vmid, $oldconf, $virtdev_hash);
+        $restore_cleanup_oldconf->($storecfg, $vmid, $oldconf, $virtdev_hash);
 
         # allocate volumes
-        my $map = $restore_allocate_devices->($cfg, $virtdev_hash, $vmid);
+        my $map = $restore_allocate_devices->($storecfg, $virtdev_hash, $vmid);
 
         # print restore information to $fifofh
         foreach my $virtdev (sort keys %$virtdev_hash) {
@@ -7702,12 +7702,12 @@ sub restore_vma_archive {
             }
 
             my $write_zeros = 1;
-            if (PVE::Storage::volume_has_feature($cfg, 'sparseinit', $volid)) {
+            if (PVE::Storage::volume_has_feature($storecfg, 'sparseinit', $volid)) {
                 $write_zeros = 0;
             }
 
-            my $path = PVE::Storage::path($cfg, $volid);
-            my $scfg = PVE::Storage::storage_config($cfg, $storeid);
+            my $path = PVE::Storage::path($storecfg, $volid);
+            my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
 
             # TODO: extending this needs a separate rationale, the amplification
             # avoided here is specific to the volblock layout
@@ -7785,7 +7785,7 @@ sub restore_vma_archive {
 
     alarm($oldtimeout) if $oldtimeout;
 
-    $restore_deactivate_volumes->($cfg, $virtdev_hash);
+    $restore_deactivate_volumes->($storecfg, $virtdev_hash);
 
     close($fifofh) if $fifofh;
     unlink $mapfifo;
@@ -7794,7 +7794,7 @@ sub restore_vma_archive {
     if ($err) {
         # what could not be removed keeps the VMID taken
         $opts->{progress}->{'volumes-left'} = 1
-            if $restore_destroy_volumes->($cfg, $virtdev_hash);
+            if $restore_destroy_volumes->($storecfg, $virtdev_hash);
         die $err;
     }
     $opts->{progress}->{'volumes-kept'} = 1;
-- 
2.47.3





^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH qemu-server 02/10] restore: skip non-main config sections
  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 ` Fiona Ebner
  2026-11-27 13:45 ` [PATCH qemu-server 03/10] restore: introduce dedicated module Fiona Ebner
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Fiona Ebner @ 2026-11-27 13:45 UTC (permalink / raw)
  To: pve-devel

This extends af288d9b ("restore: drop pending changes from the config
of the backup") to all non-main sections and to parse_backup_hints().
A vzdump backup is supposed to only include the main section and a
stray snapshot section would not match up with the state of the
restored volume, which is fresh without any snapshots.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---
 src/PVE/QemuServer.pm                     | 12 ++++++++++--
 src/test/restore-config-expected/160.conf |  6 +-----
 src/test/restore-config-input/160.conf    |  2 +-
 3 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/src/PVE/QemuServer.pm b/src/PVE/QemuServer.pm
index a26faa3d..9af86902 100644
--- a/src/PVE/QemuServer.pm
+++ b/src/PVE/QemuServer.pm
@@ -6562,6 +6562,7 @@ my $parse_backup_hints = sub {
     };
 
     my $virtdev_hash = {};
+    my $main_section_finished;
     while (defined(my $line = <$fh>)) {
         if ($line =~ m/^\#qmdump\#map:(\S+):(\S+):(\S*):(\S*):$/) {
             my ($virtdev, $devname, $storeid, $format) = ($1, $2, $3, $4);
@@ -6583,6 +6584,11 @@ my $parse_backup_hints = sub {
             $check_storage->($storeid, $scfg); # permission and content type check
 
             $virtdev_hash->{$virtdev} = $devinfo->{$devname};
+        } elsif ($main_section_finished) {
+            next;
+        } elsif ($line =~ m/^\[([^\]]*)\]\s*$/) {
+            warn "detected backup configuration with unexpected $1 section - skipping it\n";
+            $main_section_finished = 1;
         } elsif ($line =~ m/^((?:ide|sata|scsi)\d+):\s*(.*)\s*$/) {
             my $virtdev = $1;
             my $drive = parse_drive($virtdev, $2);
@@ -6656,10 +6662,12 @@ sub restore_update_config_line {
 
     # vzdump never archives pending changes, and a pending change of a crafted backup would neither
     # be checked against the privileges of the user nor be visible before the next start applies it
+    # vzdump also never archives special sections or snapshots, so skip them as well.
     if ($line =~ m/^\[([^\]]*)\]\s*$/) {
-        $cookie->{'in-pending-section'} = $1 =~ m/^PENDING$/i;
+        warn "detected backup configuration with unexpected $1 section - skipping it\n";
+        $cookie->{'finished-main-section'} = 1;
     }
-    return '' if $cookie->{'in-pending-section'};
+    return '' if $cookie->{'finished-main-section'};
 
     return '' if $line =~ m/^\#qmdump\#/;
     return '' if $line =~ m/^\#vzdump\#/;
diff --git a/src/test/restore-config-expected/160.conf b/src/test/restore-config-expected/160.conf
index 148f9a76..c334c89e 100644
--- a/src/test/restore-config-expected/160.conf
+++ b/src/test/restore-config-expected/160.conf
@@ -1,11 +1,7 @@
-# pending changes of a crafted backup are dropped, other sections stay
+# non-main sections of a crafted backup are dropped
 boot: order=scsi0
 cores: 1
 memory: 512
 name: crafted
 scsi0: target:160/vm-160-disk-0.qcow2,size=4G
 
-[snap]
-cores: 2
-memory: 512
-snaptime: 1700000000
diff --git a/src/test/restore-config-input/160.conf b/src/test/restore-config-input/160.conf
index b3f41450..3c0aa141 100644
--- a/src/test/restore-config-input/160.conf
+++ b/src/test/restore-config-input/160.conf
@@ -1,4 +1,4 @@
-# pending changes of a crafted backup are dropped, other sections stay
+# non-main sections of a crafted backup are dropped
 boot: order=scsi0
 cores: 1
 memory: 512
-- 
2.47.3





^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH qemu-server 03/10] restore: introduce dedicated module
  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 ` Fiona Ebner
  2026-11-27 13:45 ` [PATCH qemu-server 04/10] restore: move helper for cleaning up target to module Fiona Ebner
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Fiona Ebner @ 2026-11-27 13:45 UTC (permalink / raw)
  To: pve-devel

Initially, with the helper for allocating and activating the volumes
required for restore.

No functional change intended.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---
 src/PVE/QemuServer.pm         | 50 +++--------------------------------
 src/PVE/QemuServer/Makefile   |  1 +
 src/PVE/QemuServer/Restore.pm | 48 +++++++++++++++++++++++++++++++++
 3 files changed, 53 insertions(+), 46 deletions(-)
 create mode 100644 src/PVE/QemuServer/Restore.pm

diff --git a/src/PVE/QemuServer.pm b/src/PVE/QemuServer.pm
index 9af86902..87869e9d 100644
--- a/src/PVE/QemuServer.pm
+++ b/src/PVE/QemuServer.pm
@@ -97,6 +97,7 @@ use PVE::QemuServer::QemuImage;
 use PVE::QemuServer::QMPHelpers
     qw(get_running_qemu_version qemu_deviceadd qemu_devicedel qemu_objectadd qemu_objectdel);
 use PVE::QemuServer::QSD;
+use PVE::QemuServer::Restore;
 use PVE::QemuServer::RNG qw(parse_rng print_rng_device_commandline print_rng_object_commandline);
 use PVE::QemuServer::RunState;
 use PVE::QemuServer::StateFile;
@@ -6614,49 +6615,6 @@ my $parse_backup_hints = sub {
     return $virtdev_hash;
 };
 
-# Helper to allocate and activate all volumes required for a restore
-#
-# $storecfg: Storage configuration
-# $virtdev_hash: as returned by parse_backup_hints()
-#
-# Returns: { $virtdev => $volid }
-my $restore_allocate_devices = sub {
-    my ($storecfg, $virtdev_hash, $vmid) = @_;
-
-    my $map = {};
-    foreach my $virtdev (sort keys %$virtdev_hash) {
-        my $d = $virtdev_hash->{$virtdev};
-        die "got no size for '$virtdev'\n" if !defined($d->{size});
-        my $alloc_size = int(($d->{size} + 1024 - 1) / 1024);
-        my $storeid = $d->{storeid};
-        my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
-
-        # falls back to default format if requested format is not supported
-        $d->{format} = PVE::Storage::resolve_format_hint($storecfg, $storeid, $d->{format});
-
-        my $name;
-        if ($d->{is_cloudinit}) {
-            $name = "vm-$vmid-cloudinit";
-            my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
-            if ($scfg->{path}) {
-                $name .= ".$d->{format}";
-            }
-        }
-
-        my $volid =
-            PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $d->{format}, $name, $alloc_size);
-
-        print STDERR "new volume ID is '$volid'\n";
-        $d->{volid} = $volid;
-
-        PVE::Storage::activate_volumes($storecfg, [$volid]);
-
-        $map->{$virtdev} = $volid;
-    }
-
-    return $map;
-};
-
 sub restore_update_config_line {
     my ($cookie, $map, $line, $unique) = @_;
 
@@ -7086,7 +7044,7 @@ sub restore_proxmox_backup_archive {
         $restore_cleanup_oldconf->($storecfg, $vmid, $oldconf, $virtdev_hash);
 
         # allocate volumes
-        my $map = $restore_allocate_devices->($storecfg, $virtdev_hash, $vmid);
+        my $map = PVE::QemuServer::Restore::allocate_volumes($storecfg, $virtdev_hash, $vmid);
 
         foreach my $virtdev (sort keys %$virtdev_hash) {
             my $d = $virtdev_hash->{$virtdev};
@@ -7267,7 +7225,7 @@ sub restore_external_archive {
         $restore_cleanup_oldconf->($storecfg, $vmid, $oldconf, $virtdev_hash);
 
         # allocate volumes
-        my $map = $restore_allocate_devices->($storecfg, $virtdev_hash, $vmid);
+        my $map = PVE::QemuServer::Restore::allocate_volumes($storecfg, $virtdev_hash, $vmid);
 
         for my $virtdev (sort keys $virtdev_hash->%*) {
             my $d = $virtdev_hash->{$virtdev};
@@ -7694,7 +7652,7 @@ sub restore_vma_archive {
         $restore_cleanup_oldconf->($storecfg, $vmid, $oldconf, $virtdev_hash);
 
         # allocate volumes
-        my $map = $restore_allocate_devices->($storecfg, $virtdev_hash, $vmid);
+        my $map = PVE::QemuServer::Restore::allocate_volumes($storecfg, $virtdev_hash, $vmid);
 
         # print restore information to $fifofh
         foreach my $virtdev (sort keys %$virtdev_hash) {
diff --git a/src/PVE/QemuServer/Makefile b/src/PVE/QemuServer/Makefile
index 61ed1a4e..62f69f29 100644
--- a/src/PVE/QemuServer/Makefile
+++ b/src/PVE/QemuServer/Makefile
@@ -25,6 +25,7 @@ SOURCES=Agent.pm	\
 	QemuImage.pm	\
 	QMPHelpers.pm	\
 	QSD.pm		\
+	Restore.pm	\
 	RNG.pm		\
 	RunState.pm	\
 	Snapshot.pm	\
diff --git a/src/PVE/QemuServer/Restore.pm b/src/PVE/QemuServer/Restore.pm
new file mode 100644
index 00000000..134c4766
--- /dev/null
+++ b/src/PVE/QemuServer/Restore.pm
@@ -0,0 +1,48 @@
+package PVE::QemuServer::Restore;
+
+use v5.36;
+
+use PVE::Storage;
+
+# Helper to allocate and activate all volumes required for a restore
+#
+# $storecfg: Storage configuration
+# $virtdev_hash: as returned by parse_backup_hints()
+#
+# Returns: { $virtdev => $volid }
+sub allocate_volumes($storecfg, $virtdev_hash, $vmid) {
+    my $map = {};
+    for my $virtdev (sort keys $virtdev_hash->%*) {
+        my $d = $virtdev_hash->{$virtdev};
+        die "got no size for '$virtdev'\n" if !defined($d->{size});
+        my $alloc_size = int(($d->{size} + 1024 - 1) / 1024);
+        my $storeid = $d->{storeid};
+        my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
+
+        # falls back to default format if requested format is not supported
+        $d->{format} = PVE::Storage::resolve_format_hint($storecfg, $storeid, $d->{format});
+
+        my $name;
+        if ($d->{is_cloudinit}) {
+            $name = "vm-$vmid-cloudinit";
+            my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
+            if ($scfg->{path}) {
+                $name .= ".$d->{format}";
+            }
+        }
+
+        my $volid =
+            PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $d->{format}, $name, $alloc_size);
+
+        print STDERR "new volume ID is '$volid'\n";
+        $d->{volid} = $volid;
+
+        PVE::Storage::activate_volumes($storecfg, [$volid]);
+
+        $map->{$virtdev} = $volid;
+    }
+
+    return $map;
+}
+
+1;
-- 
2.47.3





^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH qemu-server 04/10] restore: move helper for cleaning up target to module
  2026-11-27 13:45 [PATCH-SERIES qemu-server 00/10] restore: check permissions up-front Fiona Ebner
                   ` (2 preceding siblings ...)
  2026-11-27 13:45 ` [PATCH qemu-server 03/10] restore: introduce dedicated module Fiona Ebner
@ 2026-11-27 13:45 ` Fiona Ebner
  2026-11-27 13:45 ` [PATCH qemu-server 05/10] restore: move helper for parsing backup device hints " Fiona Ebner
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Fiona Ebner @ 2026-11-27 13:45 UTC (permalink / raw)
  To: pve-devel

Rename it to cleanup_old_volumes() to better describe its function.

No functional change intended.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---
 src/PVE/QemuServer.pm         | 56 ++++-------------------------------
 src/PVE/QemuServer/Restore.pm | 48 ++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+), 50 deletions(-)

diff --git a/src/PVE/QemuServer.pm b/src/PVE/QemuServer.pm
index 87869e9d..da7e7d36 100644
--- a/src/PVE/QemuServer.pm
+++ b/src/PVE/QemuServer.pm
@@ -6494,53 +6494,6 @@ sub restore_file_archive {
     }
 }
 
-# helper to remove disks that will not be used after restore
-my $restore_cleanup_oldconf = sub {
-    my ($storecfg, $vmid, $oldconf, $virtdev_hash) = @_;
-
-    my $kept_disks = {};
-
-    PVE::QemuConfig->foreach_volume(
-        $oldconf,
-        sub {
-            my ($ds, $drive) = @_;
-
-            return if drive_is_cdrom($drive, 1) || drive_has_absolute_path($drive);
-
-            my $volid = $drive->{file};
-            my ($path, $owner) = PVE::Storage::path($storecfg, $volid);
-            return if !$path || !$owner || ($owner != $vmid);
-
-            # Note: only delete disk we want to restore
-            # other volumes will become unused
-            if ($virtdev_hash->{$ds}) {
-                eval { PVE::Storage::vdisk_free($storecfg, $volid); };
-                if (my $err = $@) {
-                    warn $err;
-                }
-            } else {
-                $kept_disks->{$volid} = 1;
-            }
-        },
-    );
-
-    # after the restore we have no snapshots anymore
-    for my $snapname (keys $oldconf->{snapshots}->%*) {
-        my $snap = $oldconf->{snapshots}->{$snapname};
-        if ($snap->{vmstate}) {
-            eval { PVE::Storage::vdisk_free($storecfg, $snap->{vmstate}); };
-            if (my $err = $@) {
-                warn $err;
-            }
-        }
-
-        for my $volid (keys $kept_disks->%*) {
-            eval { PVE::Storage::volume_snapshot_delete($storecfg, $volid, $snapname); };
-            warn $@ if $@;
-        }
-    }
-};
-
 # Helper to parse vzdump backup device hints
 #
 # $rpcenv: Environment, used to ckeck storage permissions
@@ -7041,7 +6994,8 @@ sub restore_proxmox_backup_archive {
         # create empty/temp config
         restore_write_config($vmid, { memory => 128, lock => 'create' }, $options);
 
-        $restore_cleanup_oldconf->($storecfg, $vmid, $oldconf, $virtdev_hash);
+        PVE::QemuServer::Restore::cleanup_old_volumes($storecfg, $vmid, $oldconf,
+            $virtdev_hash);
 
         # allocate volumes
         my $map = PVE::QemuServer::Restore::allocate_volumes($storecfg, $virtdev_hash, $vmid);
@@ -7222,7 +7176,8 @@ sub restore_external_archive {
         # create empty/temp config
         restore_write_config($vmid, { memory => 128, lock => 'create' }, $options);
 
-        $restore_cleanup_oldconf->($storecfg, $vmid, $oldconf, $virtdev_hash);
+        PVE::QemuServer::Restore::cleanup_old_volumes($storecfg, $vmid, $oldconf,
+            $virtdev_hash);
 
         # allocate volumes
         my $map = PVE::QemuServer::Restore::allocate_volumes($storecfg, $virtdev_hash, $vmid);
@@ -7649,7 +7604,8 @@ sub restore_vma_archive {
 
         # create empty/temp config
         restore_write_config($vmid, { memory => 128, lock => 'create' }, $opts);
-        $restore_cleanup_oldconf->($storecfg, $vmid, $oldconf, $virtdev_hash);
+        PVE::QemuServer::Restore::cleanup_old_volumes($storecfg, $vmid, $oldconf,
+            $virtdev_hash);
 
         # allocate volumes
         my $map = PVE::QemuServer::Restore::allocate_volumes($storecfg, $virtdev_hash, $vmid);
diff --git a/src/PVE/QemuServer/Restore.pm b/src/PVE/QemuServer/Restore.pm
index 134c4766..a98d8d3b 100644
--- a/src/PVE/QemuServer/Restore.pm
+++ b/src/PVE/QemuServer/Restore.pm
@@ -4,6 +4,9 @@ use v5.36;
 
 use PVE::Storage;
 
+use PVE::QemuConfig;
+use PVE::QemuServer::Drive qw(drive_has_absolute_path drive_is_cdrom);
+
 # Helper to allocate and activate all volumes required for a restore
 #
 # $storecfg: Storage configuration
@@ -45,4 +48,49 @@ sub allocate_volumes($storecfg, $virtdev_hash, $vmid) {
     return $map;
 }
 
+# helper to remove disks that will not be used after restore
+sub cleanup_old_volumes($storecfg, $vmid, $oldconf, $virtdev_hash) {
+    my $kept_disks = {};
+
+    PVE::QemuConfig->foreach_volume(
+        $oldconf,
+        sub {
+            my ($ds, $drive) = @_;
+
+            return if drive_is_cdrom($drive, 1) || drive_has_absolute_path($drive);
+
+            my $volid = $drive->{file};
+            my ($path, $owner) = PVE::Storage::path($storecfg, $volid);
+            return if !$path || !$owner || ($owner != $vmid);
+
+            # Note: only delete disk we want to restore
+            # other volumes will become unused
+            if ($virtdev_hash->{$ds}) {
+                eval { PVE::Storage::vdisk_free($storecfg, $volid); };
+                if (my $err = $@) {
+                    warn $err;
+                }
+            } else {
+                $kept_disks->{$volid} = 1;
+            }
+        },
+    );
+
+    # after the restore we have no snapshots anymore
+    for my $snapname (keys $oldconf->{snapshots}->%*) {
+        my $snap = $oldconf->{snapshots}->{$snapname};
+        if ($snap->{vmstate}) {
+            eval { PVE::Storage::vdisk_free($storecfg, $snap->{vmstate}); };
+            if (my $err = $@) {
+                warn $err;
+            }
+        }
+
+        for my $volid (keys $kept_disks->%*) {
+            eval { PVE::Storage::volume_snapshot_delete($storecfg, $volid, $snapname); };
+            warn $@ if $@;
+        }
+    }
+}
+
 1;
-- 
2.47.3





^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH qemu-server 05/10] restore: move helper for parsing backup device hints to module
  2026-11-27 13:45 [PATCH-SERIES qemu-server 00/10] restore: check permissions up-front Fiona Ebner
                   ` (3 preceding siblings ...)
  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 ` Fiona Ebner
  2026-11-27 13:45 ` [PATCH qemu-server 06/10] restore: move helpers to deactivate and destroy volumes " Fiona Ebner
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Fiona Ebner @ 2026-11-27 13:45 UTC (permalink / raw)
  To: pve-devel

No functional change intended.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---
 src/PVE/QemuServer.pm         | 88 ++++-------------------------------
 src/PVE/QemuServer/Restore.pm | 77 +++++++++++++++++++++++++++++-
 2 files changed, 85 insertions(+), 80 deletions(-)

diff --git a/src/PVE/QemuServer.pm b/src/PVE/QemuServer.pm
index da7e7d36..3194c677 100644
--- a/src/PVE/QemuServer.pm
+++ b/src/PVE/QemuServer.pm
@@ -6494,80 +6494,6 @@ sub restore_file_archive {
     }
 }
 
-# Helper to parse vzdump backup device hints
-#
-# $rpcenv: Environment, used to ckeck storage permissions
-# $user: User ID, to check storage permissions
-# $storecfg: Storage configuration
-# $fh: the file handle for reading the configuration
-# $devinfo: should contain device sizes for all backu-up'ed devices
-# $options: backup options (pool, default storage)
-#
-# Return: $virtdev_hash, updates $devinfo (add devname, virtdev, format, storeid)
-my $parse_backup_hints = sub {
-    my ($rpcenv, $user, $storecfg, $fh, $devinfo, $options) = @_;
-
-    my $check_storage = sub { # assert if an image can be allocate
-        my ($storeid, $scfg) = @_;
-        die "Content type 'images' is not available on storage '$storeid'\n"
-            if !PVE::Storage::Common::content_allows($scfg->{content}, 'vm-vol');
-        $rpcenv->check($user, "/storage/$storeid", ['Datastore.AllocateSpace'])
-            if $user ne 'root@pam';
-    };
-
-    my $virtdev_hash = {};
-    my $main_section_finished;
-    while (defined(my $line = <$fh>)) {
-        if ($line =~ m/^\#qmdump\#map:(\S+):(\S+):(\S*):(\S*):$/) {
-            my ($virtdev, $devname, $storeid, $format) = ($1, $2, $3, $4);
-            die "archive does not contain data for drive '$virtdev'\n"
-                if !$devinfo->{$devname};
-
-            if (defined($options->{storage})) {
-                $storeid = $options->{storage} || 'local';
-            } elsif (!$storeid) {
-                $storeid = 'local';
-            }
-            $format = 'raw' if !$format;
-            $devinfo->{$devname}->{devname} = $devname;
-            $devinfo->{$devname}->{virtdev} = $virtdev;
-            $devinfo->{$devname}->{format} = $format;
-            $devinfo->{$devname}->{storeid} = $storeid;
-
-            my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
-            $check_storage->($storeid, $scfg); # permission and content type check
-
-            $virtdev_hash->{$virtdev} = $devinfo->{$devname};
-        } elsif ($main_section_finished) {
-            next;
-        } elsif ($line =~ m/^\[([^\]]*)\]\s*$/) {
-            warn "detected backup configuration with unexpected $1 section - skipping it\n";
-            $main_section_finished = 1;
-        } elsif ($line =~ m/^((?:ide|sata|scsi)\d+):\s*(.*)\s*$/) {
-            my $virtdev = $1;
-            my $drive = parse_drive($virtdev, $2);
-
-            if (drive_is_cloudinit($drive)) {
-                my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file});
-                $storeid = $options->{storage} if defined($options->{storage});
-                my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
-                my $format = eval { checked_volume_format($storecfg, $drive->{file}) } // 'raw';
-
-                $check_storage->($storeid, $scfg); # permission and content type check
-
-                $virtdev_hash->{$virtdev} = {
-                    format => $format,
-                    storeid => $storeid,
-                    size => PVE::QemuServer::Cloudinit::CLOUDINIT_DISK_SIZE,
-                    is_cloudinit => 1,
-                };
-            }
-        }
-    }
-
-    return $virtdev_hash;
-};
-
 sub restore_update_config_line {
     my ($cookie, $map, $line, $unique) = @_;
 
@@ -6986,8 +6912,9 @@ sub restore_proxmox_backup_archive {
         my $fh = IO::File->new($cfgfn, "r")
             || die "unable to read qemu-server.conf - $!\n";
 
-        $virtdev_hash =
-            $parse_backup_hints->($rpcenv, $user, $storecfg, $fh, $devinfo, $options);
+        $virtdev_hash = PVE::QemuServer::Restore::parse_backup_hints(
+            $rpcenv, $user, $storecfg, $fh, $devinfo, $options,
+        );
 
         # fixme: rate limit?
 
@@ -7170,8 +7097,9 @@ sub restore_external_archive {
 
         my $fh = IO::File->new($cfgfn, "r") or die "unable to read qemu-server.conf - $!\n";
 
-        $virtdev_hash =
-            $parse_backup_hints->($rpcenv, $user, $storecfg, $fh, $devinfo, $options);
+        $virtdev_hash = PVE::QemuServer::Restore::parse_backup_hints(
+            $rpcenv, $user, $storecfg, $fh, $devinfo, $options,
+        );
 
         # create empty/temp config
         restore_write_config($vmid, { memory => 128, lock => 'create' }, $options);
@@ -7586,7 +7514,9 @@ sub restore_vma_archive {
         my $fwcfgfn = "$tmpdir/qemu-server.fw";
         restore_firewall_config($vmid, $fwcfgfn) if -f $fwcfgfn;
 
-        $virtdev_hash = $parse_backup_hints->($rpcenv, $user, $storecfg, $fh, $devinfo, $opts);
+        $virtdev_hash = PVE::QemuServer::Restore::parse_backup_hints(
+            $rpcenv, $user, $storecfg, $fh, $devinfo, $opts,
+        );
 
         foreach my $info (values %{$virtdev_hash}) {
             my $storeid = $info->{storeid};
diff --git a/src/PVE/QemuServer/Restore.pm b/src/PVE/QemuServer/Restore.pm
index a98d8d3b..80a7ddcd 100644
--- a/src/PVE/QemuServer/Restore.pm
+++ b/src/PVE/QemuServer/Restore.pm
@@ -3,9 +3,12 @@ package PVE::QemuServer::Restore;
 use v5.36;
 
 use PVE::Storage;
+use PVE::Storage::Common;
 
 use PVE::QemuConfig;
-use PVE::QemuServer::Drive qw(drive_has_absolute_path drive_is_cdrom);
+use PVE::QemuServer::Cloudinit;
+use PVE::QemuServer::Drive
+    qw(checked_volume_format drive_has_absolute_path drive_is_cdrom drive_is_cloudinit parse_drive);
 
 # Helper to allocate and activate all volumes required for a restore
 #
@@ -93,4 +96,76 @@ sub cleanup_old_volumes($storecfg, $vmid, $oldconf, $virtdev_hash) {
     }
 }
 
+# Helper to parse vzdump backup device hints
+#
+# $rpcenv: Environment, used to ckeck storage permissions
+# $user: User ID, to check storage permissions
+# $storecfg: Storage configuration
+# $fh: the file handle for reading the configuration
+# $devinfo: should contain device sizes for all backu-up'ed devices
+# $options: backup options (pool, default storage)
+#
+# Return: $virtdev_hash, updates $devinfo (add devname, virtdev, format, storeid)
+sub parse_backup_hints($rpcenv, $user, $storecfg, $fh, $devinfo, $options) {
+    my $check_storage = sub { # assert if an image can be allocate
+        my ($storeid, $scfg) = @_;
+        die "Content type 'images' is not available on storage '$storeid'\n"
+            if !PVE::Storage::Common::content_allows($scfg->{content}, 'vm-vol');
+        $rpcenv->check($user, "/storage/$storeid", ['Datastore.AllocateSpace'])
+            if $user ne 'root@pam';
+    };
+
+    my $virtdev_hash = {};
+    my $main_section_finished;
+    while (defined(my $line = <$fh>)) {
+        if ($line =~ m/^\#qmdump\#map:(\S+):(\S+):(\S*):(\S*):$/) {
+            my ($virtdev, $devname, $storeid, $format) = ($1, $2, $3, $4);
+            die "archive does not contain data for drive '$virtdev'\n"
+                if !$devinfo->{$devname};
+
+            if (defined($options->{storage})) {
+                $storeid = $options->{storage} || 'local';
+            } elsif (!$storeid) {
+                $storeid = 'local';
+            }
+            $format = 'raw' if !$format;
+            $devinfo->{$devname}->{devname} = $devname;
+            $devinfo->{$devname}->{virtdev} = $virtdev;
+            $devinfo->{$devname}->{format} = $format;
+            $devinfo->{$devname}->{storeid} = $storeid;
+
+            my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
+            $check_storage->($storeid, $scfg); # permission and content type check
+
+            $virtdev_hash->{$virtdev} = $devinfo->{$devname};
+        } elsif ($main_section_finished) {
+            next;
+        } elsif ($line =~ m/^\[([^\]]*)\]\s*$/) {
+            warn "detected backup configuration with unexpected $1 section - skipping it\n";
+            $main_section_finished = 1;
+        } elsif ($line =~ m/^((?:ide|sata|scsi)\d+):\s*(.*)\s*$/) {
+            my $virtdev = $1;
+            my $drive = parse_drive($virtdev, $2);
+
+            if (drive_is_cloudinit($drive)) {
+                my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file});
+                $storeid = $options->{storage} if defined($options->{storage});
+                my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
+                my $format = eval { checked_volume_format($storecfg, $drive->{file}) } // 'raw';
+
+                $check_storage->($storeid, $scfg); # permission and content type check
+
+                $virtdev_hash->{$virtdev} = {
+                    format => $format,
+                    storeid => $storeid,
+                    size => PVE::QemuServer::Cloudinit::CLOUDINIT_DISK_SIZE,
+                    is_cloudinit => 1,
+                };
+            }
+        }
+    }
+
+    return $virtdev_hash;
+}
+
 1;
-- 
2.47.3





^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH qemu-server 06/10] restore: move helpers to deactivate and destroy volumes to module
  2026-11-27 13:45 [PATCH-SERIES qemu-server 00/10] restore: check permissions up-front Fiona Ebner
                   ` (4 preceding siblings ...)
  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 ` Fiona Ebner
  2026-11-27 13:45 ` [PATCH qemu-server 07/10] restore: move helpers to check identity and write config " Fiona Ebner
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Fiona Ebner @ 2026-11-27 13:45 UTC (permalink / raw)
  To: pve-devel

No functional change intended.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---
 src/PVE/QemuServer.pm         | 44 +++++------------------------------
 src/PVE/QemuServer/Restore.pm | 28 ++++++++++++++++++++++
 2 files changed, 34 insertions(+), 38 deletions(-)

diff --git a/src/PVE/QemuServer.pm b/src/PVE/QemuServer.pm
index 3194c677..8415bb62 100644
--- a/src/PVE/QemuServer.pm
+++ b/src/PVE/QemuServer.pm
@@ -6575,38 +6575,6 @@ sub restore_update_config_line {
     return $res;
 }
 
-my $restore_deactivate_volumes = sub {
-    my ($storecfg, $virtdev_hash) = @_;
-
-    my $vollist = [];
-    for my $dev (values $virtdev_hash->%*) {
-        push $vollist->@*, $dev->{volid} if $dev->{volid};
-    }
-
-    eval { PVE::Storage::deactivate_volumes($storecfg, $vollist); };
-    print STDERR $@ if $@;
-};
-
-# Returns the number of volumes that could not be removed.
-my $restore_destroy_volumes = sub {
-    my ($storecfg, $virtdev_hash) = @_;
-
-    my $left = 0;
-    for my $dev (values $virtdev_hash->%*) {
-        my $volid = $dev->{volid} or next;
-        eval {
-            PVE::Storage::vdisk_free($storecfg, $volid);
-            print STDERR "temporary volume '$volid' successfully removed\n";
-        };
-        if (my $err = $@) {
-            print STDERR "unable to cleanup '$volid' - $err";
-            $left++;
-        }
-    }
-
-    return $left;
-};
-
 sub restore_merge_config {
     my ($filename, $backup_conf_raw, $override_conf) = @_;
 
@@ -6985,7 +6953,7 @@ sub restore_proxmox_backup_archive {
     my $err = $@;
 
     if ($err || !$options->{live}) {
-        $restore_deactivate_volumes->($storecfg, $virtdev_hash);
+        PVE::QemuServer::Restore::deactivate_volumes($storecfg, $virtdev_hash);
     }
 
     rmtree $tmpdir;
@@ -6993,7 +6961,7 @@ sub restore_proxmox_backup_archive {
     if ($err) {
         # what could not be removed keeps the VMID taken
         $options->{progress}->{'volumes-left'} = 1
-            if $restore_destroy_volumes->($storecfg, $virtdev_hash);
+            if PVE::QemuServer::Restore::destroy_volumes($storecfg, $virtdev_hash);
         die $err;
     }
     $options->{progress}->{'volumes-kept'} = 1;
@@ -7166,7 +7134,7 @@ sub restore_external_archive {
     warn "backup provider cleanup after restore failed - $@" if $@;
 
     if ($err) {
-        $restore_deactivate_volumes->($storecfg, $virtdev_hash);
+        PVE::QemuServer::Restore::deactivate_volumes($storecfg, $virtdev_hash);
     }
 
     rmtree($tmpdir);
@@ -7174,7 +7142,7 @@ sub restore_external_archive {
     if ($err) {
         # what could not be removed keeps the VMID taken
         $options->{progress}->{'volumes-left'} = 1
-            if $restore_destroy_volumes->($storecfg, $virtdev_hash);
+            if PVE::QemuServer::Restore::destroy_volumes($storecfg, $virtdev_hash);
         die $err;
     }
     $options->{progress}->{'volumes-kept'} = 1;
@@ -7637,7 +7605,7 @@ sub restore_vma_archive {
 
     alarm($oldtimeout) if $oldtimeout;
 
-    $restore_deactivate_volumes->($storecfg, $virtdev_hash);
+    PVE::QemuServer::Restore::deactivate_volumes($storecfg, $virtdev_hash);
 
     close($fifofh) if $fifofh;
     unlink $mapfifo;
@@ -7646,7 +7614,7 @@ sub restore_vma_archive {
     if ($err) {
         # what could not be removed keeps the VMID taken
         $opts->{progress}->{'volumes-left'} = 1
-            if $restore_destroy_volumes->($storecfg, $virtdev_hash);
+            if PVE::QemuServer::Restore::destroy_volumes($storecfg, $virtdev_hash);
         die $err;
     }
     $opts->{progress}->{'volumes-kept'} = 1;
diff --git a/src/PVE/QemuServer/Restore.pm b/src/PVE/QemuServer/Restore.pm
index 80a7ddcd..928fb811 100644
--- a/src/PVE/QemuServer/Restore.pm
+++ b/src/PVE/QemuServer/Restore.pm
@@ -168,4 +168,32 @@ sub parse_backup_hints($rpcenv, $user, $storecfg, $fh, $devinfo, $options) {
     return $virtdev_hash;
 }
 
+sub deactivate_volumes($storecfg, $virtdev_hash) {
+    my $vollist = [];
+    for my $dev (values $virtdev_hash->%*) {
+        push $vollist->@*, $dev->{volid} if $dev->{volid};
+    }
+
+    eval { PVE::Storage::deactivate_volumes($storecfg, $vollist); };
+    print STDERR $@ if $@;
+}
+
+# Returns the number of volumes that could not be removed.
+sub destroy_volumes($storecfg, $virtdev_hash) {
+    my $left = 0;
+    for my $dev (values $virtdev_hash->%*) {
+        my $volid = $dev->{volid} or next;
+        eval {
+            PVE::Storage::vdisk_free($storecfg, $volid);
+            print STDERR "temporary volume '$volid' successfully removed\n";
+        };
+        if (my $err = $@) {
+            print STDERR "unable to cleanup '$volid' - $err";
+            $left++;
+        }
+    }
+
+    return $left;
+}
+
 1;
-- 
2.47.3





^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH qemu-server 07/10] restore: move helpers to check identity and write config to module
  2026-11-27 13:45 [PATCH-SERIES qemu-server 00/10] restore: check permissions up-front Fiona Ebner
                   ` (5 preceding siblings ...)
  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
  2026-11-27 13:45 ` [PATCH qemu-server 08/10] restore: check permissions up-front Fiona Ebner
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Fiona Ebner @ 2026-11-27 13:45 UTC (permalink / raw)
  To: pve-devel

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





^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH qemu-server 08/10] restore: check permissions up-front
  2026-11-27 13:45 [PATCH-SERIES qemu-server 00/10] restore: check permissions up-front Fiona Ebner
                   ` (6 preceding siblings ...)
  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 ` 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
  9 siblings, 0 replies; 11+ messages in thread
From: Fiona Ebner @ 2026-11-27 13:45 UTC (permalink / raw)
  To: pve-devel

Check if the user has the necessary permissions before destroying the
old configuration and restoring all drives, which might take a long
time.

Moving the checks is made possible by filling in a placeholder value
in restore_update_config_line() instead of the final volume ID and
replacing it later during volume allocation.

For tar, the permissions are not checked up-front and the map is still
needed in restore_update_config_line(), since it does not go through
allocation like the other restore types.

The tests used restore_update_config_line() for simulating failure
during 'data' restore. Switch to using Restore::allocate_volumes()
instead.

The test using hostpci0 as an unprivileged user now behaves quite
differently, since the permission check is up-front. Add a second test
failing when assigning the VM to the pool to still test that behavior.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---
 src/PVE/API2/Qemu.pm                      |   2 +-
 src/PVE/QemuServer.pm                     | 122 ++++++++++++----------
 src/PVE/QemuServer/Restore.pm             |  28 +++--
 src/test/run_qemu_restore_config_tests.pl |  70 ++++++++++---
 4 files changed, 144 insertions(+), 78 deletions(-)

diff --git a/src/PVE/API2/Qemu.pm b/src/PVE/API2/Qemu.pm
index 05ed7997..988d9c45 100644
--- a/src/PVE/API2/Qemu.pm
+++ b/src/PVE/API2/Qemu.pm
@@ -1546,7 +1546,7 @@ __PACKAGE__->register_method({
                     'allow-identity-mismatch' => $allow_identity_mismatch,
                 };
                 if (my $volid = $archive->{volid}) {
-                    # best effort, real check is after restoring!
+                    # best effort, real check is during restore!
                     my $merged = eval {
                         my $old_conf = PVE::Storage::extract_vzdump_config($storecfg, $volid);
                         PVE::QemuServer::restore_merge_config(
diff --git a/src/PVE/QemuServer.pm b/src/PVE/QemuServer.pm
index cc573cfd..baaa6ef0 100644
--- a/src/PVE/QemuServer.pm
+++ b/src/PVE/QemuServer.pm
@@ -6495,7 +6495,9 @@ sub restore_file_archive {
 }
 
 sub restore_update_config_line {
-    my ($cookie, $map, $line, $unique) = @_;
+    my ($cookie, $virtdev_hash, $line, $unique, $map) = @_;
+
+    die "internal error - need map or virtdev hash\n" if !($virtdev_hash || $map);
 
     # vzdump never archives pending changes, and a pending change of a crafted backup would neither
     # be checked against the privileges of the user nor be visible before the next start applies it
@@ -6546,9 +6548,15 @@ sub restore_update_config_line {
         my $di = parse_drive($virtdev, $value);
         if (defined($di->{backup}) && !$di->{backup}) {
             $res .= "#$line";
-        } elsif ($map->{$virtdev}) {
+        } elsif ($map->{$virtdev} || $virtdev_hash->{$virtdev}) {
             delete $di->{format}; # format can change on restore
-            $di->{file} = $map->{$virtdev};
+            if ($map->{$virtdev}) {
+                $di->{file} = $map->{$virtdev};
+            } else {
+                # The actual volume ID is set when allocating later.
+                my $storeid = $virtdev_hash->{$virtdev}->{storeid};
+                $di->{file} = "${storeid}:to-be-allocated";
+            }
             $value = print_drive($di);
             $res .= "$virtdev: $value\n";
         } else {
@@ -6777,7 +6785,7 @@ sub restore_proxmox_backup_archive {
 
     my $cfs_path = PVE::QemuConfig->cfs_config_path($vmid);
     my $oldconf = PVE::Cluster::cfs_read_file($cfs_path);
-    my $new_conf_raw = '';
+    my $new_conf;
 
     my $rpcenv = PVE::RPCEnvironment::get();
     my $devinfo = {}; # info about drives included in backup
@@ -6843,6 +6851,26 @@ sub restore_proxmox_backup_archive {
             $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";
+        }
+
+        $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
@@ -6854,7 +6882,7 @@ sub restore_proxmox_backup_archive {
             $virtdev_hash);
 
         # allocate volumes
-        my $map = PVE::QemuServer::Restore::allocate_volumes($storecfg, $virtdev_hash, $vmid);
+        PVE::QemuServer::Restore::allocate_volumes($storecfg, $virtdev_hash, $vmid, $new_conf);
 
         foreach my $virtdev (sort keys %$virtdev_hash) {
             my $d = $virtdev_hash->{$virtdev};
@@ -6899,17 +6927,6 @@ sub restore_proxmox_backup_archive {
             print "restore proxmox backup image: $dbg_cmdstring\n";
             run_command($pbs_restore_cmd);
         }
-
-        $fh->seek(0, 0) || die "seek failed - $!\n";
-
-        my $cookie = { netcount => 0 };
-        while (defined(my $line = <$fh>)) {
-            $new_conf_raw .= restore_update_config_line(
-                $cookie, $map, $line, $options->{unique},
-            );
-        }
-
-        $fh->close();
     };
     my $err = $@;
 
@@ -6927,13 +6944,6 @@ sub restore_proxmox_backup_archive {
     }
     $options->{progress}->{'volumes-kept'} = 1;
 
-    if ($options->{live}) {
-        # keep lock during live-restore
-        $new_conf_raw .= "\nlock: create";
-    }
-
-    my $new_conf = restore_merge_config($conffile, $new_conf_raw, $options->{override_conf});
-    check_restore_permissions($rpcenv, $user, $new_conf);
     PVE::QemuServer::Restore::write_config($vmid, $new_conf, $options);
 
     eval { rescan($vmid, 1); };
@@ -6986,7 +6996,7 @@ sub restore_external_archive {
 
     my $cfs_path = PVE::QemuConfig->cfs_config_path($vmid);
     my $oldconf = PVE::Cluster::cfs_read_file($cfs_path);
-    my $new_conf_raw = '';
+    my $new_conf;
 
     my $rpcenv = PVE::RPCEnvironment::get();
     my $devinfo = {}; # info about drives included in backup
@@ -7030,6 +7040,21 @@ sub restore_external_archive {
             $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();
+
+        $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,
@@ -7039,7 +7064,7 @@ sub restore_external_archive {
             $virtdev_hash);
 
         # allocate volumes
-        my $map = PVE::QemuServer::Restore::allocate_volumes($storecfg, $virtdev_hash, $vmid);
+        PVE::QemuServer::Restore::allocate_volumes($storecfg, $virtdev_hash, $vmid, $new_conf);
 
         for my $virtdev (sort keys $virtdev_hash->%*) {
             my $d = $virtdev_hash->{$virtdev};
@@ -7079,17 +7104,6 @@ sub restore_external_archive {
             }
             die $err if $err;
         }
-
-        $fh->seek(0, 0) || die "seek failed - $!\n";
-
-        my $cookie = { netcount => 0 };
-        while (defined(my $line = <$fh>)) {
-            $new_conf_raw .= restore_update_config_line(
-                $cookie, $map, $line, $options->{unique},
-            );
-        }
-
-        $fh->close();
     };
     my $err = $@;
 
@@ -7110,8 +7124,6 @@ sub restore_external_archive {
     }
     $options->{progress}->{'volumes-kept'} = 1;
 
-    my $new_conf = restore_merge_config($conffile, $new_conf_raw, $options->{override_conf});
-    check_restore_permissions($rpcenv, $user, $new_conf);
     PVE::QemuServer::Restore::write_config($vmid, $new_conf, $options);
 
     eval { rescan($vmid, 1); };
@@ -7426,7 +7438,7 @@ sub restore_vma_archive {
 
     my $cfs_path = PVE::QemuConfig->cfs_config_path($vmid);
     my $oldconf = PVE::Cluster::cfs_read_file($cfs_path);
-    my $new_conf_raw = '';
+    my $new_conf;
 
     my %storage_limits;
 
@@ -7451,6 +7463,21 @@ sub restore_vma_archive {
             $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});
@@ -7473,7 +7500,7 @@ sub restore_vma_archive {
             $virtdev_hash);
 
         # allocate volumes
-        my $map = PVE::QemuServer::Restore::allocate_volumes($storecfg, $virtdev_hash, $vmid);
+        PVE::QemuServer::Restore::allocate_volumes($storecfg, $virtdev_hash, $vmid, $new_conf);
 
         # print restore information to $fifofh
         foreach my $virtdev (sort keys %$virtdev_hash) {
@@ -7507,17 +7534,6 @@ sub restore_vma_archive {
             print
                 "map '$d->{devname}' to '$path' (write zeros = ${write_zeros}${cache_info})\n";
         }
-
-        $fh->seek(0, 0) || die "seek failed - $!\n";
-
-        my $cookie = { netcount => 0 };
-        while (defined(my $line = <$fh>)) {
-            $new_conf_raw .= restore_update_config_line(
-                $cookie, $map, $line, $opts->{unique},
-            );
-        }
-
-        $fh->close();
     };
 
     my $oldtimeout;
@@ -7586,8 +7602,6 @@ sub restore_vma_archive {
     }
     $opts->{progress}->{'volumes-kept'} = 1;
 
-    my $new_conf = restore_merge_config($conffile, $new_conf_raw, $opts->{override_conf});
-    check_restore_permissions($rpcenv, $user, $new_conf);
     PVE::QemuServer::Restore::write_config($vmid, $new_conf, $opts);
 
     eval { rescan($vmid, 1); };
@@ -7685,7 +7699,7 @@ sub restore_tar_archive {
         my $cookie = { netcount => 0 };
         while (defined(my $line = <$srcfd>)) {
             $new_conf_raw .= restore_update_config_line(
-                $cookie, $map, $line, $opts->{unique},
+                $cookie, undef, $line, $opts->{unique}, $map,
             );
         }
 
diff --git a/src/PVE/QemuServer/Restore.pm b/src/PVE/QemuServer/Restore.pm
index 583f7581..a67ac121 100644
--- a/src/PVE/QemuServer/Restore.pm
+++ b/src/PVE/QemuServer/Restore.pm
@@ -8,17 +8,22 @@ use PVE::Storage::Common;
 
 use PVE::QemuConfig;
 use PVE::QemuServer::Cloudinit;
-use PVE::QemuServer::Drive
-    qw(checked_volume_format drive_has_absolute_path drive_is_cdrom drive_is_cloudinit parse_drive);
+use PVE::QemuServer::Drive qw(
+    checked_volume_format
+    drive_has_absolute_path
+    drive_is_cdrom
+    drive_is_cloudinit
+    parse_drive
+    print_drive
+);
 
 # Helper to allocate and activate all volumes required for a restore
 #
 # $storecfg: Storage configuration
 # $virtdev_hash: as returned by parse_backup_hints()
-#
-# Returns: { $virtdev => $volid }
-sub allocate_volumes($storecfg, $virtdev_hash, $vmid) {
-    my $map = {};
+# $vmid: the VM ID
+# $new_conf: the new configuration for the restored VM, for filling in volid placeholders
+sub allocate_volumes($storecfg, $virtdev_hash, $vmid, $new_conf) {
     for my $virtdev (sort keys $virtdev_hash->%*) {
         my $d = $virtdev_hash->{$virtdev};
         die "got no size for '$virtdev'\n" if !defined($d->{size});
@@ -46,10 +51,15 @@ sub allocate_volumes($storecfg, $virtdev_hash, $vmid) {
 
         PVE::Storage::activate_volumes($storecfg, [$volid]);
 
-        $map->{$virtdev} = $volid;
+        # There can be #qmdump#map lines that are not part of the config and these were restored in
+        # the past too.
+        # FIXME: MAJOR VERSION: consider allocating only drives that are referenced in the config.
+        if ($new_conf->{$virtdev}) {
+            my $drive = parse_drive($virtdev, $new_conf->{$virtdev});
+            $drive->{file} = $volid;
+            $new_conf->{$virtdev} = print_drive($drive);
+        }
     }
-
-    return $map;
 }
 
 # helper to remove disks that will not be used after restore
diff --git a/src/test/run_qemu_restore_config_tests.pl b/src/test/run_qemu_restore_config_tests.pl
index 3ab81fab..503438e2 100755
--- a/src/test/run_qemu_restore_config_tests.pl
+++ b/src/test/run_qemu_restore_config_tests.pl
@@ -67,7 +67,7 @@ dir_glob_foreach(
 
         while (defined(my $line = <$fh>)) {
             $got .= PVE::QemuServer::restore_update_config_line(
-                $cookie, $map, $line, 0,
+                $cookie, undef, $line, 0, $map,
             );
         }
 
@@ -232,7 +232,12 @@ $qemu_server_module->mock(
         return $qemu_server_module->original('restore_firewall_config')->(@_);
     },
     restore_update_config_line => sub {
-        die "restoring the data failed\n" if $fail_at eq 'data';
+        my ($cookie, $virtdev_hash, $line, $unique, $map) = @_;
+        if ($fail_at eq 'data' && defined($map)) {
+            # NOTE this only triggers for tar restore. Other types fail at
+            # PVE::QemuServer::Restore::allocate_volumes()
+            die "restoring the data failed\n";
+        }
         return $qemu_server_module->original('restore_update_config_line')->(@_);
     },
     tar_archive_read_firstfile => sub { return 'qemu-server.conf'; },
@@ -281,6 +286,15 @@ $qemu_server_module->mock(
     },
 );
 
+# TODO once there are dedicated functions for the actual data part of the restore, mock those.
+my $qemu_server_restore_module = Test::MockModule->new('PVE::QemuServer::Restore');
+$qemu_server_restore_module->mock(
+    allocate_volumes => sub {
+        die "restoring the data failed\n" if $fail_at eq 'data';
+        return $qemu_server_restore_module->original('allocate_volumes')->(@_);
+    },
+);
+
 my $task_error;
 my $rpcenv_module = Test::MockModule->new('PVE::RPCEnvironment');
 $rpcenv_module->mock(
@@ -543,11 +557,15 @@ subtest 'failed removal of a restored volume keeps the VMID' => sub {
     my $run_command = \&PVE::QemuServer::run_command;
     local *PVE::QemuServer::run_command = sub {
         my ($cmd) = @_;
-        return 0 if ref($cmd) && $cmd->[0] eq '/usr/bin/pbs-restore';
+        if (ref($cmd) && $cmd->[0] eq '/usr/bin/pbs-restore') {
+            die "restoring the data failed\n" if $fail_at eq 'pbs-restore';
+            return 0;
+        }
         return $run_command->(@_);
     };
 
-    local $fail_at = 'data';
+    local $fail_at = 'pbs-restore';
+
     my $backup = backup_config()
         . "scsi0: target:vm-100-disk-0,size=1M\n#qmdump#map:scsi0:drive-scsi0:target:raw:\n";
     my ($err, $output) = restore_api('pbs', $backup, {});
@@ -1000,27 +1018,51 @@ subtest 'retry in place after a failed live restore' => sub {
 
 subtest 'retry in place after a failure once the restored volumes stay' => sub {
     QemuServerTest::FakeCFS::reset();
-    my $backup = $BACKUP_WITH_UID . "hostpci0: 0000:01:00.0\n";
+    my $backup = $BACKUP_WITH_UID;
 
-    # the permission check needs the restored config, so it runs once the data is restored
-    $rpcenv->set_user('user@pve');
-    my ($err, $output) = restore_api('vma file', $backup, {});
-    $rpcenv->set_user('root@pam');
-    is(
-        $err,
-        "only root can set 'hostpci0' config for non-mapped devices\n",
-        'restore refused after restoring the data',
-    );
+    my $fail_assign = 1;
+
+    local *PVE::RPCEnvironment::check_pool_exist = sub { return 1; };
+    local *PVE::RPCEnvironment::check_vm_perm = sub { return 1; };
+    local *PVE::AccessControl::add_vm_to_pool = sub {
+        die "failed to assign VM to pool\n" if $fail_assign;
+        return;
+    };
+
+    my ($err, $output) = restore_api('vma file', $backup, { pool => 'somepool' });
+    is($err, "failed to assign VM to pool\n", 'restore failed after restoring the data');
     ok(defined($QemuServerTest::FakeCFS::configs->{100}), 'VM is left behind');
     is(pending_uid_of(100), $UID_BACKUP,
         'leftover VM has the uid of the backup as pending uid');
 
+    $fail_assign = 0;
+
     ($err, $output) = restore_api('vma file', $backup, { force => 1 });
     is($err, '', 'retry succeeds') or diag($output);
     is(stored_uid_of(100), $UID_BACKUP, 'retry adopts the uid of the backup');
     is(pending_uid_of(100), undef, 'retry: pending uid is removed');
 };
 
+subtest 'permission error leads to early failure' => sub {
+    QemuServerTest::FakeCFS::reset();
+    my $backup = $BACKUP_WITH_UID . "hostpci0: 0000:01:00.0\n";
+
+    $rpcenv->set_user('user@pve');
+    my ($err, $output) = restore_api('vma file', $backup, {});
+    is(
+        $err,
+        "only root can set 'hostpci0' config for non-mapped devices\n",
+        'restore refused because of missing permissions',
+    );
+    is(stored_config(100), undef, 'no config created');
+
+    $rpcenv->set_user('root@pam');
+    ($err, $output) = restore_api('vma file', $backup, {});
+    is($err, '', 'privileged user succeeds') or diag($output);
+    is(stored_uid_of(100), $UID_BACKUP, 'uid matches that of the backup');
+    is(pending_uid_of(100), undef, 'there is no pending uid');
+};
+
 my $FAILURES = {
     # right after the check, before the restore writes a config itself
     firewall => "unable to restore the firewall config\n",
-- 
2.47.3





^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH qemu-server 09/10] restore: extract common helper for preparing new configuration
  2026-11-27 13:45 [PATCH-SERIES qemu-server 00/10] restore: check permissions up-front Fiona Ebner
                   ` (7 preceding siblings ...)
  2026-11-27 13:45 ` [PATCH qemu-server 08/10] restore: check permissions up-front Fiona Ebner
@ 2026-11-27 13:45 ` Fiona Ebner
  2026-11-27 13:45 ` [PATCH qemu-server 10/10] restore: prepare new config before modifying the target Fiona Ebner
  9 siblings, 0 replies; 11+ messages in thread
From: Fiona Ebner @ 2026-11-27 13:45 UTC (permalink / raw)
  To: pve-devel

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





^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH qemu-server 10/10] restore: prepare new config before modifying the target
  2026-11-27 13:45 [PATCH-SERIES qemu-server 00/10] restore: check permissions up-front Fiona Ebner
                   ` (8 preceding siblings ...)
  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 ` Fiona Ebner
  9 siblings, 0 replies; 11+ messages in thread
From: Fiona Ebner @ 2026-11-27 13:45 UTC (permalink / raw)
  To: pve-devel

In particular, check the permissions before recording a pending uid,
before removing the replication state and before restoring the
firewall configuration.

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

diff --git a/src/PVE/QemuServer.pm b/src/PVE/QemuServer.pm
index 9bfb6906..7ee1ec17 100644
--- a/src/PVE/QemuServer.pm
+++ b/src/PVE/QemuServer.pm
@@ -6862,6 +6862,10 @@ sub restore_proxmox_backup_archive {
         $param = [$pbs_backup_name, "qemu-server.conf", $cfgfn];
         PVE::Storage::PBSPlugin::run_raw_client_cmd($scfg, $storeid, $cmd, $param);
 
+        ($new_conf, $virtdev_hash) = restore_prepare_new_config(
+            $rpcenv, $user, $vmid, $cfgfn, $storecfg, $devinfo, $options,
+        );
+
         PVE::QemuServer::Restore::check_identity(
             $vmid, PVE::Tools::file_get_contents($cfgfn), $options,
         );
@@ -6876,10 +6880,6 @@ sub restore_proxmox_backup_archive {
             restore_firewall_config($vmid, $firewall_config_fn);
         }
 
-        ($new_conf, $virtdev_hash) = restore_prepare_new_config(
-            $rpcenv, $user, $vmid, $cfgfn, $storecfg, $devinfo, $options,
-        );
-
         # fixme: rate limit?
 
         # create empty/temp config
@@ -7030,22 +7030,22 @@ sub restore_external_archive {
         my $data = $backup_provider->archive_get_guest_config($volname)
             or die "backup provider failed to extract guest configuration\n";
 
+        PVE::Tools::file_set_contents($cfgfn, $data);
+
+        ($new_conf, $virtdev_hash) = restore_prepare_new_config(
+            $rpcenv, $user, $vmid, $cfgfn, $storecfg, $devinfo, $options,
+        );
+
         PVE::QemuServer::Restore::check_identity($vmid, $data, $options);
 
         $options->{progress}->{'target-changed'} = 1;
         PVE::ReplicationState::delete_guest_states($vmid);
 
-        PVE::Tools::file_set_contents($cfgfn, $data);
-
         if ($data = $backup_provider->archive_get_firewall_config($volname)) {
             PVE::Tools::file_set_contents($firewall_config_fn, $data);
             restore_firewall_config($vmid, $firewall_config_fn);
         }
 
-        ($new_conf, $virtdev_hash) = restore_prepare_new_config(
-            $rpcenv, $user, $vmid, $cfgfn, $storecfg, $devinfo, $options,
-        );
-
         # create empty/temp config
         PVE::QemuServer::Restore::write_config(
             $vmid, { memory => 128, lock => 'create' }, $options,
@@ -7434,6 +7434,9 @@ sub restore_vma_archive {
     my $print_devmap = sub {
         my $cfgfn = "$tmpdir/qemu-server.conf";
 
+        ($new_conf, $virtdev_hash) =
+            restore_prepare_new_config($rpcenv, $user, $vmid, $cfgfn, $storecfg, $devinfo, $opts);
+
         PVE::QemuServer::Restore::check_identity(
             $vmid, PVE::Tools::file_get_contents($cfgfn), $opts,
         );
@@ -7441,10 +7444,6 @@ sub restore_vma_archive {
         $opts->{progress}->{'target-changed'} = 1;
         PVE::ReplicationState::delete_guest_states($vmid);
 
-        ($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;
 
-- 
2.47.3





^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2026-11-27 13:48 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal