From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <pve-devel-bounces@lists.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9])
	by lore.proxmox.com (Postfix) with ESMTPS id EB0081FF15C
	for <inbox@lore.proxmox.com>; Fri, 27 Jun 2025 14:42:34 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id E637C14431;
	Fri, 27 Jun 2025 14:43:03 +0200 (CEST)
To: pve-devel@lists.proxmox.com
Date: Fri, 27 Jun 2025 14:42:25 +0200
In-Reply-To: <20250627124228.1542397-1-alexandre.derumier@groupe-cyllene.com>
References: <20250627124228.1542397-1-alexandre.derumier@groupe-cyllene.com>
MIME-Version: 1.0
Message-ID: <mailman.693.1751028182.395.pve-devel@lists.proxmox.com>
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Post: <mailto:pve-devel@lists.proxmox.com>
From: Alexandre Derumier via pve-devel <pve-devel@lists.proxmox.com>
Precedence: list
Cc: Alexandre Derumier <alexandre.derumier@groupe-cyllene.com>
X-Mailman-Version: 2.1.29
X-BeenThere: pve-devel@lists.proxmox.com
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
Reply-To: Proxmox VE development discussion <pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
Subject: [pve-devel] [PATCH qemu-server 3/6] convert drive device to json
 format
Content-Type: multipart/mixed; boundary="===============8510108529634968349=="
Errors-To: pve-devel-bounces@lists.proxmox.com
Sender: "pve-devel" <pve-devel-bounces@lists.proxmox.com>

--===============8510108529634968349==
Content-Type: message/rfc822
Content-Disposition: inline

Return-Path: <root@formationkvm1.odiso.net>
X-Original-To: pve-devel@lists.proxmox.com
Delivered-To: pve-devel@lists.proxmox.com
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (2048 bits))
	(No client certificate requested)
	by lists.proxmox.com (Postfix) with ESMTPS id 61BA2D1FCC
	for <pve-devel@lists.proxmox.com>; Fri, 27 Jun 2025 14:43:02 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id 3D098141EF
	for <pve-devel@lists.proxmox.com>; Fri, 27 Jun 2025 14:42:32 +0200 (CEST)
Received: from bastiontest.odiso.net (unknown [IPv6:2a0a:1580:2000:6700::14])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (2048 bits))
	(No client certificate requested)
	by firstgate.proxmox.com (Proxmox) with ESMTPS
	for <pve-devel@lists.proxmox.com>; Fri, 27 Jun 2025 14:42:30 +0200 (CEST)
Received: from formationkvm1.odiso.net (unknown [10.11.201.57])
	by bastiontest.odiso.net (Postfix) with ESMTP id D4CF2862E4C;
	Fri, 27 Jun 2025 14:42:29 +0200 (CEST)
Received: by formationkvm1.odiso.net (Postfix, from userid 0)
	id CC0FE115F57B; Fri, 27 Jun 2025 14:42:29 +0200 (CEST)
From: Alexandre Derumier <alexandre.derumier@groupe-cyllene.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH qemu-server 3/6] convert drive device to json format
Date: Fri, 27 Jun 2025 14:42:25 +0200
Message-Id: <20250627124228.1542397-4-alexandre.derumier@groupe-cyllene.com>
X-Mailer: git-send-email 2.39.5
In-Reply-To: <20250627124228.1542397-1-alexandre.derumier@groupe-cyllene.com>
References: <20250627124228.1542397-1-alexandre.derumier@groupe-cyllene.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  1
	AWL                    -0.894 Adjusted score from AWL reputation of From: address
	BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
	DMARC_NONE                0.1 DMARC none policy
	HEADER_FROM_DIFFERENT_DOMAINS  0.001 From and EnvelopeFrom 2nd level mail domains are different
	KAM_DMARC_NONE           0.25 DKIM has Failed or SPF has failed on the message and the domain has no DMARC policy
	KAM_DMARC_STATUS         0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
	KAM_LAZY_DOMAIN_SECURITY      1 Sending domain does not have any anti-forgery methods
	KAM_MAILER                  2 Automated Mailer Tag Left in Email
	RDNS_NONE               0.793 Delivered to internal network by a host with no rDNS
	SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
	SPF_NONE                0.001 SPF: sender does not publish an SPF Record
	URIBL_BLOCKED           0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked.  See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [qemuserver.pm,drivedevice.pm]

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

diff --git a/src/PVE/QemuServer.pm b/src/PVE/QemuServer.pm
index a5b5ad33..035034ec 100644
--- a/src/PVE/QemuServer.pm
+++ b/src/PVE/QemuServer.pm
@@ -72,7 +72,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;
@@ -81,7 +81,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;
@@ -3504,10 +3504,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 });
         },
     );
 
@@ -3736,10 +3734,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); };
@@ -3758,9 +3756,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 $@;
@@ -3829,7 +3827,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);
@@ -3839,7 +3837,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 1e875cb9..103180f3 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,28 @@ 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->{bus} = $pciaddr->{bus};
+	$device->{addr} = $pciaddr->{addr};
+	$device->{driver} = 'virtio-blk-pci';
 	# 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->{id} = $drive_id;
+	$device->{iothread} = "iothread-$drive_id" if $drive->{iothread};
     } elsif ($drive->{interface} eq 'scsi') {
 
 	my ($maxdev, $controller, $controller_prefix) = scsihw_infos($conf, $drive);
@@ -67,30 +71,34 @@ sub print_drivedevice_full {
 	my $device_type =
 	    PVE::QemuServer::Drive::get_scsi_device_type($drive, $storecfg, $machine_version);
 
+	$device->{driver} = "scsi-$device_type";
+	$device->{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";
+	$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;
 	    }
 	}
 
@@ -111,27 +119,28 @@ sub print_drivedevice_full {
 
 	my $device_type = ($drive->{media} && $drive->{media} eq 'cdrom') ? "cd" : "hd";
 
-	$device = "ide-$device_type";
+	$device->{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";
+	$drive->{id} = $drive_id;
 
 	if ($device_type eq 'hd') {
 	    if (my $model = $drive->{model}) {
 		$model = URI::Escape::uri_unescape($model);
-		$device .= ",model=$model";
+		$device->{model} = $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 +148,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 +160,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



--===============8510108529634968349==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

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

--===============8510108529634968349==--