- $conf->{$opt}->{gw} = $newnet->{gw};
- PVE::LXC::write_config($vmid, $conf);
- $lxc_setup->setup_network($conf);
- }
- }
-
- 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 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) {
+ if ($newnet->{$gw}) {
+ eval { &$netcmd($family_opt, '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($family_opt, 'addr', 'del', $newnet->{$ip}, 'dev', $eth); };
+ warn $@ if $@; # no need to die here
+ }
+ return;
+ }
+ } else {
+ eval { &$netcmd($family_opt, 'route', 'del', 'default'); };
+ # if the route was not deleted, the guest might have deleted it manually
+ # warn and continue
+ warn $@ if $@;
+ }