From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: <h.duerr@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 9A7B5A0E9F for <pve-devel@lists.proxmox.com>; Fri, 10 Nov 2023 10:34:34 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 841461D9AA for <pve-devel@lists.proxmox.com>; Fri, 10 Nov 2023 10:34:34 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (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, 10 Nov 2023 10:34:33 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 9D5AD47A91 for <pve-devel@lists.proxmox.com>; Fri, 10 Nov 2023 10:34:33 +0100 (CET) From: Hannes Duerr <h.duerr@proxmox.com> To: pve-devel@lists.proxmox.com Date: Fri, 10 Nov 2023 10:33:57 +0100 Message-Id: <20231110093358.62006-2-h.duerr@proxmox.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231110093358.62006-1-h.duerr@proxmox.com> References: <20231110093358.62006-1-h.duerr@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.003 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record T_SCC_BODY_TEXT_LINE -0.01 - Subject: [pve-devel] [PATCH v3 qemu-server 1/2] Create get_scsi_devicetype and move it and its dependencies to QemuServer/Drive.pm X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com> 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/> List-Post: <mailto:pve-devel@lists.proxmox.com> List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help> List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe> X-List-Received-Date: Fri, 10 Nov 2023 09:34:34 -0000 Encapsulation of the functionality for determining the scsi device type in a new function for reusability and moving the function and its dependencies to Qemuserver/Drive.qm for a better overview Signed-off-by: Hannes Duerr <h.duerr@proxmox.com> --- PVE/QemuServer.pm | 87 ++----------------------------------- PVE/QemuServer/Drive.pm | 95 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 84 deletions(-) diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm index dbcd568..9a83021 100644 --- a/PVE/QemuServer.pm +++ b/PVE/QemuServer.pm @@ -1339,66 +1339,6 @@ sub pve_verify_hotplug_features { die "unable to parse hotplug option\n"; } -sub scsi_inquiry { - my($fh, $noerr) = @_; - - my $SG_IO = 0x2285; - my $SG_GET_VERSION_NUM = 0x2282; - - my $versionbuf = "\x00" x 8; - my $ret = ioctl($fh, $SG_GET_VERSION_NUM, $versionbuf); - if (!$ret) { - die "scsi ioctl SG_GET_VERSION_NUM failoed - $!\n" if !$noerr; - return; - } - my $version = unpack("I", $versionbuf); - if ($version < 30000) { - die "scsi generic interface too old\n" if !$noerr; - return; - } - - my $buf = "\x00" x 36; - my $sensebuf = "\x00" x 8; - my $cmd = pack("C x3 C x1", 0x12, 36); - - # see /usr/include/scsi/sg.h - my $sg_io_hdr_t = "i i C C s I P P P I I i P C C C C S S i I I"; - - my $packet = pack( - $sg_io_hdr_t, ord('S'), -3, length($cmd), length($sensebuf), 0, length($buf), $buf, $cmd, $sensebuf, 6000 - ); - - $ret = ioctl($fh, $SG_IO, $packet); - if (!$ret) { - die "scsi ioctl SG_IO failed - $!\n" if !$noerr; - return; - } - - my @res = unpack($sg_io_hdr_t, $packet); - if ($res[17] || $res[18]) { - die "scsi ioctl SG_IO status error - $!\n" if !$noerr; - return; - } - - my $res = {}; - $res->@{qw(type removable vendor product revision)} = unpack("C C x6 A8 A16 A4", $buf); - - $res->{removable} = $res->{removable} & 128 ? 1 : 0; - $res->{type} &= 0x1F; - - return $res; -} - -sub path_is_scsi { - my ($path) = @_; - - my $fh = IO::File->new("+<$path") || return; - my $res = scsi_inquiry($fh, 1); - close($fh); - - return $res; -} - sub print_tabletdevice_full { my ($conf, $arch) = @_; @@ -1443,31 +1383,10 @@ sub print_drivedevice_full { my ($maxdev, $controller, $controller_prefix) = scsihw_infos($conf, $drive); my $unit = $drive->{index} % $maxdev; - my $devicetype = 'hd'; - my $path = ''; - if (drive_is_cdrom($drive)) { - $devicetype = 'cd'; - } else { - if ($drive->{file} =~ m|^/|) { - $path = $drive->{file}; - if (my $info = path_is_scsi($path)) { - if ($info->{type} == 0 && $drive->{scsiblock}) { - $devicetype = 'block'; - } elsif ($info->{type} == 1) { # tape - $devicetype = 'generic'; - } - } - } else { - $path = PVE::Storage::path($storecfg, $drive->{file}); - } - # for compatibility only, we prefer scsi-hd (#2408, #2355, #2380) - my $version = extract_version($machine_type, kvm_user_version()); - if ($path =~ m/^iscsi\:\/\// && - !min_version($version, 4, 1)) { - $devicetype = 'generic'; - } - } + my $machine_version = extract_version($machine_type, kvm_user_version()); + my $devicetype = PVE::QemuServer::Drive::get_scsi_devicetype( + $drive, $storecfg, $machine_version); if (!$conf->{scsihw} || $conf->{scsihw} =~ m/^lsi/ || $conf->{scsihw} eq 'pvscsi') { $device = "scsi-$devicetype,bus=$controller_prefix$controller.0,scsi-id=$unit"; diff --git a/PVE/QemuServer/Drive.pm b/PVE/QemuServer/Drive.pm index e24ba12..7056daa 100644 --- a/PVE/QemuServer/Drive.pm +++ b/PVE/QemuServer/Drive.pm @@ -15,6 +15,7 @@ is_valid_drivename drive_is_cloudinit drive_is_cdrom drive_is_read_only +get_scsi_devicetype parse_drive print_drive ); @@ -760,4 +761,98 @@ sub resolve_first_disk { return; } +sub scsi_inquiry { + my($fh, $noerr) = @_; + + my $SG_IO = 0x2285; + my $SG_GET_VERSION_NUM = 0x2282; + + my $versionbuf = "\x00" x 8; + my $ret = ioctl($fh, $SG_GET_VERSION_NUM, $versionbuf); + if (!$ret) { + die "scsi ioctl SG_GET_VERSION_NUM failoed - $!\n" if !$noerr; + return; + } + my $version = unpack("I", $versionbuf); + if ($version < 30000) { + die "scsi generic interface too old\n" if !$noerr; + return; + } + + my $buf = "\x00" x 36; + my $sensebuf = "\x00" x 8; + my $cmd = pack("C x3 C x1", 0x12, 36); + + # see /usr/include/scsi/sg.h + my $sg_io_hdr_t = "i i C C s I P P P I I i P C C C C S S i I I"; + + my $packet = pack( + $sg_io_hdr_t, ord('S'), -3, length($cmd), length($sensebuf), 0, length($buf), $buf, $cmd, $sensebuf, 6000 + ); + + $ret = ioctl($fh, $SG_IO, $packet); + if (!$ret) { + die "scsi ioctl SG_IO failed - $!\n" if !$noerr; + return; + } + + my @res = unpack($sg_io_hdr_t, $packet); + if ($res[17] || $res[18]) { + die "scsi ioctl SG_IO status error - $!\n" if !$noerr; + return; + } + + my $res = {}; + $res->@{qw(type removable vendor product revision)} = unpack("C C x6 A8 A16 A4", $buf); + + $res->{removable} = $res->{removable} & 128 ? 1 : 0; + $res->{type} &= 0x1F; + + return $res; +} + +sub path_is_scsi { + my ($path) = @_; + + my $fh = IO::File->new("+<$path") || return; + my $res = scsi_inquiry($fh, 1); + close($fh); + + return $res; +} + +sub get_scsi_devicetype { + my ($drive, $storecfg, $machine_version) = @_; + + my $devicetype = 'hd'; + my $path = ''; + if (drive_is_cdrom($drive)) { + $devicetype = 'cd'; + } else { + if ($drive->{file} =~ m|^/|) { + $path = $drive->{file}; + if (my $info = path_is_scsi($path)) { + if ($info->{type} == 0 && $drive->{scsiblock}) { + $devicetype = 'block'; + } elsif ($info->{type} == 1) { # tape + $devicetype = 'generic'; + } + } + } elsif ($drive->{file} =~ m/local-lvm:/){ + # special syntax cannot be parsed to path + $path = "local-lvm"; + } else { + $path = PVE::Storage::path($storecfg, $drive->{file}); + } + + # for compatibility only, we prefer scsi-hd (#2408, #2355, #2380) + if ($path =~ m/^iscsi\:\/\// && + !min_version($machine_version, 4, 1)) { + $devicetype = 'generic'; + } + } + + return $devicetype; +} + 1; -- 2.39.2