From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <s.reiter@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 831D960BD2
 for <pve-devel@lists.proxmox.com>; Mon, 19 Oct 2020 14:19:45 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 816332C612
 for <pve-devel@lists.proxmox.com>; Mon, 19 Oct 2020 14:19:45 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [212.186.127.180])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id 276FC2C5ED
 for <pve-devel@lists.proxmox.com>; Mon, 19 Oct 2020 14:19:44 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id E8D1745E0D
 for <pve-devel@lists.proxmox.com>; Mon, 19 Oct 2020 14:19:43 +0200 (CEST)
From: Stefan Reiter <s.reiter@proxmox.com>
To: pve-devel@lists.proxmox.com
Cc: d.csapak@proxmox.com,
	w.bumiller@proxmox.com
Date: Mon, 19 Oct 2020 14:18:41 +0200
Message-Id: <20201019121842.20277-7-s.reiter@proxmox.com>
X-Mailer: git-send-email 2.20.1
In-Reply-To: <20201019121842.20277-1-s.reiter@proxmox.com>
References: <20201019121842.20277-1-s.reiter@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.035 Adjusted score from AWL reputation of From: address
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 RCVD_IN_DNSWL_MED        -2.3 Sender listed at https://www.dnswl.org/,
 medium trust
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pve-devel] [PATCH v2 qemu-server 6/7] fix vm_resume and allow
 vm_start with QMP status 'shutdown'
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Mon, 19 Oct 2020 12:19:45 -0000

When the VM is in status 'shutdown', i.e. after the guest issues a
powerdown while a backup is running, QEMU requires a 'system_reset' to
be issued before 'cont' can boot the guest again.

Additionally, when the VM has been powered down during a backup, the
logically correct call would be a 'vm_start', so automatically vm_resume
from vm_start in case this situation occurs. This also means the GUI can
cope with this almost unchanged.

Signed-off-by: Stefan Reiter <s.reiter@proxmox.com>
---
 PVE/QemuServer.pm | 23 ++++++++++++++++++++---
 1 file changed, 20 insertions(+), 3 deletions(-)

diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index c712671..22484ca 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -4860,13 +4860,22 @@ sub vm_start {
 	    if !$params->{skiptemplate} && PVE::QemuConfig->is_template($conf);
 
 	my $has_suspended_lock = PVE::QemuConfig->has_lock($conf, 'suspended');
+	my $has_backup_lock = PVE::QemuConfig->has_lock($conf, 'backup');
+
+	my $running = check_running($vmid, undef, $migrate_opts->{migratedfrom});
+
+	if ($has_backup_lock && $running) {
+	    # a backup is currently running, attempt to start the guest in the
+	    # existing QEMU instance
+	    return vm_resume($vmid);
+	}
 
 	PVE::QemuConfig->check_lock($conf)
 	    if !($params->{skiplock} || $has_suspended_lock);
 
 	$params->{resume} = $has_suspended_lock || defined($conf->{vmstate});
 
-	die "VM $vmid already running\n" if check_running($vmid, undef, $migrate_opts->{migratedfrom});
+	die "VM $vmid already running\n" if $running;
 
 	if (my $storagemap = $migrate_opts->{storagemap}) {
 	    my $replicated = $migrate_opts->{replicated_volumes};
@@ -5546,9 +5555,12 @@ sub vm_resume {
     PVE::QemuConfig->lock_config($vmid, sub {
 	my $res = mon_cmd($vmid, 'query-status');
 	my $resume_cmd = 'cont';
+	my $reset = 0;
 
-	if ($res->{status} && $res->{status} eq 'suspended') {
-	    $resume_cmd = 'system_wakeup';
+	if ($res->{status}) {
+	    return if $res->{status} eq 'running'; # job done, go home
+	    $resume_cmd = 'system_wakeup' if $res->{status} eq 'suspended';
+	    $reset = 1 if $res->{status} eq 'shutdown';
 	}
 
 	if (!$nocheck) {
@@ -5559,6 +5571,11 @@ sub vm_resume {
 		if !($skiplock || PVE::QemuConfig->has_lock($conf, 'backup'));
 	}
 
+	if ($reset) {
+	    # required if a VM shuts down during a backup and we get a resume
+	    # request before the backup finishes for example
+	    mon_cmd($vmid, "system_reset");
+	}
 	mon_cmd($vmid, $resume_cmd);
     });
 }
-- 
2.20.1