qemu-server/PVE/QemuServer/Monitor.pm
Fiona Ebner 4ee69c54f6 monitor: allow passing timeout for a HMP command
Passing the timeout key with an explicit value of undef is fine,
because both the absence of the timeout key and an explicit value of
undef will lead to $timeout being undef in the qmp_cmd() function.

In preparation to increase the timeout for certain (e.g. disk-related)
HMP commands.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
2024-06-11 13:56:44 +02:00

64 lines
1.2 KiB
Perl

package PVE::QemuServer::Monitor;
use strict;
use warnings;
use PVE::SafeSyslog;
use PVE::QemuServer::Helpers;
use PVE::QMPClient;
use base 'Exporter';
our @EXPORT_OK = qw(
mon_cmd
);
sub qmp_cmd {
my ($vmid, $cmd) = @_;
my $res;
my $timeout;
if ($cmd->{arguments}) {
$timeout = delete $cmd->{arguments}->{timeout};
}
eval {
die "VM $vmid not running\n" if !PVE::QemuServer::Helpers::vm_running_locally($vmid);
my $sname = PVE::QemuServer::Helpers::qmp_socket($vmid);
if (-e $sname) { # test if VM is reasonably new and supports qmp/qga
my $qmpclient = PVE::QMPClient->new();
$res = $qmpclient->cmd($vmid, $cmd, $timeout);
} else {
die "unable to open monitor socket\n";
}
};
if (my $err = $@) {
syslog("err", "VM $vmid qmp command failed - $err");
die $err;
}
return $res;
}
sub mon_cmd {
my ($vmid, $execute, %params) = @_;
my $cmd = { execute => $execute, arguments => \%params };
return qmp_cmd($vmid, $cmd);
}
sub hmp_cmd {
my ($vmid, $cmdline, $timeout) = @_;
my $cmd = {
execute => 'human-monitor-command',
arguments => { 'command-line' => $cmdline, timeout => $timeout },
};
return qmp_cmd($vmid, $cmd);
}
1;