From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id 7A1381FF15E for ; Mon, 10 Nov 2025 18:01:20 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 911A51C4C6; Mon, 10 Nov 2025 18:02:00 +0100 (CET) From: Mira Limbeck To: pve-devel@lists.proxmox.com Date: Mon, 10 Nov 2025 18:01:24 +0100 Message-Id: <20251110170124.3460419-5-m.limbeck@proxmox.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20251110170124.3460419-1-m.limbeck@proxmox.com> References: <20251110170124.3460419-1-m.limbeck@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL -1.655 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment KAM_LAZY_DOMAIN_SECURITY 1 Sending domain does not have any anti-forgery methods KAM_MAILER 2 Automated Mailer Tag Left in Email PROLO_LEO1 0.1 Meta Catches all Leo drug variations so far RCVD_IN_VALIDITY_CERTIFIED_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_RPBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_SAFE_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RDNS_NONE 0.793 Delivered to internal network by a host with no rDNS SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_NONE 0.001 SPF: sender does not publish an SPF Record Subject: [pve-devel] [RFC storage 2/2] api: add mapping support X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox VE development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pve-devel-bounces@lists.proxmox.com Sender: "pve-devel" 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 --- 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