[pve-devel] [PATCH v3 qemu-server 4/8] refactor: create QemuServer::Monitor for high-level QMP access

Stefan Reiter s.reiter at proxmox.com
Mon Nov 11 17:30:37 CET 2019


On 11/11/19 4:37 PM, Thomas Lamprecht wrote:
> On 11/4/19 2:57 PM, Stefan Reiter wrote:
>> QMP and monitor helpers are moved from QemuServer.pm.
>>
>> By using only vm_running_locally instead of check_running, a cyclic
>> dependency to QemuConfig is avoided. This also means that the $nocheck
>> parameter serves no more purpose, and has thus been removed along with
>> vm_mon_cmd_nocheck.
>>
>> Care has been taken to avoid errors resulting from
>> this, and occasionally a manual check for a VM's existance inserted on
>> the callsite.
>>
>> Methods have been renamed to avoid redundant naming:
>> * vm_qmp_command -> qmp_cmd
>> * vm_mon_cmd -> mon_cmd
>> * vm_human_monitor_command -> hmp_cmd
>>
>> mon_cmd is exported since it has many users. This patch also changes all
>> non-package users of vm_qmp_command to use the mon_cmd helper. Includes
>> mocking for tests.
>>
>> Signed-off-by: Stefan Reiter <s.reiter at proxmox.com>
>> ---
>>
>> Sorry for the long patch, but almost all changes are just callers of
>> mon_cmd/qmp_cmd being renamed.
>>
>>
>>   PVE/API2/Qemu.pm         |  15 ++--
>>   PVE/API2/Qemu/Agent.pm   |   7 +-
>>   PVE/CLI/qm.pm            |  13 +--
>>   PVE/QemuConfig.pm        |  15 ++--
>>   PVE/QemuMigrate.pm       |  21 ++---
>>   PVE/QemuServer.pm        | 184 +++++++++++++--------------------------
>>   PVE/QemuServer/Agent.pm  |   3 +-
>>   PVE/QemuServer/Makefile  |   1 +
>>   PVE/QemuServer/Memory.pm |   9 +-
>>   PVE/VZDump/QemuServer.pm |  13 +--
>>   test/snapshot-test.pm    |  19 ++--
>>   11 files changed, 130 insertions(+), 170 deletions(-)
>>
> 
> 
> could it be that you forgot to actually stage the newly create Monitor
> module? At least I do not see any in above diffstat or (cut) diff
> itself.. ^^
> 

'git status' has proven you right :/ (well, aside from the fact that it 
isn't in the mail...)

Not a big file though, diff below. I'll include it in a v4, but I'll 
wait for feedback in general - or is this all?


diff --git a/PVE/QemuServer/Monitor.pm b/PVE/QemuServer/Monitor.pm
new file mode 100644
index 0000000..b69fef7
--- /dev/null
+++ b/PVE/QemuServer/Monitor.pm
@@ -0,0 +1,62 @@
+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 };
+    qmp_cmd($vmid, $cmd);
+}
+
+sub hmp_cmd {
+    my ($vmid, $cmdline) = @_;
+
+    my $cmd = {
+	execute => 'human-monitor-command',
+	arguments => { 'command-line' => $cmdline },
+    };
+
+    return qmp_cmd($vmid, $cmd);
+}
+
+1;




More information about the pve-devel mailing list