From 8989736707634628a43de8e2737e858518513d27 Mon Sep 17 00:00:00 2001 From: Dominik Csapak Date: Tue, 12 Apr 2016 10:54:21 +0200 Subject: [PATCH] change shutdown behaviour on suspended vm previously, when shutting down a suspended vm, we successfully send the shutdown command to it, but it will not shutdown (because it is suspended) there we will run into the timeout and either bail out with an error, or kill the process when we not kill the process and resume the vm, it will instantly shutdown, because of the previous command this patch checks the status of the vm beforehand, and either bails out with an error that you cannot shutdown a suspended vm, or stops the vm with the correct qmp command (depending of forceStop) Signed-off-by: Dominik Csapak --- PVE/API2/Qemu.pm | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm index 0d33f6c..8bb68a4 100644 --- a/PVE/API2/Qemu.pm +++ b/PVE/API2/Qemu.pm @@ -1818,13 +1818,36 @@ __PACKAGE__->register_method({ my $storecfg = PVE::Storage::config(); + my $shutdown = 1; + + # if vm is paused, do not shutdown (but stop if forceStop = 1) + # otherwise, we will infer a shutdown command, but run into the timeout, + # then when the vm is resumed, it will instantly shutdown + # + # checking the qmp status here to get feedback to the gui/cli/api + # and the status query should not take too long + my $qmpstatus; + eval { + $qmpstatus = PVE::QemuServer::vm_qmp_command($vmid, { execute => "query-status" }, 0); + }; + my $err = $@ if $@; + + if (!$err && $qmpstatus->{status} eq "paused") { + if ($param->{forceStop}) { + warn "VM is paused - stop instead of shutdown\n"; + $shutdown = 0; + } else { + die "VM is paused - cannot shutdown\n"; + } + } + my $realcmd = sub { my $upid = shift; syslog('info', "shutdown VM $vmid: $upid\n"); PVE::QemuServer::vm_stop($storecfg, $vmid, $skiplock, 0, $param->{timeout}, - 1, $param->{forceStop}, $keepActive); + $shutdown, $param->{forceStop}, $keepActive); return; }; -- 2.39.2