From: Thomas Lamprecht <t.lamprecht@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH v5 storage 01/26] api: config: activate a new storage without holding the lock
Date: Mon, 23 Nov 2026 14:31:53 +0100 [thread overview]
Message-ID: <20261123133412.3710030-2-t.lamprecht@proxmox.com> (raw)
In-Reply-To: <20261123133412.3710030-1-t.lamprecht@proxmox.com>
Adding a storage activates it, to report a problem right away, while it
holds the lock on the storage configuration. For a storage that connects
over the network, like an iSCSI target or an NVMe over Fabrics
subsystem, this can wait for unreachable portals, which blocks every
other change to the storage configuration in the cluster meanwhile. An
LVM storage on such a base also activates the base storage and volume in
its add hook, with the configuration locked.
Activate a new storage once the configuration is written and the lock
released. Another activation, like the one of pvestatd, can then run at
the same time, so a failed one is retried once. If it still fails, take
the lock again to remove the storage, unless it changed or became the
base of another storage meanwhile, and report the error as before. Other
nodes can see the storage for that time, and keep what their activations
connected or mounted.
Activate the base storage and volume of an LVM storage on an iSCSI or
NVMe over Fabrics base before taking the lock. With all paths answering,
the add hook then finds them active and does not wait for the network
with the configuration locked.
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
---
ApiChangeLog | 11 +++
src/PVE/API2/Storage/Config.pm | 69 +++++++++++----
src/test/Makefile | 4 +
src/test/run_storage_create_tests.pl | 120 +++++++++++++++++++++++++++
4 files changed, 189 insertions(+), 15 deletions(-)
create mode 100755 src/test/run_storage_create_tests.pl
diff --git a/ApiChangeLog b/ApiChangeLog
index e8fe58e8..40c3bea3 100644
--- a/ApiChangeLog
+++ b/ApiChangeLog
@@ -131,6 +131,17 @@ Future changes should be documented in here.
`cluster_lock_storage()`, and must not rely on the guest staying locked while it runs. This
depends on the version of pve-storage, not on the API version.
+* Activate a new storage after releasing the configuration lock
+
+ Adding a storage calls `activate_storage()` once its section is written and the lock on the
+ storage configuration is released, so that waiting for a network does not block other changes to
+ the configuration. As another activation can run at the same time, a failed one is retried once.
+ If it still fails, the section is removed again, unless it changed or became the base of another
+ storage meanwhile. Other nodes can activate the storage meanwhile and keep what they connected or
+ mounted. The base storage and volume of an LVM storage on an iSCSI or NVMe over Fabrics base are
+ activated before the lock is taken, so that `on_add_hook()` usually finds them active. This
+ depends on the version of pve-storage, not on the API version.
+
## Version 16:
* Add `volname_for_format()`, `get_parsed_format()`, `volname_with_format()`, `is_valid_format()` and
diff --git a/src/PVE/API2/Storage/Config.pm b/src/PVE/API2/Storage/Config.pm
index ce41c927..47d57282 100755
--- a/src/PVE/API2/Storage/Config.pm
+++ b/src/PVE/API2/Storage/Config.pm
@@ -274,7 +274,21 @@ __PACKAGE__->register_method({
assert_cluster_knows_content_types($opts->{content});
- my $returned_config;
+ # An LVM storage on a SAN base activates the base in its add hook. Connecting it can wait
+ # for the network, which must not happen while the configuration is locked, so activate it
+ # before. With all paths answering, the hook then finds it active.
+ if ($type eq 'lvm' && (my $base = $opts->{base})) {
+ my ($baseid) = PVE::Storage::parse_volume_id($base, 1);
+ my $cfg = PVE::Storage::config();
+ my $basecfg =
+ defined($baseid) ? PVE::Storage::storage_config($cfg, $baseid, 1) : undef;
+ if ($basecfg && ($basecfg->{type} eq 'iscsi' || $basecfg->{type} eq 'nvmeof')) {
+ PVE::Storage::activate_storage($cfg, $baseid);
+ PVE::Storage::activate_volumes($cfg, [$base]);
+ }
+ }
+
+ my ($returned_config, $written, $added);
PVE::Storage::lock_storage_config(
sub {
my $cfg = PVE::Storage::config();
@@ -293,25 +307,50 @@ __PACKAGE__->register_method({
. " in Proxmox VE 9. Use 'create-base-path' or 'create-subdirs' instead.\n";
}
- eval {
- # try to activate if enabled on local node,
- # we only do this to detect errors/problems sooner
- if (PVE::Storage::storage_check_enabled($cfg, $storeid, undef, 1)) {
- PVE::Storage::activate_storage($cfg, $storeid);
- }
- };
- if (my $err = $@) {
- eval { $plugin->on_delete_hook($storeid, $opts) };
- warn "$@\n" if $@;
- die $err;
- }
-
PVE::Storage::write_config($cfg);
-
+ $written = $cfg;
+ $added = PVE::Storage::config()->{ids}->{$storeid};
},
"create storage failed",
);
+ # The activation only reports a problem early. It can wait for the network, so it runs
+ # without the lock. Another activation, like the one of pvestatd, can run at the same time,
+ # and a mount or connect that lost that race fails, so a failure is retried once. If it
+ # still fails, the storage is removed again, unless it changed or got used meanwhile.
+ if (PVE::Storage::storage_check_enabled($written, $storeid, undef, 1)) {
+ eval { PVE::Storage::activate_storage($written, $storeid) };
+ if (my $err = $@) {
+ sleep(1);
+ eval { PVE::Storage::activate_storage($written, $storeid) };
+ if ($@) {
+ my $removed;
+ eval {
+ local $Storable::canonical = 1;
+ PVE::Storage::lock_storage_config(
+ sub {
+ my $cfg = PVE::Storage::config();
+ my $scfg = $cfg->{ids}->{$storeid} // return;
+ return if Storable::freeze($scfg) ne Storable::freeze($added);
+ return if PVE::Storage::storage_is_used($cfg, $storeid);
+ delete $cfg->{ids}->{$storeid};
+ PVE::Storage::write_config($cfg);
+ $removed = 1;
+ eval { $plugin->on_delete_hook($storeid, $opts) };
+ warn "$@\n" if $@;
+ },
+ "removing it failed",
+ );
+ };
+ die "create storage failed: $err" if $removed;
+ chomp(my $kept = $@);
+ chomp($err);
+ die "storage '$storeid' was added, but its activation failed - $err"
+ . ($kept ? " ($kept)" : '') . "\n";
+ }
+ }
+ }
+
my $res = {
storage => $storeid,
type => $type,
diff --git a/src/test/Makefile b/src/test/Makefile
index 3b902c43..500f9493 100644
--- a/src/test/Makefile
+++ b/src/test/Makefile
@@ -25,6 +25,7 @@ test: \
test_san_lun_scan \
test_san_taint \
test_sensitive_properties \
+ test_storage_create \
test_volume_access \
test_zfspoolplugin \
@@ -115,3 +116,6 @@ test_san_api: run_san_api_tests.pl run_san_wipe_tests.pl run_san_nvme_tests.pl
test_disk_api: run_disk_api_tests.pl
./run_disk_api_tests.pl
+
+test_storage_create: run_storage_create_tests.pl
+ ./run_storage_create_tests.pl
diff --git a/src/test/run_storage_create_tests.pl b/src/test/run_storage_create_tests.pl
new file mode 100755
index 00000000..2aacbab7
--- /dev/null
+++ b/src/test/run_storage_create_tests.pl
@@ -0,0 +1,120 @@
+#!/usr/bin/perl
+
+use v5.36;
+use lib qw(..);
+use Test::MockModule;
+use Test::More;
+
+use PVE::API2::Storage::Config;
+use PVE::Storage;
+use PVE::Storage::LVMPlugin;
+
+# Adding a storage must not wait for its connection while the configuration is locked.
+
+my $raw = "iscsi: san\n\tportal 192.0.2.1\n\ttarget iqn.2026-01.test:a\n\tcontent none\n\n"
+ . "dir: local\n\tpath /var/lib/vz\n\tcontent iso\n";
+our $locked;
+my (@events, @failures, $during, $lock_fails);
+my $storage = Test::MockModule->new('PVE::Storage');
+$storage->mock(
+ lock_storage_config => sub($code, $errmsg) {
+ push @events, 'lock';
+ die "$errmsg: got lock request timeout\n" if $lock_fails;
+ local $locked = 1;
+ $code->();
+ push @events, 'unlock';
+ },
+ config => sub { PVE::Storage::Plugin->parse_config('storage.cfg', $raw) },
+ write_config => sub { $raw = PVE::Storage::Plugin->write_config('storage.cfg', $_[0]) },
+ activate_storage => sub($cfg, $storeid, @) {
+ push @events, "activate $storeid" . ($locked ? ' locked' : '');
+ $during->() if $during;
+ die shift(@failures) if @failures;
+ },
+ activate_volumes => sub($cfg, $vols, @) {
+ push @events, "volumes @$vols" . ($locked ? ' locked' : '');
+ },
+);
+my $lvm = Test::MockModule->new('PVE::Storage::LVMPlugin');
+$lvm->mock(on_add_hook => sub { push @events, 'hook' . ($locked ? ' locked' : ''); return });
+
+my $add = sub(%param) {
+ @events = ();
+ return eval { PVE::API2::Storage::Config->create({%param}); 1 } ? '' : $@;
+};
+my $dir = sub($storeid) { (type => 'dir', storage => $storeid, path => "/srv/$storeid") };
+
+is($add->($dir->('plain')), '', 'added');
+is_deeply(\@events, [qw(lock unlock), 'activate plain'], 'activated after the lock');
+
+@failures = ("mount busy\n");
+is($add->($dir->('raced')), '', 'a failure that another activation caused');
+is_deeply(\@events, [qw(lock unlock), ('activate raced') x 2], 'is tried once more');
+
+@failures = ("no answer\n", "still no answer\n");
+like($add->($dir->('broken')), qr/^create storage failed: no answer$/, 'a failure fails');
+is_deeply(
+ \@events,
+ [qw(lock unlock), ('activate broken') x 2, qw(lock unlock)],
+ 'and takes the lock again',
+);
+unlike($raw, qr/broken/, 'to remove the storage');
+like($raw, qr/plain/, 'but not another one');
+
+@failures = ("no answer\n", "still no answer\n");
+$during = sub { $raw =~ s{(dir: changed\n)}{$1\tdisable 1\n} if $raw !~ /disable 1/ };
+like($add->($dir->('changed')), qr/was added, but its activation failed/, 'a changed storage');
+like($raw, qr/changed/, 'is kept');
+
+@failures = ("no answer\n", "still no answer\n");
+$during =
+ sub { $raw .= "\nlvm: onit\n\tvgname vg\n\tbase used:0.0.1.scsi-3600\n" if $raw !~ /onit/ };
+like($add->($dir->('used')), qr/was added, but its activation failed/, 'so is one in use');
+like($raw, qr/dir: used/, 'as base of another one');
+$during = undef;
+
+@failures = ("no answer\n", "still no answer\n");
+my $calls = 0;
+$storage->mock(
+ lock_storage_config => sub($code, $errmsg) {
+ push @events, 'lock';
+ die "$errmsg: got lock request timeout\n" if $calls++;
+ local $locked = 1;
+ $code->();
+ push @events, 'unlock';
+ },
+);
+like(
+ $add->($dir->('stuck')),
+ qr/activation failed - no answer \(removing it failed: got lock request timeout\)/,
+ 'a removal without the lock reports both errors',
+);
+like($raw, qr/dir: stuck/, 'and keeps the storage');
+$storage->mock(
+ lock_storage_config => sub($code, $errmsg) {
+ push @events, 'lock';
+ local $locked = 1;
+ $code->();
+ push @events, 'unlock';
+ },
+);
+
+my $base = 'san:0.0.1.scsi-36001405aabbccddeeff00112233445566';
+is(
+ $add->(type => 'lvm', storage => 'shared', vgname => 'vg', base => $base, shared => 1),
+ '',
+ 'an LVM storage on a base volume is added',
+);
+is_deeply(
+ \@events,
+ ['activate san', "volumes $base", 'lock', 'hook locked', 'unlock', 'activate shared'],
+ 'its base is activated before the lock',
+);
+$add->(type => 'lvm', storage => 'other', vgname => 'vg2', base => 'local:iso/x.iso');
+is_deeply(
+ \@events,
+ ['lock', 'hook locked', 'unlock', 'activate other'],
+ 'but not a base that is no SAN storage',
+);
+
+done_testing();
next prev parent reply other threads:[~2026-11-23 13:35 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 13:31 applied: [PATCH v5 storage,docs,manager 00/26] NVMe over Fabrics storage Thomas Lamprecht
2026-11-23 13:31 ` Thomas Lamprecht [this message]
2026-11-23 13:31 ` [PATCH v5 storage 02/26] diskmanage: skip SMART queries for NVMe fabric namespaces Thomas Lamprecht
2026-11-23 13:31 ` [PATCH v5 storage 03/26] diskmanage: list NVMe namespaces attached over TCP or RDMA for SAN only Thomas Lamprecht
2026-11-23 13:31 ` [PATCH v5 storage 04/26] api: disks: refuse a local wipe of a network NVMe namespace Thomas Lamprecht
2026-11-23 13:31 ` [PATCH v5 storage 05/26] nvmeof: add a storage for NVMe over Fabrics subsystems Thomas Lamprecht
2026-11-23 13:31 ` [PATCH v5 storage 06/26] d/control: bump pve-common for plain parameter error names Thomas Lamprecht
2026-11-23 13:31 ` [PATCH v5 storage 07/26] api: config: check the cluster version before the first nvmeof storage Thomas Lamprecht
2026-11-23 13:32 ` [PATCH v5 storage 08/26] lvm: allow a volume group on an NVMe over Fabrics namespace Thomas Lamprecht
2026-11-23 13:32 ` [PATCH v5 storage 09/26] api: scan: add NVMe over Fabrics discovery Thomas Lamprecht
2026-11-23 13:32 ` [PATCH v5 storage 10/26] pvesm: add a scan command for NVMe over Fabrics Thomas Lamprecht
2026-11-23 13:32 ` [PATCH v5 storage 11/26] api: san luns: attribute NVMe namespaces to storage Thomas Lamprecht
2026-11-23 13:32 ` [PATCH v5 storage 12/26] api: add node-level NVMe over Fabrics status and controller removal Thomas Lamprecht
2026-11-23 13:32 ` [PATCH v5 storage 13/26] multipath: report the NVMe host NQN of each node Thomas Lamprecht
2026-11-23 13:32 ` [PATCH v5 storage 14/26] multipath: report NVMe over Fabrics namespaces in the cluster status Thomas Lamprecht
2026-11-23 13:32 ` [PATCH v5 storage 15/26] d/control: break pve-manager versions without namespace notifications Thomas Lamprecht
2026-11-23 13:32 ` [PATCH v5 docs 16/26] storage: document NVMe over Fabrics storages Thomas Lamprecht
2026-11-23 13:32 ` [PATCH v5 manager 17/26] report: add the NVMe over Fabrics host NQN and subsystems Thomas Lamprecht
2026-11-23 13:32 ` [PATCH v5 manager 18/26] ui: storage: add NVMe over Fabrics storage Thomas Lamprecht
2026-11-23 13:32 ` [PATCH v5 manager 19/26] ui: lvm: offer NVMe over Fabrics storages as base storage Thomas Lamprecht
2026-11-23 13:32 ` [PATCH v5 manager 20/26] ui: storage: SAN setup: ignore spaces around a typed portal Thomas Lamprecht
2026-11-23 13:32 ` [PATCH v5 manager 21/26] ui: storage: add a guided setup for shared LVM on NVMe over Fabrics Thomas Lamprecht
2026-11-23 13:32 ` [PATCH v5 manager 22/26] ui: node san: list NVMe over Fabrics controllers Thomas Lamprecht
2026-11-23 13:32 ` [PATCH v5 manager 23/26] ui: san: show the NVMe host NQN of each node Thomas Lamprecht
2026-11-23 13:32 ` [PATCH v5 manager 24/26] ui: san: show that a namespace uses native multipath Thomas Lamprecht
2026-11-23 13:32 ` [PATCH v5 manager 25/26] pvestatd: notify on NVMe over Fabrics namespaces as on multipath maps Thomas Lamprecht
2026-11-23 13:32 ` [PATCH v5 manager 26/26] d/control: bump versioned dependencies for NVMe over Fabrics Thomas Lamprecht
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=20261123133412.3710030-2-t.lamprecht@proxmox.com \
--to=t.lamprecht@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