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 B2F8D1FF066 for ; Tue, 24 Nov 2026 15:57:46 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id AAE5A21731; Tue, 24 Nov 2026 15:57:09 +0100 (CET) From: Stefan Hanreich To: pve-devel@lists.proxmox.com Subject: [PATCH qemu-server 4/6] start: persist a MAC address for network devices without one Date: Tue, 24 Nov 2026 15:56:48 +0100 Message-ID: <20261124145651.335234-5-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: 1795532219893 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.184 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) 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: HV4X3YIUHYWTSBVUI7PV4LYXONH6M7I6 X-Message-ID-Hash: HV4X3YIUHYWTSBVUI7PV4LYXONH6M7I6 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 '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 --- 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; +} + 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(); -- 2.47.3