[pve-devel] [pve-common 1/3] INotify: use cidr for address on config change
Alexandre Derumier
aderumier at odiso.com
Thu Feb 20 12:55:18 CET 2020
netmask/broadcast options are deprecated since buster
https://manpages.debian.org/buster/ifupdown/interfaces.5.en.html
This still read both "old address/netmask" or "address cidr" format,
but always convert it to cidr address format
Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
src/PVE/INotify.pm | 24 ++++++-------------
test/etc_network_interfaces/t.bridge-v4-v6.pl | 11 ++-------
.../t.create_network.pl | 12 +++-------
.../t.list-interfaces.pl | 21 +++++++---------
.../t.ovs_bridge_allow.pl | 7 +-----
.../t.parsed_options.pl | 3 +--
.../etc_network_interfaces/t.unknown_order.pl | 15 ++++--------
.../t.update_network.pl | 16 ++++---------
8 files changed, 31 insertions(+), 78 deletions(-)
diff --git a/src/PVE/INotify.pm b/src/PVE/INotify.pm
index bbba7e2..aefdeac 100644
--- a/src/PVE/INotify.pm
+++ b/src/PVE/INotify.pm
@@ -1107,28 +1107,20 @@ sub __read_etc_network_interfaces {
# map address and netmask to cidr
if ($d->{address}) {
if ($d->{netmask} && $d->{netmask} =~ m/^\d+$/) { # e.g. netmask 20
- $d->{cidr} = $d->{address} . "/" . $d->{netmask};
+ $d->{address} = $d->{address} . "/" . $d->{netmask};
} elsif ($d->{netmask} &&
(my $cidr = PVE::JSONSchema::get_netmask_bits($d->{netmask}))) { # e.g. netmask 255.255.255.0
- $d->{cidr} = $d->{address} . "/" . $cidr;
- } elsif ($d->{address} =~ m!^(.*)/(\d+)$!) {
- $d->{cidr} = $d->{address};
- $d->{address} = $1;
- $d->{netmask} = $2;
- } else {
- $d->{cidr} = $d->{address};
+ $d->{address} = $d->{address} . "/" . $cidr;
}
+ #for api compatibility
+ $d->{cidr} = $d->{address}
}
# map address6 and netmask6 to cidr6
if ($d->{address6}) {
- $d->{cidr6} = $d->{address6};
- if ($d->{netmask6}) {
- $d->{cidr6} .= "/" . $d->{netmask6};
- } elsif ($d->{address6} =~ m!^(.*)/(\d+)$!) {
- $d->{address6} = $1;
- $d->{netmask6} = $2;
- }
+ $d->{address6} .= "/" . $d->{netmask6} if $d->{address6} !~ m!^(.*)/(\d+)$! && $d->{netmask6};
+ #for api compatibility
+ $d->{cidr6} = $d->{address6}
}
$d->{method} = 'manual' if !$d->{method};
@@ -1177,9 +1169,7 @@ sub __interface_to_string {
$raw .= "iface $iface $family " . $d->{"method$suffix"} . "\n";
$raw .= "\taddress " . $d->{"address$suffix"} . "\n" if $d->{"address$suffix"};
- $raw .= "\tnetmask " . $d->{"netmask$suffix"} . "\n" if $d->{"netmask$suffix"};
$raw .= "\tgateway " . $d->{"gateway$suffix"} . "\n" if $d->{"gateway$suffix"};
- $raw .= "\tbroadcast " . $d->{"broadcast$suffix"} . "\n" if $d->{"broadcast$suffix"};
my $done = { type => 1, priority => 1, method => 1, active => 1, exists => 1,
comments => 1, autostart => 1, options => 1,
diff --git a/test/etc_network_interfaces/t.bridge-v4-v6.pl b/test/etc_network_interfaces/t.bridge-v4-v6.pl
index 7818126..8a80074 100644
--- a/test/etc_network_interfaces/t.bridge-v4-v6.pl
+++ b/test/etc_network_interfaces/t.bridge-v4-v6.pl
@@ -1,8 +1,6 @@
-my $ip = '10.0.0.2';
-my $nm = '255.255.255.0';
+my $ip = '10.0.0.2/24';
my $gw = '10.0.0.1';
-my $ip6 = 'fc05::1:2';
-my $nm6 = '112';
+my $ip6 = 'fc05::1:2/112';
my $gw6 = 'fc05::1:1';
r(load('base'));
@@ -22,14 +20,12 @@ EOF
update_iface('vmbr0',
[ { family => 'inet',
address => $ip,
- netmask => $nm,
gateway => $gw } ],
autostart => 0);
expect load('base') . <<"EOF";
iface vmbr0 inet static
address $ip
- netmask $nm
gateway $gw
bridge-ports eth0
bridge-stp off
@@ -41,13 +37,11 @@ save('with-ipv4', w());
update_iface('vmbr0',
[ { family => 'inet6',
address => $ip6,
- netmask => $nm6,
gateway => $gw6 } ]);
expect load('with-ipv4') . <<"EOF";
iface vmbr0 inet6 static
address $ip6
- netmask $nm6
gateway $gw6
EOF
@@ -64,7 +58,6 @@ delete_iface('vmbr0', 'inet');
expect load('base') . <<"EOF";
iface vmbr0 inet6 static
address $ip6
- netmask $nm6
gateway $gw6
bridge-ports eth0
bridge-stp off
diff --git a/test/etc_network_interfaces/t.create_network.pl b/test/etc_network_interfaces/t.create_network.pl
index 4dca801..0ff6a29 100644
--- a/test/etc_network_interfaces/t.create_network.pl
+++ b/test/etc_network_interfaces/t.create_network.pl
@@ -13,8 +13,7 @@ r(load('brbase'));
# Variables used for the various interfaces:
#
-my $ip = '192.168.0.2';
-my $nm = '255.255.255.0';
+my $ip = '192.168.0.2/24';
my $gw = '192.168.0.1';
my $svcnodeip = '239.192.105.237';
my $physdev = 'eth0';
@@ -28,8 +27,7 @@ my $remoteip2 = '192.168.0.4';
my $vmbr0_part = <<"PART";
auto vmbr0
iface vmbr0 inet static
- address 10.0.0.2
- netmask 255.255.255.0
+ address 10.0.0.2/24
gateway 10.0.0.1
bridge-ports eth0
bridge-stp off
@@ -46,7 +44,6 @@ $config->{ifaces}->{eth1} = {
type => 'eth',
method => 'static',
address => $ip,
- netmask => $nm,
gateway => $gw,
families => ['inet'],
autostart => 1
@@ -56,7 +53,6 @@ my $eth1_part = <<"PART";
auto eth1
iface eth1 inet static
address $ip
- netmask $nm
gateway $gw
PART
chomp $eth1_part;
@@ -508,7 +504,6 @@ iface eth0 inet manual
auto eth1
iface eth1 inet6 static
address $ip
- netmask $nm
gateway $gw
iface eth2 inet manual
@@ -521,8 +516,7 @@ iface eth5 inet manual
auto vmbr0
iface vmbr0 inet static
- address 10.0.0.2
- netmask 255.255.255.0
+ address 10.0.0.2/24
gateway 10.0.0.1
bridge-ports eth0
bridge-stp off
diff --git a/test/etc_network_interfaces/t.list-interfaces.pl b/test/etc_network_interfaces/t.list-interfaces.pl
index 7e37afa..5925c35 100644
--- a/test/etc_network_interfaces/t.list-interfaces.pl
+++ b/test/etc_network_interfaces/t.list-interfaces.pl
@@ -16,13 +16,10 @@ eth100:
/proc/net/dev
my %wanted = (
- vmbr0 => { address => '192.168.1.2',
- netmask => '255.255.255.0',
+ vmbr0 => { address => '192.168.1.2/24',
gateway => '192.168.1.1',
- address6 => 'fc05::1:1',
- netmask6 => '112' },
- vmbr1 => { address => '10.0.0.5',
- netmask => '255.255.255.0' }
+ address6 => 'fc05::1:1/112'},
+ vmbr1 => { address => '10.0.0.5/24'}
);
save('interfaces', <<"/etc/network/interfaces");
@@ -40,23 +37,23 @@ iface eth100 inet manual
auto vmbr0
iface vmbr0 inet static
- address $wanted{vmbr0}->{address}
- netmask $wanted{vmbr0}->{netmask}
+ address 192.168.1.2
+ netmask 24
gateway $wanted{vmbr0}->{gateway}
bridge_ports eth0
bridge_stp off
bridge_fd 0
iface vmbr0 inet6 static
- address $wanted{vmbr0}->{address6}
- netmask $wanted{vmbr0}->{netmask6}
+ address fc05::1:1
+ netmask 112
source-directory before-ovs.d
allow-ovs vmbr1
iface vmbr1 inet static
- address $wanted{vmbr1}->{address}
- netmask $wanted{vmbr1}->{netmask}
+ address 10.0.0.5
+ netmask 255.255.255.0
ovs_type OVSBridge
ovs_ports eth100
diff --git a/test/etc_network_interfaces/t.ovs_bridge_allow.pl b/test/etc_network_interfaces/t.ovs_bridge_allow.pl
index 45fad2e..159b7d4 100644
--- a/test/etc_network_interfaces/t.ovs_bridge_allow.pl
+++ b/test/etc_network_interfaces/t.ovs_bridge_allow.pl
@@ -1,7 +1,6 @@
use strict;
-my $ip = '192.168.0.100';
-my $nm = '255.255.255.0';
+my $ip = '192.168.0.100/24';
my $gw = '192.168.0.1';
# replace proc_net_dev with one with a bunch of interfaces
@@ -17,7 +16,6 @@ r('');
new_iface('vmbr0', 'OVSBridge',
[ { family => 'inet',
address => $ip,
- netmask => $nm,
gateway => $gw } ],
autostart => 1);
@@ -42,7 +40,6 @@ iface eth3 inet manual
allow-ovs vmbr0
iface vmbr0 inet static
address $ip
- netmask $nm
gateway $gw
ovs_type OVSBridge
@@ -70,7 +67,6 @@ iface eth3 inet manual
allow-ovs vmbr0
iface vmbr0 inet static
address $ip
- netmask $nm
gateway $gw
ovs_type OVSBridge
ovs_ports eth1 eth2
@@ -105,7 +101,6 @@ iface eth2 inet manual
allow-ovs vmbr0
iface vmbr0 inet static
address $ip
- netmask $nm
gateway $gw
ovs_type OVSBridge
ovs_ports eth1
diff --git a/test/etc_network_interfaces/t.parsed_options.pl b/test/etc_network_interfaces/t.parsed_options.pl
index 40b5b11..7a32302 100644
--- a/test/etc_network_interfaces/t.parsed_options.pl
+++ b/test/etc_network_interfaces/t.parsed_options.pl
@@ -18,8 +18,7 @@ iface eth1 inet manual
auto vmbr0
iface vmbr0 inet static
- address 10.0.0.2
- netmask 255.255.255.0
+ address 10.0.0.2/24
gateway 10.0.0.1
bridge-ports eth0
bridge-stp off
diff --git a/test/etc_network_interfaces/t.unknown_order.pl b/test/etc_network_interfaces/t.unknown_order.pl
index 862e7b8..780c5ec 100644
--- a/test/etc_network_interfaces/t.unknown_order.pl
+++ b/test/etc_network_interfaces/t.unknown_order.pl
@@ -25,8 +25,7 @@ iface bond0 inet manual
auto bond1
iface bond1 inet static
- address 10.10.10.$ip
- netmask 255.255.255.0
+ address 10.10.10.$ip/24
bond-slaves eth2 eth3
bond-miimon 100
bond-mode balance-alb
@@ -41,20 +40,16 @@ iface bond2 inet manual
iface unknown3 inet static
address 0.0.0.0
- netmask 0.0.0.0
iface unknown4 inet static
address 0.0.0.0
- netmask 0.0.0.0
iface unknown5 inet static
address 0.0.0.0
- netmask 0.0.0.0
auto vmbr0
iface vmbr0 inet static
- address 192.168.100.13
- netmask 255.255.255.0
+ address 192.168.100.13/24
gateway 192.168.100.1
bridge-ports bond0
bridge-stp off
@@ -62,9 +57,7 @@ iface vmbr0 inet static
auto unknown6
iface unknown6 inet static
- address 10.10.11.13
- netmask 255.255.255.0
- network 10.10.11.0
+ address 10.10.11.13/24
pre-up ifconfig bond0 up
auto vmbr3
@@ -92,7 +85,7 @@ IFACES
}
r(wanted(13));
-update_iface('bond1', [ { family => 'inet', address => '10.10.10.11' } ]);
+update_iface('bond1', [ { family => 'inet', address => '10.10.10.11/24' } ]);
expect wanted(11);
1;
diff --git a/test/etc_network_interfaces/t.update_network.pl b/test/etc_network_interfaces/t.update_network.pl
index 4bf70cf..e205e4f 100644
--- a/test/etc_network_interfaces/t.update_network.pl
+++ b/test/etc_network_interfaces/t.update_network.pl
@@ -3,11 +3,9 @@ eth0:
eth1:
/proc/net/dev
-my $ip = '192.168.0.2';
-my $nm = '255.255.255.0';
+my $ip = '192.168.0.2/24';
my $gw = '192.168.0.1';
-my $ip6 = 'fc05::2';
-my $nm6 = '112';
+my $ip6 = 'fc05::2/112';
my $gw6 = 'fc05::1';
# Load
@@ -18,7 +16,6 @@ $config->{ifaces}->{eth1} = {
type => 'eth',
method => 'static',
address => $ip,
- netmask => $nm,
gateway => $gw,
families => ['inet'],
autostart => 1
@@ -33,13 +30,11 @@ iface eth0 inet manual
auto eth1
iface eth1 inet static
address $ip
- netmask $nm
gateway $gw
auto vmbr0
iface vmbr0 inet static
- address 10.0.0.2
- netmask 255.255.255.0
+ address 10.0.0.2/24
gateway 10.0.0.1
bridge-ports eth0
bridge-stp off
@@ -69,18 +64,15 @@ iface eth0 inet manual
auto eth1
iface eth1 inet static
address $ip
- netmask $nm
gateway $gw
iface eth1 inet6 static
address $ip6
- netmask $nm6
gateway $gw6
auto vmbr0
iface vmbr0 inet static
- address 10.0.0.2
- netmask 255.255.255.0
+ address 10.0.0.2/24
gateway 10.0.0.1
bridge-ports eth0
bridge-stp off
--
2.20.1
More information about the pve-devel
mailing list