]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/INotify.pm
Inotify: allow bond of bond
[pve-common.git] / src / PVE / INotify.pm
index 86543566fddc0a36d597b13b7e7389812354fb33..c5067de9043f557a1f2743333f645fe2c2790c28 100644 (file)
@@ -923,7 +923,8 @@ sub __read_etc_network_interfaces {
            push @{$d->{families}}, $family;
 
            while (defined ($line = <$fh>)) {
-               chomp $line;
+               $line =~ s/\s+$//; # drop trailing whitespaces
+
                if ($line =~ m/^\s*#(.*?)\s*$/) {
                    $f->{comments} = '' if !$f->{comments};
                    my $comment = decode('UTF-8', $1);
@@ -1109,23 +1110,34 @@ sub __read_etc_network_interfaces {
        }
 
        # map address and netmask to cidr
-       if ($d->{address}) {
-           if ($d->{netmask}) {
-               if ($d->{netmask} =~ m/^\d+$/) { # e.g. netmask 20
-                   $d->{address} = $d->{address} . "/" . $d->{netmask};
-               } elsif (my $mask = PVE::JSONSchema::get_netmask_bits($d->{netmask})) {
-                   $d->{address} = $d->{address} . "/" . $mask;
-               }
+       if (my $addr = $d->{address}) {
+           if (_address_is_cidr($addr)) {
+               $d->{cidr} = $addr;
+               my ($baseaddr, $mask) = _cidr_split($addr);
+               $d->{address} = $baseaddr;
+               $d->{netmask} = $mask;
+           } elsif (my $cidr = _get_cidr($d->{address}, $d->{netmask})) {
+               $d->{cidr} = $cidr;
+               (undef, $d->{netmask}) = _cidr_split($cidr);
+           } else {
+               # no mask, else we'd got a cidr above
+               $d->{cidr} = $addr ."/32";
            }
-          # for api compatibility
-          $d->{cidr} = $d->{address};
        }
 
        # map address6 and netmask6 to cidr6
-       if ($d->{address6}) {
-           $d->{address6} .= "/" . $d->{netmask6} if $d->{address6} !~ m!^(.*)/(\d+)$! && $d->{netmask6};
-           # for api compatibility
-           $d->{cidr6} = $d->{address6};
+       if (my $addr6 = $d->{address6}) {
+           if (_address_is_cidr($addr6)) {
+               $d->{cidr6} = $addr6;
+               my ($baseaddr, $mask) = _cidr_split($addr6);
+               $d->{address6} = $baseaddr;
+               $d->{netmask6} = $mask;
+           } elsif (my $cidr6 = _get_cidr($d->{address6}, $d->{netmask6})) {
+               $d->{cidr6} = $cidr6;
+           } else {
+               # no mask, else we'd got a cidr above
+               $d->{cidr6} = $addr6 ."/128";
+           }
        }
 
        $d->{method} = 'manual' if !$d->{method};
@@ -1163,6 +1175,31 @@ sub __read_etc_network_interfaces {
     return $config;
 }
 
+sub _address_is_cidr {
+    my ($addr) = @_;
+    return $addr =~ /\/\d+$/ ? 1 : 0;
+}
+
+sub _cidr_split {
+    my ($cidr) = @_;
+    $cidr =~ /^(.+)\/(\d+)$/;
+    return ($1, $2); # (address, mask)
+}
+
+sub _get_cidr {
+    my ($addr, $mask) = @_;
+
+    return $addr if _address_is_cidr($addr);
+    return undef if !$mask;
+
+    if ($mask =~ m/^\d+$/) { # cidr notation
+       return $addr . "/" . $mask;
+    } elsif (my $cidrmask = PVE::JSONSchema::get_netmask_bits($mask)) {
+       return $addr . "/" . $cidrmask;
+    }
+    return undef;
+}
+
 sub __interface_to_string {
     my ($iface, $d, $family, $first_block, $ifupdown2) = @_;
 
@@ -1199,8 +1236,8 @@ sub __interface_to_string {
        # not printing out options
     } elsif ($d->{type} eq 'bridge') {
 
-       $d->{bridge_ports} =~ s/[;,\s]+/ /g;
        my $ports = $d->{bridge_ports} || 'none';
+       $ports =~ s/[;,\s]+/ /g;
        $raw .= "\tbridge-ports $ports\n";
        $done->{bridge_ports} = 1;
 
@@ -1377,8 +1414,9 @@ sub __write_etc_network_interfaces {
     foreach my $iface (keys %$ifaces) {
        my $d = $ifaces->{$iface};
 
-       delete $d->{cidr};
-       delete $d->{cidr6};
+       my ($cidr, $cidr6) = (delete $d->{cidr}, delete $d->{cidr6});
+       $d->{address} //= $cidr;
+       $d->{address6} //= $cidr6;
 
        my $ports = '';
        foreach my $k (qw(bridge_ports ovs_ports slaves ovs_bonds)) {
@@ -1449,6 +1487,7 @@ sub __write_etc_network_interfaces {
        if ($d->{type} eq 'OVSBond' && $d->{ovs_bonds}) {
            foreach my $p (split (/\s+/, $d->{ovs_bonds})) {
                my $n = $ifaces->{$p};
+               $n->{autostart} = 1;
                die "OVS bond '$iface' - unable to find slave '$p'\n"
                    if !$n;
                die "OVS bond '$iface' - wrong interface type on slave '$p' " .
@@ -1465,11 +1504,12 @@ sub __write_etc_network_interfaces {
            my $bond_primary_is_slave = undef;
            foreach my $p (split (/\s+/, $d->{slaves})) {
                my $n = $ifaces->{$p};
+               $n->{autostart} = 1;
 
                die "bond '$iface' - unable to find slave '$p'\n"
                    if !$n;
                die "bond '$iface' - wrong interface type on slave '$p' " .
-                   "('$n->{type}' != 'eth')\n" if $n->{type} ne 'eth';
+                   "('$n->{type}' != 'eth or bond')\n" if ($n->{type} ne 'eth' && $n->{type} ne 'bond');
                &$check_mtu($ifaces, $iface, $p);
                $bond_primary_is_slave = 1 if $d->{'bond-primary'} && $d->{'bond-primary'} eq $p;
            }
@@ -1565,7 +1605,7 @@ sub __write_etc_network_interfaces {
     foreach my $iface (keys %$ifaces_copy) {
        my $d = $ifaces_copy->{$iface};
        if ($d->{type} eq 'bridge') {
-           foreach my $p (split (/\s+/, $d->{bridge_ports})) {
+           foreach my $p (split (/\s+/, $d->{bridge_ports} // '')) {
                if($p =~ m/(\S+)\.(\d+)$/) {
                    my $vlanparent = $1;
                    if (!defined($ifaces_copy->{$p})) {