read_etc_network_interfaces now opens all /proc files and
passes them as filehandles to the actual implementation
which is now is __read_etc_network_interfaces.
Similarly write_etc_network_interfaces now prints the
content string returned by the implementation now named
__write_etc_network_interfaces.
This way a testsuite can use the __* variants directly to
test the parser, passing its own versions of the /proc
files.
# }
sub read_etc_network_interfaces {
my ($filename, $fh) = @_;
# }
sub read_etc_network_interfaces {
my ($filename, $fh) = @_;
+ my $proc_net_dev = IO::File->new('/proc/net/dev', 'r');
+ my $proc_net_if_inet6 = IO::File->new('/proc/net/if_inet6', 'r');
+ return __read_etc_network_interfaces($fh, $proc_net_dev, $proc_net_if_inet6);
+}
+
+sub __read_etc_network_interfaces {
+ my ($fh, $proc_net_dev, $proc_net_if_inet6) = @_;
my $config = {};
my $ifaces = $config->{ifaces} = {};
my $config = {};
my $ifaces = $config->{ifaces} = {};
- if (my $fd2 = IO::File->new("/proc/net/dev", "r")) {
- while (defined ($line = <$fd2>)) {
+ if ($proc_net_dev) {
+ while (defined ($line = <$proc_net_dev>)) {
if ($line =~ m/^\s*(eth\d+):.*/) {
$ifaces->{$1}->{exists} = 1;
}
}
if ($line =~ m/^\s*(eth\d+):.*/) {
$ifaces->{$1}->{exists} = 1;
}
}
}
# we try to keep order inside the file
}
# we try to keep order inside the file
$d->{method} = 'manual' if !$d->{method};
$d->{method6} = 'manual' if !$d->{method6};
$d->{method} = 'manual' if !$d->{method};
$d->{method6} = 'manual' if !$d->{method6};
+
+ $d->{families} ||= ['inet'];
- if (my $fd2 = IO::File->new("/proc/net/if_inet6", "r")) {
- while (defined ($line = <$fd2>)) {
+ if ($proc_net_if_inet6) {
+ while (defined ($line = <$proc_net_if_inet6>)) {
if ($line =~ m/^[a-f0-9]{32}\s+[a-f0-9]{2}\s+[a-f0-9]{2}\s+[a-f0-9]{2}\s+[a-f0-9]{2}\s+(\S+)$/) {
$ifaces->{$1}->{active} = 1 if defined($ifaces->{$1});
}
}
if ($line =~ m/^[a-f0-9]{32}\s+[a-f0-9]{2}\s+[a-f0-9]{2}\s+[a-f0-9]{2}\s+[a-f0-9]{2}\s+(\S+)$/) {
$ifaces->{$1}->{active} = 1 if defined($ifaces->{$1});
}
}
+ close ($proc_net_if_inet6);
sub write_etc_network_interfaces {
my ($filename, $fh, $config) = @_;
sub write_etc_network_interfaces {
my ($filename, $fh, $config) = @_;
+ my $raw = __write_etc_network_interfaces($config);
+ PVE::Tools::safe_print($filename, $fh, $raw);
+}
+sub __write_etc_network_interfaces {
+ my ($config) = @_;
my $ifaces = $config->{ifaces};
my @options = @{$config->{options}};
my $ifaces = $config->{ifaces};
my @options = @{$config->{options}};
}
$raw .= $_->[1] . "\n" foreach @options;
}
$raw .= $_->[1] . "\n" foreach @options;
-
- PVE::Tools::safe_print($filename, $fh, $raw);
}
register_file('interfaces', "/etc/network/interfaces",
}
register_file('interfaces', "/etc/network/interfaces",