From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id 398B41FF0E1 for ; Mon, 16 Nov 2026 02:27:41 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 2E67621A51; Mon, 16 Nov 2026 02:25:07 +0100 (CET) From: Thomas Lamprecht To: pve-devel@lists.proxmox.com Subject: [PATCH v5 storage 18/36] api: add node-level iSCSI status Date: Mon, 16 Nov 2026 02:23:27 +0100 Message-ID: <20261116012355.3187417-19-t.lamprecht@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261116012355.3187417-1-t.lamprecht@proxmox.com> References: <20261116012355.3187417-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: 1794792242287 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.156 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) POISEN_SPAM_PILL_3 0.1 random spam to be learned in bayes 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: PVCZAUFQTKD5LZWXZ3AN543MEJOXCQBJ X-Message-ID-Hash: PVCZAUFQTKD5LZWXZ3AN543MEJOXCQBJ 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 Activating an iSCSI storage logs in on its own, but looking at the sessions of a node needs iscsiadm on a shell. Add the API module for /nodes/{node}/iscsi with a read-only view: the initiator name and alias, and the targets with their portals, session state, block devices and the storage of the target. Node records without a session are listed too, so a target that is not connected stays visible. Reading needs Sys.Audit on the node or Datastore.Allocate on /storage. Signed-off-by: Dietmar Maurer [TL: keep it read-only, without rescan and node record removal; report the initiator name and alias as top-level fields; move the iscsiadm helpers into the plugin] Signed-off-by: Thomas Lamprecht --- src/PVE/API2/ISCSI.pm | 166 +++++++++++++++++++++++++++++++++ src/PVE/API2/Makefile | 1 + src/PVE/ISCSI/Topology.pm | 19 ++++ src/PVE/Storage/ISCSIPlugin.pm | 27 ++++++ src/test/run_san_api_tests.pl | 56 ++++++++++- 5 files changed, 268 insertions(+), 1 deletion(-) create mode 100644 src/PVE/API2/ISCSI.pm diff --git a/src/PVE/API2/ISCSI.pm b/src/PVE/API2/ISCSI.pm new file mode 100644 index 00000000..005e6809 --- /dev/null +++ b/src/PVE/API2/ISCSI.pm @@ -0,0 +1,166 @@ +package PVE::API2::ISCSI; + +use v5.36; + +use PVE::JSONSchema qw(get_standard_option); +use PVE::RESTHandler; + +use PVE::ISCSI::Topology; +use PVE::Storage; +use PVE::Storage::ISCSIPlugin; + +use base qw(PVE::RESTHandler); + +my $read_permissions = { + check => [ + 'or', + ['perm', '/nodes/{node}', ['Sys.Audit']], + ['perm', '/storage', ['Datastore.Allocate']], + ], +}; + +__PACKAGE__->register_method({ + name => 'index', + path => '', + method => 'GET', + proxyto => 'node', + permissions => { user => 'all' }, + description => 'Node iSCSI index.', + parameters => + { additionalProperties => 0, properties => { node => get_standard_option('pve-node') } }, + returns => { + type => 'array', + items => { type => 'object', properties => { name => { type => 'string' } } }, + links => [{ rel => 'child', href => '{name}' }], + }, + code => sub { return [{ name => 'status' }, { name => 'target' }] }, +}); + +__PACKAGE__->register_method({ + name => 'status', + path => 'status', + method => 'GET', + protected => 1, + proxyto => 'node', + description => 'Report the iSCSI initiator identity and availability.', + permissions => $read_permissions, + parameters => + { additionalProperties => 0, properties => { node => get_standard_option('pve-node') } }, + returns => { + type => 'object', + properties => { + supported => + { type => 'boolean', description => 'Whether open-iscsi is installed.' }, + 'initiator-name' => + { type => 'string', optional => 1, description => 'The initiator IQN.' }, + 'initiator-alias' => + { type => 'string', optional => 1, description => 'The initiator alias.' }, + }, + }, + code => sub { + my $res = { supported => PVE::Storage::ISCSIPlugin::iscsi_supported() }; + my $info = PVE::Storage::ISCSIPlugin::read_initiator_info() // {}; + $res->{'initiator-name'} = $info->{name} if defined($info->{name}); + $res->{'initiator-alias'} = $info->{alias} if defined($info->{alias}); + return $res; + }, +}); + +__PACKAGE__->register_method({ + name => 'targets', + path => 'target', + method => 'GET', + protected => 1, + proxyto => 'node', + description => 'List iSCSI targets and their sessions and devices.', + permissions => $read_permissions, + parameters => + { additionalProperties => 0, properties => { node => get_standard_option('pve-node') } }, + returns => { + type => 'array', + items => { + type => 'object', + properties => { + target => { type => 'string', description => 'The target IQN.' }, + portal => { type => 'string', description => 'The portal address and port.' }, + 'session-id' => + { type => 'integer', optional => 1, description => 'The kernel session ID.' }, + state => { + type => 'string', + enum => ['logged-in', 'recovering', 'failed', 'not-connected', 'unknown'], + description => 'The connection state.', + }, + 'kernel-state' => + { type => 'string', optional => 1, description => 'The kernel session state.' }, + 'used-by-storage' => { + type => 'string', + optional => 1, + description => 'An iSCSI storage using the target.', + }, + devices => { + type => 'array', + description => 'The block devices of the session.', + items => { + type => 'object', + properties => { + devpath => + { type => 'string', description => 'The block device path.' }, + lun => { type => 'integer', description => 'The LUN number.' }, + }, + }, + }, + }, + }, + }, + code => sub { + my $sessions = PVE::ISCSI::Topology::sessions(); + my $cfg = PVE::Storage::config(); + my %owners; + for my $sid (sort keys $cfg->{ids}->%*) { + my $scfg = $cfg->{ids}->{$sid}; + $owners{ $scfg->{target} } //= $sid if $scfg->{type} eq 'iscsi' && $scfg->{target}; + } + my (@rows, %seen); + for my $sid (sort { $a <=> $b } keys %$sessions) { + my $session = $sessions->{$sid}; + my $state = $session->{state}; + my $row = { + target => $session->{target}, + portal => $session->{portal}, + 'session-id' => int($sid), + state => $state eq 'LOGGED_IN' ? 'logged-in' + : $state eq 'FAILED' ? 'recovering' + : $state eq 'FREE' ? 'failed' + : 'unknown', + 'kernel-state' => $state, + devices => [ + sort { $a->{lun} <=> $b->{lun} || $a->{devpath} cmp $b->{devpath} } + map { { devpath => "/dev/$_", lun => $session->{devices}->{$_}->{lun} } } + keys $session->{devices}->%* + ], + }; + push @rows, $row; + $seen{"$row->{target}\0$row->{portal}"} = 1; + } + if (PVE::Storage::ISCSIPlugin::iscsi_supported()) { + for my $record (PVE::Storage::ISCSIPlugin::iscsi_node_list()->@*) { + my $portal = PVE::ISCSI::Topology::normalize_portal($record->{portal}); + next if $seen{"$record->{target}\0$portal"}++; + push @rows, + { + target => $record->{target}, + portal => $portal, + state => 'not-connected', + devices => [], + }; + } + } + for my $row (@rows) { + $row->{'used-by-storage'} = $owners{ $row->{target} } + if defined($owners{ $row->{target} }); + } + return [sort { $a->{target} cmp $b->{target} || $a->{portal} cmp $b->{portal} } @rows]; + }, +}); + +1; diff --git a/src/PVE/API2/Makefile b/src/PVE/API2/Makefile index 002a3bd4..9ab978b0 100644 --- a/src/PVE/API2/Makefile +++ b/src/PVE/API2/Makefile @@ -3,6 +3,7 @@ .PHONY: install install: install -D -m 0644 Disks.pm ${DESTDIR}${PERLDIR}/PVE/API2/Disks.pm + install -D -m 0644 ISCSI.pm ${DESTDIR}${PERLDIR}/PVE/API2/ISCSI.pm install -D -m 0644 Multipath.pm ${DESTDIR}${PERLDIR}/PVE/API2/Multipath.pm make -C Storage install make -C Disks install diff --git a/src/PVE/ISCSI/Topology.pm b/src/PVE/ISCSI/Topology.pm index 52467864..146bda98 100644 --- a/src/PVE/ISCSI/Topology.pm +++ b/src/PVE/ISCSI/Topology.pm @@ -33,8 +33,18 @@ sub sessions($sysfs = undef) { my $target = file_read_firstline("$dir/targetname") // return; my $session = { target => $target, + state => file_read_firstline("$dir/state") // '', + portal => '', devices => {}, }; + my (undef, $conn) = dir_glob_regex("$dir/device", '(connection\d+:\d+)'); + if (defined($conn)) { + my $cdir = "$dir/device/$conn/iscsi_connection/$conn"; + my $address = file_read_firstline("$cdir/persistent_address") // ''; + my $port = file_read_firstline("$cdir/persistent_port") // ''; + $address = "[$address]" if $address =~ m/:/; + $session->{portal} = normalize_portal("$address:$port"); + } foreach_session_lun( "$dir/device", sub($devdir, $channel, $id, $lun) { @@ -64,4 +74,13 @@ sub device_map($sysfs = undef) { return $res; } +sub normalize_portal($portal) { + return $portal if $portal !~ m/^(.+):(\d+)$/; + my ($addr, $port) = ($1, $2); + $addr =~ s/^\[(.*)\]$/$1/; + $addr =~ s/^::ffff:(\d+\.\d+\.\d+\.\d+)$/$1/i; + $addr = "[$addr]" if $addr =~ m/:/; + return "$addr:$port"; +} + 1; diff --git a/src/PVE/Storage/ISCSIPlugin.pm b/src/PVE/Storage/ISCSIPlugin.pm index e117f39f..89cfc944 100644 --- a/src/PVE/Storage/ISCSIPlugin.pm +++ b/src/PVE/Storage/ISCSIPlugin.pm @@ -84,6 +84,33 @@ sub iscsi_session_list { return $res; } +sub iscsi_node_list { + assert_iscsi_support(); + + my $res = []; + eval { + run_command( + [$ISCSIADM, '--mode', 'node'], + errmsg => 'iscsi node record scan failed', + outfunc => sub { + my $line = shift; + if ($line =~ $ISCSI_TARGET_RE) { + push @$res, { portal => $1, target => $2 }; + } + }, + ); + }; + if (my $err = $@) { + die $err if $err !~ m/: No records found/i; + } + + return $res; +} + +sub iscsi_supported { + return -x $ISCSIADM ? 1 : 0; +} + sub iscsi_test_session { my ($sid) = @_; diff --git a/src/test/run_san_api_tests.pl b/src/test/run_san_api_tests.pl index f3846ba4..a051350d 100755 --- a/src/test/run_san_api_tests.pl +++ b/src/test/run_san_api_tests.pl @@ -5,9 +5,58 @@ use warnings; use Test::MockModule; use Test::More; use lib qw(. ..); +use PVE::API2::ISCSI; use PVE::API2::Storage::Scan; -for my $case (['PVE::API2::Storage::Scan', 'sanlunscan']) { +my $plugin = Test::MockModule->new('PVE::Storage::ISCSIPlugin'); +my $topology = Test::MockModule->new('PVE::ISCSI::Topology'); +my $storage = Test::MockModule->new('PVE::Storage'); +$plugin->mock(iscsi_supported => sub { 1 }); +$plugin->mock(read_initiator_info => sub { { name => 'iqn.test', alias => 'test' } }); +$plugin->mock(iscsi_node_list => sub { [{ target => 'iqn.target', portal => '192.0.2.2:3260' }] }); +$storage->mock(config => sub { { ids => { san => { type => 'iscsi', target => 'iqn.target' } } } }); +my $state = 'LOGGED_IN'; +$topology->mock( + sessions => sub { + return { + 7 => { + target => 'iqn.target', + portal => '192.0.2.1:3260', + state => $state, + devices => { sda => { lun => 1 }, sdb => { lun => 0 } }, + }, + }; + }, +); +my $status = PVE::API2::ISCSI->map_method_by_name('status'); +is_deeply( + $status->{code}->({ node => 'test' }), + { supported => 1, 'initiator-name' => 'iqn.test', 'initiator-alias' => 'test' }, + 'initiator identity', +); +my $targets = PVE::API2::ISCSI->map_method_by_name('targets'); +for my $case ( + ['LOGGED_IN', 'logged-in'], + ['FAILED', 'recovering'], + ['FREE', 'failed'], + ['', 'unknown'], +) { + $state = $case->[0]; + my $rows = $targets->{code}->({ node => 'test' }); + is($rows->[0]->{state}, $case->[1], "session state $state"); + is_deeply( + $rows->[0]->{devices}, + [{ devpath => '/dev/sdb', lun => 0 }, { devpath => '/dev/sda', lun => 1 }], + 'session devices ordered by LUN', + ); + is($rows->[1]->{state}, 'not-connected', 'node record without a session'); + is($rows->[0]->{'used-by-storage'}, 'san', 'target storage attribution'); + eval { PVE::JSONSchema::validate($rows, $targets->{returns}) }; + is($@, '', 'rows satisfy the schema'); +} +for my $case ( + ['PVE::API2::ISCSI', qw(status targets)], ['PVE::API2::Storage::Scan', 'sanlunscan'], +) { my ($class, @names) = @$case; for my $name (@names) { is_deeply( @@ -21,6 +70,11 @@ for my $case (['PVE::API2::Storage::Scan', 'sanlunscan']) { ); } } +is_deeply( + PVE::API2::ISCSI->map_method_by_name('index')->{code}->({}), + [{ name => 'status' }, { name => 'target' }], + 'only read-only children', +); require PVE::CLI::pvesm; my $formatter = Test::MockModule->new('PVE::CLIFormatter');