2007-10-26 22:44:33 +00:00
|
|
|
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
package xCAT_plugin::networks;
|
|
|
|
use xCAT::Table;
|
|
|
|
use Data::Dumper;
|
|
|
|
use Sys::Syslog;
|
2008-01-26 19:06:21 +00:00
|
|
|
use Socket;
|
2008-05-06 18:27:45 +00:00
|
|
|
use xCAT::Utils;
|
2007-10-26 22:44:33 +00:00
|
|
|
|
2008-05-06 18:27:45 +00:00
|
|
|
sub handled_commands
|
|
|
|
{
|
|
|
|
return {makenetworks => "networks",};
|
2008-01-26 19:06:21 +00:00
|
|
|
}
|
2007-10-26 22:44:33 +00:00
|
|
|
|
2008-05-06 18:27:45 +00:00
|
|
|
sub preprocess_request
|
|
|
|
{
|
|
|
|
my $req = shift;
|
|
|
|
my $cb = shift;
|
2009-07-15 15:23:35 +00:00
|
|
|
if ($req->{_xcatpreprocessed}->[0] == 1) { return [$req]; }
|
|
|
|
# exit if preprocessed
|
2008-05-06 18:27:45 +00:00
|
|
|
my @requests = ({%$req}); #first element is local instance
|
|
|
|
my @sn = xCAT::Utils->getSNList();
|
|
|
|
foreach my $s (@sn)
|
|
|
|
{
|
|
|
|
my $reqcopy = {%$req};
|
|
|
|
$reqcopy->{'_xcatdest'} = $s;
|
2009-07-15 15:23:35 +00:00
|
|
|
$reqcopy->{_xcatpreprocessed}->[0] = 1;
|
2008-05-06 18:27:45 +00:00
|
|
|
push @requests, $reqcopy;
|
2008-01-26 19:06:21 +00:00
|
|
|
}
|
2008-05-06 18:27:45 +00:00
|
|
|
return \@requests;
|
2008-01-26 19:06:21 +00:00
|
|
|
}
|
2008-05-06 18:27:45 +00:00
|
|
|
|
|
|
|
sub process_request
|
|
|
|
{
|
|
|
|
my $nettab = xCAT::Table->new('networks', -create => 1, -autocommit => 0);
|
|
|
|
my @rtable = split /\n/, `/bin/netstat -rn`;
|
|
|
|
open($rconf, "/etc/resolv.conf");
|
|
|
|
my @nameservers;
|
|
|
|
if ($rconf)
|
|
|
|
{
|
|
|
|
my @rcont;
|
|
|
|
while (<$rconf>)
|
|
|
|
{
|
|
|
|
push @rcont, $_;
|
|
|
|
}
|
|
|
|
close($rconf);
|
|
|
|
foreach (grep /nameserver/, @rcont)
|
|
|
|
{
|
|
|
|
my $line = $_;
|
|
|
|
my @pair;
|
|
|
|
$line =~ s/#.*//;
|
|
|
|
@pair = split(/\s+/, $line);
|
|
|
|
push @nameservers, $pair[1];
|
|
|
|
}
|
2008-04-08 18:30:47 +00:00
|
|
|
}
|
2008-05-06 18:27:45 +00:00
|
|
|
splice @rtable, 0, 2;
|
|
|
|
foreach (@rtable)
|
|
|
|
{ #should be the lines to think about, do something with U, and something else with UG
|
|
|
|
my $net;
|
|
|
|
my $mask;
|
|
|
|
my $mgtifname;
|
|
|
|
my $gw;
|
|
|
|
my @ent = split /\s+/, $_;
|
2008-06-02 12:00:46 +00:00
|
|
|
my $firstoctet = $ent[0];
|
|
|
|
$firstoctet =~ s/^(\d+)\..*/$1/;
|
2008-07-07 15:22:59 +00:00
|
|
|
if ($ent[0] eq "169.254.0.0" or ($firstoctet >= 224 and $firstoctet <= 239) or $ent[0] eq "127.0.0.0")
|
2008-05-06 18:27:45 +00:00
|
|
|
{
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
if ($ent[3] eq 'U')
|
|
|
|
{
|
|
|
|
$net = $ent[0];
|
|
|
|
$mask = $ent[2];
|
|
|
|
$mgtifname = $ent[7];
|
|
|
|
$nettab->setAttribs({'net' => $net},
|
|
|
|
{'mask' => $mask, 'mgtifname' => $mgtifname});
|
|
|
|
my $tent = $nettab->getAttribs({'net' => $net}, 'nameservers');
|
|
|
|
unless ($tent and $tent->{nameservers})
|
|
|
|
{
|
|
|
|
my $text = join ',', @nameservers;
|
|
|
|
$nettab->setAttribs({'net' => $net}, {nameservers => $text});
|
2007-10-26 22:44:33 +00:00
|
|
|
}
|
2008-05-06 18:27:45 +00:00
|
|
|
unless ($tent and $tent->{tftpserver})
|
|
|
|
{
|
|
|
|
my $netdev = $ent[7];
|
|
|
|
my @netlines = split /\n/, `/sbin/ip addr show dev $netdev`;
|
|
|
|
foreach (grep /\s*inet\b/, @netlines)
|
|
|
|
{
|
|
|
|
my @row = split(/\s+/, $_);
|
|
|
|
my $ipaddr = $row[2];
|
|
|
|
$ipaddr =~ s/\/.*//;
|
|
|
|
my @maska = split(/\./, $mask);
|
|
|
|
my @ipa = split(/\./, $ipaddr);
|
|
|
|
my @neta = split(/\./, $net);
|
|
|
|
my $isme = 1;
|
|
|
|
foreach (0 .. 3)
|
|
|
|
{
|
|
|
|
my $oct = (0 + $maska[$_]) & ($ipa[$_] + 0);
|
|
|
|
unless ($oct == $neta[$_])
|
|
|
|
{
|
|
|
|
$isme = 0;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($isme)
|
|
|
|
{
|
|
|
|
$nettab->setAttribs({'net' => $net},
|
|
|
|
{tftpserver => $ipaddr});
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
2007-10-26 22:44:33 +00:00
|
|
|
}
|
2008-05-06 18:27:45 +00:00
|
|
|
$nettab->commit;
|
|
|
|
|
|
|
|
#Nothing much sane to do for the other fields at the moment?
|
|
|
|
}
|
|
|
|
elsif ($ent[3] eq 'UG')
|
|
|
|
{
|
|
|
|
|
|
|
|
#TODO: networks through gateway. and how we might care..
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
#TODO: anything to do with such entries?
|
2007-10-26 22:44:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
1;
|