From: Dominik Csapak <d.csapak@proxmox.com>
To: Stefan Hanreich <s.hanreich@proxmox.com>, pve-devel@lists.proxmox.com
Subject: Re: [PATCH qemu-server 4/6] start: persist a MAC address for network devices without one
Date: Tue, 24 Nov 2026 19:06:12 +0100 [thread overview]
Message-ID: <9b50f088-1056-44d4-a639-24a7f319a983@proxmox.com> (raw)
In-Reply-To: <20261124145651.335234-5-s.hanreich@proxmox.com>
(minor) comment inline
On 11/24/26 3:57 PM, Stefan Hanreich wrote:
> The API always sets 'macaddr' for a network device, but manual edits or
> restored backups can lack it. For such a device, parse_net() returns a
> new random MAC address on each call, so every user of the config sees
> a different one.
>
> Generate the missing MAC addresses when starting the VM and write them
> to its config, like the pending changes there, so that all of them use
> the same address. Starting such a VM thus changes its config once.
>
> Skip this when the VM is migrated or resumed from its state, as it then
> already runs with a MAC address, which a new one would not match.
>
> Signed-off-by: Stefan Hanreich <s.hanreich@proxmox.com>
> ---
> src/PVE/QemuServer.pm | 8 ++++++++
> src/PVE/QemuServer/Network.pm | 30 +++++++++++++++++++++++++++
> src/test/Makefile | 5 ++++-
> src/test/run_network_tests.pl | 38 +++++++++++++++++++++++++++++++++++
> 4 files changed, 80 insertions(+), 1 deletion(-)
> create mode 100755 src/test/run_network_tests.pl
>
> diff --git a/src/PVE/QemuServer.pm b/src/PVE/QemuServer.pm
> index ecbaa27f..1e4fe203 100644
> --- a/src/PVE/QemuServer.pm
> +++ b/src/PVE/QemuServer.pm
> @@ -5490,6 +5490,14 @@ sub vm_start_nolock {
> $conf = PVE::QemuConfig->load_config($vmid); # update/reload
> }
>
> + # without a persisted MAC address, every user of the config would see a different random one,
> + # but a VM migrated or resumed from its state already runs with one
> + if (!$statefile && !$resume && !$migratedfrom) {
> + if (PVE::QemuServer::Network::add_missing_macs($conf)) {
> + PVE::QemuConfig->write_config($vmid, $conf);
> + }
> + }
> +
> # don't regenerate the ISO if the VM is started as part of a live migration
> # this way we can reuse the old ISO with the correct config
> if (!$migratedfrom) {
> diff --git a/src/PVE/QemuServer/Network.pm b/src/PVE/QemuServer/Network.pm
> index 01b21f86..beaf2aca 100644
> --- a/src/PVE/QemuServer/Network.pm
> +++ b/src/PVE/QemuServer/Network.pm
> @@ -272,6 +272,36 @@ sub add_random_macs {
> }
> }
>
> +# Returns the network device with a generated MAC address if it has none.
> +sub add_missing_mac {
> + my ($value) = @_;
> +
> + my $net = parse_net($value, 1);
> + return $value if !$net || defined($net->{macaddr});
> +
> + my $dc = PVE::Cluster::cfs_read_file('datacenter.cfg');
> + $net->{macaddr} = PVE::Tools::random_ether_addr($dc->{mac_prefix});
> +
> + return print_net($net);
> +}
> +
> +# Returns whether a MAC address was added to a network device.
> +sub add_missing_macs {
> + my ($conf) = @_;
> +
> + my $added;
> + for my $opt (sort keys %$conf) {
> + next if $opt !~ m/^net\d+$/;
> + my $value = add_missing_mac($conf->{$opt});
> + next if $value eq $conf->{$opt};
> +
> + $conf->{$opt} = $value;
> + $added = 1;
> + }
> +
> + return $added;
> +}
> +
this is very similar to 'add_random_macs' just above the context,
so this could probably be reused or unified
(could be cleaned up afterwards though)
> sub add_nets_bridge_fdb {
> my ($conf, $vmid) = @_;
>
> diff --git a/src/test/Makefile b/src/test/Makefile
> index 0d47205d..2d3a55da 100644
> --- a/src/test/Makefile
> +++ b/src/test/Makefile
> @@ -1,6 +1,6 @@
> all: test
>
> -test: test_snapshot test_cfg_to_cmd test_cfg_to_cmd_aarch64 test_hotplug test_hotplug_aarch64 test_pci_addr_conflicts test_pci_reservation test_qemu_img_convert test_migration test_restore_config test_parse_config test_memory test_agent test_raw_config_write test_api test_api_cloudinit_dump test_mtunnel test_guest_identity
> +test: test_snapshot test_cfg_to_cmd test_cfg_to_cmd_aarch64 test_hotplug test_hotplug_aarch64 test_pci_addr_conflicts test_pci_reservation test_qemu_img_convert test_migration test_restore_config test_parse_config test_memory test_agent test_raw_config_write test_api test_api_cloudinit_dump test_mtunnel test_guest_identity test_network
>
> test_snapshot: run_snapshot_tests.pl
> ./run_snapshot_tests.pl
> @@ -51,6 +51,9 @@ test_agent: run_agent_tests.pl
> test_raw_config_write: run_raw_config_write_tests.pl
> ./run_raw_config_write_tests.pl
>
> +test_network: run_network_tests.pl
> + ./run_network_tests.pl
> +
> test_api: run_api_tests.pl
> ./run_api_tests.pl
>
> diff --git a/src/test/run_network_tests.pl b/src/test/run_network_tests.pl
> new file mode 100755
> index 00000000..c9eeede7
> --- /dev/null
> +++ b/src/test/run_network_tests.pl
> @@ -0,0 +1,38 @@
> +#!/usr/bin/perl
> +
> +use strict;
> +use warnings;
> +
> +use lib qw(..);
> +
> +use Test::MockModule;
> +use Test::More;
> +
> +use PVE::QemuServer::Network;
> +
> +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' };
> + },
> +);
> +
> +subtest 'missing MAC addresses of network devices are added' => sub {
> + my $with_mac = 'virtio=BC:24:11:00:00:01,bridge=vmbr0';
> + my $conf = { name => 'vm100', net0 => $with_mac, net1 => 'virtio,bridge=vmbr0,firewall=1' };
> +
> + ok(PVE::QemuServer::Network::add_missing_macs($conf), 'reports the added MAC address');
> + is($conf->{net0}, $with_mac, 'existing MAC address is kept');
> + my $net1 = PVE::QemuServer::Network::parse_net($conf->{net1}, 1);
> + like($net1->{macaddr}, qr/^BC:24:11(?::[0-9A-F]{2}){3}$/i, 'MAC address uses the prefix');
> + is($net1->{firewall}, 1, 'other properties are kept');
> + is($conf->{name}, 'vm100', 'other options are kept');
> +
> + my $net1_string = $conf->{net1};
> + ok(!PVE::QemuServer::Network::add_missing_macs($conf), 'reports nothing if all are set');
> + is($conf->{net1}, $net1_string, 'added MAC address stays');
> +};
> +
> +done_testing();
next prev parent reply other threads:[~2026-11-24 18:06 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 ` [PATCH pve-container 2/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 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 [this message]
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=9b50f088-1056-44d4-a639-24a7f319a983@proxmox.com \
--to=d.csapak@proxmox.com \
--cc=pve-devel@lists.proxmox.com \
--cc=s.hanreich@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox