[PATCH qemu-server v3 08/18] blockdev: switch detach() to use QMP peer

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

src/PVE/QemuServer.pm | 6 +++---
src/PVE/QemuServer/BlockJob.pm | 4 ++--
src/PVE/QemuServer/Blockdev.pm | 22 +++++++++++-----------
src/PVE/QemuServer/VolumeChain.pm | 4 ++--
src/PVE/VZDump/QemuServer.pm | 2 +-
5 files changed, 19 insertions(+), 19 deletions(-)

diff --git a/src/PVE/QemuServer.pm b/src/PVE/QemuServer.pm
index eea5ac3b..7d1ec232 100644
--- a/src/PVE/QemuServer.pm
+++ b/src/PVE/QemuServer.pm
@@ -4089,7 +4089,7 @@ sub qemu_drivedel {

# for the switch to -blockdev
if (PVE::QemuServer::Machine::is_machine_version_at_least($machine_type, 10, 0)) {
- PVE::QemuServer::Blockdev::detach($vmid, "drive-$deviceid");
+ PVE::QemuServer::Blockdev::detach(vm_qmp_peer($vmid), "drive-$deviceid");
return 1;
} else {
my $ret = PVE::QemuServer::Monitor::hmp_cmd($vmid, "drive_del drive-$deviceid", 10 * 60);
@@ -7321,7 +7321,7 @@ sub pbs_live_restore {
. " to disconnect from Proxmox Backup Server\n";

for my $ds (sort keys %$restored_disks) {
- PVE::QemuServer::Blockdev::detach($vmid, "$ds-pbs");
+ PVE::QemuServer::Blockdev::detach(vm_qmp_peer($vmid), "$ds-pbs");
}

close($qmeventd_fd);
@@ -7440,7 +7440,7 @@ sub live_import_from_files {
print "restore-drive jobs finished successfully, removing all tracking block devices\n";

for my $ds (sort keys %$live_restore_backing) {
- PVE::QemuServer::Blockdev::detach($vmid, "drive-$ds-restore");
+ PVE::QemuServer::Blockdev::detach(vm_qmp_peer($vmid), "drive-$ds-restore");
}

close($qmeventd_fd);
diff --git a/src/PVE/QemuServer/BlockJob.pm b/src/PVE/QemuServer/BlockJob.pm
index 59b33482..f76b1bde 100644
--- a/src/PVE/QemuServer/BlockJob.pm
+++ b/src/PVE/QemuServer/BlockJob.pm
@@ -34,7 +34,7 @@ sub qemu_handle_concluded_blockjob {
$job->{'detach-node-name'} = $job->{'target-node-name'} if $qmp_info->{error} || $job->{cancel};

if (my $node_name = $job->{'detach-node-name'}) {
- eval { PVE::QemuServer::Blockdev::detach($vmid, $node_name); };
+ eval { PVE::QemuServer::Blockdev::detach(vm_qmp_peer($vmid), $node_name); };
log_warn($@) if $@;
}

@@ -509,7 +509,7 @@ sub blockdev_mirror {
if (my $err = $@) {
eval { qemu_blockjobs_cancel($vmid, $jobs) };
log_warn("unable to cancel block jobs - $@");
- eval { PVE::QemuServer::Blockdev::detach($vmid, $target_node_name); };
+ eval { PVE::QemuServer::Blockdev::detach(vm_qmp_peer($vmid), $target_node_name); };
log_warn("unable to delete blockdev '$target_node_name' - $@");
die "error starting blockdev mirrror - $err";
}
diff --git a/src/PVE/QemuServer/Blockdev.pm b/src/PVE/QemuServer/Blockdev.pm
index 495aff50..b9430a56 100644
--- a/src/PVE/QemuServer/Blockdev.pm
+++ b/src/PVE/QemuServer/Blockdev.pm
@@ -637,16 +637,16 @@ sub attach {

=head3 detach

- detach($vmid, $node_name);
+ detach($qmp_peer, $node_name);

-Detach the block device C<$node_name> from the VM C<$vmid>. Also removes associated child block
-nodes.
+Detach the block device C<$node_name> from the QMP peer C<$qmp_peer>. Also removes associated child
+block nodes.

Parameters:

=over

-=item C<$vmid>: The ID of the virtual machine.
+=item C<$qmp_peer>: QMP peer information.

=item C<$node_name>: The node name identifying the block node in QEMU.

@@ -655,11 +655,11 @@ Parameters:
=cut

sub detach {
- my ($vmid, $node_name) = @_;
+ my ($qmp_peer, $node_name) = @_;

die "Blockdev::detach - no node name\n" if !$node_name;

- my $block_info = mon_cmd($vmid, "query-named-block-nodes");
+ my $block_info = qmp_cmd($qmp_peer, "query-named-block-nodes");
$block_info = { map { $_->{'node-name'} => $_ } $block_info->@* };

my $remove_throttle_group_id;
@@ -670,7 +670,7 @@ sub detach {
while ($node_name) {
last if !$block_info->{$node_name}; # already gone

- my $res = mon_cmd($vmid, 'blockdev-del', 'node-name' => "$node_name", noerr => 1);
+ my $res = qmp_cmd($qmp_peer, 'blockdev-del', 'node-name' => "$node_name", noerr => 1);
if (my $err = $res->{error}) {
last if $err =~ m/Failed to find node with node-name/; # already gone
die "deleting blockdev '$node_name' failed : $err\n";
@@ -684,7 +684,7 @@ sub detach {
}

if ($remove_throttle_group_id) {
- eval { mon_cmd($vmid, 'object-del', id => $remove_throttle_group_id); };
+ eval { qmp_cmd($qmp_peer, 'object-del', id => $remove_throttle_group_id); };
die "removing throttle group failed - $@\n" if $@;
}

@@ -694,7 +694,7 @@ sub detach {
sub detach_tpm_backup_node {
my ($vmid) = @_;

- detach($vmid, "drive-tpmstate0-backup");
+ detach(vm_qmp_peer($vmid), "drive-tpmstate0-backup");
}

sub detach_fleecing_block_nodes {
@@ -706,7 +706,7 @@ sub detach_fleecing_block_nodes {
next if !is_fleecing_top_node($node_name);

$log_func->('info', "detaching (old) fleecing image '$node_name'");
- eval { detach($vmid, $node_name) };
+ eval { detach(vm_qmp_peer($vmid), $node_name) };
$log_func->('warn', "error detaching (old) fleecing image '$node_name' - $@") if $@;
}
}
@@ -746,7 +746,7 @@ my sub blockdev_change_medium {
# force eject if locked
mon_cmd($vmid, "blockdev-open-tray", force => JSON::true, id => "$qdev_id");
mon_cmd($vmid, "blockdev-remove-medium", id => "$qdev_id");
- detach($vmid, "drive-$qdev_id");
+ detach(vm_qmp_peer($vmid), "drive-$qdev_id");

return if $drive->{file} eq 'none';

diff --git a/src/PVE/QemuServer/VolumeChain.pm b/src/PVE/QemuServer/VolumeChain.pm
index 08acbc05..e522e201 100644
--- a/src/PVE/QemuServer/VolumeChain.pm
+++ b/src/PVE/QemuServer/VolumeChain.pm
@@ -59,7 +59,7 @@ sub blockdev_external_snapshot {
sub blockdev_delete {
my ($storecfg, $vmid, $drive, $file_blockdev, $fmt_blockdev, $snap) = @_;

- eval { PVE::QemuServer::Blockdev::detach($vmid, $fmt_blockdev->{'node-name'}); };
+ eval { PVE::QemuServer::Blockdev::detach(vm_qmp_peer($vmid), $fmt_blockdev->{'node-name'}); };
warn "detaching block node for $file_blockdev->{filename} failed - $@" if $@;

#delete the file (don't use vdisk_free as we don't want to delete all snapshot chain)
@@ -181,7 +181,7 @@ sub blockdev_replace {
}

# delete old file|fmt nodes
- eval { PVE::QemuServer::Blockdev::detach($vmid, $src_blockdev_name); };
+ eval { PVE::QemuServer::Blockdev::detach(vm_qmp_peer($vmid), $src_blockdev_name); };
warn "detaching block node for $src_snap failed - $@" if $@;
}

diff --git a/src/PVE/VZDump/QemuServer.pm b/src/PVE/VZDump/QemuServer.pm
index 48994612..ad65b5d0 100644
--- a/src/PVE/VZDump/QemuServer.pm
+++ b/src/PVE/VZDump/QemuServer.pm
@@ -631,7 +631,7 @@ my sub detach_fleecing_images {
for my $di ($disks->@*) {
if (my $volid = $di->{'fleece-volid'}) {
my $node_name = "$di->{qmdevice}-fleecing";
- eval { PVE::QemuServer::Blockdev::detach($vmid, $node_name) };
+ eval { PVE::QemuServer::Blockdev::detach(vm_qmp_peer($vmid), $node_name) };
}
}
}
--
2.47.3