From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id 74B071FF065 for ; Mon, 23 Nov 2026 14:35:11 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 006B821749; Mon, 23 Nov 2026 14:34:32 +0100 (CET) From: Thomas Lamprecht 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 Message-ID: <20261123133412.3710030-2-t.lamprecht@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261123133412.3710030-1-t.lamprecht@proxmox.com> References: <20261123133412.3710030-1-t.lamprecht@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1795440863845 X-SPAM-LEVEL: Spam detection results: 0 AWL -0.002 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) POISEN_SPAM_PILL 0.1 Meta: its spam POISEN_SPAM_PILL_1 0.1 random spam to be learned in bayes POISEN_SPAM_PILL_3 0.1 random spam to be learned in bayes RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: PZMGXNIQXVGBAECKN4WBYW6MYT4FHQXB X-Message-ID-Hash: PZMGXNIQXVGBAECKN4WBYW6MYT4FHQXB X-MailFrom: t.lamprecht@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: 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 --- 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();