[PATCH qemu-server 02/10] add print_drivedevice_controller && print_drivedevice_iothread

Alexandre Derumier alexandre.derumier at groupe-cyllene.com
Wed Jul 2 16:48:52 CEST 2025


Signed-off-by: Alexandre Derumier <alexandre.derumier at groupe-cyllene.com>
---
 src/PVE/QemuServer.pm             | 60 +++----------------------
 src/PVE/QemuServer/DriveDevice.pm | 75 +++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+), 54 deletions(-)

diff --git a/src/PVE/QemuServer.pm b/src/PVE/QemuServer.pm
index eed56b0d..6b26c7d3 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_full scsihw_infos);
+use PVE::QemuServer::DriveDevice qw(print_drivedevice_controller print_drivedevice_full print_drivedevice_iothread scsihw_infos);
 use PVE::QemuServer::Machine;
 use PVE::QemuServer::Memory qw(get_current_memory);
 use PVE::QemuServer::MetaInfo;
@@ -3444,8 +3444,7 @@ sub config_to_command {
         push @$devices, '-watchdog-action', $wdopts->{action} if $wdopts->{action};
     }
 
-    my $scsicontroller = {};
-    my $ahcicontroller = {};
+    my $used_controller = {};
     my $scsihw = defined($conf->{scsihw}) ? $conf->{scsihw} : $defaults->{scsihw};
 
     # Add iscsi initiator name if available
@@ -3467,58 +3466,11 @@ sub config_to_command {
 
             $drive->{bootindex} = $bootorder->{$ds} if $bootorder->{$ds};
 
-            if ($drive->{interface} eq 'virtio') {
-                push @$cmd, '-object', "iothread,id=iothread-$ds" if $drive->{iothread};
-            }
-
-            if ($drive->{interface} eq 'scsi') {
-
-                my ($maxdev, $controller, $controller_prefix) = scsihw_infos($conf, $drive);
-
-                die
-                    "scsi$drive->{index}: machine version 4.1~pve2 or higher is required to use more than 14 SCSI disks\n"
-                    if $drive->{index} > 13 && !&$version_guard(4, 1, 2);
-
-                my $pciaddr = print_pci_addr("$controller_prefix$controller", $bridges, $arch);
-                my $scsihw_type =
-                    $scsihw =~ m/^virtio-scsi-single/ ? "virtio-scsi-pci" : $scsihw;
-
-                my $iothread = '';
-                if (
-                    $conf->{scsihw}
-                    && $conf->{scsihw} eq "virtio-scsi-single"
-                    && $drive->{iothread}
-                ) {
-                    $iothread .= ",iothread=iothread-$controller_prefix$controller";
-                    push @$cmd, '-object', "iothread,id=iothread-$controller_prefix$controller";
-                } elsif ($drive->{iothread}) {
-                    log_warn(
-                        "iothread is only valid with virtio disk or virtio-scsi-single controller, ignoring\n"
-                    );
-                }
-
-                my $queues = '';
-                if (
-                    $conf->{scsihw}
-                    && $conf->{scsihw} eq "virtio-scsi-single"
-                    && $drive->{queues}
-                ) {
-                    $queues = ",num_queues=$drive->{queues}";
-                }
+            my $iothread = print_drivedevice_iothread($conf, $drive);
+            push @$cmd, '-object', $iothread if $iothread;
 
-                push @$devices, '-device',
-                    "$scsihw_type,id=$controller_prefix$controller$pciaddr$iothread$queues"
-                    if !$scsicontroller->{$controller};
-                $scsicontroller->{$controller} = 1;
-            }
-
-            if ($drive->{interface} eq 'sata') {
-                my $controller = int($drive->{index} / $PVE::QemuServer::Drive::MAX_SATA_DISKS);
-                my $pciaddr = print_pci_addr("ahci$controller", $bridges, $arch);
-                push @$devices, '-device', "ahci,id=ahci$controller,multifunction=on$pciaddr"
-                    if !$ahcicontroller->{$controller};
-                $ahcicontroller->{$controller} = 1;
-            }
+            my $controller = print_drivedevice_controller($conf, $drive, $scsihw, $arch, $bridges, $used_controller);
+            push @$devices, '-device', $controller if $controller;
 
             my $live_restore = $live_restore_backing->{$ds};
 
diff --git a/src/PVE/QemuServer/DriveDevice.pm b/src/PVE/QemuServer/DriveDevice.pm
index 764afc93..0e1b54a1 100644
--- a/src/PVE/QemuServer/DriveDevice.pm
+++ b/src/PVE/QemuServer/DriveDevice.pm
@@ -14,6 +14,8 @@ use base qw(Exporter);
 
 our @EXPORT_OK = qw(
     print_drivedevice_full
+    print_drivedevice_controller
+    print_drivedevice_iothread
     scsihw_infos
 );
 
@@ -160,4 +162,77 @@ sub print_drivedevice_full {
     return $device;
 }
 
+sub print_drivedevice_controller {
+    my ($conf, $drive, $scsihw, $arch, $bridges, $used_controller) = @_;
+
+    if ($drive->{interface} eq 'scsi') {
+
+	my ($maxdev, $controller, $controller_prefix) = scsihw_infos($conf, $drive);
+	return if $used_controller->{scsi}->{$controller};
+
+	my $pciaddr = print_pci_addr("$controller_prefix$controller", $bridges, $arch);
+	my $scsihw_type =
+	    $scsihw =~ m/^virtio-scsi-single/ ? "virtio-scsi-pci" : $scsihw;
+
+	my $iothread = '';
+	if (
+	    $conf->{scsihw}
+	    && $conf->{scsihw} eq "virtio-scsi-single"
+	    && $drive->{iothread}
+	) {
+	    $iothread .= ",iothread=iothread-$controller_prefix$controller";
+	}
+
+	my $queues = '';
+	if (
+	    $conf->{scsihw}
+	    && $conf->{scsihw} eq "virtio-scsi-single"
+	    && $drive->{queues}
+	) {
+	    $queues = ",num_queues=$drive->{queues}";
+	}
+
+	$used_controller->{scsi}->{$controller} = 1;
+	return "$scsihw_type,id=$controller_prefix$controller$pciaddr$iothread$queues";
+    }
+
+    if ($drive->{interface} eq 'sata') {
+	my $controller = int($drive->{index} / $PVE::QemuServer::Drive::MAX_SATA_DISKS);
+	return if $used_controller->{ahci}->{$controller};
+
+	my $pciaddr = print_pci_addr("ahci$controller", $bridges, $arch);
+
+	$used_controller->{ahci}->{$controller} = 1;
+	return "ahci,id=ahci$controller,multifunction=on$pciaddr";
+
+    }
+}
+
+sub print_drivedevice_iothread {
+    my ($conf, $drive) = @_;
+
+    my $drive_id = PVE::QemuServer::Drive::get_drive_id($drive);
+
+    if ($drive->{interface} eq 'virtio') {
+	return "iothread,id=iothread-$drive_id" if $drive->{iothread};
+    }
+
+    if ($drive->{interface} eq 'scsi') {
+
+	my ($maxdev, $controller, $controller_prefix) = scsihw_infos($conf, $drive);
+
+	if (
+	    $conf->{scsihw}
+	    && $conf->{scsihw} eq "virtio-scsi-single"
+	    && $drive->{iothread}
+	) {
+	    return "iothread,id=iothread-$controller_prefix$controller";
+	} elsif ($drive->{iothread}) {
+	    log_warn(
+		"iothread is only valid with virtio disk or virtio-scsi-single controller, ignoring\n"
+	    );
+	}
+    }
+}
+
 1;
-- 
2.39.5




More information about the pve-devel mailing list