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);
}
# 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};
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) = @_;
# 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;
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)) {
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' " .
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;
}
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})) {