From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <pve-devel-bounces@lists.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
	by lore.proxmox.com (Postfix) with ESMTPS id D8D081FF389
	for <inbox@lore.proxmox.com>; Wed, 22 May 2024 14:11:56 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id 25F33BAC5;
	Wed, 22 May 2024 14:12:14 +0200 (CEST)
To: pve-devel@lists.proxmox.com
Date: Wed, 22 May 2024 14:05:50 +0200
In-Reply-To: <20240522120553.49114-1-krambrock@hrz.uni-marburg.de>
References: <20240522120553.49114-1-krambrock@hrz.uni-marburg.de>
MIME-Version: 1.0
Message-ID: <mailman.225.1716379933.319.pve-devel@lists.proxmox.com>
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Post: <mailto:pve-devel@lists.proxmox.com>
From: Daniel Krambrock via pve-devel <pve-devel@lists.proxmox.com>
Precedence: list
Cc: Daniel Krambrock <krambrock@hrz.uni-marburg.de>
X-Mailman-Version: 2.1.29
X-BeenThere: pve-devel@lists.proxmox.com
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
Reply-To: Proxmox VE development discussion <pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
Subject: [pve-devel] [PATCH manager] close #4369: make VMID suggestion
 strategy configurable
Content-Type: multipart/mixed; boundary="===============1732891111198085800=="
Errors-To: pve-devel-bounces@lists.proxmox.com
Sender: "pve-devel" <pve-devel-bounces@lists.proxmox.com>

--===============1732891111198085800==
Content-Type: message/rfc822
Content-Disposition: inline

Return-Path: <root@vhrz2211.hrz.uni-marburg.de>
X-Original-To: pve-devel@lists.proxmox.com
Delivered-To: pve-devel@lists.proxmox.com
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (2048 bits))
	(No client certificate requested)
	by lists.proxmox.com (Postfix) with ESMTPS id 2A0A4C4A04
	for <pve-devel@lists.proxmox.com>; Wed, 22 May 2024 14:12:13 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id 0C390B8C1
	for <pve-devel@lists.proxmox.com>; Wed, 22 May 2024 14:11:43 +0200 (CEST)
Received: from vhrz1173.hrz.uni-marburg.de (vhrz1173.HRZ.Uni-Marburg.DE [137.248.1.43])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (2048 bits))
	(No client certificate requested)
	by firstgate.proxmox.com (Proxmox) with ESMTPS
	for <pve-devel@lists.proxmox.com>; Wed, 22 May 2024 14:11:41 +0200 (CEST)
Received: from vhrz1173.hrz.uni-marburg.de (vhrz1173.HRZ.Uni-Marburg.DE [137.248.1.43])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (4096 bits))
	(No client certificate requested)
	by vhrz1865.HRZ.Uni-Marburg.DE (Postfix) with ESMTPS id 4VkqlX6cZyzygR;
	Wed, 22 May 2024 14:06:16 +0200 (CEST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=uni-marburg.de;
	s=dkim; t=1716379576;
	h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
	 to:to:cc:cc:mime-version:mime-version:content-type:
	 content-transfer-encoding:content-transfer-encoding:
	 in-reply-to:in-reply-to:references:references;
	bh=VPo4tdaGKrNOC0QEBP16pty+c0Uk2ffqJQd0ovRJ6b8=;
	b=iycp6XPbJDhSOzCvjl6Ec5020BUTJPNXNyjJB9dQek2SC3h6RiI2c7+ACHiH7eRjAkL2Fm
	ROc9bzvfh0/KthvY8BWP/uwroCdw82Xd+9qY3tAHfEVTxYyebc23jUhfLqdlThikpu/cyh
	pXOoepuhx9wfUZC0PBDpMiGKtXax+j8=
Authentication-Results: smtpd-out;
	dkim=none;
	spf=none (smtpd-out: domain of root@vhrz2211.hrz.uni-marburg.de has no SPF policy when checking 137.248.1.43) smtp.mailfrom=root@vhrz2211.hrz.uni-marburg.de
Received: from vhrz2211.hrz.uni-marburg.de (vhrz2211.HRZ.Uni-Marburg.DE [172.25.3.10])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256)
	(No client certificate requested)
	by vhrz1865.HRZ.Uni-Marburg.DE (Postfix) with ESMTPS id 4VkqlX5XxjzygK;
	Wed, 22 May 2024 14:06:16 +0200 (CEST)
Received: from root by vhrz2211.hrz.uni-marburg.de with local (Exim 4.96)
	(envelope-from <root@vhrz2211.hrz.uni-marburg.de>)
	id 1s9kjY-000Cob-2K;
	Wed, 22 May 2024 14:06:16 +0200
From: Daniel Krambrock <krambrock@hrz.uni-marburg.de>
To: pve-devel@lists.proxmox.com
Subject: [PATCH manager] close #4369: make VMID suggestion strategy configurable
Date: Wed, 22 May 2024 14:05:50 +0200
Message-Id: <20240522120553.49114-2-krambrock@hrz.uni-marburg.de>
X-Mailer: git-send-email 2.39.2
In-Reply-To: <20240522120553.49114-1-krambrock@hrz.uni-marburg.de>
References: <20240522120553.49114-1-krambrock@hrz.uni-marburg.de>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-Rspamd-Score: -4.80 / 12.00 / 15.00
X-Rspamd-Queue-Id: 4VkqlX6cZyzygR
X-SPAM-LEVEL: Spam detection results:  0
	AWL                     0.025 Adjusted score from AWL reputation of From: address
	BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
	DKIM_SIGNED               0.1 Message has a DKIM or DK signature, not necessarily valid
	DKIM_VALID               -0.1 Message has at least one valid DKIM or DK signature
	DMARC_PASS               -0.1 DMARC pass policy
	SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
	SPF_NONE                0.001 SPF: sender does not publish an SPF Record

VMID suggestion strategy is set in datacenter config

Signed-off-by: Daniel Krambrock <krambrock@hrz.uni-marburg.de>
---
 PVE/API2/Cluster.pm           | 16 +++++++++--
 PVE/Makefile                  |  1 +
 PVE/UsedVmidList.pm           | 53 +++++++++++++++++++++++++++++++++++
 www/manager6/dc/OptionView.js | 14 +++++++++
 4 files changed, 82 insertions(+), 2 deletions(-)
 create mode 100644 PVE/UsedVmidList.pm

diff --git a/PVE/API2/Cluster.pm b/PVE/API2/Cluster.pm
index 04387ab4..be79ddf0 100644
--- a/PVE/API2/Cluster.pm
+++ b/PVE/API2/Cluster.pm
@@ -20,6 +20,7 @@ use PVE::RPCEnvironment;
 use PVE::SafeSyslog;
 use PVE::Storage;
 use PVE::Tools qw(extract_param);
+use PVE::UsedVmidList;
 
 use PVE::API2::ACMEAccount;
 use PVE::API2::ACMEPlugin;
@@ -813,12 +814,23 @@ __PACKAGE__->register_method({
 
 	my $dc_conf = PVE::Cluster::cfs_read_file('datacenter.cfg');
 	my $next_id = $dc_conf->{'next-id'} // {};
+	my $strategy = $dc_conf->{'next-id-strategy'} // "next-free";
 
 	my $lower = $next_id->{lower} // 100;
 	my $upper = $next_id->{upper} // (1000 * 1000); # note, lower than the schema-maximum
 
-	for (my $i = $lower; $i < $upper; $i++) {
-	    return $i if !defined($idlist->{$i});
+	if ($strategy eq "next-free") {
+	    for (my $i = $lower; $i < $upper; $i++) {
+	        return $i if !defined($idlist->{$i});
+	    }
+	} elsif ($strategy eq "max+1") {
+	    return %$idlist ? (List::Util::max(keys %$idlist) + 1) : $lower
+	} elsif ($strategy eq "list") {
+	    for (my $i = $lower; $i < $upper; $i++) {
+	        return $i if (!defined($idlist->{$i}) and !PVE::UsedVmidList::is_on_vmid_list($i)) ;
+	    }
+	} else {
+	    die "unable to get any free VMID with strategy $strategy\n";
 	}
 
 	die "unable to get any free VMID in range [$lower, $upper]\n";
diff --git a/PVE/Makefile b/PVE/Makefile
index 660de4d0..fe627296 100644
--- a/PVE/Makefile
+++ b/PVE/Makefile
@@ -14,6 +14,7 @@ PERLSOURCE = 			\
 	Jobs.pm			\
 	NodeConfig.pm		\
 	Report.pm		\
+	UsedVmidList.pm		\
 	VZDump.pm
 
 all: pvecfg.pm $(SUBDIRS)
diff --git a/PVE/UsedVmidList.pm b/PVE/UsedVmidList.pm
new file mode 100644
index 00000000..a60c75d6
--- /dev/null
+++ b/PVE/UsedVmidList.pm
@@ -0,0 +1,53 @@
+package PVE::UsedVmidList;
+
+use strict;
+use warnings;
+
+use PVE::Cluster;
+
+my $parse_vmid_list = sub {
+    my ($filename, $raw) = @_;
+
+    return [] if !defined($raw);
+
+    my @parsed;
+    my @lines = split(/\n/, $raw);
+    foreach my $line (@lines) {
+	next if $line =~ m/^\s*$/;
+
+	if ($line =~ m/^(\d+)$/) {
+	    push(@parsed, $1);
+	} else {
+	    warn "Skipping invalid used_vmids.list entry: $line\n";
+	}
+    }
+
+    return \@parsed;
+};
+
+my $write_vmid_list = sub {
+    my ($filename, @data) = @_;
+
+    return join("\n", sort @data);
+};
+
+PVE::Cluster::cfs_register_file('used_vmids.list', $parse_vmid_list, $write_vmid_list);
+
+sub add_vmid {
+    my ($vmid) = @_;
+
+    PVE::Cluster::cfs_lock_file('used_vmids.list', 10, sub {
+	my $vmid_list = PVE::Cluster::cfs_read_file('used_vmids.list');
+
+	push(@$vmid_list, $vmid);
+	PVE::Cluster::cfs_write_file('used_vmids.list', join("\n", @$vmid_list));
+    });
+}
+
+sub is_on_vmid_list {
+    my ($vmid) = @_;
+    my $vmid_list = PVE::Cluster::cfs_read_file('used_vmids.list');
+    return scalar(grep { $_ == $vmid } @$vmid_list);
+}
+
+1;
diff --git a/www/manager6/dc/OptionView.js b/www/manager6/dc/OptionView.js
index b200fd12..613a2183 100644
--- a/www/manager6/dc/OptionView.js
+++ b/www/manager6/dc/OptionView.js
@@ -339,6 +339,20 @@ Ext.define('PVE.dc.OptionView', {
 		submitValue: true,
 	    }],
 	});
+	me.add_combobox_row('next-id-strategy', gettext('Next Free VMID Strategy'), {
+	    renderer: v => {
+		if (v === '__default__') {return Proxmox.Utils.defaultText;}
+	        return v;
+	    },
+	    comboItems: [
+		['__default__', Proxmox.Utils.defaultText + ' (next-free)'],
+		['next-free', 'next-free'],
+		['max+1', 'max+1'],
+		['list', 'list'],
+	    ],
+	    defaultValue: '__default__',
+	    deleteEmpty: true,
+	});
 	me.rows['tag-style'] = {
 	    required: true,
 	    renderer: (value) => {
-- 
2.39.2



--===============1732891111198085800==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

--===============1732891111198085800==--