fix #2788: do not resume vms after backup if they were paused before

by checking if the vm is paused at the beginning and skipping the
resume now we also skip the qga freeze/thaw (which cannot work if the
vm is paused)

moved the 'vm_is_paused' sub from the api to PVE/QemuServer.pm so it
is available everywhere we need it.

since a suspend backup would pause the vm anyway, we can skip that
step also

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
Tested-by: Fabian Ebner <f.ebner@proxmox.com>
This commit is contained in:
Dominik Csapak 2021-01-20 13:32:04 +01:00 committed by Thomas Lamprecht
parent 99676a6c1a
commit b08c37c363
3 changed files with 21 additions and 14 deletions

View File

@ -2417,16 +2417,6 @@ __PACKAGE__->register_method({
return $rpcenv->fork_worker('qmreset', $vmid, $authuser, $realcmd);
}});
my sub vm_is_paused {
my ($vmid) = @_;
my $qmpstatus = eval {
PVE::QemuConfig::assert_config_exists_on_node($vmid);
mon_cmd($vmid, "query-status");
};
warn "$@\n" if $@;
return $qmpstatus && $qmpstatus->{status} eq "paused";
}
__PACKAGE__->register_method({
name => 'vm_shutdown',
path => '{vmid}/status/shutdown',
@ -2495,7 +2485,7 @@ __PACKAGE__->register_method({
#
# checking the qmp status here to get feedback to the gui/cli/api
# and the status query should not take too long
if (vm_is_paused($vmid)) {
if (PVE::QemuServer::vm_is_paused($vmid)) {
if ($param->{forceStop}) {
warn "VM is paused - stop instead of shutdown\n";
$shutdown = 0;
@ -2571,7 +2561,7 @@ __PACKAGE__->register_method({
my $node = extract_param($param, 'node');
my $vmid = extract_param($param, 'vmid');
die "VM is paused - cannot shutdown\n" if vm_is_paused($vmid);
die "VM is paused - cannot shutdown\n" if PVE::QemuServer::vm_is_paused($vmid);
die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);

View File

@ -7389,4 +7389,14 @@ sub complete_migration_storage {
return $res;
}
sub vm_is_paused {
my ($vmid) = @_;
my $qmpstatus = eval {
PVE::QemuConfig::assert_config_exists_on_node($vmid);
mon_cmd($vmid, "query-status");
};
warn "$@\n" if $@;
return $qmpstatus && $qmpstatus->{status} eq "paused";
}
1;

View File

@ -62,8 +62,11 @@ sub prepare {
if defined($conf->{name});
$self->{vm_was_running} = 1;
$self->{vm_was_paused} = 0;
if (!PVE::QemuServer::check_running($vmid)) {
$self->{vm_was_running} = 0;
} elsif (PVE::QemuServer::vm_is_paused($vmid)) {
$self->{vm_was_paused} = 1;
}
$task->{hostname} = $conf->{name};
@ -176,12 +179,16 @@ sub start_vm {
sub suspend_vm {
my ($self, $task, $vmid) = @_;
return if $self->{vm_was_paused};
$self->cmd ("qm suspend $vmid --skiplock");
}
sub resume_vm {
my ($self, $task, $vmid) = @_;
return if $self->{vm_was_paused};
$self->cmd ("qm resume $vmid --skiplock");
}
@ -794,7 +801,7 @@ sub _get_task_devlist {
sub qga_fs_freeze {
my ($self, $task, $vmid) = @_;
return if !$self->{vmlist}->{$vmid}->{agent} || $task->{mode} eq 'stop' || !$self->{vm_was_running};
return if !$self->{vmlist}->{$vmid}->{agent} || $task->{mode} eq 'stop' || !$self->{vm_was_running} || $self->{vm_was_paused};
if (!PVE::QemuServer::qga_check_running($vmid, 1)) {
$self->loginfo("skipping guest-agent 'fs-freeze', agent configured but not running?");
@ -872,7 +879,7 @@ sub register_qmeventd_handle {
sub resume_vm_after_job_start {
my ($self, $task, $vmid) = @_;
return if !$self->{vm_was_running};
return if !$self->{vm_was_running} || $self->{vm_was_paused};
if (my $stoptime = $task->{vmstoptime}) {
my $delay = time() - $task->{vmstoptime};