[pve-devel] [PATCH 3/3] LXC: more compact network configuration

Wolfgang Bumiller w.bumiller at proxmox.com
Tue Jun 30 13:47:53 CEST 2015


Deduplicated network setup code.
---
 src/PVE/LXC.pm | 117 +++++++++++++++++----------------------------------------
 1 file changed, 35 insertions(+), 82 deletions(-)

diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
index 36c3995..bd2ab08 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -1246,98 +1246,51 @@ sub update_ipconfig {
 
     my $lxc_setup = PVE::LXCSetup->new($conf, $rootdir);
 
-    my $update_gateway;
-    if (&$safe_string_ne($conf->{$opt}->{gw}, $newnet->{gw})) {
-
-	$update_gateway = 1;
-	if ($conf->{$opt}->{gw}) {
-	    my $cmd = ['lxc-attach', '-n', $vmid, '-s', 'NETWORK', '--', '/sbin/ip', 'route', 'del', 'default', 'via', $conf->{$opt}->{gw} ];
-	    eval { PVE::Tools::run_command($cmd); };
-	    warn $@ if $@; # ignore errors here
-	    delete $conf->{$opt}->{gw};
-	    PVE::LXC::write_config($vmid, $conf);
-	    $lxc_setup->setup_network($conf);
-	}
-    }
-
-    if (&$safe_string_ne($conf->{$opt}->{ip}, $newnet->{ip})) {
-
-	if ($conf->{$opt}->{ip}) {
-	    my $cmd = ['lxc-attach', '-n', $vmid, '-s', 'NETWORK', '--', '/sbin/ip', 'addr', 'del', $conf->{$opt}->{ip}, 'dev', $eth  ];
-	    eval { PVE::Tools::run_command($cmd); };
-	    warn $@ if $@; # ignore errors here
-	    delete $conf->{$opt}->{ip};
-	    PVE::LXC::write_config($vmid, $conf);
-	    $lxc_setup->setup_network($conf);
-	}
-
-	if ($newnet->{ip}) {
-	    my $cmd = ['lxc-attach', '-n', $vmid, '-s', 'NETWORK', '--', '/sbin/ip', 'addr', 'add', $newnet->{ip}, 'dev', $eth  ];
-	    PVE::Tools::run_command($cmd);
-
-	    $conf->{$opt}->{ip} = $newnet->{ip};
-	    PVE::LXC::write_config($vmid, $conf);
-	    $lxc_setup->setup_network($conf);
+    my $optdata = $conf->{$opt};
+    my @deleted;
+    my @added;
+    my $change = sub {
+	my ($prop, $target, @command) = @_;
+	my $cmd = ['lxc-attach', '-n', $vmid, '-s', 'NETWORK', '--', '/sbin/ip', @command];
+	eval { PVE::Tools::run_command($cmd); };
+	if (my $err = $@) {
+	    warn $err;
+	} else {
+	    push @$target, $prop;
 	}
-    }
-
-    if ($update_gateway) {
-
-	if ($newnet->{gw}) {
-	    my $cmd = ['lxc-attach', '-n', $vmid, '-s', 'NETWORK', '--', '/sbin/ip', 'route', 'add', 'default', 'via', $newnet->{gw} ];
-	    PVE::Tools::run_command($cmd);
+    };
 
-	    $conf->{$opt}->{gw} = $newnet->{gw};
-	    PVE::LXC::write_config($vmid, $conf);
-	    $lxc_setup->setup_network($conf);
-        }
-    }
+    my $change_ip_config = sub {
+	my ($suffix) = @_;
+	my $gw= "gw$suffix";
+	my $ip= "ip$suffix";
 
-    my $update_gateway6;
-    if (&$safe_string_ne($conf->{$opt}->{gw6}, $newnet->{gw6})) {
-	
-	$update_gateway6 = 1;
-	if ($conf->{$opt}->{gw6}) {
-	    my $cmd = ['lxc-attach', '-n', $vmid, '-s', 'NETWORK', '--', '/sbin/ip', 'route', 'del', 'default', 'via', $conf->{$opt}->{gw6} ];
-	    eval { PVE::Tools::run_command($cmd); };
-	    warn $@ if $@; # ignore errors here
-	    delete $conf->{$opt}->{gw6};
-	    PVE::LXC::write_config($vmid, $conf);
-	    $lxc_setup->setup_network($conf);
+	my $update_gateway = 0;
+	if (&$safe_string_ne($optdata->{$gw}, $newnet->{$gw})) {
+	    $update_gateway = 1;
+	    &$change($gw => \@deleted, ('route', 'del', 'default', 'via', $optdata->{$gw}));
 	}
-    }
-
-    if (&$safe_string_ne($conf->{$opt}->{ip6}, $newnet->{ip6})) {
 
-	if ($conf->{$opt}->{ip6}) {
-	    my $cmd = ['lxc-attach', '-n', $vmid, '-s', 'NETWORK', '--', '/sbin/ip', 'addr', 'del', $conf->{$opt}->{ip6}, 'dev', $eth  ];
-	    eval { PVE::Tools::run_command($cmd); };
-	    warn $@ if $@; # ignore errors here
-	    delete $conf->{$opt}->{ip6};
-	    PVE::LXC::write_config($vmid, $conf);
-	    $lxc_setup->setup_network($conf);
+	if (&$safe_string_ne($optdata->{$ip}, $newnet->{$ip})) {
+	    &$change($ip => \@deleted, ('addr', 'del', $optdata->{$ip}, 'dev', $eth))
+		if $optdata->{$ip};
+	    &$change($ip => \@added, ('addr', 'add', $newnet->{$ip}, 'dev', $eth))
+		if $newnet->{$ip};
 	}
 
-	if ($newnet->{ip6}) {
-	    my $cmd = ['lxc-attach', '-n', $vmid, '-s', 'NETWORK', '--', '/sbin/ip', 'addr', 'add', $newnet->{ip6}, 'dev', $eth  ];
-	    PVE::Tools::run_command($cmd);
-
-	    $conf->{$opt}->{ip6} = $newnet->{ip6};
-	    PVE::LXC::write_config($vmid, $conf);
-	    $lxc_setup->setup_network($conf);
+	if ($update_gateway && $newnet->{$gw}) {
+	    &$change($gw => \@added, ('route', 'add', 'default', 'via', $newnet->{$gw}));
 	}
-    }
-
-    if ($update_gateway6) {
+    };
 
-	if ($newnet->{gw6}) {
-	    my $cmd = ['lxc-attach', '-n', $vmid, '-s', 'NETWORK', '--', '/sbin/ip', 'route', 'add', 'default', 'via', $newnet->{gw6} ];
-	    PVE::Tools::run_command($cmd);
+    &$change_ip_config('');
+    &$change_ip_config('6');
 
-	    $conf->{$opt}->{gw6} = $newnet->{gw6};
-	    PVE::LXC::write_config($vmid, $conf);
-	    $lxc_setup->setup_network($conf);
-        }
+    delete $optdata->{$_} foreach (@deleted);
+    $optdata->{$_} = $newnet->{$_} foreach (@added);
+    if (@deleted || @added) {
+	PVE::LXC::write_config($vmid, $conf);
+	$lxc_setup->setup_network($conf);
     }
 }
 
-- 
2.1.4





More information about the pve-devel mailing list