public inbox for pve-devel@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 06/10] convert drive device to json format
Date: Wed,  2 Jul 2025 16:48:56 +0200	[thread overview]
Message-ID: <mailman.806.1751467755.395.pve-devel@lists.proxmox.com> (raw)
In-Reply-To: <20250702144900.3963405-1-alexandre.derumier@groupe-cyllene.com>

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

From: Alexandre Derumier <alexandre.derumier@groupe-cyllene.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH qemu-server 06/10] convert drive device to json format
Date: Wed,  2 Jul 2025 16:48:56 +0200
Message-ID: <20250702144900.3963405-7-alexandre.derumier@groupe-cyllene.com>

Signed-off-by: Alexandre Derumier <alexandre.derumier@groupe-cyllene.com>
---
 src/PVE/QemuServer.pm             | 26 ++++++------
 src/PVE/QemuServer/DriveDevice.pm | 66 +++++++++++++++----------------
 2 files changed, 45 insertions(+), 47 deletions(-)

diff --git a/src/PVE/QemuServer.pm b/src/PVE/QemuServer.pm
index 215c070d..5ec1842c 100644
--- a/src/PVE/QemuServer.pm
+++ b/src/PVE/QemuServer.pm
@@ -73,7 +73,7 @@ use PVE::QemuServer::Drive qw(
     print_drive
     storage_allows_io_uring_default
 );
-use PVE::QemuServer::DriveDevice qw(print_drivedevice_controller print_drivedevice_full print_drivedevice_iothread scsihw_infos);
+use PVE::QemuServer::DriveDevice qw(print_drivedevice_controller get_drivedevice print_drivedevice_iothread scsihw_infos);
 use PVE::QemuServer::Machine;
 use PVE::QemuServer::Memory qw(get_current_memory);
 use PVE::QemuServer::MetaInfo;
@@ -82,7 +82,7 @@ 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);
 use PVE::QemuServer::QemuImage;
-use PVE::QemuServer::QMPHelpers qw(qemu_deviceadd qemu_devicedel qemu_objectadd qemu_objectdel);
+use PVE::QemuServer::QMPHelpers qw(qemu_deviceadd qemu_devicedel qemu_objectadd qemu_objectdel qmp_deviceadd qmp_devicedel);
 use PVE::QemuServer::RNG qw(parse_rng print_rng_device_commandline print_rng_object_commandline);
 use PVE::QemuServer::RunState;
 use PVE::QemuServer::StateFile;
@@ -3507,10 +3507,8 @@ sub config_to_command {
                 push @$devices, '-drive', $drive_cmd;
             }
 
-            push @$devices, '-device',
-                print_drivedevice_full(
-                    $storecfg, $conf, $vmid, $drive, $bridges, $arch, $machine_type,
-                );
+            my $drive_device = get_drivedevice($storecfg, $conf, $vmid, $drive, $bridges, $arch, $machine_type);
+            push @$devices, '-device', to_json($drive_device, { canonical => 1 });
         },
     );
 
@@ -3733,10 +3731,10 @@ sub vm_deviceplug {
         qemu_iothread_add($vmid, $deviceid, $device);
 
         qemu_driveadd($storecfg, $vmid, $device);
-        my $devicefull =
-            print_drivedevice_full($storecfg, $conf, $vmid, $device, undef, $arch, $machine_type);
+        my $drive_device =
+            get_drivedevice($storecfg, $conf, $vmid, $device, undef, $arch, $machine_type);
 
-        qemu_deviceadd($vmid, $devicefull);
+        qmp_deviceadd($vmid, $drive_device);
         eval { qemu_deviceaddverify($vmid, $deviceid); };
         if (my $err = $@) {
             eval { qemu_drivedel($vmid, $deviceid); };
@@ -3755,9 +3753,9 @@ sub vm_deviceplug {
         qemu_findorcreatescsihw($storecfg, $conf, $vmid, $device, $arch, $machine_type);
         qemu_driveadd($storecfg, $vmid, $device);
 
-        my $devicefull =
-            print_drivedevice_full($storecfg, $conf, $vmid, $device, undef, $arch, $machine_type);
-        eval { qemu_deviceadd($vmid, $devicefull); };
+        my $drive_device =
+            get_drivedevice($storecfg, $conf, $vmid, $device, undef, $arch, $machine_type);
+        eval { qmp_deviceadd($vmid, $drive_device); };
         if (my $err = $@) {
             eval { qemu_drivedel($vmid, $deviceid); };
             warn $@ if $@;
@@ -3826,7 +3824,7 @@ sub vm_deviceunplug {
     } elsif ($deviceid =~ m/^(virtio)(\d+)$/) {
         my $device = parse_drive($deviceid, $conf->{$deviceid});
 
-        qemu_devicedel($vmid, $deviceid);
+        qmp_devicedel($vmid, $deviceid);
         qemu_devicedelverify($vmid, $deviceid);
         qemu_drivedel($vmid, $deviceid);
         qemu_iothread_del($vmid, $deviceid, $device);
@@ -3836,7 +3834,7 @@ sub vm_deviceunplug {
     } elsif ($deviceid =~ m/^(scsi)(\d+)$/) {
         my $device = parse_drive($deviceid, $conf->{$deviceid});
 
-        qemu_devicedel($vmid, $deviceid);
+        qmp_devicedel($vmid, $deviceid);
         qemu_devicedelverify($vmid, $deviceid);
         qemu_drivedel($vmid, $deviceid);
         qemu_deletescsihw($conf, $vmid, $deviceid);
diff --git a/src/PVE/QemuServer/DriveDevice.pm b/src/PVE/QemuServer/DriveDevice.pm
index 0e1b54a1..bc9deab3 100644
--- a/src/PVE/QemuServer/DriveDevice.pm
+++ b/src/PVE/QemuServer/DriveDevice.pm
@@ -8,12 +8,12 @@ use URI::Escape;
 use PVE::QemuServer::Drive qw (drive_is_cdrom);
 use PVE::QemuServer::Helpers qw(kvm_user_version min_version);
 use PVE::QemuServer::Machine;
-use PVE::QemuServer::PCI qw(print_pci_addr);
+use PVE::QemuServer::PCI qw(get_pci_addr print_pci_addr);
 
 use base qw(Exporter);
 
 our @EXPORT_OK = qw(
-    print_drivedevice_full
+    get_drivedevice
     print_drivedevice_controller
     print_drivedevice_iothread
     scsihw_infos
@@ -41,24 +41,25 @@ sub scsihw_infos {
     return ($maxdev, $controller, $controller_prefix);
 }
 
-sub print_drivedevice_full {
+sub get_drivedevice {
     my ($storecfg, $conf, $vmid, $drive, $bridges, $arch, $machine_type) = @_;
 
-    my $device = '';
+    my $device = {};
+
     my $maxdev = 0;
 
     my $machine_version = PVE::QemuServer::Machine::extract_version($machine_type, kvm_user_version());
 
     my $drive_id = PVE::QemuServer::Drive::get_drive_id($drive);
+
     if ($drive->{interface} eq 'virtio') {
-	my $pciaddr = print_pci_addr("$drive_id", $bridges, $arch);
-	$device = 'virtio-blk-pci';
+	my $pciaddr = get_pci_addr("$drive_id", $bridges, $arch);
+	$device = { id => $drive_id , driver => 'virtio-blk-pci', bus => $pciaddr->{bus}, addr => $pciaddr->{addr} };
 	# for the switch to -blockdev, there is no blockdev for 'none'
 	if (!min_version($machine_version, 10, 0) || $drive->{file} ne 'none') {
-	    $device .= ",drive=drive-$drive_id";
+	    $device->{drive} = "drive-$drive_id";
 	}
-	$device .= ",id=${drive_id}${pciaddr}";
-	$device .= ",iothread=iothread-$drive_id" if $drive->{iothread};
+	$device->{iothread} = "iothread-$drive_id" if $drive->{iothread};
     } elsif ($drive->{interface} eq 'scsi') {
 
 	my ($maxdev, $controller, $controller_prefix) = scsihw_infos($conf, $drive);
@@ -67,30 +68,32 @@ sub print_drivedevice_full {
 	my $device_type =
 	    PVE::QemuServer::Drive::get_scsi_device_type($drive, $storecfg, $machine_version);
 
+	$device = { id => $drive_id, driver => "scsi-$device_type", bus => "$controller_prefix$controller.0" };
+
 	if (!$conf->{scsihw} || $conf->{scsihw} =~ m/^lsi/ || $conf->{scsihw} eq 'pvscsi') {
-	    $device = "scsi-$device_type,bus=$controller_prefix$controller.0,scsi-id=$unit";
+	    $device->{'scsi-id'} = $unit;
 	} else {
-	    $device = "scsi-$device_type,bus=$controller_prefix$controller.0,channel=0,scsi-id=0"
-		. ",lun=$drive->{index}";
+	    $device->{'scsi-id'} = 0;
+	    $device->{channel} = 0;
+	    $device->{lun} = int($drive->{index});
 	}
 	# for the switch to -blockdev, there is no blockdev for 'none'
 	if (!min_version($machine_version, 10, 0) || $drive->{file} ne 'none') {
-	    $device .= ",drive=drive-$drive_id";
+	    $device->{drive} = "drive-$drive_id";
 	}
-	$device .= ",id=$drive_id";
 
 	if ($drive->{ssd} && ($device_type eq 'block' || $device_type eq 'hd')) {
-	    $device .= ",rotation_rate=1";
+	    $device->{'rotation-rate'} = 1;
 	}
-	$device .= ",wwn=$drive->{wwn}" if $drive->{wwn};
+	$device->{wwn} = $drive->{wwn} if $drive->{wwn};
 
 	# only scsi-hd and scsi-cd support passing vendor and product information
 	if ($device_type eq 'hd' || $device_type eq 'cd') {
 	    if (my $vendor = $drive->{vendor}) {
-		$device .= ",vendor=$vendor";
+		$device->{vendor} = $vendor;
 	    }
 	    if (my $product = $drive->{product}) {
-		$device .= ",product=$product";
+		$device->{product} = $product;
 	    }
 	}
 
@@ -110,28 +113,26 @@ sub print_drivedevice_full {
 	}
 
 	my $device_type = ($drive->{media} && $drive->{media} eq 'cdrom') ? "cd" : "hd";
-
-	$device = "ide-$device_type";
+	$device = { id => $drive_id , driver => "ide-$device_type" };
 	if ($drive->{interface} eq 'ide') {
-	    $device .= ",bus=ide.$controller,unit=$unit";
+	    $device->{bus} = "ide.$controller";
+	    $device->{unit} = $unit;
 	} else {
-	    $device .= ",bus=ahci$controller.$unit";
+	    $device->{bus} = "ahci$controller.$unit";
 	}
 	if (!min_version($machine_version, 10, 0) || $drive->{file} ne 'none') {
-	    $device .= ",drive=drive-$drive_id";
+	    $device->{drive} = "drive-$drive_id";
 	}
-	$device .= ",id=$drive_id";
 
 	if ($device_type eq 'hd') {
 	    if (my $model = $drive->{model}) {
-		$model = URI::Escape::uri_unescape($model);
-		$device .= ",model=$model";
+		$device->{model} = URI::Escape::uri_unescape($model);
 	    }
 	    if ($drive->{ssd}) {
-		$device .= ",rotation_rate=1";
+		$device->{'rotation-rate'} = 1;
 	    }
 	}
-	$device .= ",wwn=$drive->{wwn}" if $drive->{wwn};
+	$device->{wwn} = $drive->{wwn} if $drive->{wwn};
     } elsif ($drive->{interface} eq 'usb') {
 	die "implement me";
 	#  -device ide-drive,bus=ide.1,unit=0,drive=drive-ide0-1-0,id=ide0-1-0
@@ -139,11 +140,10 @@ sub print_drivedevice_full {
 	die "unsupported interface type";
     }
 
-    $device .= ",bootindex=$drive->{bootindex}" if $drive->{bootindex};
+    $device->{bootindex} = $drive->{bootindex} if $drive->{bootindex};
 
     if (my $serial = $drive->{serial}) {
-	$serial = URI::Escape::uri_unescape($serial);
-	$device .= ",serial=$serial";
+	$device->{serial} = URI::Escape::uri_unescape($serial);
     }
 
     if (min_version($machine_version, 10, 0)) { # for the switch to -blockdev
@@ -152,10 +152,10 @@ sub print_drivedevice_full {
 	    if (my $cache = $drive->{cache}) {
 		$write_cache = 'off' if $cache eq 'writethrough' || $cache eq 'directsync';
 	    }
-	    $device .= ",write-cache=$write_cache";
+	    $device->{'write-cache'} = $write_cache;
 	}
 	for my $o (qw(rerror werror)) {
-	    $device .= ",$o=$drive->{$o}" if defined($drive->{$o});
+	    $device->{$o} = $drive->{$o} if defined($drive->{$o});
 	}
     }
 
-- 
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-07-02 14:49 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20250702144900.3963405-1-alexandre.derumier@groupe-cyllene.com>
2025-07-02 14:48 ` [pve-devel] [PATCH qemu-server 01/10] introduce DriveDevice module Alexandre Derumier via pve-devel
2025-07-02 14:48 ` [pve-devel] [PATCH qemu-server 02/10] add print_drivedevice_controller && print_drivedevice_iothread Alexandre Derumier via pve-devel
2025-07-02 14:48 ` [pve-devel] [PATCH qemu-server 03/10] hotplug: drive controller : use print_drivedevice_iothread && print_drivedevice_controller Alexandre Derumier via pve-devel
2025-07-02 14:48 ` [pve-devel] [PATCH qemu-server 04/10] pci: add get_pci_addr Alexandre Derumier via pve-devel
2025-07-02 14:48 ` [pve-devel] [PATCH qemu-server 05/10] qmphelpers: add qmp_deviceadd && qmp_devicedel Alexandre Derumier via pve-devel
2025-07-02 14:48 ` Alexandre Derumier via pve-devel [this message]
2025-07-02 14:48 ` [pve-devel] [PATCH qemu-server 07/10] convert iothread to json Alexandre Derumier via pve-devel
2025-07-02 14:48 ` [pve-devel] [PATCH qemu-server 08/10] convert disk controller device to json format Alexandre Derumier via pve-devel
2025-07-02 14:48 ` [pve-devel] [PATCH qemu-server 09/10] tests: cfg2cmd: convert drive devices " Alexandre Derumier via pve-devel
2025-07-02 14:49 ` [pve-devel] [PATCH qemu-server 10/10] RFC: add multiple iothreads 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.806.1751467755.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal