diff options
Diffstat (limited to 'perl-install/network.pm')
-rw-r--r-- | perl-install/network.pm | 187 |
1 files changed, 104 insertions, 83 deletions
diff --git a/perl-install/network.pm b/perl-install/network.pm index 2bf55a448..4ec01e5fb 100644 --- a/perl-install/network.pm +++ b/perl-install/network.pm @@ -20,22 +20,20 @@ use log; sub read_conf { my ($file) = @_; my %netc = getVarsFromSh($file); - $netc{dnsServer} = delete $netc{NS0}; + foreach (0..2) { + exists $netc{"NS$_"} and $netc{dnsServers}{delete $netc{"NS$_"}} = $_ + 1; + } \%netc; } sub read_resolv_conf { my ($file) = @_; - my %netc; - my @l; + my ($i, %netc); - local *F; - open F, $file or die "cannot open $file: $!"; + local *F; open F, $file or die "cannot open $file: $!"; foreach (<F>) { - push @l, $1 if (/^\s*nameserver\s+([^\s]+)/); + /^\s*nameserver\s+(\S+)/ and $netc{dnsServers}{$1} = ++$i; } - - $netc{$_} = shift @l foreach qw(dnsServer dnsServer2 dnsServer3); \%netc; } @@ -51,15 +49,15 @@ sub read_interface_conf { sub up_it { my ($prefix, $intfs) = @_; - $_->{isUp} and return foreach @$intfs; + $_->{isUp} and return foreach values %$intfs; my $f = "/etc/resolv.conf"; symlink "$prefix/$f", $f; run_program::rooted($prefix, "/etc/rc.d/init.d/network", "start"); - $_->{isUp} = 1 foreach @$intfs; + $_->{isUp} = 1 foreach values %$intfs; } sub down_it { my ($prefix, $intfs) = @_; run_program::rooted($prefix, "/etc/rc.d/init.d/network", "stop"); - $_->{isUp} = 1 foreach @$intfs; + $_->{isUp} = 1 foreach values %$intfs; } sub write_conf { @@ -78,20 +76,27 @@ sub write_conf { sub write_resolv_conf { my ($file, $netc) = @_; - #- We always write these, even if they were autoconfigured. Otherwise, the reverse name lookup in the install doesn't work. unless ($netc->{DOMAINNAME} || dnsServers($netc)) { unlink($file); log::l("neither domain name nor dns server are configured"); return 0; } - substInFile { - s/^([^#].*\n)/\#$1/; - if (eof) { - $_ .= "search $netc->{DOMAINNAME}\n" if $netc->{DOMAINNAME}; - $_ .= "nameserver $_\n" foreach dnsServers($netc); - } - } $file; + my (%search, %dns, @unknown); + local *F; open F, $file; + foreach (<F>) { + /^\s*search\s+(.*?)\s*$/ and $search{$1} = $., next; + /^\s*nameserver\s+(.*?)\s*$/ and $dns{$1} = $., next; + /^[#\s]*(\S.*?)\s*$/ and push @unknown, $1; + } + + close F; open F, ">$file" or die "cannot write $file: $!"; + print F "# search $_\n" foreach grep { $_ ne $netc->{DOMAINNAME} } sort { $search{$a} <=> $search{$b} } keys %search; + print F "search $netc->{DOMAINNAME}\n\n" if $netc->{DOMAINNAME}; + print F "# nameserver $_\n" foreach grep { ! exists $netc->{dnsServers}{$_} } sort { $dns{$a} <=> $dns{$b} } keys %dns; + print F "nameserver $_\n" foreach dnsServers($netc); + print F "\n"; + print F "# $_\n" foreach @unknown; #-res_init(); # reinit the resolver so DNS changes take affect 1; @@ -172,14 +177,12 @@ sub resolv($) { sub dnsServers { my ($netc) = @_; - grep { $_ } map { $netc->{$_} } qw(dnsServer dnsServer2 dnsServer3); + sort { $netc->{dnsServers}{$a} <=> $netc->{dnsServers}{$b} } grep { $_ } keys %{$netc->{dnsServers} || {}}; } sub findIntf { my ($intf, $device) = @_; - my ($l) = grep { $_->{DEVICE} eq $device } @$intf; - push @$intf, $l = { DEVICE => $device } unless $l; - $l; + $intf->{$device} ||= { DEVICE => $device }; } #PAD \s* a la fin my $ip_regexp = qr/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/; @@ -242,14 +245,12 @@ sub configureNetwork { my @l = detect_devices::getNet() or die _("no network card found"); my $last; foreach ($::beginner ? $l[0] : @l) { - my $intf2 = findIntf($intf ||= [], $_); + my $intf2 = findIntf($intf ||= {}, $_); add2hash($intf2, $last); add2hash($intf2, { NETMASK => '255.255.255.0' }); configureNetworkIntf($in, $intf2) or last; $netc ||= {}; - delete $netc->{dnsServer}; - delete $netc->{GATEWAY}; $last = $intf2; } #- { @@ -262,13 +263,71 @@ _("Please enter your host name if you know it. Some DHCP servers require the hostname to work. Your host name should be a fully-qualified host name, such as ``mybox.mylab.myco.com''."), - [_("Host name:")], [ \$netc->{HOSTNAME} ]); + [_("Host name")], [ \$netc->{HOSTNAME} ]); } else { configureNetworkNet($in, $netc, $last ||= {}, @l); } miscellaneousNetwork($in); } + +sub configureNetworkIntf { + my ($in, $intf) = @_; + my $pump = $intf->{BOOTPROTO} =~ /^(dhcp|bootp)$/; + delete $intf->{NETWORK}; + delete $intf->{BROADCAST}; + my @fields = qw(IPADDR NETMASK); + $::isStandalone or $in->set_help('configureNetworkIP'); + $in->ask_from_entries_ref(_("Configuring network device %s", $intf->{DEVICE}), +($::isStandalone ? '' : _("Configuring network device %s", $intf->{DEVICE}) . "\n\n") . +_("Please enter the IP configuration for this machine. +Each item should be entered as an IP address in dotted-decimal +notation (for example, 1.2.3.4)."), + [ _("IP address"), _("Netmask"), _("Automatic IP") ], + [ \$intf->{IPADDR}, \$intf->{NETMASK}, { val => \$pump, type => "bool", text => _("(bootp/dhcp)") } ], + complete => sub { + $intf->{BOOTPROTO} = $pump ? "dhcp" : "static"; + return 0 if $pump; + for (my $i = 0; $i < @fields; $i++) { + unless (is_ip($intf->{$fields[$i]})) { + $in->ask_warn('', _("IP address should be in format 1.2.3.4")); + return (1,$i); + } + return 0; + } + }, + focus_out => sub { + $intf->{NETMASK} ||= netmask($intf->{IPADDR}) unless $_[0] + } + ); +} + +sub configureNetworkNet { + my ($in, $netc, $intf, @devices) = @_; + + my ($dns1, $dns2) = dnsServers($netc); + my ($lvl_dns1, $lvl_dns2) = (delete $netc->{dnsServers}{$dns1}, delete $netc->{dnsServers}{$dns2}); + $dns1 ||= dns($intf->{IPADDR}); + $netc->{GATEWAY} ||= gateway($intf->{IPADDR}); + + $in->ask_from_entries_refH(_("Configuring network"), +_("Please enter your host name. +Your host name should be a fully-qualified host name, +such as ``mybox.mylab.myco.com''. +You may also enter the IP address of the gateway if you have one"), + [ _("Host name") => \$netc->{HOSTNAME}, + _("First DNS server") => \$dns1, + _("Second DNS server") => \$dns2, + _("Gateway") => \$netc->{GATEWAY}, + $::expert ? (_("Gateway device") => {val => \$netc->{GATEWAYDEV}, list => \@devices }) : (), + ], + ) or return; + + my ($old_dns1, $old_dns2) = dnsServers($netc); + $netc->{dnsServers}{$dns1} = $lvl_dns1 || 1; + $netc->{dnsServers}{$dns2} = $lvl_dns2 || $lvl_dns1 + 1 || 2; +} + sub miscellaneousNetwork { my ($in, $clicked) = @_; my $u = $::o->{miscellaneous} ||= {}; @@ -289,73 +348,35 @@ sub miscellaneousNetwork { ) || return; } -sub configureNetworkNet { - my ($in, $netc, $intf, @devices) = @_; - - $netc->{dnsServer} ||= dns($intf->{IPADDR}); - $netc->{GATEWAY} ||= gateway($intf->{IPADDR}); - - $in->ask_from_entries_ref(_("Configuring network"), -_("Please enter your host name. -Your host name should be a fully-qualified host name, -such as ``mybox.mylab.myco.com''. -You may also enter the IP address of the gateway if you have one"), - [_("Host name:"), _("DNS server:"), _("Gateway:"), $::expert ? _("Gateway device:") : ()], - [(map { \$netc->{$_} } qw(HOSTNAME dnsServer GATEWAY)), - {val => \$netc->{GATEWAYDEV}, list => \@devices}] - ); +sub read_all_conf { + my ($prefix, $netc, $intf) = @_; + $netc ||= {}; $intf ||= {}; + add2hash($netc, read_conf("$prefix/etc/sysconfig/network")) if -r "$prefix/etc/sysconfig/network"; + add2hash($netc, read_resolv_conf("$prefix/etc/resolv.conf")) if -r "$prefix/etc/resolv.conf"; + foreach (all("$prefix/etc/sysconfig/network-scripts")) { + if (/ifcfg-(\w+)/ && $1 ne 'lo' && $1 !~ /ppp/) { + my $intf = findIntf($intf, $1); + add2hash($intf, { getVarsFromSh("$prefix/etc/sysconfig/network-scripts/$_") }); + } + } } - sub configureNetwork2 { - my ($in, $prefix, $netc, $intf) = @_; + my ($prefix, $netc, $intf, $install) = @_; my $etc = "$prefix/etc"; write_conf("$etc/sysconfig/network", $netc); write_resolv_conf("$etc/resolv.conf", $netc); - write_interface_conf("$etc/sysconfig/network-scripts/ifcfg-$_->{DEVICE}", $_) foreach @{$intf}; - add2hosts("$etc/hosts", $netc->{HOSTNAME}, map { $_->{IPADDR} } @{$intf}); + write_interface_conf("$etc/sysconfig/network-scripts/ifcfg-$_->{DEVICE}", $_) foreach values %$intf; + add2hosts("$etc/hosts", $netc->{HOSTNAME}, map { $_->{IPADDR} } values %$intf); sethostname($netc) unless $::testing; addDefaultRoute($netc) unless $::testing; - - grep { $_->{BOOTPROTO} =~ /^(dhcp)$/ } @{$intf} and $::isStandalone ? system("urpmi --auto dhcpd") : $in->pkg_install("dhcpcd"); - # Handle also pump (this is still in initscripts no?) - grep { $_->{BOOTPROTO} =~ /^(pump|bootp)$/ } @{$intf} and $::isStandalone ? system("urpmi --auto pump") : $in->pkg_install("pump"); + + grep { $_->{BOOTPROTO} =~ /^(dhcp)$/ } values %$intf and $install && $install->('dhcpd'); + grep { $_->{BOOTPROTO} =~ /^(pump|bootp)$/ } values %$intf and $install && $install->('pump'); #-res_init(); #- reinit the resolver so DNS changes take affect - any::miscellaneousNetwork($in, $prefix); -} - - -sub configureNetworkIntf { - my ($in, $intf) = @_; - my $pump = $intf->{BOOTPROTO} =~ /^(dhcp|bootp)$/; - delete $intf->{NETWORK}; - delete $intf->{BROADCAST}; - my @fields = qw(IPADDR NETMASK); - $::isStandalone or $in->set_help('configureNetworkIP'); - $in->ask_from_entries_ref(_("Configuring network device %s", $intf->{DEVICE}), -($::isStandalone ? '' : _("Configuring network device %s", $intf->{DEVICE}) . "\n\n") . -_("Please enter the IP configuration for this machine. -Each item should be entered as an IP address in dotted-decimal -notation (for example, 1.2.3.4)."), - [ _("IP address:"), _("Netmask:"), _("Automatic IP") ], - [ \$intf->{IPADDR}, \$intf->{NETMASK}, { val => \$pump, type => "bool", text => _("(bootp/dhcp)") } ], - complete => sub { - $intf->{BOOTPROTO} = $pump ? "dhcp" : "static"; - return 0 if $pump; - for (my $i = 0; $i < @fields; $i++) { - unless (is_ip($intf->{$fields[$i]})) { - $in->ask_warn('', _("IP address should be in format 1.2.3.4")); - return (1,$i); - } - return 0; - } - }, - focus_out => sub { - $intf->{NETMASK} ||= netmask($intf->{IPADDR}) unless $_[0] - } - ); + any::miscellaneousNetwork($prefix); } |