[pve-devel] [PATCH container] fix: #1218 Add flag 'unique' to pct restore in order to set new MAC addresses to NICs

Christian Ebner c.ebner at proxmox.com
Mon Apr 1 17:45:24 CEST 2019


Signed-off-by: Christian Ebner <c.ebner at proxmox.com>
---
 src/PVE/API2/LXC.pm   | 9 ++++++++-
 src/PVE/LXC/Create.pm | 9 ++++++++-
 2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/src/PVE/API2/LXC.pm b/src/PVE/API2/LXC.pm
index 6de121f..3d5460c 100644
--- a/src/PVE/API2/LXC.pm
+++ b/src/PVE/API2/LXC.pm
@@ -144,6 +144,12 @@ __PACKAGE__->register_method({
 		type => 'boolean',
 		description => "Mark this as restore task.",
 	    },
+	    unique => {
+		optional => 1,
+		type => 'boolean',
+		description => "Assign a unique random ethernet address.",
+		requires => 'restore',
+	    },
 	    pool => {
 		optional => 1,
 		type => 'string', format => 'pve-poolid',
@@ -197,6 +203,7 @@ __PACKAGE__->register_method({
 	# 'unprivileged' is read-only, so we can't pass it to update_pct_config
 	my $unprivileged = extract_param($param, 'unprivileged');
 	my $restore = extract_param($param, 'restore');
+	my $unique = extract_param($param, 'unique');
 
 	if ($restore) {
 	    # fixme: limit allowed parameters
@@ -397,7 +404,7 @@ __PACKAGE__->register_method({
 		    PVE::LXC::Create::restore_archive($archive, $rootdir, $conf, $ignore_unpack_errors, $bwlimit);
 
 		    if ($restore) {
-			PVE::LXC::Create::restore_configuration($vmid, $rootdir, $conf, !$is_root);
+			PVE::LXC::Create::restore_configuration($vmid, $rootdir, $conf, !$is_root, $unique);
 		    } else {
 			my $lxc_setup = PVE::LXC::Setup->new($conf, $rootdir); # detect OS
 			PVE::LXC::Config->write_config($vmid, $conf); # safe config (after OS detection)
diff --git a/src/PVE/LXC/Create.pm b/src/PVE/LXC/Create.pm
index 564d6db..39827a6 100644
--- a/src/PVE/LXC/Create.pm
+++ b/src/PVE/LXC/Create.pm
@@ -159,7 +159,7 @@ sub recover_config {
 }
 
 sub restore_configuration {
-    my ($vmid, $rootdir, $conf, $restricted) = @_;
+    my ($vmid, $rootdir, $conf, $restricted, $unique) = @_;
 
     # restore: try to extract configuration from archive
 
@@ -184,6 +184,13 @@ sub restore_configuration {
 		warn "--------------------------------\n";
 		next;
 	    }
+	    if (($unique && $key =~ /^net\d+/)) {
+		my $net = PVE::LXC::Config->parse_lxc_network($oldconf->{$key});
+		my $dc = PVE::Cluster::cfs_read_file('datacenter.cfg');
+		$net->{hwaddr} = PVE::Tools::random_ether_addr($dc->{mac_prefix});
+		$conf->{$key} = PVE::LXC::Config->print_lxc_network($net);
+		next;
+	    }
 	    $conf->{$key} = $oldconf->{$key} if !defined($conf->{$key});
 	}
 	unlink($pct_cfg_fn);
-- 
2.11.0




More information about the pve-devel mailing list