[PATCH qemu-server v3 13/18] block job: switch qemu_drive_mirror_monitor() to use QMP peer

Fiona Ebner
2026-04-23T09:40:35Z

Take care to only allow a different destination ID when having the
main QEMU instance as a peer, because the required freeze/thaw or
suspend/resume can only be done then.

Also adds the missing $op argument in the signature of the mocked
function in the migration tests for completeness.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---
src/PVE/QemuMigrate.pm | 2 +-
src/PVE/QemuServer.pm | 12 ++++++---
src/PVE/QemuServer/BlockJob.pm | 33 ++++++++++++++---------
src/PVE/QemuServer/VolumeChain.pm | 8 ++++--
src/test/MigrationTest/QemuMigrateMock.pm | 2 +-
5 files changed, 38 insertions(+), 19 deletions(-)

diff --git a/src/PVE/QemuMigrate.pm b/src/PVE/QemuMigrate.pm
index 54b8a65d..bdf6770d 100644
--- a/src/PVE/QemuMigrate.pm
+++ b/src/PVE/QemuMigrate.pm
@@ -1571,7 +1571,7 @@ sub phase2 {
# thus, this command changes to it to blockjob complete (see qapi docs)
eval {
PVE::QemuServer::BlockJob::monitor(
- $vmid, undef, $self->{storage_migration_jobs}, 'cancel',
+ vm_qmp_peer($vmid), undef, $self->{storage_migration_jobs}, 'cancel',
);
};
if (my $err = $@) {
diff --git a/src/PVE/QemuServer.pm b/src/PVE/QemuServer.pm
index 36cbfd59..93f653c6 100644
--- a/src/PVE/QemuServer.pm
+++ b/src/PVE/QemuServer.pm
@@ -7321,7 +7321,9 @@ sub pbs_live_restore {
}

mon_cmd($vmid, 'cont');
- PVE::QemuServer::BlockJob::monitor($vmid, undef, $jobs, 'auto', 0, 'stream');
+ PVE::QemuServer::BlockJob::monitor(
+ vm_qmp_peer($vmid), undef, $jobs, 'auto', 0, 'stream',
+ );

print "restore-drive jobs finished successfully, removing all tracking block devices"
. " to disconnect from Proxmox Backup Server\n";
@@ -7441,7 +7443,9 @@ sub live_import_from_files {
}

mon_cmd($vmid, 'cont');
- PVE::QemuServer::BlockJob::monitor($vmid, undef, $jobs, 'auto', 0, 'stream');
+ PVE::QemuServer::BlockJob::monitor(
+ vm_qmp_peer($vmid), undef, $jobs, 'auto', 0, 'stream',
+ );

print "restore-drive jobs finished successfully, removing all tracking block devices\n";

@@ -7951,7 +7955,9 @@ sub clone_disk {
# if this is the case, we have to complete any block-jobs still there from
# previous drive-mirrors
if (($completion && $completion eq 'complete') && (scalar(keys %$jobs) > 0)) {
- PVE::QemuServer::BlockJob::monitor($vmid, $newvmid, $jobs, $completion, $qga);
+ PVE::QemuServer::BlockJob::monitor(
+ vm_qmp_peer($vmid), $newvmid, $jobs, $completion, $qga,
+ );
}
goto no_data_clone;
}
diff --git a/src/PVE/QemuServer/BlockJob.pm b/src/PVE/QemuServer/BlockJob.pm
index d6eb2a8e..76518b1f 100644
--- a/src/PVE/QemuServer/BlockJob.pm
+++ b/src/PVE/QemuServer/BlockJob.pm
@@ -84,7 +84,10 @@ sub qemu_blockjobs_cancel {
# 'skip': wait until all jobs are ready, return with block jobs in ready state
# 'auto': wait until all jobs disappear, only use for jobs which complete automatically
sub monitor {
- my ($vmid, $vmiddst, $jobs, $completion, $qga, $op) = @_;
+ my ($qmp_peer, $vmiddst, $jobs, $completion, $qga, $op) = @_;
+
+ die "drive mirror: different destination is only supported when peer is main QEMU instance\n"
+ if $vmiddst && $qmp_peer->{type} ne 'qmp';

$completion //= 'complete';
$op //= "mirror";
@@ -96,7 +99,7 @@ sub monitor {
while (1) {
die "block job ('$op') timed out\n" if $err_complete > 300;

- my $stats = mon_cmd($vmid, "query-block-jobs");
+ my $stats = qmp_cmd($qmp_peer, "query-block-jobs");
my $ctime = time();

my $running_jobs = {};
@@ -121,9 +124,7 @@ sub monitor {
die "$job_id: '$op' has been cancelled\n" if !defined($job);

if ($job && $job->{status} eq 'concluded') {
- qemu_handle_concluded_blockjob(
- vm_qmp_peer($vmid), $job_id, $job, $jobs->{$job_id},
- );
+ qemu_handle_concluded_blockjob($qmp_peer, $job_id, $job, $jobs->{$job_id});
}

my $busy = $job->{busy};
@@ -164,7 +165,8 @@ sub monitor {
# do the complete later (or has already been done)
last if $completion eq 'skip' || $completion eq 'auto';

- if ($vmiddst && $vmiddst != $vmid) {
+ if ($qmp_peer->{type} eq 'qmp' && $vmiddst && $vmiddst != $qmp_peer->{id}) {
+ my $vmid = $qmp_peer->{id};
my $should_fsfreeze = $qga && qga_check_running($vmid);
if ($should_fsfreeze) {
print "issuing guest agent 'guest-fsfreeze-freeze' command\n";
@@ -184,7 +186,7 @@ sub monitor {
}

# if we clone a disk for a new target vm, we don't switch the disk
- qemu_blockjobs_cancel(vm_qmp_peer($vmid), $jobs);
+ qemu_blockjobs_cancel($qmp_peer, $jobs);

if ($should_fsfreeze) {
print "issuing guest agent 'guest-fsfreeze-thaw' command\n";
@@ -211,10 +213,10 @@ sub monitor {
eval {
if ($completion eq 'complete') {
$detach_node_name = $jobs->{$job_id}->{'source-node-name'};
- mon_cmd($vmid, 'job-complete', id => $job_id);
+ qmp_cmd($qmp_peer, 'job-complete', id => $job_id);
} elsif ($completion eq 'cancel') {
$detach_node_name = $jobs->{$job_id}->{'target-node-name'};
- mon_cmd($vmid, 'block-job-cancel', device => $job_id);
+ qmp_cmd($qmp_peer, 'block-job-cancel', device => $job_id);
} else {
die "invalid completion value: $completion\n";
}
@@ -241,7 +243,7 @@ sub monitor {
my $err = $@;

if ($err) {
- eval { qemu_blockjobs_cancel(vm_qmp_peer($vmid), $jobs) };
+ eval { qemu_blockjobs_cancel($qmp_peer, $jobs) };
die "block job ($op) error: $err";
}
}
@@ -320,7 +322,7 @@ sub qemu_drive_mirror {
die "mirroring error: $err\n";
}

- monitor($vmid, $vmiddst, $jobs, $completion, $qga);
+ monitor(vm_qmp_peer($vmid), $vmiddst, $jobs, $completion, $qga);
}

# Callers should version guard this (only available with a binary >= QEMU 8.2)
@@ -516,7 +518,14 @@ sub blockdev_mirror {
log_warn("unable to delete blockdev '$target_node_name' - $@");
die "error starting blockdev mirrror - $err";
}
- monitor($vmid, $dest->{vmid}, $jobs, $completion, $options->{'guest-agent'}, 'mirror');
+ monitor(
+ vm_qmp_peer($vmid),
+ $dest->{vmid},
+ $jobs,
+ $completion,
+ $options->{'guest-agent'},
+ 'mirror',
+ );
}

sub mirror {
diff --git a/src/PVE/QemuServer/VolumeChain.pm b/src/PVE/QemuServer/VolumeChain.pm
index 8c0749de..8dd29d61 100644
--- a/src/PVE/QemuServer/VolumeChain.pm
+++ b/src/PVE/QemuServer/VolumeChain.pm
@@ -255,7 +255,9 @@ sub blockdev_commit {
# 'block-commit' will complete automatically.
my $complete = $src_snap && $src_snap ne 'current' ? 'auto' : 'complete';

- PVE::QemuServer::BlockJob::monitor($vmid, undef, $jobs, $complete, 0, 'commit');
+ PVE::QemuServer::BlockJob::monitor(
+ vm_qmp_peer($vmid), undef, $jobs, $complete, 0, 'commit',
+ );

blockdev_delete(
$storecfg, $vmid, $drive, $src_file_blockdev, $src_fmt_blockdev, $src_snap,
@@ -329,7 +331,9 @@ sub blockdev_stream {
mon_cmd($vmid, 'block-stream', %$options);
$jobs->{$job_id} = {};

- PVE::QemuServer::BlockJob::monitor($vmid, undef, $jobs, 'auto', 0, 'stream');
+ PVE::QemuServer::BlockJob::monitor(
+ vm_qmp_peer($vmid), undef, $jobs, 'auto', 0, 'stream',
+ );

blockdev_delete($storecfg, $vmid, $drive, $snap_file_blockdev, $snap_fmt_blockdev, $snap);
}
diff --git a/src/test/MigrationTest/QemuMigrateMock.pm b/src/test/MigrationTest/QemuMigrateMock.pm
index 8cd2da12..a12310ec 100644
--- a/src/test/MigrationTest/QemuMigrateMock.pm
+++ b/src/test/MigrationTest/QemuMigrateMock.pm
@@ -176,7 +176,7 @@ $qemu_server_blockjob_module->mock(
common_mirror_mock($source->{vmid}, $drive_id);
},
monitor => sub {
- my ($vmid, $vmiddst, $jobs, $completion, $qga) = @_;
+ my ($qmp_peer, $vmiddst, $jobs, $completion, $qga, $op) = @_;

if (
$fail_config->{block_job_monitor}
--
2.47.3