public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Mira Limbeck <m.limbeck@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH v2 storage 07/15] iscsi: introduce helper to update discovery db
Date: Thu, 30 Apr 2026 19:27:05 +0200	[thread overview]
Message-ID: <20260430173220.441001-8-m.limbeck@proxmox.com> (raw)
In-Reply-To: <20260430173220.441001-1-m.limbeck@proxmox.com>

In the case of using mappings, running a discovery against the
configured portals could lead to lots of additional node entries, as can
be seen with the original iSCSI plugin way.

Add a discovery db update helper that adds and removes node entries
based on what is configured via mappings.
For the non-mapping case a discovery is done. If no portal entries are
returned, only the portal from the storage config is kept for the
target.

Also adds a helper to gather all node entries.

Signed-off-by: Mira Limbeck <m.limbeck@proxmox.com>
---
 src/PVE/Storage/ISCSIPlugin.pm | 164 +++++++++++++++++++++++++++++++++
 1 file changed, 164 insertions(+)

diff --git a/src/PVE/Storage/ISCSIPlugin.pm b/src/PVE/Storage/ISCSIPlugin.pm
index 8f2bdb5..0f77f3f 100644
--- a/src/PVE/Storage/ISCSIPlugin.pm
+++ b/src/PVE/Storage/ISCSIPlugin.pm
@@ -79,6 +79,36 @@ my $get_local_config = sub {
     }
 };
 
+sub iscsi_node_list {
+    assert_iscsi_support();
+
+    my $cmd = [$ISCSIADM, '--mode', 'node'];
+
+    my $res = {};
+    eval {
+        run_command(
+            $cmd,
+            errmsg => 'iscsi node scan failed',
+            outfunc => sub {
+                my $line = shift;
+
+                # example: 10.67.1.144:3260,4294967295 iqn.2003-01.org.linux-iscsi.iscsi.x8664:sn.81bb080df375
+                if ($line =~ m/^$ISCSI_TARGET_RE$/) {
+                    my ($portal, $target) = ($1, $2);
+
+                    push $res->{$target}->@*, $portal;
+                }
+            },
+        );
+    };
+
+    if (my $err = $@) {
+        die $err if $err !~ m/: No records found$/i;
+    }
+
+    return $res;
+}
+
 sub iscsi_session_list {
     assert_iscsi_support();
 
@@ -368,6 +398,140 @@ sub iscsi_device_list {
     return $res;
 }
 
+sub update_iscsi_discovery_db {
+    my ($local_cfg, $cache) = @_;
+
+    if ($local_cfg->{is_mapping}) {
+        # do mapping specific discoverydb update
+
+        my $host_targets = iscsi_node_list();
+        my $mapped_targets = $local_cfg->{targets};
+        my $added = {};
+        my $removed = {};
+
+        for my $host_target (keys $host_targets->%*) {
+            if (!defined($mapped_targets->{$host_target})) {
+                # add all portals of that target to be removed
+                $removed->{$host_target} = $host_targets->{$host_target};
+            } else {
+                for my $host_target_portal ($host_targets->{$host_target}->@*) {
+                    if (!grep(/^\Q$host_target_portal\E$/, $mapped_targets->{$host_target}->@*)) {
+                        push $removed->{$host_target}->@*, $host_target_portal;
+                    }
+                }
+            }
+        }
+
+        for my $mapped_target (keys $mapped_targets->%*) {
+            if (!defined($host_targets->{$mapped_target})) {
+                # add all portals of that target to be added
+                $added->{$mapped_target} = $mapped_targets->{$mapped_target};
+            } else {
+                for my $mapped_target_portal ($mapped_targets->{$mapped_target}->@*) {
+                    if (
+                        !grep(/^\Q$mapped_target_portal\E$/,
+                            $host_targets->{$mapped_target}->@*)
+                    ) {
+                        push $added->{$mapped_target}->@*, $mapped_target_portal;
+                    }
+                }
+            }
+        }
+
+        # remove stale entries
+        for my $target (keys $removed->%*) {
+            my $target_sessions = iscsi_session($cache, $target);
+            my $sessions = {};
+            for my $target_session ($target_sessions->@*) {
+                $sessions->{ $target_session->{portal} } = $target_session->{session_id};
+            }
+
+            my $removed_sessions = [];
+            for my $portal ($removed->{$target}->@*) {
+                # log out of session before removing the stale node entry
+                # iscsiadm returns an error otherwise
+                print "removing stale iscsi session: $target via $portal\n";
+                if (defined($sessions->{$portal})) {
+                    my $cmd = [
+                        $ISCSIADM, '--mode', 'session', '--sid', $sessions->{$portal},
+                        '--logout',
+                    ];
+                    eval { run_command($cmd); };
+                    warn "failed to log out of stale session: $@\n" if $@;
+
+                    # remove logged out sessions
+                    # the list of still active sessions will be used to update cache later
+                    delete $sessions->{$portal};
+                }
+                print "removing stale iscsi target entry: $target via $portal\n";
+                my $cmd = [
+                    $ISCSIADM,
+                    '--mode',
+                    'node',
+                    '--target',
+                    $target,
+                    '--portal',
+                    $portal,
+                    '-o',
+                    'delete',
+                ];
+                eval { run_command($cmd); };
+                warn "failed to remove stale node entry: $@\n" if $@;
+            }
+            # update cache with list of active sessions
+            $cache->{iscsi_sessions}->{$target} =
+                [map { { portal => $_, session_id => $sessions->{$_} } } keys $sessions->%*];
+        }
+
+        # add new mapping entries
+        for my $target (keys $added->%*) {
+            for my $portal ($added->{$target}->@*) {
+                print "adding new iscsi target entry: $target via $portal\n";
+                my $cmd = [
+                    $ISCSIADM,
+                    '--mode',
+                    'node',
+                    '--target',
+                    $target,
+                    '--portal',
+                    $portal,
+                    '-o',
+                    'new',
+                ];
+                eval { run_command($cmd); };
+                warn "failed to add new node entry: $@\n" if $@;
+            }
+        }
+
+    } else {
+        my $portals = undef;
+        my $target = undef;
+        for my $config_target (keys $local_cfg->{targets}->%*) {
+            $target = $config_target;
+            $portals = iscsi_portals($config_target, $local_cfg->{targets}->{$config_target}->[0]);
+            last;
+        }
+        die "no target found for discovery\n" if !defined($target);
+        die "no portals found for discovery\n" if !defined($portals);
+
+        my $res = eval { iscsi_discovery($target, $portals, $cache); };
+        warn $@ if $@;
+
+        if (defined($res->{$target})) {
+            $local_cfg->{targets}->{$target} = [map { $_->{portal} } $res->{$target}->@*];
+        } else {
+            # no portal is discovered, keep already configured node entries
+            # in that case, otherwise we might remove sessions still in use
+            #
+            # cleanup in that case should be done manually after verifying that
+            # active sessions are no longer needed
+            $local_cfg->{targets}->{$target} = $portals;
+        }
+    }
+
+    return 1;
+}
+
 # Configuration
 
 sub type {
-- 
2.47.3




  parent reply	other threads:[~2026-04-30 17:34 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-30 17:26 [PATCH v2 cluster/storage/manager 00/15] storage mapping Mira Limbeck
2026-04-30 17:26 ` [PATCH v2 cluster 01/15] mapping: add storage.cfg Mira Limbeck
2026-04-30 17:27 ` [PATCH v2 storage 02/15] mapping: add base plugin Mira Limbeck
2026-04-30 17:35   ` Mira Limbeck
2026-04-30 17:27 ` [PATCH v2 storage 03/15] mapping: add iSCSI plugin Mira Limbeck
2026-04-30 17:27 ` [PATCH v2 storage 04/15] iscsi: introduce mapping support Mira Limbeck
2026-04-30 17:27 ` [PATCH v2 storage 05/15] iscsi: add helper to get local config Mira Limbeck
2026-04-30 17:27 ` [PATCH v2 storage 06/15] iscsi: change functions to handle mappings Mira Limbeck
2026-04-30 17:27 ` Mira Limbeck [this message]
2026-04-30 17:27 ` [PATCH v2 storage 08/15] iscsi: rework to update discovery db and simplify login Mira Limbeck
2026-04-30 17:27 ` [PATCH v2 storage 09/15] iscsi: remove stale sessions in non-mapping case Mira Limbeck
2026-04-30 17:27 ` [PATCH v2 storage 10/15] api: add mapping support Mira Limbeck
2026-04-30 17:27 ` [PATCH v2 storage 11/15] mapping: iscsi: add discovery-portal config option Mira Limbeck
2026-04-30 17:27 ` [PATCH v2 storage 12/15] iscsi: add support for non-persistent discovery Mira Limbeck
2026-04-30 17:38   ` Mira Limbeck
2026-04-30 17:27 ` [PATCH v2 storage 13/15] api: add non-persistent iscsi discovery option Mira Limbeck
2026-04-30 17:27 ` [POC v2 storage 14/15] mapping: add zfspool plugin Mira Limbeck
2026-04-30 17:27 ` [PATCH v2 manager 15/15] api: mapping: add storage mapping path Mira Limbeck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260430173220.441001-8-m.limbeck@proxmox.com \
    --to=m.limbeck@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal