2008-02-22 21:15:19 +00:00
|
|
|
package xCAT_plugin::mknb;
|
2008-07-18 19:59:20 +00:00
|
|
|
use strict;
|
2008-02-22 21:15:19 +00:00
|
|
|
use File::Temp qw(tempdir);
|
|
|
|
use xCAT::Utils;
|
2012-08-09 04:07:40 +00:00
|
|
|
use xCAT::TableUtils;
|
2008-02-22 21:15:19 +00:00
|
|
|
use File::Path;
|
|
|
|
use File::Copy;
|
|
|
|
|
|
|
|
sub handled_commands {
|
|
|
|
return {
|
|
|
|
mknb => 'mknb',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub process_request {
|
|
|
|
my $request = shift;
|
|
|
|
my $callback = shift;
|
2012-05-23 05:12:18 +00:00
|
|
|
#my $sitetab = xCAT::Table->new('site');
|
2008-02-22 21:15:19 +00:00
|
|
|
my $serialport;
|
|
|
|
my $serialspeed;
|
|
|
|
my $serialflow;
|
|
|
|
my $xcatdport = 3001;
|
2012-05-23 05:12:18 +00:00
|
|
|
#if ($sitetab) {
|
|
|
|
#my $portent = $sitetab->getAttribs({key=>'defserialport'},'value');
|
2012-08-09 04:07:40 +00:00
|
|
|
my @entries = xCAT::TableUtils->get_site_attribute("defserialport");
|
2012-05-23 05:12:18 +00:00
|
|
|
my $t_entry = $entries[0];
|
|
|
|
if ( defined($t_entry) ) {
|
|
|
|
$serialport=$t_entry;
|
2008-02-22 21:15:19 +00:00
|
|
|
}
|
2012-05-23 05:12:18 +00:00
|
|
|
#$portent = $sitetab->getAttribs({key=>'defserialspeed'},'value');
|
2012-08-09 04:07:40 +00:00
|
|
|
@entries = xCAT::TableUtils->get_site_attribute("defserialspeed");
|
2012-05-23 05:12:18 +00:00
|
|
|
$t_entry = $entries[0];
|
|
|
|
if ( defined($t_entry) ) {
|
|
|
|
$serialspeed=$t_entry;
|
2008-02-22 21:15:19 +00:00
|
|
|
}
|
2012-05-23 05:12:18 +00:00
|
|
|
#$portent = $sitetab->getAttribs({key=>'defserialflow'},'value');
|
2012-08-09 04:07:40 +00:00
|
|
|
@entries = xCAT::TableUtils->get_site_attribute("defserialflow");
|
2012-05-23 05:12:18 +00:00
|
|
|
$t_entry = $entries[0];
|
|
|
|
if ( defined($t_entry) ) {
|
|
|
|
$serialflow=$t_entry;
|
2008-02-22 21:15:19 +00:00
|
|
|
}
|
2012-05-23 05:12:18 +00:00
|
|
|
#$portent = $sitetab->getAttribs({key=>'xcatdport'},'value');
|
2012-08-09 04:07:40 +00:00
|
|
|
@entries = xCAT::TableUtils->get_site_attribute("xcatdport");
|
2012-05-23 05:12:18 +00:00
|
|
|
$t_entry = $entries[0];
|
|
|
|
if ( defined($t_entry) ) {
|
|
|
|
$xcatdport=$t_entry;
|
2008-02-22 21:15:19 +00:00
|
|
|
}
|
2012-05-23 05:12:18 +00:00
|
|
|
#$sitetab->close;
|
|
|
|
#}
|
2008-02-22 21:15:19 +00:00
|
|
|
|
2012-08-09 04:07:40 +00:00
|
|
|
my $tftpdir = xCAT::TableUtils->getTftpDir();
|
2012-01-10 15:15:51 +00:00
|
|
|
my $arch = $request->{arg}->[0];
|
|
|
|
if (! $arch) {
|
|
|
|
$callback->({error=>"Need to specify architecture (x86, x86_64 or ppc64)"},{errorcode=>[1]});
|
2008-02-22 21:15:19 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-05-23 19:19:40 +00:00
|
|
|
unless (-d "$::XCATROOT/share/xcat/netboot/$arch" or -d "$::XCATROOT/share/xcat/netboot/genesis/$arch") {
|
|
|
|
$callback->({error=>"Unable to find directory $::XCATROOT/share/xcat/netboot/$arch or $::XCATROOT/share/xcat/netboot/genesis/$arch",errorcode=>[1]});
|
2008-02-22 21:15:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
unless ( -r "/root/.ssh/id_rsa.pub" ) {
|
2008-05-16 19:17:44 +00:00
|
|
|
if (-r "/root/.ssh/id_rsa") {
|
|
|
|
$callback->({data=>["Extracting ssh public key from private key"]});
|
|
|
|
my $rc = system('ssh-keygen -y -f /root/.ssh/id_rsa > /root/.ssh/id_rsa.pub');
|
|
|
|
if ($rc) {
|
|
|
|
$callback->({error=>["Failure executing ssh-keygen for root"],errorcode=>[1]});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$callback->({data=>["Generating ssh private key for root"]});
|
|
|
|
my $rc=system('ssh-keygen -t rsa -q -b 2048 -N "" -f /root/.ssh/id_rsa');
|
|
|
|
if ($rc) {
|
|
|
|
$callback->({error=>["Failure executing ssh-keygen for root"],errorcode=>[1]});
|
|
|
|
}
|
2008-02-22 21:15:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
my $tempdir = tempdir("mknb.$$.XXXXXX",TMPDIR=>1);
|
|
|
|
unless ($tempdir) {
|
|
|
|
$callback->({error=>["Failed to create a temporary directory"],errorcode=>[1]});
|
|
|
|
return;
|
|
|
|
}
|
2011-09-13 13:56:43 +00:00
|
|
|
my $rc;
|
|
|
|
my $invisibletouch=0;
|
|
|
|
if (-e "$::XCATROOT/share/xcat/netboot/genesis/$arch") {
|
|
|
|
$rc = system("cp -a $::XCATROOT/share/xcat/netboot/genesis/$arch/fs/* $tempdir");
|
|
|
|
$rc = system("cp -a $::XCATROOT/share/xcat/netboot/genesis/$arch/kernel $tftpdir/xcat/genesis.kernel.$arch");
|
|
|
|
$invisibletouch=1;
|
|
|
|
} else {
|
|
|
|
$rc = system("cp -a $::XCATROOT/share/xcat/netboot/$arch/nbroot/* $tempdir");
|
|
|
|
}
|
2008-02-22 21:15:19 +00:00
|
|
|
if ($rc) {
|
|
|
|
system("rm -rf $tempdir");
|
2011-09-13 13:56:43 +00:00
|
|
|
if ($invisibletouch) {
|
|
|
|
$callback->({error=>["Failed to copy $::XCATROOT/share/xcat/netboot/genesis/$arch/fs contents"],errorcode=>[1]});
|
|
|
|
} else {
|
|
|
|
$callback->({error=>["Failed to copy $::XCATROOT/share/xcat/netboot/$arch/nbroot/ contents"],errorcode=>[1]});
|
|
|
|
}
|
2008-02-22 21:15:19 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-09-13 18:12:39 +00:00
|
|
|
my $sshdir;
|
2011-09-13 13:56:43 +00:00
|
|
|
if ($invisibletouch) {
|
|
|
|
$sshdir="/.ssh";
|
|
|
|
} else {
|
|
|
|
$sshdir="/root/.ssh";
|
|
|
|
}
|
|
|
|
mkpath($tempdir."$sshdir");
|
|
|
|
chmod(0700,$tempdir."$sshdir");
|
2011-09-13 19:12:37 +00:00
|
|
|
copy("/root/.ssh/id_rsa.pub","$tempdir$sshdir/authorized_keys");
|
2011-09-13 13:56:43 +00:00
|
|
|
chmod(0600,"$tempdir$sshdir/authorized_keys");
|
|
|
|
if (not $invisibletouch and -r "/etc/xcat/hostkeys/ssh_host_key") {
|
2009-02-10 19:48:29 +00:00
|
|
|
copy("/etc/xcat/hostkeys/ssh_host_key","$tempdir/etc/ssh_host_key");
|
|
|
|
copy("/etc/xcat/hostkeys/ssh_host_rsa_key","$tempdir/etc/ssh_host_rsa_key");
|
|
|
|
copy("/etc/xcat/hostkeys/ssh_host_dsa_key","$tempdir/etc/ssh_host_dsa_key");
|
2008-02-25 19:47:41 +00:00
|
|
|
chmod(0600,<$tempdir/etc/ssh_*>);
|
2008-02-22 21:15:19 +00:00
|
|
|
}
|
2011-09-13 13:56:43 +00:00
|
|
|
unless ($invisibletouch or -r "$tempdir/etc/ssh_host_key") {
|
2008-02-22 21:15:19 +00:00
|
|
|
system("ssh-keygen -t rsa1 -f $tempdir/etc/ssh_host_key -C '' -N ''");
|
|
|
|
system("ssh-keygen -t rsa -f $tempdir/etc/ssh_host_rsa_key -C '' -N ''");
|
|
|
|
system("ssh-keygen -t dsa -f $tempdir/etc/ssh_host_dsa_key -C '' -N ''");
|
|
|
|
}
|
2014-09-24 09:38:54 +00:00
|
|
|
my $initrd_file = undef;
|
2012-01-26 22:06:09 +00:00
|
|
|
my $lzma_exit_value=1;
|
2011-09-13 13:56:43 +00:00
|
|
|
if ($invisibletouch) {
|
2012-01-26 22:06:09 +00:00
|
|
|
my $done=0;
|
2012-01-16 14:34:32 +00:00
|
|
|
if (-x "/usr/bin/lzma") { #let's reclaim some of that size...
|
|
|
|
$callback->({data=>["Creating genesis.fs.$arch.lzma in $tftpdir/xcat"]});
|
|
|
|
system("cd $tempdir; find . | cpio -o -H newc | lzma -C crc32 -9 > $tftpdir/xcat/genesis.fs.$arch.lzma");
|
2012-01-26 22:06:09 +00:00
|
|
|
$lzma_exit_value=$? >> 8;
|
|
|
|
if ($lzma_exit_value) {
|
|
|
|
$callback->({data=>["Creating genesis.fs.$arch.lzma in $tftpdir/xcat failed, falling back to gzip"]});
|
2012-01-31 16:45:38 +00:00
|
|
|
unlink ("$tftpdir/xcat/genesis.fs.$arch.lzma");
|
2012-01-26 22:06:09 +00:00
|
|
|
} else {
|
|
|
|
$done = 1;
|
2014-09-24 09:38:54 +00:00
|
|
|
$initrd_file = "$tftpdir/xcat/genesis.fs.$arch.lzma";
|
2012-01-26 22:06:09 +00:00
|
|
|
}
|
2012-02-09 16:18:40 +00:00
|
|
|
}
|
2012-01-26 22:06:09 +00:00
|
|
|
|
|
|
|
if (not $done) {
|
2011-09-13 13:56:43 +00:00
|
|
|
$callback->({data=>["Creating genesis.fs.$arch.gz in $tftpdir/xcat"]});
|
|
|
|
system("cd $tempdir; find . | cpio -o -H newc | gzip -9 > $tftpdir/xcat/genesis.fs.$arch.gz");
|
2014-09-24 09:38:54 +00:00
|
|
|
$initrd_file = "$tftpdir/xcat/genesis.fs.$arch.gz";
|
2012-01-16 14:34:32 +00:00
|
|
|
}
|
2011-09-13 13:56:43 +00:00
|
|
|
} else {
|
|
|
|
$callback->({data=>["Creating nbfs.$arch.gz in $tftpdir/xcat"]});
|
|
|
|
system("cd $tempdir; find . | cpio -o -H newc | gzip -9 > $tftpdir/xcat/nbfs.$arch.gz");
|
2014-09-24 09:38:54 +00:00
|
|
|
$initrd_file = "$tftpdir/xcat/nbfs.$arch.gz";
|
2011-09-13 13:56:43 +00:00
|
|
|
}
|
2008-05-07 19:50:14 +00:00
|
|
|
system ("rm -rf $tempdir");
|
2014-09-24 09:38:54 +00:00
|
|
|
unless ($initrd_file) {
|
|
|
|
$callback->({data=>["Creating filesystem file in $tftpdir/xcat failed"]});
|
|
|
|
return;
|
|
|
|
}
|
2012-08-09 04:07:40 +00:00
|
|
|
my $hexnets = xCAT::NetworkUtils->my_hexnets();
|
2012-08-14 11:55:48 +00:00
|
|
|
my $normnets = xCAT::NetworkUtils->my_nets();
|
2008-02-22 21:15:19 +00:00
|
|
|
my $consolecmdline;
|
2008-02-25 14:37:58 +00:00
|
|
|
if (defined($serialport) and $serialspeed) {
|
2009-09-15 17:59:11 +00:00
|
|
|
$consolecmdline = "console=tty0 console=ttyS$serialport,$serialspeed";
|
2008-02-22 21:15:19 +00:00
|
|
|
if ($serialflow =~ /cts/ or $serialflow =~ /hard/) {
|
|
|
|
$consolecmdline .= "n8r";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
my $cfgfile;
|
2008-02-26 18:48:43 +00:00
|
|
|
if ($arch =~ /x86/) {
|
2009-06-26 19:29:01 +00:00
|
|
|
mkpath("$tftpdir/xcat/xnba/nets");
|
|
|
|
chmod(0755,"$tftpdir/xcat/xnba");
|
2009-06-29 18:37:45 +00:00
|
|
|
chmod(0755,"$tftpdir/xcat/xnba/nets");
|
2008-02-27 19:06:18 +00:00
|
|
|
mkpath("$tftpdir/pxelinux.cfg");
|
|
|
|
chmod(0755,"$tftpdir/pxelinux.cfg");
|
2008-02-26 18:48:43 +00:00
|
|
|
if (! -r "$tftpdir/pxelinux.0") {
|
2008-06-26 14:35:44 +00:00
|
|
|
unless (-r "/usr/lib/syslinux/pxelinux.0" or -r "/usr/share/syslinux/pxelinux.0") {
|
2008-02-26 18:48:43 +00:00
|
|
|
$callback->({error=>["Unable to find pxelinux.0 "],errorcode=>[1]});
|
|
|
|
return;
|
|
|
|
}
|
2008-06-26 14:35:44 +00:00
|
|
|
if (-r "/usr/lib/syslinux/pxelinux.0") {
|
|
|
|
copy("/usr/lib/syslinux/pxelinux.0","$tftpdir/pxelinux.0");
|
|
|
|
} else {
|
|
|
|
copy("/usr/share/syslinux/pxelinux.0","$tftpdir/pxelinux.0");
|
|
|
|
}
|
2008-02-26 18:48:43 +00:00
|
|
|
chmod(0644,"$tftpdir/pxelinux.0");
|
|
|
|
}
|
2009-02-05 15:46:43 +00:00
|
|
|
} elsif ($arch =~ /ppc/) {
|
2014-09-24 09:38:54 +00:00
|
|
|
mkpath("$tftpdir/pxelinux.cfg/p/");
|
2008-02-26 18:48:43 +00:00
|
|
|
}
|
2008-04-25 14:03:55 +00:00
|
|
|
my $dopxe=0;
|
2009-06-26 19:29:01 +00:00
|
|
|
foreach (keys %{$normnets}) {
|
|
|
|
my $net = $_;
|
|
|
|
$net =~s/\//_/;
|
|
|
|
$dopxe=0;
|
|
|
|
if ($arch =~ /x86/) { #only do pxe if just x86 or x86_64 and no x86
|
2011-09-13 13:56:43 +00:00
|
|
|
if ($arch =~ /x86_64/ and not $invisibletouch) {
|
2009-06-29 18:37:45 +00:00
|
|
|
if (-r "$tftpdir/xcat/xnba/nets/$net") {
|
2009-06-26 19:29:01 +00:00
|
|
|
my $cfg;
|
|
|
|
my @contents;
|
|
|
|
open($cfg,"<","$tftpdir/xcat/xnba/nets/$net");
|
|
|
|
@contents = <$cfg>;
|
|
|
|
close($cfg);
|
|
|
|
if (grep (/x86_64/,@contents)) {
|
|
|
|
$dopxe=1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$dopxe = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$dopxe = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($dopxe) {
|
|
|
|
my $cfg;
|
|
|
|
open($cfg,">","$tftpdir/xcat/xnba/nets/$net");
|
|
|
|
print $cfg "#!gpxe\n";
|
2011-09-13 13:56:43 +00:00
|
|
|
if ($invisibletouch) {
|
|
|
|
print $cfg 'imgfetch -n kernel http://${next-server}/tftpboot/xcat/genesis.kernel.'."$arch quiet xcatd=".$normnets->{$_}.":$xcatdport $consolecmdline BOOTIF=01-".'${netX/machyp}'."\n";
|
2012-01-26 22:06:09 +00:00
|
|
|
if ($lzma_exit_value) {
|
2011-09-13 13:56:43 +00:00
|
|
|
print $cfg 'imgfetch -n nbfs http://${next-server}/tftpboot/xcat/genesis.fs.'."$arch.gz\n";
|
2012-01-26 22:06:09 +00:00
|
|
|
} else {
|
|
|
|
print $cfg 'imgfetch -n nbfs http://${next-server}/tftpboot/xcat/genesis.fs.'."$arch.lzma\n";
|
|
|
|
}
|
2011-09-13 13:56:43 +00:00
|
|
|
} else {
|
2009-06-29 18:37:45 +00:00
|
|
|
print $cfg 'imgfetch -n kernel http://${next-server}/tftpboot/xcat/nbk.'."$arch quiet xcatd=".$normnets->{$_}.":$xcatdport $consolecmdline\n";
|
|
|
|
print $cfg 'imgfetch -n nbfs http://${next-server}/tftpboot/xcat/nbfs.'."$arch.gz\n";
|
2011-09-13 13:56:43 +00:00
|
|
|
}
|
2009-06-26 19:29:01 +00:00
|
|
|
print $cfg "imgload kernel\n";
|
|
|
|
print $cfg "imgexec kernel\n";
|
|
|
|
close($cfg);
|
2011-09-13 13:56:43 +00:00
|
|
|
if ($invisibletouch and $arch =~ /x86_64/) { #UEFI time
|
|
|
|
open($cfg,">","$tftpdir/xcat/xnba/nets/$net.elilo");
|
2011-09-15 22:00:55 +00:00
|
|
|
print $cfg "default=\"xCAT Genesis\"\ndelay=5\n\n";
|
|
|
|
print $cfg 'image=/tftpboot/xcat/genesis.kernel.'."$arch\n";
|
|
|
|
print $cfg " label=\"xCAT Genesis\"\n";
|
2012-04-05 21:12:08 +00:00
|
|
|
if ($lzma_exit_value) {
|
2011-09-15 22:00:55 +00:00
|
|
|
print $cfg " initrd=/tftpboot/xcat/genesis.fs.$arch.gz\n";
|
2012-04-05 21:12:08 +00:00
|
|
|
} else {
|
|
|
|
print $cfg " initrd=/tftpboot/xcat/genesis.fs.$arch.lzma\n";
|
|
|
|
}
|
2011-09-15 22:00:55 +00:00
|
|
|
print $cfg " append=\"quiet xcatd=".$normnets->{$_}.":$xcatdport destiny=discover $consolecmdline BOOTIF=%B\"\n";
|
2011-09-13 13:56:43 +00:00
|
|
|
close($cfg);
|
|
|
|
open($cfg,">","$tftpdir/xcat/xnba/nets/$net.uefi");
|
|
|
|
print $cfg "#!gpxe\n";
|
2011-09-15 22:00:55 +00:00
|
|
|
print $cfg 'chain http://${next-server}/tftpboot/xcat/elilo-x64.efi -C /tftpboot/xcat/xnba/nets/'."$net.elilo\n";
|
2011-09-13 13:56:43 +00:00
|
|
|
close($cfg);
|
2011-09-13 18:12:39 +00:00
|
|
|
}
|
2011-09-13 13:56:43 +00:00
|
|
|
|
2009-06-26 19:29:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$dopxe=0;
|
2008-02-22 21:15:19 +00:00
|
|
|
foreach (keys %{$hexnets}) {
|
2008-04-25 14:03:55 +00:00
|
|
|
$dopxe=0;
|
|
|
|
if ($arch =~ /x86/) { #only do pxe if just x86 or x86_64 and no x86
|
|
|
|
if ($arch =~ /x86_64/) {
|
|
|
|
if (-r "$tftpdir/pxelinux.cfg/".uc($_)) {
|
|
|
|
my $pcfg;
|
|
|
|
open($pcfg,"<","$tftpdir/pxelinux.cfg/".uc($_));
|
|
|
|
my @pcfgcontents = <$pcfg>;
|
|
|
|
close($pcfg);
|
|
|
|
if (grep (/x86_64/,@pcfgcontents)) {
|
|
|
|
$dopxe=1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$dopxe=1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$dopxe=1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($dopxe) {
|
2008-02-22 21:15:19 +00:00
|
|
|
open($cfgfile,">","$tftpdir/pxelinux.cfg/".uc($_));
|
|
|
|
print $cfgfile "DEFAULT xCAT\n";
|
|
|
|
print $cfgfile " LABEL xCAT\n";
|
|
|
|
print $cfgfile " KERNEL xcat/nbk.$arch\n";
|
2008-03-21 20:00:07 +00:00
|
|
|
print $cfgfile " APPEND initrd=xcat/nbfs.$arch.gz quiet xcatd=".$hexnets->{$_}.":$xcatdport $consolecmdline\n";
|
2008-02-22 21:15:19 +00:00
|
|
|
close($cfgfile);
|
|
|
|
} elsif ($arch =~ /ppc/) {
|
2014-09-24 09:41:55 +00:00
|
|
|
open($cfgfile,">", "$tftpdir/pxelinux.cfg/p/default");
|
2014-09-24 09:38:54 +00:00
|
|
|
print $cfgfile "default xCAT\n";
|
|
|
|
print $cfgfile " label xCAT\n";
|
|
|
|
print $cfgfile " kernel http://".$hexnets->{$_}.":80/$tftpdir/xcat/genesis.kernel.$arch\n";
|
|
|
|
print $cfgfile " initrd http://".$hexnets->{$_}.":80/$initrd_file\n";
|
|
|
|
print $cfgfile ' append "quiet xcatd='.$hexnets->{$_}.":$xcatdport $consolecmdline\"\n";
|
2008-02-22 21:15:19 +00:00
|
|
|
close($cfgfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|