[pve-devel] [PATCH v2] LXC: more compact network configuration
Wolfgang Bumiller
w.bumiller at proxmox.com
Wed Jul 1 12:06:01 CEST 2015
Deduplicated network setup code.
Using 'ip route replace' to replace or add the route.
This strategy can be rolled back safely:
1) add new ip (no harm done, old ip still exists)
2) replace route
on error:
Delete the new ip, old one is still in place.
If deleting the new ip fails, it was already modified
from within the container, so we only warn about it.
on success do (3):
3) delete old ip
New IP + gateway are in place, old IP can be deleted.
If deletion fails, we only warn like in the error case.
---
src/PVE/LXC.pm | 132 ++++++++++++++++++++++-----------------------------------
1 file changed, 50 insertions(+), 82 deletions(-)
diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
index 36c3995..27c85c4 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -1246,99 +1246,67 @@ 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 $netcmd = sub {
+ my $cmd = ['lxc-attach', '-n', $vmid, '-s', 'NETWORK', '--', '/sbin/ip', @_];
+ PVE::Tools::run_command($cmd);
+ };
- if ($update_gateway) {
+ my $change_ip_config = sub {
+ my ($suffix) = @_;
+ my $gw= "gw$suffix";
+ my $ip= "ip$suffix";
- if ($newnet->{gw}) {
- my $cmd = ['lxc-attach', '-n', $vmid, '-s', 'NETWORK', '--', '/sbin/ip', 'route', 'add', 'default', 'via', $newnet->{gw} ];
- PVE::Tools::run_command($cmd);
+ my $change_ip = &$safe_string_ne($optdata->{$ip}, $newnet->{$ip});
+ my $change_gw = &$safe_string_ne($optdata->{$gw}, $newnet->{$gw});
- $conf->{$opt}->{gw} = $newnet->{gw};
- PVE::LXC::write_config($vmid, $conf);
- $lxc_setup->setup_network($conf);
- }
- }
+ return if !$change_ip && !$change_gw;
- 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);
+ # step 1: add new IP, if this fails we cancel
+ if ($change_ip) {
+ eval { &$netcmd('addr', 'add', $newnet->{$ip}, 'dev', $eth); };
+ if (my $err = $@) {
+ warn $err;
+ return;
+ }
}
- }
- 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);
+ # step 2: replace gateway
+ # If this fails we delete the added IP and cancel.
+ # If it succeeds we save the config and delete the old IP, ignoring
+ # errors. The config is then saved.
+ # Note: 'ip route replace' can add
+ if ($change_gw) {
+ eval { &$netcmd('route', 'replace', 'default', 'via', $newnet->{$gw}); };
+ if (my $err = $@) {
+ warn $err;
+ # the route was not replaced, the old IP is still available
+ # rollback (delete new IP) and cancel
+ if ($change_ip) {
+ eval { &$netcmd('addr', 'del', $newnet->{$ip}, 'dev', $eth); };
+ warn $@ if $@; # no need to die here
+ }
+ return;
+ }
}
- 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);
+ # from this point on we safe the configuration
+ # step 3: delete old IP ignoring errors
+ if ($change_ip) {
+ eval { &$netcmd('addr', 'del', $optdata->{$ip}, 'dev', $eth); };
+ warn $@ if $@; # no need to die here
}
- }
-
- 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);
+ $optdata->{$ip} = $newnet->{$ip};
+ $optdata->{$gw} = $newnet->{$gw};
+ PVE::LXC::write_config($vmid, $conf);
+ $lxc_setup->setup_network($conf);
+ };
- $conf->{$opt}->{gw6} = $newnet->{gw6};
- PVE::LXC::write_config($vmid, $conf);
- $lxc_setup->setup_network($conf);
- }
- }
+ &$change_ip_config('');
+ &$change_ip_config('6');
}
1;
--
2.1.4
More information about the pve-devel
mailing list