[PATCH qemu-server 04/13] blockdev: add support to qemu_driveadd && qemu_drivedel
Alexandre Derumier
alexandre.derumier at groupe-cyllene.com
Tue Jun 3 09:55:43 CEST 2025
fixme:
- backup seem to use a tpmstate0-backup drive. Not sure how it's works, but
I think it could be converted to blockdev too
- verify drivedel with fleecing temp image
Signed-off-by: Alexandre Derumier <alexandre.derumier at groupe-cyllene.com>
---
PVE/QemuServer.pm | 93 ++++++++++++++++++++++++++++++--------
PVE/QemuServer/Blockdev.pm | 1 +
2 files changed, 74 insertions(+), 20 deletions(-)
diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index b15b05aa..fa072fca 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -52,7 +52,7 @@ use PVE::QMPClient;
use PVE::QemuConfig;
use PVE::QemuConfig::NoWrite;
use PVE::QemuServer::Helpers qw(config_aware_timeout min_version kvm_user_version windows_version);
-use PVE::QemuServer::Blockdev qw(generate_drive_blockdev);
+use PVE::QemuServer::Blockdev qw(generate_drive_blockdev generate_throttle_group);
use PVE::QemuServer::Cloudinit;
use PVE::QemuServer::CGroup;
use PVE::QemuServer::CPUConfig qw(print_cpu_device get_cpu_options get_cpu_bitness is_native_arch get_amd_sev_object get_amd_sev_type);
@@ -4130,7 +4130,7 @@ sub vm_deviceplug {
qemu_deviceadd($vmid, $devicefull);
eval { qemu_deviceaddverify($vmid, $deviceid); };
if (my $err = $@) {
- eval { qemu_drivedel($vmid, $deviceid); };
+ eval { qemu_drivedel($vmid, $deviceid, $device); };
warn $@ if $@;
die $err;
}
@@ -4159,7 +4159,7 @@ sub vm_deviceplug {
my $devicefull = print_drivedevice_full($storecfg, $conf, $vmid, $device, undef, $arch, $machine_type);
eval { qemu_deviceadd($vmid, $devicefull); };
if (my $err = $@) {
- eval { qemu_drivedel($vmid, $deviceid); };
+ eval { qemu_drivedel($vmid, $deviceid, $device); };
warn $@ if $@;
die $err;
}
@@ -4220,7 +4220,7 @@ sub vm_deviceunplug {
qemu_devicedel($vmid, $deviceid);
qemu_devicedelverify($vmid, $deviceid);
- qemu_drivedel($vmid, $deviceid);
+ qemu_drivedel($vmid, $deviceid, $device);
qemu_iothread_del($vmid, $deviceid, $device);
} elsif ($deviceid =~ m/^(virtioscsi|scsihw)(\d+)$/) {
qemu_devicedel($vmid, $deviceid);
@@ -4230,7 +4230,7 @@ sub vm_deviceunplug {
qemu_devicedel($vmid, $deviceid);
qemu_devicedelverify($vmid, $deviceid);
- qemu_drivedel($vmid, $deviceid);
+ qemu_drivedel($vmid, $deviceid, $device);
qemu_deletescsihw($conf, $vmid, $deviceid);
qemu_iothread_del($vmid, "virtioscsi$device->{index}", $device)
@@ -4281,30 +4281,83 @@ sub qemu_iothread_del {
sub qemu_driveadd {
my ($storecfg, $vmid, $device) = @_;
- my $kvmver = get_running_qemu_version($vmid);
- my $io_uring = min_version($kvmver, 6, 0);
- my $drive = print_drive_commandline_full($storecfg, $vmid, $device, undef, $io_uring);
- $drive =~ s/\\/\\\\/g;
- my $ret = PVE::QemuServer::Monitor::hmp_cmd($vmid, "drive_add auto \"$drive\"", 60);
+ my $machine_type = PVE::QemuServer::Machine::get_current_qemu_machine($vmid);
+
+ if (PVE::QemuServer::Machine::is_machine_version_at_least($machine_type, 10, 0)) {
+
+ my $drive_id = PVE::QemuServer::Drive::get_drive_id($device);
+ # always add a throttle-group, as it's mandatory for the throttle-filter root node.
+ my $throttle_group = generate_throttle_group($device);
+ mon_cmd($vmid, 'object-add', "qom-type" => "throttle-group", %$throttle_group);
+
+ # The throttle filter is the root node with a stable name attached to the device,
+ # and currently it's not possible to insert it later
+ my $blockdev = generate_drive_blockdev($storecfg, $device);
+ mon_cmd($vmid, 'blockdev-add', %$blockdev, timeout => 10 * 60);
+ return 1;
+
+ } else {
+
+ my $kvmver = get_running_qemu_version($vmid);
+ my $io_uring = min_version($kvmver, 6, 0);
+ my $drive = print_drive_commandline_full($storecfg, $vmid, $device, undef, $io_uring);
+ $drive =~ s/\\/\\\\/g;
+ my $ret = PVE::QemuServer::Monitor::hmp_cmd($vmid, "drive_add auto \"$drive\"", 60);
+
+ # If the command succeeds qemu prints: "OK"
+ return 1 if $ret =~ m/OK/s;
+
+ die "adding drive failed: $ret\n";
+ }
+}
- # If the command succeeds qemu prints: "OK"
- return 1 if $ret =~ m/OK/s;
- die "adding drive failed: $ret\n";
+my sub qemu_drivedel_backingchain;
+sub qemu_drivedel_backingchain {
+ my ($fmt_node, $vmid) = @_;
+
+ qemu_drivedel_backingchain($fmt_node->{backing}, $vmid) if $fmt_node->{backing};
+
+ eval { mon_cmd($vmid, 'blockdev-del', 'node-name' => $fmt_node->{'node-name'}); };
+ my $file_node = $fmt_node->{file};
+ eval { mon_cmd($vmid, 'blockdev-del', 'node-name' => $file_node->{'node-name'}); };
}
sub qemu_drivedel {
- my ($vmid, $deviceid) = @_;
+ my ($vmid, $deviceid, $device) = @_;
+
+ my $machine_type = PVE::QemuServer::Machine::get_current_qemu_machine($vmid);
- my $ret = PVE::QemuServer::Monitor::hmp_cmd($vmid, "drive_del drive-$deviceid", 10 * 60);
- $ret =~ s/^\s+//;
+ if (PVE::QemuServer::Machine::is_machine_version_at_least($machine_type, 10, 0)) {
+ #remove top-node
+ eval { mon_cmd($vmid, 'blockdev-del', 'node-name' => "drive-$deviceid", timeout => 10 * 60); };
+ die "deleting blockdev $deviceid failed : $@\n" if $@;
- return 1 if $ret eq "";
+ eval { mon_cmd($vmid, 'object-del', id => "throttle-drive-$deviceid"); };
+ die "deleting throttle group throttle-drive-$deviceid failed : $@\n" if $@;
- # NB: device not found errors mean the drive was auto-deleted and we ignore the error
- return 1 if $ret =~ m/Device \'.*?\' not found/s;
+ #qemu auto-remove fmt && file-node without backing chain
+ #fixme: backup|fleecing temp drivedel don't pass $device currently, maybe it's enough
+ #as they don't have backing chain
+ if($device) {
+ my $storecfg = PVE::Storage::config();
+ my $blockdev = generate_drive_blockdev($storecfg, $device);
+ my $fmt_node = $blockdev->{file};
+ qemu_drivedel_backingchain($fmt_node, $vmid);
+ }
+
+ } else {
+
+ my $ret = PVE::QemuServer::Monitor::hmp_cmd($vmid, "drive_del drive-$deviceid", 10 * 60);
+ $ret =~ s/^\s+//;
- die "deleting drive $deviceid failed : $ret\n";
+ return 1 if $ret eq "";
+
+ # NB: device not found errors mean the drive was auto-deleted and we ignore the error
+ return 1 if $ret =~ m/Device \'.*?\' not found/s;
+
+ die "deleting drive $deviceid failed : $ret\n";
+ }
}
sub qemu_deviceaddverify {
diff --git a/PVE/QemuServer/Blockdev.pm b/PVE/QemuServer/Blockdev.pm
index b4dd1ef5..01fef66d 100644
--- a/PVE/QemuServer/Blockdev.pm
+++ b/PVE/QemuServer/Blockdev.pm
@@ -10,6 +10,7 @@ use base qw(Exporter);
our @EXPORT_OK = qw(
generate_drive_blockdev
+generate_throttle_group
);
sub encode_base62 {
--
2.39.5
More information about the pve-devel
mailing list