2007-10-26 22:44:33 +00:00
|
|
|
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
package xCAT_plugin::pxe;
|
|
|
|
use Data::Dumper;
|
|
|
|
use Sys::Syslog;
|
|
|
|
use Socket;
|
2008-03-04 19:36:35 +00:00
|
|
|
use File::Copy;
|
2007-10-26 22:44:33 +00:00
|
|
|
|
|
|
|
my $request;
|
|
|
|
my $callback;
|
|
|
|
my $dhcpconf = "/etc/dhcpd.conf";
|
|
|
|
my $tftpdir = "/tftpboot";
|
|
|
|
#my $dhcpver = 3;
|
|
|
|
|
|
|
|
my %usage = (
|
|
|
|
"nodeset" => "Usage: nodeset <noderange> [install|shell|boot|runcmd=bmcsetup]",
|
|
|
|
);
|
|
|
|
sub handled_commands {
|
|
|
|
return {
|
|
|
|
nodeset => "noderes:netboot"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub check_dhcp {
|
|
|
|
#TODO: omapi magic to do things right
|
|
|
|
my $node = shift;
|
|
|
|
my $dhcpfile;
|
|
|
|
open ($dhcpfile,$dhcpconf);
|
|
|
|
while (<$dhcpfile>) {
|
|
|
|
if (/host $node\b/) {
|
|
|
|
close $dhcpfile;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close $dhcpfile;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub getstate {
|
|
|
|
my $node = shift;
|
|
|
|
if (check_dhcp($node)) {
|
|
|
|
if (-r $tftpdir . "/pxelinux.cfg/".$node) {
|
|
|
|
my $fhand;
|
|
|
|
open ($fhand,$tftpdir . "/pxelinux.cfg/".$node);
|
|
|
|
my $headline = <$fhand>;
|
|
|
|
close $fhand;
|
|
|
|
$headline =~ s/^#//;
|
|
|
|
chomp($headline);
|
|
|
|
return $headline;
|
|
|
|
} else {
|
|
|
|
return "boot";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return "discover";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub setstate {
|
|
|
|
=pod
|
|
|
|
|
|
|
|
This function will manipulate the pxelinux.cfg structure to match what the noderes/chain tables indicate the node should be booting.
|
|
|
|
|
|
|
|
=cut
|
|
|
|
my $node = shift;
|
|
|
|
my $restab = xCAT::Table->new('noderes');
|
|
|
|
my $kern = $restab->getNodeAttribs($node,['kernel','initrd','kcmdline']);
|
|
|
|
my $pcfg;
|
|
|
|
open($pcfg,'>',$tftpdir."/pxelinux.cfg/".$node);
|
|
|
|
my $chaintab = xCAT::Table->new('chain');
|
|
|
|
my $cref=$chaintab->getNodeAttribs($node,['currstate']);
|
|
|
|
if ($cref->{currstate}) {
|
|
|
|
print $pcfg "#".$cref->{currstate}."\n";
|
|
|
|
}
|
|
|
|
print $pcfg "DEFAULT xCAT\n";
|
|
|
|
print $pcfg "LABEL xCAT\n";
|
2008-02-02 19:39:33 +00:00
|
|
|
$chaintab = xCAT::Table->new('chain');
|
2007-10-26 22:44:33 +00:00
|
|
|
my $stref = $chaintab->getNodeAttribs($node,['currstate']);
|
|
|
|
if ($stref and $stref->{currstate} eq "boot") {
|
|
|
|
print $pcfg "LOCALBOOT 0\n";
|
|
|
|
close($pcfg);
|
|
|
|
} elsif ($kern and $kern->{kernel}) {
|
|
|
|
#It's time to set pxelinux for this node to boot the kernel..
|
|
|
|
print $pcfg " KERNEL ".$kern->{kernel}."\n";
|
|
|
|
if ($kern->{initrd} or $kern->{kcmdline}) {
|
|
|
|
print $pcfg " APPEND ";
|
|
|
|
}
|
|
|
|
if ($kern and $kern->{initrd}) {
|
|
|
|
print $pcfg "initrd=".$kern->{initrd}." ";
|
|
|
|
}
|
|
|
|
if ($kern and $kern->{kcmdline}) {
|
|
|
|
print $pcfg $kern->{kcmdline}."\n";
|
|
|
|
} else {
|
|
|
|
print $pcfg "\n";
|
|
|
|
}
|
|
|
|
close($pcfg);
|
|
|
|
my $inetn = inet_aton($node);
|
|
|
|
unless ($inetn) {
|
|
|
|
syslog("local1|err","xCAT unable to resolve IP for $node in pxe plugin");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else { #TODO: actually, should possibly default to xCAT image?
|
|
|
|
print $pcfg "LOCALBOOT 0\n";
|
|
|
|
close($pcfg);
|
|
|
|
}
|
2008-02-28 00:17:24 +00:00
|
|
|
my $mactab = xCAT::Table->new('mac'); #to get all the hostnames
|
|
|
|
my %ipaddrs;
|
2007-10-26 22:44:33 +00:00
|
|
|
my $ip = inet_ntoa(inet_aton($node));;
|
|
|
|
unless ($ip) {
|
|
|
|
syslog("local1|err","xCAT unable to resolve IP in pxe plugin");
|
|
|
|
return;
|
|
|
|
}
|
2008-02-28 00:17:24 +00:00
|
|
|
$ipaddrs{$ip} = 1;
|
|
|
|
if ($mactab) {
|
|
|
|
my $ment = $mactab->getNodeAttribs($node,['mac']);
|
|
|
|
if ($ment and $ment->{mac}) {
|
|
|
|
my @macs = split(/\|/,$ment->{mac});
|
|
|
|
foreach (@macs) {
|
|
|
|
if (/!(.*)/) {
|
|
|
|
$ipaddrs{inet_ntoa(inet_aton($1))} = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach $ip (keys %ipaddrs) {
|
|
|
|
my @ipa=split(/\./,$ip);
|
|
|
|
my $pname = sprintf("%02X%02X%02X%02X",@ipa);
|
|
|
|
unlink($tftpdir."/pxelinux.cfg/".$pname);
|
|
|
|
link($tftpdir."/pxelinux.cfg/".$node,$tftpdir."/pxelinux.cfg/".$pname);
|
|
|
|
}
|
2007-10-26 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
my $errored = 0;
|
|
|
|
sub pass_along {
|
|
|
|
my $resp = shift;
|
|
|
|
$callback->($resp);
|
|
|
|
if ($resp and $resp->{errorcode} and $resp->{errorcode}->[0]) {
|
|
|
|
$errored=1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-01-27 20:45:31 +00:00
|
|
|
sub preprocess_request {
|
|
|
|
my $req = shift;
|
2008-02-02 19:39:33 +00:00
|
|
|
$callback = shift;
|
|
|
|
if ($req->{_xcatdest}) { return [$req]; } #Exit if the packet has been preprocessed in its history
|
|
|
|
my @requests = ({%$req}); #Start with a straight copy to reflect local instance
|
|
|
|
my $sitetab = xCAT::Table->new('site');
|
|
|
|
(my $ent) = $sitetab->getAttribs({key=>'xcatservers'},'value');
|
|
|
|
$sitetab->close;
|
|
|
|
if ($ent and $ent->{value}) {
|
|
|
|
foreach (split /,/,$ent->{value}) {
|
|
|
|
if (xCAT::Utils->thishostisnot($_)) {
|
|
|
|
my $reqcopy = {%$req};
|
|
|
|
$reqcopy->{'_xcatdest'} = $_;
|
|
|
|
push @requests,$reqcopy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return \@requests;
|
2008-01-27 20:45:31 +00:00
|
|
|
}
|
2008-02-02 19:39:33 +00:00
|
|
|
#sub preprocess_request {
|
|
|
|
# my $req = shift;
|
|
|
|
# my $callback = shift;
|
|
|
|
# my %localnodehash;
|
|
|
|
# my %dispatchhash;
|
|
|
|
# my $nrtab = xCAT::Table->new('noderes');
|
|
|
|
# foreach my $node (@{$req->{node}}) {
|
|
|
|
# my $nodeserver;
|
|
|
|
# my $tent = $nrtab->getNodeAttribs($node,['tftpserver']);
|
|
|
|
# if ($tent) { $nodeserver = $tent->{tftpserver} }
|
|
|
|
# unless ($tent and $tent->{tftpserver}) {
|
|
|
|
# $tent = $nrtab->getNodeAttribs($node,['servicenode']);
|
|
|
|
# if ($tent) { $nodeserver = $tent->{servicenode} }
|
|
|
|
# }
|
|
|
|
# if ($nodeserver) {
|
|
|
|
# $dispatchhash{$nodeserver}->{$node} = 1;
|
|
|
|
# } else {
|
|
|
|
# $localnodehash{$node} = 1;
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
# my @requests;
|
|
|
|
# my $reqc = {%$req};
|
|
|
|
# $reqc->{node} = [ keys %localnodehash ];
|
|
|
|
# if (scalar(@{$reqc->{node}})) { push @requests,$reqc }
|
|
|
|
#
|
|
|
|
# foreach my $dtarg (keys %dispatchhash) { #iterate dispatch targets
|
|
|
|
# my $reqcopy = {%$req}; #deep copy
|
|
|
|
# $reqcopy->{'_xcatdest'} = $dtarg;
|
|
|
|
# $reqcopy->{node} = [ keys %{$dispatchhash{$dtarg}}];
|
|
|
|
# push @requests,$reqcopy;
|
|
|
|
# }
|
|
|
|
# return \@requests;
|
|
|
|
#}
|
2007-10-26 22:44:33 +00:00
|
|
|
|
|
|
|
sub process_request {
|
|
|
|
$request = shift;
|
|
|
|
$callback = shift;
|
|
|
|
my $sub_req = shift;
|
|
|
|
my @args;
|
|
|
|
my @nodes;
|
2008-02-02 19:39:33 +00:00
|
|
|
my @rnodes;
|
2007-10-26 22:44:33 +00:00
|
|
|
if (ref($request->{node})) {
|
2008-02-02 19:39:33 +00:00
|
|
|
@rnodes = @{$request->{node}};
|
2007-10-26 22:44:33 +00:00
|
|
|
} else {
|
2008-02-02 19:39:33 +00:00
|
|
|
if ($request->{node}) { @rnodes = ($request->{node}); }
|
2007-10-26 22:44:33 +00:00
|
|
|
}
|
2008-02-02 19:39:33 +00:00
|
|
|
unless (@rnodes) {
|
2007-10-26 22:44:33 +00:00
|
|
|
if ($usage{$request->{command}->[0]}) {
|
|
|
|
$callback->({data=>$usage{$request->{command}->[0]}});
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2008-02-02 19:39:33 +00:00
|
|
|
@nodes = ();
|
|
|
|
foreach (@rnodes) {
|
|
|
|
if (xCAT::Utils->nodeonmynet($_)) {
|
|
|
|
push @nodes,$_;
|
|
|
|
}
|
|
|
|
}
|
2008-02-26 18:48:43 +00:00
|
|
|
if (! -r "$tftpdir/pxelinux.0") {
|
|
|
|
unless (-r "/usr/lib/syslinux/pxelinux.0") {
|
|
|
|
$callback->({error=>["Unable to find pxelinux.0 "],errorcode=>[1]});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
copy("/usr/lib/syslinux/pxelinux.0","$tftpdir/pxelinux.0");
|
|
|
|
chmod(0644,"$tftpdir/pxelinux.0");
|
|
|
|
}
|
|
|
|
unless ( -r "$tftpdir/pxelinux.0" ) {
|
2008-02-27 14:59:40 +00:00
|
|
|
$callback->({errror=>["Unable to find pxelinux.0 from syslinux"],errorcode=>[1]});
|
2008-02-26 18:48:43 +00:00
|
|
|
return;
|
|
|
|
}
|
2008-02-02 19:39:33 +00:00
|
|
|
|
2007-10-26 22:44:33 +00:00
|
|
|
|
|
|
|
if (ref($request->{arg})) {
|
|
|
|
@args=@{$request->{arg}};
|
|
|
|
} else {
|
|
|
|
@args=($request->{arg});
|
|
|
|
}
|
|
|
|
unless ($args[0] eq 'stat' or $args[0] eq 'enact') {
|
|
|
|
$sub_req->({command=>['setdestiny'],
|
|
|
|
node=>\@nodes,
|
|
|
|
arg=>[$args[0]]},\&pass_along);
|
|
|
|
}
|
|
|
|
if ($errored) { return; }
|
|
|
|
foreach (@nodes) {
|
|
|
|
my %response;
|
|
|
|
$response{node}->[0]->{name}->[0]=$_;
|
|
|
|
if ($args[0] eq 'stat') {
|
|
|
|
$response{node}->[0]->{data}->[0]= getstate($_);
|
|
|
|
$callback->(\%response);
|
|
|
|
} elsif ($args[0] eq 'enact') {
|
|
|
|
setstate($_);
|
|
|
|
} elsif ($args[0]) { #If anything else, send it on to the destiny plugin, then setstate
|
|
|
|
setstate($_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
1;
|