all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Alexandre Derumier via pve-devel <pve-devel@lists.proxmox.com>
To: pve-devel@lists.proxmox.com
Cc: Alexandre Derumier <alexandre.derumier@groupe-cyllene.com>
Subject: [pve-devel] [PATCH qemu-server 04/13] blockdev: add support to qemu_driveadd && qemu_drivedel
Date: Tue,  3 Jun 2025 09:55:43 +0200	[thread overview]
Message-ID: <mailman.210.1748937408.395.pve-devel@lists.proxmox.com> (raw)
In-Reply-To: <20250603075558.627850-1-alexandre.derumier@groupe-cyllene.com>

[-- Attachment #1: Type: message/rfc822, Size: 9395 bytes --]

From: Alexandre Derumier <alexandre.derumier@groupe-cyllene.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH qemu-server 04/13] blockdev: add support to qemu_driveadd && qemu_drivedel
Date: Tue,  3 Jun 2025 09:55:43 +0200
Message-ID: <20250603075558.627850-9-alexandre.derumier@groupe-cyllene.com>

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@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



[-- Attachment #2: Type: text/plain, Size: 160 bytes --]

_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

  parent reply	other threads:[~2025-06-03  7:57 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20250603075558.627850-1-alexandre.derumier@groupe-cyllene.com>
2025-06-03  7:55 ` [pve-devel] [PATCH pve-qemu 1/1] add fake 10.0 Alexandre Derumier via pve-devel
2025-06-03  7:55 ` [pve-devel] [PATCH qemu-server 01/13] blockdev: cmdline: add blockdev syntax support Alexandre Derumier via pve-devel
2025-06-05 13:23   ` Fiona Ebner
2025-06-05 14:17     ` Fabian Grünbichler
2025-06-05 14:39     ` DERUMIER, Alexandre via pve-devel
     [not found]     ` <ee4940c99e9866910405b492dad15c68718c49ea.camel@groupe-cyllene.com>
2025-06-06  7:50       ` Fiona Ebner
2025-06-06  8:27         ` Fabian Grünbichler
2025-06-06  9:54           ` DERUMIER, Alexandre via pve-devel
2025-06-06  8:50   ` Fiona Ebner
2025-06-06  9:42     ` DERUMIER, Alexandre via pve-devel
     [not found]     ` <0605a27428cfb920ffefc51abdfec050a0a6b535.camel@groupe-cyllene.com>
2025-06-06 10:57       ` DERUMIER, Alexandre via pve-devel
2025-06-10 14:03   ` Fiona Ebner
2025-06-11  6:37     ` DERUMIER, Alexandre via pve-devel
     [not found]     ` <405662415075c0248618833b512b58258f80e0f7.camel@groupe-cyllene.com>
2025-06-11  7:24       ` Fiona Ebner
2025-06-11 14:02         ` DERUMIER, Alexandre via pve-devel
     [not found]         ` <733f5eefb25b76a0794a84d4f93ada8d70aea7be.camel@groupe-cyllene.com>
2025-06-11 14:13           ` Fiona Ebner
2025-06-12  5:20             ` DERUMIER, Alexandre via pve-devel
2025-06-03  7:55 ` [pve-devel] [PATCH pve-storage 1/9] common: add qemu_img_create an preallocation_cmd_option Alexandre Derumier via pve-devel
2025-06-03  7:55 ` [pve-devel] [PATCH qemu-server 02/13] blockdev: add support for ovmf && efidisk Alexandre Derumier via pve-devel
2025-06-03  7:55 ` [pve-devel] [PATCH pve-storage 2/9] common: add qemu_img_create_qcow2_backed Alexandre Derumier via pve-devel
2025-06-03  7:55 ` [pve-devel] [PATCH qemu-server 03/13] blockdev: vm_devices_list : fix block-query Alexandre Derumier via pve-devel
2025-06-17 14:44   ` Fiona Ebner
2025-06-03  7:55 ` [pve-devel] [PATCH pve-storage 3/9] common: add qemu_img_info helper Alexandre Derumier via pve-devel
2025-06-03  7:55 ` Alexandre Derumier via pve-devel [this message]
2025-06-03  7:55 ` [pve-devel] [PATCH pve-storage 4/9] common: add qemu_img_measure Alexandre Derumier via pve-devel
2025-06-03  7:55 ` [pve-devel] [PATCH qemu-server 05/13] blockdev: add support to qemu_block_set_io_throttle Alexandre Derumier via pve-devel
2025-06-03  7:55 ` [pve-devel] [PATCH pve-storage 5/9] rename_volume: add source && target snap Alexandre Derumier via pve-devel
2025-06-03  7:55 ` [pve-devel] [PATCH qemu-server 06/13] blockdev: add support for cdrom media eject/insert Alexandre Derumier via pve-devel
2025-06-03  7:55 ` [pve-devel] [PATCH pve-storage 6/9] storage: volume_snapshot: add $running param + api bump Alexandre Derumier via pve-devel
2025-06-03  7:55 ` [pve-devel] [PATCH qemu-server 07/13] blockdev: add support for block_resize Alexandre Derumier via pve-devel
2025-06-03  7:55 ` [pve-devel] [PATCH pve-storage 7/9] qcow2: add external snapshot support Alexandre Derumier via pve-devel
2025-06-03  7:55 ` [pve-devel] [PATCH qemu-server 08/13] blockdev: add support for nbd_export: block-export-add Alexandre Derumier via pve-devel
2025-06-03  7:55 ` [pve-devel] [PATCH pve-storage 8/9] lvmplugin: add qcow2 snapshot Alexandre Derumier via pve-devel
2025-06-03  7:55 ` [pve-devel] [PATCH qemu-server 09/13] blockdev: add blockdev_mirror Alexandre Derumier via pve-devel
2025-06-03  7:55 ` [pve-devel] [PATCH pve-storage 9/9] storage: add volume_support_qemu_snapshot Alexandre Derumier via pve-devel
2025-06-03  7:55 ` [pve-devel] [PATCH qemu-server 10/13] blockdev: change aio on target if io_uring is not default Alexandre Derumier via pve-devel
2025-06-03  7:55 ` [pve-devel] [PATCH qemu-server 11/13] qemu_img convert : add external snapshot support Alexandre Derumier via pve-devel
2025-06-03  7:55 ` [pve-devel] [PATCH qemu-server 12/13] blockdev: add backing_chain support Alexandre Derumier via pve-devel
2025-06-03  7:55 ` [pve-devel] [PATCH qemu-server 13/13] qcow2: add external snapshot support Alexandre Derumier via pve-devel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=mailman.210.1748937408.395.pve-devel@lists.proxmox.com \
    --to=pve-devel@lists.proxmox.com \
    --cc=alexandre.derumier@groupe-cyllene.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal