[pve-devel] [PATCH qemu-server] vzdump: use minimal VM config for offline backup

Stefan Reiter s.reiter at proxmox.com
Mon Aug 24 11:21:31 CEST 2020


In case we backup a stopped VM, we start an instance of QEMU to run the
backup job. This instance will be killed afterwards without ever running
the actual VM, so there's no need to potentially allocate or use host
system resources for features never used.

The minimal_trim_opts array contains elements that will be cleaned from
the config before starting.

We only write back the config in case of resume, which is never set together
with the new "minimal" option.

Reported in the forum:
https://forum.proxmox.com/threads/pbs-tries-to-start-vms-during-backup-not-enough-ram.74773/

Signed-off-by: Stefan Reiter <s.reiter at proxmox.com>
---

It is weird to me that this even happens, AFAIU QEMU only allocates the guest
RAM as virtual as long as the guest doesn't write to it - but the forum post
proves that it does, and it's probably also a good idea to not assign hostpci
and usb devices for offline backups.

 PVE/QemuServer.pm        | 34 ++++++++++++++++++++++++++++++++++
 PVE/VZDump/QemuServer.pm |  1 +
 2 files changed, 35 insertions(+)

diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index a5ee8e2..73eb561 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -4778,6 +4778,37 @@ sub vm_migrate_alloc_nbd_disks {
     return $nbd;
 }
 
+my @minimal_trim_opts = (
+    qr/^hostpci\d+/,
+    qr/^serial\d+/,
+    qr/^usb\d+/,
+    qr/^net\d+/,
+    qr/^rng\d+/,
+    qr/^ivshmem/,
+    qr/^hugepages/,
+    qr/^smp/,
+    qr/^cores/,
+    qr/^sockets/,
+    qr/^vcpus/,
+    qr/^cpu/,
+    qr/^agent/,
+    qr/^numa(?:\d+)?/,
+);
+
+my sub vm_trim_conf_minimal {
+    my ($conf) = @_;
+
+    $conf->{memory} = 1;
+    $conf->{balloon} = 0;
+
+    # remove anything that does not affect backup but can claim host resources
+    foreach my $r (@minimal_trim_opts) {
+	foreach my $key (keys %{$conf}) {
+	    delete $conf->{$key} if $key =~ $r;
+	}
+    }
+}
+
 # see vm_start_nolock for parameters, additionally:
 # migrate_opts:
 #   storagemap = parsed storage map for allocating NBD disks
@@ -4823,6 +4854,7 @@ sub vm_start {
 #   timeout => in seconds
 #   paused => start VM in paused state (backup)
 #   resume => resume from hibernation
+#   minimal => only use necessary resources (backup)
 # migrate_opts:
 #   nbd => volumes for NBD exports (vm_migrate_alloc_nbd_disks)
 #   migratedfrom => source node
@@ -4851,6 +4883,8 @@ sub vm_start_nolock {
 	$conf = PVE::QemuConfig->load_config($vmid); # update/reload
     }
 
+    vm_trim_conf_minimal($conf) if $params->{minimal};
+
     PVE::QemuServer::Cloudinit::generate_cloudinitconfig($conf, $vmid);
 
     my $defaults = load_defaults();
diff --git a/PVE/VZDump/QemuServer.pm b/PVE/VZDump/QemuServer.pm
index 7297795..ea8faa3 100644
--- a/PVE/VZDump/QemuServer.pm
+++ b/PVE/VZDump/QemuServer.pm
@@ -827,6 +827,7 @@ sub enforce_vm_running_for_backup {
 	    skiplock => 1,
 	    skiptemplate => 1,
 	    paused => 1,
+	    minimal => !$self->{vm_was_running},
 	};
 	PVE::QemuServer::vm_start($self->{storecfg}, $vmid, $params);
     };
-- 
2.20.1






More information about the pve-devel mailing list