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 E62B51FF0E6 for ; Fri, 24 Jul 2026 16:27:24 +0200 (CEST) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 9876E214C1; Fri, 24 Jul 2026 16:27:24 +0200 (CEST) From: David Riley To: pve-devel@lists.proxmox.com Subject: [PATCH pve-common v3 01/12] tools: add helpers for version comparison Date: Fri, 24 Jul 2026 16:25:17 +0200 Message-ID: <20260724142528.109453-2-d.riley@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260724142528.109453-1-d.riley@proxmox.com> References: <20260724142528.109453-1-d.riley@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1784903178482 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.102 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_LOW -0.7 Sender listed at https://www.dnswl.org/, low 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: Z2E5WWI6NWBS6EZTHUCMYRQRU6PWDNSA X-Message-ID-Hash: Z2E5WWI6NWBS6EZTHUCMYRQRU6PWDNSA X-MailFrom: d.riley@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: Move 'version_cmp' and 'pvecfg_min_version' over from qemu-server, to make them more accessible. Prepare for #7294 to enable centralized cluster wide version assertions. Originally-by: Thomas Lamprecht Originally-by: Alexandre Derumier Signed-off-by: David Riley Reviewed-by: Daniel Kral Link: https://bugzilla.proxmox.com/show_bug.cgi?id=7294 --- src/PVE/Tools.pm | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm index 2915b61..445cb74 100644 --- a/src/PVE/Tools.pm +++ b/src/PVE/Tools.pm @@ -53,6 +53,8 @@ our @EXPORT_OK = qw( extract_sensitive_params file_copy get_host_arch + pvecfg_min_version + version_cmp CLONE_NEWNS CLONE_NEWUTS CLONE_NEWIPC @@ -1628,4 +1630,43 @@ sub is_deeply { return 1; } +sub pvecfg_min_version { + my ($verstr, $major, $minor, $release) = @_; + + return 0 if !$verstr; + + if ($verstr =~ m/^(\d+)\.(\d+)(?:[.-](\d+))?/) { + return 1 if version_cmp($1, $major, $2, $minor, $3 // 0, $release) >= 0; + return 0; + } + + die "internal error: cannot check version of invalid string '$verstr'"; +} + +# gets in pairs the versions you want to compares, i.e.: +# ($a-major, $b-major, $a-minor, $b-minor, $a-extra, $b-extra, ...) +# returns 0 if same, -1 if $a is older than $b, +1 if $a is newer than $b +sub version_cmp { + my @versions = @_; + + my $size = scalar(@versions); + + return 0 if $size == 0; + + if ($size & 1) { + my (undef, $fn, $line) = caller(0); + die "cannot compare odd count of versions, called from $fn:$line\n"; + } + + for (my $i = 0; $i < $size; $i += 2) { + my ($left, $right) = splice(@versions, 0, 2); + $left //= 0; + $right //= 0; + + return 1 if $left > $right; + return -1 if $left < $right; + } + return 0; +} + 1; -- 2.47.3