[pve-devel] [PATCH qemu-server 4/5] resume suspended vm on start

Dominik Csapak d.csapak at proxmox.com
Mon Mar 11 15:45:53 CET 2019


if a vm has the 'suspend' lock, we resume with the saved state
and remove the lock, the saved vmstate and the saved runningmachine
after the vm started

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 PVE/QemuServer.pm | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 46620de..baebb62 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -4002,6 +4002,12 @@ sub config_to_command {
     push @$cmd, '-global', join(',', @$globalFlags)
 	if scalar(@$globalFlags);
 
+    if (my $vmstate = $conf->{vmstate}) {
+	my $path = PVE::Storage::path($storecfg, $vmstate);
+	PVE::Storage::activate_volumes($storecfg, [$vmstate]);
+	push @$cmd, '-loadstate', $path;
+    }
+
     # add custom args
     if ($conf->{args}) {
 	my $aa = PVE::Tools::split_args($conf->{args});
@@ -5148,7 +5154,8 @@ sub vm_start {
 
 	die "you can't start a vm if it's a template\n" if PVE::QemuConfig->is_template($conf);
 
-	PVE::QemuConfig->check_lock($conf) if !$skiplock;
+	PVE::QemuConfig->check_lock($conf)
+	    if !($skiplock || PVE::QemuConfig->has_lock($conf, 'suspend'));
 
 	die "VM $vmid already running\n" if check_running($vmid, undef, $migratedfrom);
 
@@ -5214,6 +5221,12 @@ sub vm_start {
 
 	PVE::GuestHelpers::exec_hookscript($conf, $vmid, 'pre-start', 1);
 
+	if (PVE::QemuConfig->has_lock($conf, 'suspend')) {
+	    # if we want to resume a suspended vm,
+	    # we have to use the saved machine type, otherwise it cannot work
+	    $forcemachine = $conf->{runningmachine};
+	}
+
 	my ($cmd, $vollist, $spice_port) = config_to_command($storecfg, $vmid, $conf, $defaults, $forcemachine);
 
 	my $migrate_port = 0;
@@ -5411,6 +5424,17 @@ sub vm_start {
 		    property => "guest-stats-polling-interval",
 		    value => 2) if (!defined($conf->{balloon}) || $conf->{balloon});
 
+	if (PVE::QemuConfig->has_lock($conf, 'suspend') &&
+	    (my $vmstate = $conf->{vmstate}))
+	{
+	    delete $conf->{lock};
+	    delete $conf->{vmstate};
+	    delete $conf->{runningmachine};
+	    PVE::Storage::deactivate_volumes($storecfg, [$vmstate]);
+	    PVE::Storage::vdisk_free($storecfg, $vmstate);
+	    PVE::QemuConfig->write_config($vmid, $conf);
+	}
+
 	PVE::GuestHelpers::exec_hookscript($conf, $vmid, 'post-start');
     });
 }
-- 
2.11.0




More information about the pve-devel mailing list