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 A027F1FF0AA for ; Tue, 22 Sep 2026 17:55:38 +0200 (CEST) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id F0184214A3; Tue, 22 Sep 2026 17:55:34 +0200 (CEST) Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Tue, 22 Sep 2026 17:55:30 +0200 Message-Id: From: =?utf-8?q?Michael_K=C3=B6ppl?= To: "Fiona Ebner" , =?utf-8?q?Michael_K=C3=B6ppl?= , Subject: Re: [PATCH pve-guest-common v5 3/7] add module to track previously used guest IDs X-Mailer: aerc 0.22.0 References: <20260921155410.938337-1-m.koeppl@proxmox.com> <20260921155410.938337-4-m.koeppl@proxmox.com> <0ee919d3-7ff6-4905-ae82-c348f8445037@proxmox.com> In-Reply-To: <0ee919d3-7ff6-4905-ae82-c348f8445037@proxmox.com> X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1790092530488 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.457 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_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: GACXMBHTXSOQDFLAJD6K3JOLVBDYPDQX X-Message-ID-Hash: GACXMBHTXSOQDFLAJD6K3JOLVBDYPDQX X-MailFrom: m.koeppl@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: On Tue Sep 22, 2026 at 4:22 PM CEST, Fiona Ebner wrote: [snip] >> diff --git a/src/PVE/UsedGuestIDs.pm b/src/PVE/UsedGuestIDs.pm >> new file mode 100644 >> index 0000000..31036ff >> --- /dev/null >> +++ b/src/PVE/UsedGuestIDs.pm >> @@ -0,0 +1,93 @@ >> +package PVE::UsedGuestIDs; > > Nit: maybe GuestID as a more general name? That would suggest moving or > adding other stuff related to guest IDs here in the future. Otherwise, > the module is quite specific. We would need to rename add_id() to > add_used_id(), read_list() -> list_used_ids() for clarity then. I think I'd also prefer PVE::GuestID here to avoid possibly having another module later on or having to refactor this one. Will adapt for v6! > >> + >> +use strict; >> +use warnings; > > Nit: I now this got picked up from before, but it would be nice to use > v5.36; and function signatures for new modules. will adapt for v6, thanks! > >> + >> +use PVE::Cluster qw( >> + cfs_lock_file >> + cfs_read_file >> + cfs_register_file >> + cfs_write_file >> +); >> + >> +my $FILENAME =3D 'used-guest-ids'; >> + >> +my $parse_id_list =3D sub { > > Nit: If writing with a signature, use 'my sub', and similarly for the > other two private functions. will also be updated for v6, thanks! > >> + my ($filename, $raw) =3D @_; >> + >> + my $used_ids =3D {}; >> + >> + return $used_ids if !defined($raw); >> + >> + for my $line (split(/\n/, $raw)) { >> + next if $line =3D~ m/^\s*$/; >> + >> + if ($line =3D~ m/^(\d+)$/) { >> + $used_ids->{$1} =3D 1; >> + } elsif ($line =3D~ m/^(\d+)-(\d+)$/) { >> + my ($start, $end) =3D ($1, $2); >> + if ($start > $end) { >> + warn "skipping reversed range in $filename: $line\n"; >> + next; >> + } >> + $used_ids->{$_} =3D 1 for $start .. $end; > > Nit: we could be smarter and avoid adding every single ID to the hash > (with a lot of (past) guests that can become a non-trivial cost), and > instead track ranges. Instead of having a public read_list() function > then, there could be a check_id_unused() function. When writing out the > file we can still merge adjacent ranges. Thanks for pointing this out! I think the range approach is a good idea! Should we have a public function that returns the end of a range for a given guest ID instead of the boolean check_id_unused()? We could then use it to skip ranges when searching for a free guest ID at /cluster/nextid instead of invoking cfs_read_file() for each check if a candidate ID is available. Something like this: my $i =3D $lower; while ($i < $upper) { if (defined($idlist->{$i})) { $i++; next; } if ($check_unique) { if (defined(my $end =3D PVE::UsedGuestIDs::get_used_range_end($i))) { $i =3D $end + 1; next; } } return $i; } > >> + } else { >> + warn "skipping invalid entry in $filename: $line\n"; >> + } >> + } >> + >> + return $used_ids; >> +}; >> + >> +my $format_entry =3D sub { >> + my ($start, $last) =3D @_; >> + return $start =3D=3D $last ? "$start\n" : "$start-$last\n"; >> +}; [snip]