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 362F41FF13C for ; Thu, 30 Apr 2026 19:33:40 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 62E7615CDF; Thu, 30 Apr 2026 19:32:54 +0200 (CEST) From: Mira Limbeck To: pve-devel@lists.proxmox.com Subject: [PATCH v2 storage 10/15] api: add mapping support Date: Thu, 30 Apr 2026 19:27:08 +0200 Message-ID: <20260430173220.441001-11-m.limbeck@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260430173220.441001-1-m.limbeck@proxmox.com> References: <20260430173220.441001-1-m.limbeck@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -1.602 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 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 Message-ID-Hash: 2DX7HUNGIIFISFEY6VDWPLEIQQE36QRK X-Message-ID-Hash: 2DX7HUNGIIFISFEY6VDWPLEIQQE36QRK X-MailFrom: mira@nena.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: introduces the necessary API to create, update and delete storage mappings Signed-off-by: Mira Limbeck --- v2: - now generic over mapping plugin src/PVE/API2/Storage/Makefile | 2 +- src/PVE/API2/Storage/Mapping.pm | 213 ++++++++++++++++++++++++++++++ src/PVE/Storage/Mapping/Plugin.pm | 14 ++ 3 files changed, 228 insertions(+), 1 deletion(-) 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..91483cd --- /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}->{$type}; + 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/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.47.3