* [pve-devel] [PATCH v3 qemu-server 0/2] fix #4957: add vendor and product information passthrough for SCSI-Disks
@ 2023-11-10 9:33 Hannes Duerr
2023-11-10 9:33 ` [pve-devel] [PATCH v3 qemu-server 1/2] Create get_scsi_devicetype and move it and its dependencies to QemuServer/Drive.pm Hannes Duerr
2023-11-10 9:33 ` [pve-devel] [PATCH v3 qemu-server 2/2] fix #4957: add vendor and product information passthrough for SCSI-Disks Hannes Duerr
0 siblings, 2 replies; 5+ messages in thread
From: Hannes Duerr @ 2023-11-10 9:33 UTC (permalink / raw)
To: pve-devel
changes in v2:
- when calling the API to create/update a VM, check whether the devices
are "scsi-hd" or "scsi-cd" devices,where there is the option to add
vendor and product information, if not error out
- change the format in product_fmt and vendor_fmt to a pattern that only
allows 40 characters consisting of upper and lower case letters, numbers and '-' and '_'.
changes in v3:
- splitup into preparation and fix patch
- move get_scsi_devicetype into QemuServer/Drive.pm
- refactor check_scsi_feature_compatibility to assert_scsi_feature_compatibility
- assert_scsi_feature_compatibility before creating the device
- handle 'local-lvm:' syntax in get_scsi_devicetype
- fix style issues
Hannes Duerr (2):
Create get_scsi_devicetype and move it and its dependencies to
QemuServer/Drive.pm
fix #4957: add vendor and product information passthrough for
SCSI-Disks
PVE/API2/Qemu.pm | 12 ++++
PVE/QemuServer.pm | 115 +++++++++++---------------------------
PVE/QemuServer/Drive.pm | 119 ++++++++++++++++++++++++++++++++++++++++
3 files changed, 162 insertions(+), 84 deletions(-)
--
2.39.2
^ permalink raw reply [flat|nested] 5+ messages in thread
* [pve-devel] [PATCH v3 qemu-server 1/2] Create get_scsi_devicetype and move it and its dependencies to QemuServer/Drive.pm
2023-11-10 9:33 [pve-devel] [PATCH v3 qemu-server 0/2] fix #4957: add vendor and product information passthrough for SCSI-Disks Hannes Duerr
@ 2023-11-10 9:33 ` Hannes Duerr
2023-11-16 10:15 ` Fiona Ebner
2023-11-10 9:33 ` [pve-devel] [PATCH v3 qemu-server 2/2] fix #4957: add vendor and product information passthrough for SCSI-Disks Hannes Duerr
1 sibling, 1 reply; 5+ messages in thread
From: Hannes Duerr @ 2023-11-10 9:33 UTC (permalink / raw)
To: pve-devel
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* [pve-devel] [PATCH v3 qemu-server 2/2] fix #4957: add vendor and product information passthrough for SCSI-Disks
2023-11-10 9:33 [pve-devel] [PATCH v3 qemu-server 0/2] fix #4957: add vendor and product information passthrough for SCSI-Disks Hannes Duerr
2023-11-10 9:33 ` [pve-devel] [PATCH v3 qemu-server 1/2] Create get_scsi_devicetype and move it and its dependencies to QemuServer/Drive.pm Hannes Duerr
@ 2023-11-10 9:33 ` Hannes Duerr
2023-11-16 10:37 ` Fiona Ebner
1 sibling, 1 reply; 5+ messages in thread
From: Hannes Duerr @ 2023-11-10 9:33 UTC (permalink / raw)
To: pve-devel
adds vendor and product information for SCSI devices to the json schema and
checks in the VM create/update API call if it is possible to add these to QEMU as a device option
Signed-off-by: Hannes Duerr <h.duerr@proxmox.com>
---
PVE/API2/Qemu.pm | 12 ++++++++++++
PVE/QemuServer.pm | 28 ++++++++++++++++++++++++++++
PVE/QemuServer/Drive.pm | 24 ++++++++++++++++++++++++
3 files changed, 64 insertions(+)
diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index 38bdaab..9d8171a 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -1013,6 +1013,13 @@ __PACKAGE__->register_method({
my $conf = $param;
my $arch = PVE::QemuServer::get_vm_arch($conf);
+ for my $opt (sort keys $param->%*) {
+ if ($opt =~ m/scsi/) {
+ PVE::QemuServer::assert_scsi_feature_compatibility(
+ $opt, $conf, $storecfg, $param->{$opt});
+ }
+ }
+
$conf->{meta} = PVE::QemuServer::new_meta_info_string();
my $vollist = [];
@@ -1828,6 +1835,11 @@ my $update_vm_api = sub {
PVE::QemuServer::vmconfig_register_unused_drive($storecfg, $vmid, $conf, PVE::QemuServer::parse_drive($opt, $conf->{pending}->{$opt}))
if defined($conf->{pending}->{$opt});
+ if ($opt =~ m/scsi/) {
+ PVE::QemuServer::assert_scsi_feature_compatibility(
+ $opt, $conf, $storecfg, $param->{$opt});
+ }
+
my (undef, $created_opts) = $create_disks->(
$rpcenv,
$authuser,
diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 9a83021..9c998d6 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -1368,6 +1368,24 @@ my sub get_drive_id {
return "$drive->{interface}$drive->{index}";
}
+sub assert_scsi_feature_compatibility {
+ my ($opt, $conf, $storecfg, $drive_attributes) = @_;
+
+ my $drive = parse_drive($opt, $drive_attributes);
+
+ my $machine_type = get_vm_machine($conf, undef, $conf->{arch});
+ my $machine_version = extract_version($machine_type, kvm_user_version());
+ my $drivetype = PVE::QemuServer::Drive::get_scsi_devicetype(
+ $drive, $storecfg, $machine_version);
+
+ if ($drivetype ne 'hd' && $drivetype ne 'cd') {
+ if ($drive_attributes =~ m/vendor/ || $drive_attributes =~ m/product/) {
+ die "only 'scsi-hd' and 'scsi-cd' devices".
+ "support passing vendor and product information\n";
+ }
+ }
+}
+
sub print_drivedevice_full {
my ($storecfg, $conf, $vmid, $drive, $bridges, $arch, $machine_type) = @_;
@@ -1401,6 +1419,16 @@ sub print_drivedevice_full {
}
$device .= ",wwn=$drive->{wwn}" if $drive->{wwn};
+ # only scsi-hd and scsi-cd support passing vendor and product information
+ if ($devicetype eq 'hd' || $devicetype eq 'cd') {
+ if (my $vendor = $drive->{vendor}) {
+ $device .= ",vendor=$vendor";
+ }
+ if (my $product = $drive->{product}) {
+ $device .= ",product=$product";
+ }
+ }
+
} elsif ($drive->{interface} eq 'ide' || $drive->{interface} eq 'sata') {
my $maxdev = ($drive->{interface} eq 'sata') ? $PVE::QemuServer::Drive::MAX_SATA_DISKS : 2;
my $controller = int($drive->{index} / $maxdev);
diff --git a/PVE/QemuServer/Drive.pm b/PVE/QemuServer/Drive.pm
index 7056daa..66a4816 100644
--- a/PVE/QemuServer/Drive.pm
+++ b/PVE/QemuServer/Drive.pm
@@ -160,6 +160,26 @@ my %iothread_fmt = ( iothread => {
optional => 1,
});
+my %product_fmt = (
+ product => {
+ type => 'string',
+ pattern => '[A-Za-z0-9\-_]{,40}',
+ format_description => 'product',
+ description => "The drive's product name, up to 40 bytes long.",
+ optional => 1,
+ },
+);
+
+my %vendor_fmt = (
+ vendor => {
+ type => 'string',
+ pattern => '[A-Za-z0-9\-_]{,40}',
+ format_description => 'vendor',
+ description => "The drive's vendor name, up to 40 bytes long.",
+ optional => 1,
+ },
+);
+
my %model_fmt = (
model => {
type => 'string',
@@ -277,10 +297,12 @@ PVE::JSONSchema::register_standard_option("pve-qm-ide", $idedesc);
my $scsi_fmt = {
%drivedesc_base,
%iothread_fmt,
+ %product_fmt,
%queues_fmt,
%readonly_fmt,
%scsiblock_fmt,
%ssd_fmt,
+ %vendor_fmt,
%wwn_fmt,
};
my $scsidesc = {
@@ -401,10 +423,12 @@ my $alldrive_fmt = {
%drivedesc_base,
%iothread_fmt,
%model_fmt,
+ %product_fmt,
%queues_fmt,
%readonly_fmt,
%scsiblock_fmt,
%ssd_fmt,
+ %vendor_fmt,
%wwn_fmt,
%tpmversion_fmt,
%efitype_fmt,
--
2.39.2
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [pve-devel] [PATCH v3 qemu-server 1/2] Create get_scsi_devicetype and move it and its dependencies to QemuServer/Drive.pm
2023-11-10 9:33 ` [pve-devel] [PATCH v3 qemu-server 1/2] Create get_scsi_devicetype and move it and its dependencies to QemuServer/Drive.pm Hannes Duerr
@ 2023-11-16 10:15 ` Fiona Ebner
0 siblings, 0 replies; 5+ messages in thread
From: Fiona Ebner @ 2023-11-16 10:15 UTC (permalink / raw)
To: Proxmox VE development discussion, Hannes Duerr
Nit: Like can be seen from the "and" in the commit title, it's actually
two changes. Moving the dependencies first, and then introducing the new
helper in a second patch would be even better. But no big deal :)
Am 10.11.23 um 10:33 schrieb Hannes Duerr:
> +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";
What if the storage is called differently ;)
Note, there also is a NEW_DISK_RE in API2/Qemu.pm you can use to compare
(needs to be moved to the Drive.pm module first - make it
our $NEW_DISK_RE = ...;
to be able to reference it from API2/Qemu.pm afterwards)
Why set the path to some fake value and not just return the device type
early?
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [pve-devel] [PATCH v3 qemu-server 2/2] fix #4957: add vendor and product information passthrough for SCSI-Disks
2023-11-10 9:33 ` [pve-devel] [PATCH v3 qemu-server 2/2] fix #4957: add vendor and product information passthrough for SCSI-Disks Hannes Duerr
@ 2023-11-16 10:37 ` Fiona Ebner
0 siblings, 0 replies; 5+ messages in thread
From: Fiona Ebner @ 2023-11-16 10:37 UTC (permalink / raw)
To: Proxmox VE development discussion, Hannes Duerr
Am 10.11.23 um 10:33 schrieb Hannes Duerr:
> adds vendor and product information for SCSI devices to the json schema and
> checks in the VM create/update API call if it is possible to add these to QEMU as a device option
>
> Signed-off-by: Hannes Duerr <h.duerr@proxmox.com>
> ---
> PVE/API2/Qemu.pm | 12 ++++++++++++
> PVE/QemuServer.pm | 28 ++++++++++++++++++++++++++++
> PVE/QemuServer/Drive.pm | 24 ++++++++++++++++++++++++
> 3 files changed, 64 insertions(+)
>
> diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
> index 38bdaab..9d8171a 100644
> --- a/PVE/API2/Qemu.pm
> +++ b/PVE/API2/Qemu.pm
> @@ -1013,6 +1013,13 @@ __PACKAGE__->register_method({
> my $conf = $param;
> my $arch = PVE::QemuServer::get_vm_arch($conf);
>
> + for my $opt (sort keys $param->%*) {
> + if ($opt =~ m/scsi/) {
Should be anchored and best to even match for the number too, i.e.
^scsi(\d)+$ to make it future-proof. E.g. there could be a
'scsi-defaults' option at some point
> + PVE::QemuServer::assert_scsi_feature_compatibility(
> + $opt, $conf, $storecfg, $param->{$opt});
> + }
> + }
> +
> $conf->{meta} = PVE::QemuServer::new_meta_info_string();
>
> my $vollist = [];
> @@ -1828,6 +1835,11 @@ my $update_vm_api = sub {
> PVE::QemuServer::vmconfig_register_unused_drive($storecfg, $vmid, $conf, PVE::QemuServer::parse_drive($opt, $conf->{pending}->{$opt}))
> if defined($conf->{pending}->{$opt});
>
> + if ($opt =~ m/scsi/) {
> + PVE::QemuServer::assert_scsi_feature_compatibility(
> + $opt, $conf, $storecfg, $param->{$opt});
> + }
> +
> my (undef, $created_opts) = $create_disks->(
> $rpcenv,
> $authuser,
> diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
> index 9a83021..9c998d6 100644
> --- a/PVE/QemuServer.pm
> +++ b/PVE/QemuServer.pm
> @@ -1368,6 +1368,24 @@ my sub get_drive_id {
> return "$drive->{interface}$drive->{index}";
> }
>
> +sub assert_scsi_feature_compatibility {
> + my ($opt, $conf, $storecfg, $drive_attributes) = @_;
> +
Since it's only used in API2/Qemu.pm, should it live there?
> + my $drive = parse_drive($opt, $drive_attributes);
> +
> + my $machine_type = get_vm_machine($conf, undef, $conf->{arch});
> + my $machine_version = extract_version($machine_type, kvm_user_version());
> + my $drivetype = PVE::QemuServer::Drive::get_scsi_devicetype(
> + $drive, $storecfg, $machine_version);
> +
> + if ($drivetype ne 'hd' && $drivetype ne 'cd') {
> + if ($drive_attributes =~ m/vendor/ || $drive_attributes =~ m/product/) {
Please check $drive->{vendor} and $drive->{product} here. These regexes
are brittle and not future-proof.
> + die "only 'scsi-hd' and 'scsi-cd' devices".
> + "support passing vendor and product information\n";
Could be a raise_param_exc and maybe give a hint about pass-through? For
example:
only 'scsi-hd' and 'scsi-cd' devices (e.g. not pass-through)
Like that users will know in most situations what the issue is.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2023-11-16 10:37 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-10 9:33 [pve-devel] [PATCH v3 qemu-server 0/2] fix #4957: add vendor and product information passthrough for SCSI-Disks Hannes Duerr
2023-11-10 9:33 ` [pve-devel] [PATCH v3 qemu-server 1/2] Create get_scsi_devicetype and move it and its dependencies to QemuServer/Drive.pm Hannes Duerr
2023-11-16 10:15 ` Fiona Ebner
2023-11-10 9:33 ` [pve-devel] [PATCH v3 qemu-server 2/2] fix #4957: add vendor and product information passthrough for SCSI-Disks Hannes Duerr
2023-11-16 10:37 ` Fiona Ebner
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox