[pve-devel] [PATCH common] network interfaces code is split into syntax dependent and independent parts

Igor Vlasenko ivlasenko at gmail.com
Tue May 17 18:36:27 CEST 2016


I am trying to port PVE to ALT Linux. It use etcnet as network
configuration subsystem
instead of /etc/network/interfaces.

The patch below adds nothing.
It is just a refactoring: the code for reading and writing
/etc/network/interfaces
is split into syntax dependent and syntax independent parts.
As a result, the code for etcnet will be able to use the syntax
independent parts directly instead of copy-pasting it. Also, adding
support for other network configuration subsystems
in the future becomes easier.

I hope for the patch to be included.

Signed-off-by: Igor Vlasenko <viy at altlinux.org>
---
 src/PVE/INotify.pm | 101 +++++++++++++++++++++++++++++++++--------------------
 1 file changed, 63 insertions(+), 38 deletions(-)

diff --git a/src/PVE/INotify.pm b/src/PVE/INotify.pm
index c34659f..76bcc7a 100644
--- a/src/PVE/INotify.pm
+++ b/src/PVE/INotify.pm
@@ -798,15 +798,6 @@ sub __read_etc_network_interfaces {

     my $line;

-    if ($proc_net_dev) {
-    while (defined ($line = <$proc_net_dev>)) {
-        if ($line =~ m/^\s*(eth\d+|en[^:.]+):.*/) {
-        $ifaces->{$1}->{exists} = 1;
-        }
-    }
-    close($proc_net_dev);
-    }
-
     # we try to keep order inside the file
     my $priority = 2; # 1 is reserved for lo

@@ -905,6 +896,55 @@ sub __read_etc_network_interfaces {
     }
     }

+    __postread_setup_network_interfaces($config, $proc_net_dev,
$active_ifaces);
+
+    # OVS bridges create "allow-$BRIDGE $IFACE" lines which we need to remove
+    # from the {options} hash for them to be removed correctly.
+    @$options = grep {defined($_)} map {
+    my ($pri, $line) = @$_;
+    if ($line =~ /^allow-(\S+)\s+(.*)$/) {
+        my $bridge = $1;
+        my @ports = split(/\s+/, $2);
+        if (defined(my $br = $ifaces->{$bridge})) {
+        # if this port is part of a bridge, remove it
+        my %in_ovs_ports = map {$_=>1} split(/\s+/, $br->{ovs_ports});
+        @ports = grep { not $in_ovs_ports{$_} } @ports;
+        }
+        # create the allow line for the remaining ports, or delete if empty
+        if (@ports) {
+        [$pri, "allow-$bridge " . join(' ', @ports)];
+        } else {
+        undef;
+        }
+    } else {
+        # don't modify other lines
+        $_;
+    }
+    } @$options;
+
+    return $config;
+}
+
+# There are unofficial ports of PVE to platforms that use other
network configuration subsystems
+# such as etcnet instead of /etc/network/interfaces. The code below
is independent of
+# the network configuration subsystem used and can be shared between
different platforms.
+sub __postread_setup_network_interfaces {
+    my ($config, $proc_net_dev, $active_ifaces) = @_;
+
+    my $ifaces = $config->{ifaces} = {};
+    my $options = $config->{options} = [];
+
+    my $line;
+
+    if ($proc_net_dev) {
+    while (defined ($line = <$proc_net_dev>)) {
+        if ($line =~ m/^\s*(eth\d+|en[^:.]+):.*/) {
+        $ifaces->{$1}->{exists} = 1;
+        }
+    }
+    close($proc_net_dev);
+    }
+
     foreach my $ifname (@$active_ifaces) {
     if (my $iface = $ifaces->{$ifname}) {
         $iface->{active} = 1;
@@ -1000,32 +1040,6 @@ sub __read_etc_network_interfaces {

     $d->{families} ||= ['inet'];
     }
-
-    # OVS bridges create "allow-$BRIDGE $IFACE" lines which we need to remove
-    # from the {options} hash for them to be removed correctly.
-    @$options = grep {defined($_)} map {
-    my ($pri, $line) = @$_;
-    if ($line =~ /^allow-(\S+)\s+(.*)$/) {
-        my $bridge = $1;
-        my @ports = split(/\s+/, $2);
-        if (defined(my $br = $ifaces->{$bridge})) {
-        # if this port is part of a bridge, remove it
-        my %in_ovs_ports = map {$_=>1} split(/\s+/, $br->{ovs_ports});
-        @ports = grep { not $in_ovs_ports{$_} } @ports;
-        }
-        # create the allow line for the remaining ports, or delete if empty
-        if (@ports) {
-        [$pri, "allow-$bridge " . join(' ', @ports)];
-        } else {
-        undef;
-        }
-    } else {
-        # don't modify other lines
-        $_;
-    }
-    } @$options;
-
-    return $config;
 }

 sub __interface_to_string {
@@ -1170,12 +1184,14 @@ sub write_etc_network_interfaces {
     my $raw = __write_etc_network_interfaces($config);
     PVE::Tools::safe_print($filename, $fh, $raw);
 }
-sub __write_etc_network_interfaces {
+
+# There are unofficial ports of PVE to platforms that use other
network configuration subsystems
+# such as etcnet instead of /etc/network/interfaces. The code below
is independent of
+# the network configuration subsystem used and can be shared between
different platforms.
+sub __prewrite_setup_network_interfaces {
     my ($config) = @_;

     my $ifaces = $config->{ifaces};
-    my @options = @{$config->{options}};
-
     my $used_ports = {};

     foreach my $iface (keys %$ifaces) {
@@ -1253,6 +1269,15 @@ sub __write_etc_network_interfaces {
         }
     }
     }
+}
+
+sub __write_etc_network_interfaces {
+    my ($config) = @_;
+
+    __prewrite_setup_network_interfaces($config);
+
+    my $ifaces = $config->{ifaces};
+    my @options = @{$config->{options}};

     my $raw = <<'NETWORKDOC';
 # network interface settings; autogenerated
-- 
2.6.5



More information about the pve-devel mailing list