]> git.proxmox.com Git - pve-container.git/commitdiff
centos: deal with gateways outside the subnet
authorWolfgang Bumiller <w.bumiller@proxmox.com>
Fri, 16 Oct 2015 13:57:11 +0000 (15:57 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Mon, 19 Oct 2015 04:58:51 +0000 (06:58 +0200)
src/PVE/LXC/Setup/Redhat.pm

index c15673a1cdaa35afec3b8f7009f491a9810adb72..095507a553f67fd407e5d590ac06f39bacfafe17 100644 (file)
@@ -4,6 +4,7 @@ use strict;
 use warnings;
 use Data::Dumper;
 use PVE::Tools;
+use PVE::Network;
 use PVE::LXC;
 
 use PVE::LXC::Setup::Base;
@@ -195,6 +196,8 @@ sub setup_network {
        next if !$d->{name};
 
        my $filename = "/etc/sysconfig/network-scripts/ifcfg-$d->{name}";
+       my $routefile = "/etc/sysconfig/network-scripts/route-$d->{name}";
+       my $routes = '';
        my $had_v4 = 0;
 
        if ($d->{ip} && $d->{ip} ne 'manual') {
@@ -212,6 +215,10 @@ sub setup_network {
                }
            }
            $self->ct_file_set_contents($filename, $data);
+           if (!PVE::Network::is_ip_in_cidr($d->{gw}, $d->{ip}, 4)) {
+               $routes .= "$d->{gw} dev $d->{name}\n";
+               $routes .= "default via $d->{gw}\n";
+           }
            # If we also have an IPv6 configuration it'll end up in an alias
            # interface becuase otherwise RH doesn't support mixing dhcpv4 with
            # a static ipv6 address.
@@ -241,6 +248,29 @@ sub setup_network {
                }
            }
            $self->ct_file_set_contents($filename, $data);
+           if (!PVE::Network::is_ip_in_cidr($d->{gw6}, $d->{ip6}, 6)) {
+               $routes .= "$d->{gw6} dev $d->{name}\n";
+               $routes .= "default via $d->{gw6}\n";
+           }
+       }
+
+       # To keep user-defined routes in route-$iface we mark ours:
+       my $head = "# --- BEGIN PVE ROUTES ---\n";
+       my $tail = "# --- END PVE ROUTES ---\n";
+       $routes = $head . $routes . $tail if $routes;
+       if ($self->ct_file_exists($routefile)) {
+           # if it exists we update by first removing our old rules
+           my $old = $self->ct_file_get_contents($routefile);
+           $old =~ s/(?:^|(?<=\n))\Q$head\E.*\Q$tail\E//gs;
+           chomp $old;
+           if ($old) {
+               $self->ct_file_set_contents($routefile, $routes . $old . "\n");
+           } else {
+               # or delete if we aren't adding routes and the file's now empty
+               $self->ct_unlink($routefile);
+           }
+       } elsif ($routes) {
+           $self->ct_file_set_contents($routefile, $routes);
        }
     }
 }