From owner-root Sat Apr 19 16:27:10 2025 Message-ID: <1745080030.onrvLew8G0XY@netbsd.ax> Date: Sat, 19 Apr 2025 19:27:10 +0300 From: kim@netbsd.org (Kimmo Suominen) To: whom-it-may-concern:; Subject: Use LUN zero for virtio-scsi-single drives Not all SCSI subsystems can readily deal with a sparse LUN space. For example, NetBSD and OpenBSD will stop scanning further LUNs when a LUN reports either "NOT PRESENT" or "NO DEVICE". Qemu, when configured to use a disk with a LUN higher than 0 and with nothing configured on LUN 0 of the same target, reports both flags for LUN 0, resulting in the disk not being attached. (FreeBSD appears to always scan up to 8 LUNs.) Without this patch, a virtual machine configured in Proxmox with the `virtio-scsi-single` controller and three SCSI drives would end up with the following drives, each on its own SCSI bus: *------- Host Adapter ID (could be numbers other than those shown) | +----- Channel (hard-coded to zero) | | +--- Target (hard-coded to zero) | | | +- LUN (NOT hard-coded to zero; uses `$drive->{index}`) 7:0:0:0 8:0:0:1 9:0:0:2 This is because `$drive->{index}` is used as the LUN number. As can be observed, the targets for the higher drive numbers have an arbitrary gap in LUN numbering. Normal SCSI hardware is always required to implement LUN 0 to at least report the number of LUNs on the target. It is unusual that there would be gaps in the LUN numbering of available (and present) LUNs. The following patch hard-codes the `virtio-scsi-single` drive to always use LUN 0. This results in the drives being attached successfully on all operating systems. --- /usr/share/perl5/PVE/QemuServer.pm.orig 2025-04-19 19:21:19.704952303 +0300 +++ /usr/share/perl5/PVE/QemuServer.pm 2025-04-19 20:17:37.000000000 +0300 @@ -1324,8 +1324,12 @@ if (!$conf->{scsihw} || $conf->{scsihw} =~ m/^lsi/ || $conf->{scsihw} eq 'pvscsi') { $device = "scsi-$device_type,bus=$controller_prefix$controller.0,scsi-id=$unit"; } else { - $device = "scsi-$device_type,bus=$controller_prefix$controller.0,channel=0,scsi-id=0" - .",lun=$drive->{index}"; + $device = "scsi-$device_type,bus=$controller_prefix$controller.0,channel=0,scsi-id=0"; + if ($conf->{scsihw} eq 'virtio-scsi-single') { + $device .= ",lun=0"; + } else { + $device .= ",lun=$drive->{index}"; + } } $device .= ",drive=drive-$drive_id,id=$drive_id"; > dpkg -S /usr/share/perl5/PVE/QemuServer.pm qemu-server: /usr/share/perl5/PVE/QemuServer.pm > dpkg -s qemu-server | grep ^Version: Version: 8.3.12 I'm releasing the code I wrote for the above patch to the public domain. Please note that the original code patched may be subject to copyright and a license. Kind regards, Kimmo Suominen