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 747331FF0E1 for ; Mon, 16 Nov 2026 01:47:53 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 63106216F6; Mon, 16 Nov 2026 01:47:18 +0100 (CET) From: Thomas Lamprecht To: pve-devel@lists.proxmox.com Subject: [PATCH cluster 06/34] cluster: add helper to read the cluster ID Date: Mon, 16 Nov 2026 01:46:18 +0100 Message-ID: <20261116004703.2908294-7-t.lamprecht@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261116004703.2908294-1-t.lamprecht@proxmox.com> References: <20261116004703.2908294-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: 1794790029094 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.221 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: XQ37QXOTERXJTBML2UEUHEZQB6BEXBDG X-Message-ID-Hash: XQ37QXOTERXJTBML2UEUHEZQB6BEXBDG 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 --- src/PVE/Cluster.pm | 16 +++++++++++++ src/pmxcfs/status.c | 1 + src/test/Makefile | 6 ++++- src/test/test_cluster_id.pl | 47 +++++++++++++++++++++++++++++++++++++ 4 files changed, 69 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 10c5686..010956b 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, @@ -603,6 +604,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 4f80641..641d807 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 cdd37d0..c84c435 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 +check: corosync-parser-test test-mac-prefix test-cluster-id .PHONY: corosync-parser-test corosync-parser-test: @@ -14,5 +14,9 @@ corosync-parser-test: test-mac-prefix: perl test_mac_prefix.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..31a0cbf --- /dev/null +++ b/src/test/test_cluster_id.pl @@ -0,0 +1,47 @@ +use strict; +use warnings; + +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