all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Mira Limbeck <m.limbeck@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [RFC storage 2/2] api: add mapping support
Date: Mon, 10 Nov 2025 18:01:24 +0100	[thread overview]
Message-ID: <20251110170124.3460419-5-m.limbeck@proxmox.com> (raw)
In-Reply-To: <20251110170124.3460419-1-m.limbeck@proxmox.com>

ISCSI `map` parameter being optional allows iSCSI storage mappings to be
created with a non-existing map list. This can make sense since once
`map` is specified, it needs values for `node`, `target` and `portals`.

Signed-off-by: Mira Limbeck <m.limbeck@proxmox.com>
---
 src/PVE/API2/Storage/Makefile     |   2 +-
 src/PVE/API2/Storage/Mapping.pm   | 213 ++++++++++++++++++++++++++++++
 src/PVE/Storage/Mapping/ISCSI.pm  |   2 +-
 src/PVE/Storage/Mapping/Plugin.pm |  14 ++
 4 files changed, 229 insertions(+), 2 deletions(-)
 create mode 100644 src/PVE/API2/Storage/Mapping.pm

diff --git a/src/PVE/API2/Storage/Makefile b/src/PVE/API2/Storage/Makefile
index 1705080..3792cec 100644
--- a/src/PVE/API2/Storage/Makefile
+++ b/src/PVE/API2/Storage/Makefile
@@ -1,5 +1,5 @@
 
-SOURCES= Content.pm Status.pm Config.pm PruneBackups.pm Scan.pm FileRestore.pm
+SOURCES= Content.pm Status.pm Config.pm PruneBackups.pm Scan.pm FileRestore.pm Mapping.pm
 
 .PHONY: install
 install:
diff --git a/src/PVE/API2/Storage/Mapping.pm b/src/PVE/API2/Storage/Mapping.pm
new file mode 100644
index 0000000..e90345d
--- /dev/null
+++ b/src/PVE/API2/Storage/Mapping.pm
@@ -0,0 +1,213 @@
+package PVE::API2::Storage::Mapping;
+
+use strict;
+use warnings;
+
+use PVE::RESTHandler;
+use PVE::Tools qw(extract_param);
+
+use base qw(PVE::RESTHandler);
+
+my $storage_mapping_type_enum = PVE::Storage::Mapping::Plugin->lookup_types();
+
+__PACKAGE__->register_method({
+    name => 'index',
+    path => '',
+    method => 'GET',
+    description => 'List all storage mappings.',
+    permissions => {
+        user => 'all',
+    },
+    parameters => {
+        additionalProperties => 0,
+        properties => {
+            type => {
+                description => "List only storage of specific type.",
+                type => 'string',
+                enum => $storage_mapping_type_enum,
+                optional => 1,
+            },
+        },
+    },
+    returns => {
+        type => 'array',
+        items => {
+            type => 'object',
+            properties => {
+                id => {
+                    type => 'string',
+                    format => 'pve-storage-id',
+                },
+                type => {
+                    type => 'string',
+                },
+                map => {
+                    type => 'string',
+                },
+            },
+        },
+    },
+    code => sub {
+        my ($param) = @_;
+
+        my $rpcenv = PVE::RPCEnvironment::get();
+        my $authuser = $rpcenv->get_user();
+
+        my $cfg = PVE::Storage::Mapping::Plugin::config();
+
+        my $mapping_privs = ['Mapping.Audit', 'Mapping.Modify', 'Mapping.Use'];
+
+        my $res = [];
+        foreach my $id (keys $cfg->{ids}->%*) {
+            my $type = $cfg->{ids}->{$id}->{type};
+
+            next if defined($param->{type}) && $type ne $param->{type};
+            next
+                if !$rpcenv->check_any($authuser, "/mapping/storage/$type/$id", $mapping_privs, 1);
+
+            my $map = $cfg->{ids}->{$id}->{map};
+
+            push @$res,
+                {
+                    id => $id,
+                    type => $type,
+                    map => $map,
+                };
+        }
+
+        return $res;
+    },
+});
+
+__PACKAGE__->register_method({
+    name => 'create',
+    path => '',
+    method => 'POST',
+    description => 'Create a new storage mapping.',
+    permissions => {
+        check => ['perm', '/mapping/storage/', ['Mapping.Modify']],
+    },
+    parameters => PVE::Storage::Mapping::Plugin->createSchema(),
+    returns => {
+        type => 'null',
+    },
+    code => sub {
+        my ($param) = @_;
+
+        my $id = extract_param($param, 'id');
+        my $type = extract_param($param, 'type');
+
+        my $plugin = PVE::Storage::Mapping::Plugin->lookup($type);
+        my $opts = $plugin->check_config($id, $param, 1, 1);
+
+        PVE::Storage::Mapping::Plugin::lock_storage_mapping_config(
+            sub {
+                my $cfg = PVE::Storage::Mapping::Plugin::config();
+
+                die "storage mapping ID '$id' already defined\n" if defined($cfg->{ids}->{$id});
+
+                $cfg->{ids}->{$id} = $opts;
+
+                PVE::Storage::Mapping::Plugin::write_storage_mapping_config($cfg);
+            },
+            "create storage mapping failed",
+        );
+
+        return;
+    },
+});
+
+__PACKAGE__->register_method({
+    name => 'update',
+    path => '{id}',
+    method => 'PUT',
+    description => 'Update a storage mapping.',
+    permissions => {
+        check => ['perm', '/mapping/storage/{id}', ['Mapping.Modify']],
+    },
+    parameters => PVE::Storage::Mapping::Plugin->updateSchema(),
+    returns => {
+        type => 'null',
+    },
+    code => sub {
+        my ($param) = @_;
+
+        my $digest = extract_param($param, 'digest');
+        my $delete = extract_param($param, 'delete');
+        my $id = extract_param($param, 'id');
+        my $type = extract_param($param, 'type');
+
+        if ($delete) {
+            $delete = [PVE::Tools::split_list($delete)];
+        }
+
+        my $plugin = PVE::Storage::Mapping::Plugin->lookup($type);
+        my $opts = $plugin->check_config($id, $param, 1, 1);
+
+        PVE::Storage::Mapping::Plugin::lock_storage_mapping_config(
+            sub {
+                my $cfg = PVE::Storage::Mapping::Plugin::config();
+
+                PVE::Tools::assert_if_modified($cfg->{digest}, $digest) if defined($digest);
+
+                die "storage mapping ID '$id' does not exist\n" if !defined($cfg->{ids}->{$id});
+
+                my $data = $cfg->{ids}->{$id};
+                my $options = $plugin->private()->{options}->{iscsi};
+                PVE::SectionConfig::delete_from_config($data, $options, $opts, $delete);
+                foreach my $key (keys $opts->%*) {
+                    $data->{$key} = $opts->{$key};
+                }
+
+                PVE::Storage::Mapping::Plugin::write_storage_mapping_config($cfg);
+            },
+            "update storage mapping failed",
+        );
+
+        return;
+    },
+});
+
+__PACKAGE__->register_method({
+    name => 'delete',
+    path => '{id}',
+    method => 'DELETE',
+    description => 'Remove a storage mapping.',
+    permissions => {
+        check => ['perm', '/mapping/storage', ['Mapping.Modify']],
+    },
+    parameters => {
+        additionalProperties => 0,
+        properties => {
+            id => {
+                type => 'string',
+                format => 'pve-storage-id',
+            },
+        },
+    },
+    returns => {
+        type => 'null',
+    },
+    code => sub {
+        my ($param) = @_;
+
+        my $id = $param->{id};
+
+        PVE::Storage::Mapping::Plugin::lock_storage_mapping_config(
+            sub {
+                my $cfg = PVE::Storage::Mapping::Plugin::config();
+
+                if ($cfg->{ids}->{$id}) {
+                    delete $cfg->{ids}->{$id};
+                }
+
+                PVE::Storage::Mapping::Plugin::write_storage_mapping_config($cfg);
+            },
+            "delete storage mapping failed",
+        );
+
+        return;
+    },
+});
+
+1;
diff --git a/src/PVE/Storage/Mapping/ISCSI.pm b/src/PVE/Storage/Mapping/ISCSI.pm
index 34d00c5..cec2d02 100644
--- a/src/PVE/Storage/Mapping/ISCSI.pm
+++ b/src/PVE/Storage/Mapping/ISCSI.pm
@@ -43,7 +43,7 @@ sub properties {
 sub options {
     return {
         description => { optional => 1 },
-        map => {},
+        map => { optional => 1 },
     };
 }
 
diff --git a/src/PVE/Storage/Mapping/Plugin.pm b/src/PVE/Storage/Mapping/Plugin.pm
index 2da2e26..a0d3198 100644
--- a/src/PVE/Storage/Mapping/Plugin.pm
+++ b/src/PVE/Storage/Mapping/Plugin.pm
@@ -71,4 +71,18 @@ sub get_map_format {
     die "implement in subclass\n";
 }
 
+sub lock_storage_mapping_config {
+    my ($code, $errmsg) = @_;
+
+    cfs_lock_file($FILENAME, undef, $code);
+    if (my $err = $@) {
+        $errmsg ? die "$errmsg: $err" : die $err;
+    }
+}
+
+sub write_storage_mapping_config {
+    my ($cfg) = @_;
+
+    cfs_write_file($FILENAME, $cfg);
+}
 1;
-- 
2.39.5


_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


  parent reply	other threads:[~2025-11-10 17:01 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-10 17:01 [pve-devel] [RFC cluster/manager/storage 0/4] add storage " Mira Limbeck
2025-11-10 17:01 ` [pve-devel] [RFC cluster] mapping: add storage.cfg Mira Limbeck
2025-11-10 17:01 ` [pve-devel] [RFC manager] api: mapping: add storage mapping path Mira Limbeck
2025-11-10 17:01 ` [pve-devel] [RFC storage 1/2] add basic mapping support and iSCSI mapping plugin Mira Limbeck
2025-11-10 17:01 ` Mira Limbeck [this message]
2025-11-11  9:08 ` [pve-devel] [RFC cluster/manager/storage 0/4] add storage mapping support Mira Limbeck
2025-11-11 16:56 ` Friedrich Weber

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=20251110170124.3460419-5-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal