From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id 52F231FF0C0 for ; Wed, 18 Nov 2026 07:53:48 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id A43F721716; Wed, 18 Nov 2026 07:53:11 +0100 (CET) From: Thomas Lamprecht To: pve-devel@lists.proxmox.com Subject: [PATCH v6 storage 06/42] iscsi: factor out session device map from device list Date: Wed, 18 Nov 2026 07:52:23 +0100 Message-ID: <20261118065302.3033937-7-t.lamprecht@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261118065302.3033937-1-t.lamprecht@proxmox.com> References: <20261118065302.3033937-1-t.lamprecht@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1794984787295 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.184 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: ZE4TWJ4Y4FLNUKNMDAOVF4GCLGRSUKHC X-Message-ID-Hash: ZE4TWJ4Y4FLNUKNMDAOVF4GCLGRSUKHC X-MailFrom: t.lamprecht@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Dietmar Maurer The SAN LUN inventory and the multipath handling need to know which block devices belong to which iSCSI session, target and LUN. So far only the device list of the plugin collects that. Provide it as a map from kernel device names in a module of its own and build the device list from it. The device list stays the same. Only the fallback for the 'block:' links that old kernels had instead of the 'block' directory is gone. Signed-off-by: Dietmar Maurer [TL: put the session walk into the new PVE::ISCSI::Topology module, with signatures, instead of the plugin; drop the fallback for old kernels] Signed-off-by: Thomas Lamprecht --- src/PVE/ISCSI/Topology.pm | 69 +++++++++++++++++++++ src/PVE/Makefile | 1 + src/PVE/Storage/ISCSIPlugin.pm | 107 +++++++++------------------------ 3 files changed, 100 insertions(+), 77 deletions(-) create mode 100644 src/PVE/ISCSI/Topology.pm diff --git a/src/PVE/ISCSI/Topology.pm b/src/PVE/ISCSI/Topology.pm new file mode 100644 index 00000000..4ebfaccd --- /dev/null +++ b/src/PVE/ISCSI/Topology.pm @@ -0,0 +1,69 @@ +package PVE::ISCSI::Topology; + +use v5.36; + +use PVE::Tools qw(dir_glob_foreach dir_glob_regex file_read_firstline); + +our $SYSFS = '/sys'; + +# Offload initiators can run several sessions on one SCSI host, so the LUNs of a session are the +# ones below its own target, not all LUNs of its host. +sub foreach_session_lun($sessiondir, $code) { + dir_glob_foreach( + $sessiondir, + 'target\d+:\d+:\d+', + sub($targetdir) { + dir_glob_foreach( + "$sessiondir/$targetdir", + '\d+:(\d+):(\d+):(\d+)', + sub($hctl, $channel, $id, $lun) { + $code->("$sessiondir/$targetdir/$hctl", $channel, $id, $lun); + }, + ); + }, + ); +} + +sub sessions($sysfs = undef) { + $sysfs //= $SYSFS; + my $res = {}; + dir_glob_foreach( + "$sysfs/class/iscsi_session", + 'session(\d+)', + sub($ent, $sid) { + my $dir = "$sysfs/class/iscsi_session/$ent"; + my $target = file_read_firstline("$dir/targetname") // return; + my $session = { + target => $target, + devices => {}, + }; + foreach_session_lun( + "$dir/device", + sub($devdir, $channel, $id, $lun) { + return if (file_read_firstline("$devdir/type") // '') ne '0'; + my (undef, $dev) = dir_glob_regex("$devdir/block/", '([A-Za-z]\S*)'); + return if !defined($dev); + $session->{devices}->{$dev} = { + target => $target, + session => int($sid), + channel => int($channel), + id => int($id), + lun => int($lun), + }; + }, + ); + $res->{$sid} = $session; + }, + ); + return $res; +} + +sub device_map($sysfs = undef) { + my $res = {}; + for my $session (values %{ sessions($sysfs) }) { + $res->{$_} = $session->{devices}->{$_} for keys $session->{devices}->%*; + } + return $res; +} + +1; diff --git a/src/PVE/Makefile b/src/PVE/Makefile index 9e9f6aac..bd2ddfb9 100644 --- a/src/PVE/Makefile +++ b/src/PVE/Makefile @@ -4,6 +4,7 @@ install: install -D -m 0644 Storage.pm ${DESTDIR}${PERLDIR}/PVE/Storage.pm install -D -m 0644 Diskmanage.pm ${DESTDIR}${PERLDIR}/PVE/Diskmanage.pm + install -D -m 0644 ISCSI/Topology.pm ${DESTDIR}${PERLDIR}/PVE/ISCSI/Topology.pm install -D -m 0644 CephConfig.pm ${DESTDIR}${PERLDIR}/PVE/CephConfig.pm install -D -m 0644 GuestImport.pm ${DESTDIR}${PERLDIR}/PVE/GuestImport.pm make -C Storage install diff --git a/src/PVE/Storage/ISCSIPlugin.pm b/src/PVE/Storage/ISCSIPlugin.pm index 1e349cde..8ba96bed 100644 --- a/src/PVE/Storage/ISCSIPlugin.pm +++ b/src/PVE/Storage/ISCSIPlugin.pm @@ -10,6 +10,7 @@ use IO::File; use PVE::JSONSchema qw(get_standard_option); use PVE::Storage::Plugin; +use PVE::ISCSI::Topology; use PVE::Tools qw(run_command file_read_firstline trim dir_glob_regex dir_glob_foreach $IPV4RE $IPV6RE); @@ -290,89 +291,41 @@ sub load_stable_scsi_paths { return $stable_paths; } -# Offload initiators can run several sessions on one SCSI host, so the LUNs of a session are the -# ones below its own target, not all LUNs of its host. -my sub foreach_session_lun { - my ($sessiondir, $code) = @_; - - dir_glob_foreach( - $sessiondir, - 'target\d+:\d+:\d+', - sub { - my ($targetdir) = @_; - dir_glob_foreach( - "$sessiondir/$targetdir", - '\d+:(\d+):(\d+):(\d+)', - sub { - my ($hctl, $channel, $id, $lun) = @_; - $code->("$sessiondir/$targetdir/$hctl", $channel, $id, $lun); - }, - ); - }, - ); -} - sub iscsi_device_list { my $res = {}; - my $dirname = "$SYSFS/class/iscsi_session"; - my $stable_paths = load_stable_scsi_paths(); - dir_glob_foreach( - $dirname, - 'session(\d+)', - sub { - my ($ent, $session) = @_; - - my $target = file_read_firstline("$dirname/$ent/targetname"); - return if !$target; - - foreach_session_lun( - "$dirname/$ent/device", - sub { - my ($devdir, $channel, $id, $lun) = @_; - - my $type = file_read_firstline("$devdir/type"); - return if !defined($type) || $type ne '0'; # list disks only - - my $bdev; - if (-d "$devdir/block") { # newer kernels - (undef, $bdev) = dir_glob_regex("$devdir/block/", '([A-Za-z]\S*)'); - } else { - (undef, $bdev) = dir_glob_regex($devdir, 'block:(\S+)'); - } - return if !$bdev; - - #check multipath - if (-d "$SYSFS/block/$bdev/holders") { - my $multipathdev = - dir_glob_regex("$SYSFS/block/$bdev/holders", '[A-Za-z]\S*'); - $bdev = $multipathdev if $multipathdev; - } - - my $blockdev = $stable_paths->{$bdev}; - return if !$blockdev; - - my $size = file_read_firstline("$SYSFS/block/$bdev/size"); - return if !$size; - - my $volid = "$channel.$id.$lun.$blockdev"; - - $res->{$target}->{$volid} = { - 'format' => 'raw', - 'size' => int($size * 512), - 'vmid' => 0, # not assigned to any vm - 'channel' => int($channel), - 'id' => int($id), - 'lun' => int($lun), - }; - }, - ); - - }, - ); + my $device_map = PVE::ISCSI::Topology::device_map($SYSFS); + + for my $bdev (sort keys %$device_map) { + my $info = $device_map->{$bdev}; + my ($channel, $id, $lun) = $info->@{qw(channel id lun)}; + + #check multipath + if (-d "$SYSFS/block/$bdev/holders") { + my $multipathdev = dir_glob_regex("$SYSFS/block/$bdev/holders", '[A-Za-z]\S*'); + $bdev = $multipathdev if $multipathdev; + } + + my $blockdev = $stable_paths->{$bdev}; + next if !$blockdev; + + my $size = file_read_firstline("$SYSFS/block/$bdev/size"); + next if !$size; + + my $volid = "$channel.$id.$lun.$blockdev"; + + $res->{ $info->{target} }->{$volid} = { + 'format' => 'raw', + 'size' => int($size * 512), + 'vmid' => 0, # not assigned to any vm + 'channel' => $channel, + 'id' => $id, + 'lun' => $lun, + }; + } return $res; }