From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id 948BA1FF0DA for ; Thu, 19 Nov 2026 20:19:56 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id E070C21756; Thu, 19 Nov 2026 20:19:25 +0100 (CET) From: Thomas Lamprecht To: pve-devel@lists.proxmox.com Subject: [PATCH cluster v5 06/41] cluster: add helper to read the cluster ID Date: Thu, 19 Nov 2026 20:18:38 +0100 Message-ID: <20261119191914.2336934-7-t.lamprecht@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261119191914.2336934-1-t.lamprecht@proxmox.com> References: <20261119191914.2336934-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: 1795115959435 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.158 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) 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: ZHJ3MMW3Z5HBJOYTAV4MGZ6SOUK7FT3T X-Message-ID-Hash: ZHJ3MMW3Z5HBJOYTAV4MGZ6SOUK7FT3T 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: Consumers such as the API should not need to know the cluster ID file format. Add get_cluster_id() with the same validation rules as pmxcfs. It returns undef for missing, empty, or invalid content, treating those cases as "no ID yet". Register the file for version tracking so cfs_read_file() refreshes its cached value when the file changes. Do not provide a writer: pmxcfs owns the file. Signed-off-by: Thomas Lamprecht Reviewed-by: Dominik Csapak Tested-by: Dominik Csapak --- no changes since v3 changes v2 -> v3: * rebase only (test Makefile) changes v1 -> v2: * use v5.36 in the new test script src/PVE/Cluster.pm | 16 +++++++++++++ src/pmxcfs/status.c | 1 + src/test/Makefile | 6 ++++- src/test/test_cluster_id.pl | 46 +++++++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 src/test/test_cluster_id.pl diff --git a/src/PVE/Cluster.pm b/src/PVE/Cluster.pm index 6ace846..076e49f 100644 --- a/src/PVE/Cluster.pm +++ b/src/PVE/Cluster.pm @@ -54,6 +54,7 @@ my $observed = { 'replication.cfg' => 1, 'corosync.conf' => 1, 'corosync.conf.new' => 1, + 'cluster-id' => 1, 'firewall/cluster.fw' => 1, 'user.cfg' => 1, 'domains.cfg' => 1, @@ -604,6 +605,21 @@ sub cfs_write_file { PVE::Tools::file_set_contents($fsname, $raw, undef, $force_utf8); } +my $parse_cluster_id = sub { + my ($filename, $raw) = @_; + + return undef if !defined($raw); + return $raw =~ m/^([a-z2-7]{20})\n?\z/ ? $1 : undef; +}; + +cfs_register_file('cluster-id', $parse_cluster_id); + +# Returns the ID of the cluster, or of the node if it is standalone, undef if there is none yet. IPC +# errors other than a missing file are raised, like for other pmxcfs reads. +sub get_cluster_id { + return cfs_read_file('cluster-id'); +} + my $cfs_lock = sub { my ($lockid, $timeout, $code, @param) = @_; diff --git a/src/pmxcfs/status.c b/src/pmxcfs/status.c index afffa96..7977682 100644 --- a/src/pmxcfs/status.c +++ b/src/pmxcfs/status.c @@ -80,6 +80,7 @@ typedef struct { static memdb_change_t memdb_change_array[] = { {.path = "corosync.conf"}, {.path = "corosync.conf.new"}, + {.path = "cluster-id"}, {.path = "storage.cfg"}, {.path = "user.cfg"}, {.path = "domains.cfg"}, diff --git a/src/test/Makefile b/src/test/Makefile index 3a47b8c..9b65883 100644 --- a/src/test/Makefile +++ b/src/test/Makefile @@ -4,7 +4,7 @@ cpgtest: cpgtest.c gcc -Wall cpgtest.c $(shell pkg-config --cflags --libs libcpg libqb) -o cpgtest .PHONY: check install clean distclean -check: corosync-parser-test test-mac-prefix test-token-policy +check: corosync-parser-test test-mac-prefix test-token-policy test-cluster-id .PHONY: corosync-parser-test corosync-parser-test: @@ -18,5 +18,9 @@ test-mac-prefix: test-token-policy: perl test_token_policy.pl +.PHONY: test-cluster-id +test-cluster-id: + perl test_cluster_id.pl + distclean: clean clean: diff --git a/src/test/test_cluster_id.pl b/src/test/test_cluster_id.pl new file mode 100644 index 0000000..33102e0 --- /dev/null +++ b/src/test/test_cluster_id.pl @@ -0,0 +1,46 @@ +use v5.36; + +use Test::MockModule; +use Test::More; + +use lib ('.', '..'); + +use PVE::Cluster; + +my $raw; +my $cluster_module = Test::MockModule->new('PVE::Cluster'); +$cluster_module->mock( + get_config => sub { + my ($path) = @_; + die "unexpected path '$path'\n" if $path ne 'cluster-id'; + return $raw; + }, +); + +my $cases = [ + ["abcdefghijklmnopqrst\n", 'abcdefghijklmnopqrst', 'valid ID'], + ["uvwxyz234567abcdefgh\n", 'uvwxyz234567abcdefgh', 'valid ID with digits'], + ['abcdefghijklmnopqrst', 'abcdefghijklmnopqrst', 'valid ID without newline'], + [undef, undef, 'missing file'], + ['', undef, 'empty file'], + ["\n", undef, 'only a newline'], + ["abcdefghijklmnopqrst\n\n", undef, 'trailing empty line'], + ["abcdefghijklmnopqrst \n", undef, 'trailing space'], + [" abcdefghijklmnopqrst\n", undef, 'leading space'], + ["abcdefghijklmnopqrs\n", undef, 'too short'], + ["abcdefghijklmnop\n", undef, '16 characters'], + ["abcdefghijklmnopqrstu\n", undef, 'too long'], + ['abcdefghijklmnopqrstu', undef, 'too long without newline'], + ["ABCDEFGHIJKLMNOPQRST\n", undef, 'uppercase'], + ["abcdefghijklmnopqr01\n", undef, 'digits outside the base32 alphabet'], + ["abcdefghijklmnopqr89\n", undef, 'more digits outside the base32 alphabet'], + ["abcdefgh\njklmnopqrst\n", undef, 'embedded newline'], +]; + +for my $case ($cases->@*) { + my ($content, $expected, $name) = $case->@*; + $raw = $content; + is(PVE::Cluster::get_cluster_id(), $expected, $name); +} + +done_testing(); -- 2.47.3