From: Stefan Hanreich <s.hanreich@proxmox.com>
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 [thread overview]
Message-ID: <20261124145651.335234-3-s.hanreich@proxmox.com> (raw)
In-Reply-To: <20261124145651.335234-1-s.hanreich@proxmox.com>
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 <s.hanreich@proxmox.com>
---
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
next prev parent reply other threads:[~2026-11-24 14:57 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 14:56 [PATCH container/manager/qemu-server 0/6] persist auto-generated MAC addresses of guest network devices Stefan Hanreich
2026-11-24 14:56 ` [PATCH pve-container 1/6] setup: do not read datacenter.cfg inside the container's root Stefan Hanreich
2026-11-24 14:56 ` Stefan Hanreich [this message]
2026-11-24 18:06 ` [PATCH pve-container 2/6] start: persist a MAC address for network devices without one Dominik Csapak
2026-11-24 14:56 ` [PATCH pve-container 3/6] restore: add missing MAC addresses of the backup config Stefan Hanreich
2026-11-24 14:56 ` [PATCH qemu-server 4/6] start: persist a MAC address for network devices without one Stefan Hanreich
2026-11-24 18:06 ` Dominik Csapak
2026-11-24 14:56 ` [PATCH qemu-server 5/6] restore: add missing MAC addresses of the backup config Stefan Hanreich
2026-11-24 14:56 ` [PATCH pve-manager 6/6] ui: lxc: network: handle network devices without a MAC address Stefan Hanreich
2026-11-24 18:06 ` [PATCH container/manager/qemu-server 0/6] persist auto-generated MAC addresses of guest network devices Dominik Csapak
2026-11-24 18:38 ` Dominik Csapak
2026-11-24 19:09 ` Stefan Hanreich
2026-11-24 23:45 ` applied: " Thomas Lamprecht
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261124145651.335234-3-s.hanreich@proxmox.com \
--to=s.hanreich@proxmox.com \
--cc=pve-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.