[PATCH qemu-server v3 04/18] monitor: add vm_qmp_peer() helper

Fiona Ebner
2026-04-23T09:38:13Z

Avoid duplicating this information to ensure consistency and improve
readability.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---
src/PVE/QemuServer.pm | 10 ++++------
src/PVE/QemuServer/Blockdev.pm | 4 ++--
src/PVE/QemuServer/Monitor.pm | 9 ++++++++-
src/PVE/VZDump/QemuServer.pm | 4 ++--
4 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/src/PVE/QemuServer.pm b/src/PVE/QemuServer.pm
index 476fef0b..76f32894 100644
--- a/src/PVE/QemuServer.pm
+++ b/src/PVE/QemuServer.pm
@@ -82,7 +82,7 @@ use PVE::QemuServer::DriveDevice qw(print_drivedevice_full scsihw_infos);
use PVE::QemuServer::Machine;
use PVE::QemuServer::Memory qw(get_current_memory);
use PVE::QemuServer::MetaInfo;
-use PVE::QemuServer::Monitor qw(mon_cmd);
+use PVE::QemuServer::Monitor qw(mon_cmd vm_qmp_peer);
use PVE::QemuServer::Network;
use PVE::QemuServer::OVMF;
use PVE::QemuServer::PCI qw(print_pci_addr print_pcie_addr print_pcie_root_port parse_hostpci);
@@ -2711,7 +2711,7 @@ sub vmstatus {
my $statuscb = sub {
my ($vmid, $resp) = @_;

- my $qmp_peer = { name => "VM $vmid", id => $vmid, type => 'qmp' };
+ my $qmp_peer = vm_qmp_peer($vmid);

$qmpclient->queue_cmd($qmp_peer, $proxmox_support_cb, 'query-proxmox-support');
$qmpclient->queue_cmd($qmp_peer, $blockstatscb, 'query-blockstats');
@@ -2733,8 +2733,7 @@ sub vmstatus {
foreach my $vmid (keys %$list) {
next if $opt_vmid && ($vmid ne $opt_vmid);
next if !$res->{$vmid}->{pid}; # not running
- my $qmp_peer = { name => "VM $vmid", id => $vmid, type => 'qmp' };
- $qmpclient->queue_cmd($qmp_peer, $statuscb, 'query-status');
+ $qmpclient->queue_cmd(vm_qmp_peer($vmid), $statuscb, 'query-status');
}

$qmpclient->queue_execute(undef, 2);
@@ -3189,8 +3188,7 @@ sub config_to_command {

my $use_virtio = 0;

- my $qmpsocket =
- PVE::QemuServer::Helpers::qmp_socket({ name => "VM $vmid", id => $vmid, type => 'qmp' });
+ my $qmpsocket = PVE::QemuServer::Helpers::qmp_socket(vm_qmp_peer($vmid));
push @$cmd, '-chardev', "socket,id=qmp,path=$qmpsocket,server=on,wait=off";
push @$cmd, '-mon', "chardev=qmp,mode=control";

diff --git a/src/PVE/QemuServer/Blockdev.pm b/src/PVE/QemuServer/Blockdev.pm
index be907be8..80c1178e 100644
--- a/src/PVE/QemuServer/Blockdev.pm
+++ b/src/PVE/QemuServer/Blockdev.pm
@@ -14,7 +14,7 @@ use PVE::Storage;
use PVE::QemuServer::Drive qw(drive_is_cdrom);
use PVE::QemuServer::Helpers;
use PVE::QemuServer::Machine;
-use PVE::QemuServer::Monitor qw(mon_cmd qmp_cmd);
+use PVE::QemuServer::Monitor qw(mon_cmd qmp_cmd vm_qmp_peer);

use base qw(Exporter);

@@ -588,7 +588,7 @@ sub attach {
if ($options->{qsd}) {
$qmp_peer = { name => "QEMU storage daemon $id", id => $id, type => 'qsd' };
} else {
- $qmp_peer = { name => "VM $id", id => $id, type => 'qmp' };
+ $qmp_peer = vm_qmp_peer($id);
}

my $machine_version;
diff --git a/src/PVE/QemuServer/Monitor.pm b/src/PVE/QemuServer/Monitor.pm
index 423c40a9..e7f08e23 100644
--- a/src/PVE/QemuServer/Monitor.pm
+++ b/src/PVE/QemuServer/Monitor.pm
@@ -11,6 +11,7 @@ use base 'Exporter';
our @EXPORT_OK = qw(
mon_cmd
qmp_cmd
+ vm_qmp_peer
);

=head3 qmp_cmd
@@ -102,6 +103,12 @@ sub qmp_cmd {
return $res;
}

+sub vm_qmp_peer {
+ my ($vmid) = @_;
+
+ return { name => "VM $vmid", id => $vmid, type => 'qmp' };
+}
+
sub qsd_cmd {
my ($id, $execute, %params) = @_;

@@ -121,7 +128,7 @@ sub hmp_cmd {
my ($vmid, $cmdline, $timeout) = @_;

return qmp_cmd(
- { name => "VM $vmid", id => $vmid, type => 'qmp' }, 'human-monitor-command',
+ vm_qmp_peer($vmid), 'human-monitor-command',
'command-line' => $cmdline,
timeout => $timeout,
);
diff --git a/src/PVE/VZDump/QemuServer.pm b/src/PVE/VZDump/QemuServer.pm
index 32f92829..48994612 100644
--- a/src/PVE/VZDump/QemuServer.pm
+++ b/src/PVE/VZDump/QemuServer.pm
@@ -34,7 +34,7 @@ use PVE::QemuServer::Blockdev;
use PVE::QemuServer::Drive qw(checked_volume_format);
use PVE::QemuServer::Helpers;
use PVE::QemuServer::Machine;
-use PVE::QemuServer::Monitor qw(mon_cmd);
+use PVE::QemuServer::Monitor qw(mon_cmd vm_qmp_peer);
use PVE::QemuServer::QMPHelpers;

use base qw (PVE::VZDump::Plugin);
@@ -996,7 +996,7 @@ sub archive_vma {
}

my $qmpclient = PVE::QMPClient->new();
- my $qmp_peer = { name => "VM $vmid", id => $vmid, type => 'qmp' };
+ my $qmp_peer = vm_qmp_peer($vmid);
my $backup_cb = sub {
my ($vmid, $resp) = @_;
$backup_job_uuid = $resp->{return}->{UUID};
--
2.47.3