[pve-devel] [RFC PATCH qemu-server] pass datacenter.cfg's mac_prefix to random_ether_addr

Wolfgang Bumiller w.bumiller at proxmox.com
Wed Jul 13 16:25:44 CEST 2016


---
 PVE/API2/Qemu.pm  |  3 ++-
 PVE/QemuServer.pm | 10 +++++++---
 2 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index 7337887..df0518d 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -2311,7 +2311,8 @@ __PACKAGE__->register_method({
 		# always change MAC! address
 		if ($opt =~ m/^net(\d+)$/) {
 		    my $net = PVE::QemuServer::parse_net($value);
-		    $net->{macaddr} =  PVE::Tools::random_ether_addr();
+		    my $dc = PVE::Cluster::cfs_read_file('datacenter.cfg');
+		    $net->{macaddr} =  PVE::Tools::random_ether_addr($dc->{mac_prefix});
 		    $newconf->{$opt} = PVE::QemuServer::print_net($net);
 		} elsif (PVE::QemuServer::is_valid_drivename($opt)) {
 		    my $drive = PVE::QemuServer::parse_drive($opt, $value);
diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 75f6445..12f5be8 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -1692,7 +1692,10 @@ sub parse_net {
 	warn $@;
 	return undef;
     }
-    $res->{macaddr} = PVE::Tools::random_ether_addr() if !defined($res->{macaddr});
+    if (!defined($res->{macaddr})) {
+	my $dc = PVE::Cluster::cfs_read_file('datacenter.cfg');
+	$res->{macaddr} = PVE::Tools::random_ether_addr($dc->{mac_prefix});
+    }
     return $res;
 }
 
@@ -4932,12 +4935,13 @@ sub restore_update_config_line {
     return if $line =~ m/^parent:/;
     return if $line =~ m/^template:/; # restored VM is never a template
 
+    my $dc = PVE::Cluster::cfs_read_file('datacenter.cfg');
     if (($line =~ m/^(vlan(\d+)):\s*(\S+)\s*$/)) {
 	# try to convert old 1.X settings
 	my ($id, $ind, $ethcfg) = ($1, $2, $3);
 	foreach my $devconfig (PVE::Tools::split_list($ethcfg)) {
 	    my ($model, $macaddr) = split(/\=/, $devconfig);
-	    $macaddr = PVE::Tools::random_ether_addr() if !$macaddr || $unique;
+	    $macaddr = PVE::Tools::random_ether_addr($dc->{mac_prefix}) if !$macaddr || $unique;
 	    my $net = {
 		model => $model,
 		bridge => "vmbr$ind",
@@ -4951,7 +4955,7 @@ sub restore_update_config_line {
     } elsif (($line =~ m/^(net\d+):\s*(\S+)\s*$/) && $unique) {
 	my ($id, $netstr) = ($1, $2);
 	my $net = parse_net($netstr);
-	$net->{macaddr} = PVE::Tools::random_ether_addr() if $net->{macaddr};
+	$net->{macaddr} = PVE::Tools::random_ether_addr($dc->{mac_prefix}) if $net->{macaddr};
 	$netstr = print_net($net);
 	print $outfd "$id: $netstr\n";
     } elsif ($line =~ m/^((ide|scsi|virtio|sata)\d+):\s*(\S+)\s*$/) {
-- 
2.1.4




More information about the pve-devel mailing list