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 7FE751FF066 for ; Tue, 24 Nov 2026 15:57:05 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id AFF2321603; Tue, 24 Nov 2026 15:57:03 +0100 (CET) From: Stefan Hanreich To: pve-devel@lists.proxmox.com Subject: [PATCH pve-container 2/6] start: persist a MAC address for network devices without one Date: Tue, 24 Nov 2026 15:56:46 +0100 Message-ID: <20261124145651.335234-3-s.hanreich@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261124145651.335234-1-s.hanreich@proxmox.com> References: <20261124145651.335234-1-s.hanreich@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1795532219767 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.136 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) PROLO_LEO2 0.1 Meta Catches all Leo drug variations so far 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: ECX3DPRKYHNWCNOZ6ORSODVSU7KBAFAC X-Message-ID-Hash: ECX3DPRKYHNWCNOZ6ORSODVSU7KBAFAC X-MailFrom: s.hanreich@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: The API always sets 'hwaddr' for a network device, but manual edits or restored backups can lack it. For such a device, parse_lxc_network() returns a new random MAC address on each call, so every user of the config sees a different one: - LXC gets none, so the kernel assigns a random address to the end of the veth pair inside the container. - The SDN DHCP mapping gets an address that nothing else uses. Generate the missing MAC addresses when starting the container and write them to the config, as already done with the pending changes. Signed-off-by: Stefan Hanreich --- src/PVE/LXC.pm | 5 +++++ src/PVE/LXC/Config.pm | 19 +++++++++++++++++++ src/test/config_tests.pl | 30 ++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm index 53464de..1829d2b 100644 --- a/src/PVE/LXC.pm +++ b/src/PVE/LXC.pm @@ -3185,6 +3185,11 @@ sub vm_start { $conf = PVE::LXC::Config->load_config($vmid); # update/reload } + # without a persisted MAC address, every user of the config would see a different random one + if (PVE::LXC::Config->add_missing_hwaddrs($conf)) { + PVE::LXC::Config->write_config($vmid, $conf); + } + update_lxc_config($vmid, $conf); eval { diff --git a/src/PVE/LXC/Config.pm b/src/PVE/LXC/Config.pm index a12b975..da15194 100644 --- a/src/PVE/LXC/Config.pm +++ b/src/PVE/LXC/Config.pm @@ -1631,6 +1631,25 @@ sub parse_lxc_network { return $res; } +# Returns whether a MAC address was added to a network device. +sub add_missing_hwaddrs { + my ($class, $conf) = @_; + + my $added; + for my $opt (sort keys %$conf) { + next if $opt !~ m/^net\d+$/; + my $net = $class->parse_lxc_network($conf->{$opt}, 1); + next if $net->{hwaddr}; + + my $dc = PVE::Cluster::cfs_read_file('datacenter.cfg'); + $net->{hwaddr} = PVE::Tools::random_ether_addr($dc->{mac_prefix}); + $conf->{$opt} = $class->print_lxc_network($net); + $added = 1; + } + + return $added; +} + sub parse_features { my ($class, $data) = @_; return {} if !$data; diff --git a/src/test/config_tests.pl b/src/test/config_tests.pl index 685ba9d..182c4bf 100755 --- a/src/test/config_tests.pl +++ b/src/test/config_tests.pl @@ -6,6 +6,7 @@ use warnings; use lib qw(..); use Test::More; +use Test::MockModule; use PVE::LXC::Config; use PVE::LXC::Create; @@ -212,4 +213,33 @@ subtest 'meta property' => sub { ok(!exists(PVE::LXC::Config->json_config_properties({})->{meta}), 'meta cannot be set'); }; +subtest 'missing MAC addresses of network devices are added' => sub { + my $cluster_module = Test::MockModule->new('PVE::Cluster'); + $cluster_module->mock( + cfs_read_file => sub { + my ($filename) = @_; + die "unexpected read of $filename\n" if $filename ne 'datacenter.cfg'; + return { mac_prefix => 'BC:24:11' }; + }, + ); + + my $with_hwaddr = 'name=eth0,bridge=vmbr0,hwaddr=BC:24:11:00:00:01,type=veth'; + my $conf = { + hostname => 'ct100', + net0 => $with_hwaddr, + net1 => 'name=eth1,bridge=vmbr0,type=veth', + }; + + ok(PVE::LXC::Config->add_missing_hwaddrs($conf), 'reports the added MAC address'); + is($conf->{net0}, $with_hwaddr, 'existing MAC address is kept'); + my $net1 = PVE::LXC::Config->parse_lxc_network($conf->{net1}, 1); + like($net1->{hwaddr}, qr/^BC:24:11(?::[0-9A-F]{2}){3}$/i, 'MAC address uses the prefix'); + is($net1->{name}, 'eth1', 'other properties are kept'); + is($conf->{hostname}, 'ct100', 'other options are kept'); + + my $net1_string = $conf->{net1}; + ok(!PVE::LXC::Config->add_missing_hwaddrs($conf), 'reports nothing if all are set'); + is($conf->{net1}, $net1_string, 'added MAC address stays'); +}; + done_testing(); -- 2.47.3