5bc1400711
git-svn-id: https://svn.code.sf.net/p/xcat/code/xcat-core/trunk@647 8638fb3e-16cb-4fca-ae20-7b5d299a9bcd
336 lines
10 KiB
Perl
336 lines
10 KiB
Perl
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
package xCAT::PPCboot;
|
|
use strict;
|
|
use Getopt::Long;
|
|
use xCAT::PPCcli qw(SUCCESS EXPECT_ERROR RC_ERROR NR_ERROR);
|
|
|
|
|
|
##########################################################################
|
|
# Parse the command line for options and operands
|
|
##########################################################################
|
|
sub parse_args {
|
|
|
|
my $request = shift;
|
|
my %opt = ();
|
|
my $cmd = $request->{command};
|
|
my $args = $request->{arg};
|
|
my @VERSION = qw( 2.0 );
|
|
|
|
#############################################
|
|
# Responds with usage statement
|
|
#############################################
|
|
local *usage = sub {
|
|
return( [ $_[0],
|
|
"rnetboot -h|--help",
|
|
"rnetboot -v|--version",
|
|
"rnetboot [-V|--verbose] noderange -S server -G gateway -C client -m MAC-address",
|
|
" -h writes usage information to standard output",
|
|
" -v displays command version",
|
|
" -C IP of the partition to network boot",
|
|
" -G Gateway IP of the partition specified",
|
|
" -S IP of the machine to retrieve network boot image",
|
|
" -m MAC address of network adapter to use for network boot",
|
|
" -V verbose output" ]);
|
|
};
|
|
#############################################
|
|
# Process command-line arguments
|
|
#############################################
|
|
if ( !defined( $args )) {
|
|
return( usage() );
|
|
}
|
|
#############################################
|
|
# Checks case in GetOptions, allows opts
|
|
# to be grouped (e.g. -vx), and terminates
|
|
# at the first unrecognized option.
|
|
#############################################
|
|
@ARGV = @$args;
|
|
$Getopt::Long::ignorecase = 0;
|
|
Getopt::Long::Configure( "bundling" );
|
|
|
|
if ( !GetOptions( \%opt,
|
|
qw(h|help V|Verbose v|version C=s G=s S=s m=s ))) {
|
|
return( usage() );
|
|
}
|
|
####################################
|
|
# Option -h for Help
|
|
####################################
|
|
if ( exists( $opt{h} )) {
|
|
return( usage() );
|
|
}
|
|
####################################
|
|
# Option -v for version
|
|
####################################
|
|
if ( exists( $opt{v} )) {
|
|
return( \@VERSION );
|
|
}
|
|
####################################
|
|
# Check for "-" with no option
|
|
####################################
|
|
if ( grep(/^-$/, @ARGV )) {
|
|
return(usage( "Missing option: -" ));
|
|
}
|
|
####################################
|
|
# Check for an extra argument
|
|
####################################
|
|
if ( defined( $ARGV[0] )) {
|
|
return(usage( "Invalid Argument: $ARGV[0]" ));
|
|
}
|
|
####################################
|
|
# Option -m required
|
|
####################################
|
|
if ( !exists($opt{m}) ) {
|
|
return(usage( "Missing option: -m" ));
|
|
}
|
|
####################################
|
|
# Options -C -G -S required
|
|
####################################
|
|
foreach ( qw(C G S) ) {
|
|
if ( !exists($opt{$_}) ) {
|
|
return(usage( "Missing option: -$_" ));
|
|
}
|
|
}
|
|
my $result = validate_ip( $opt{C}, $opt{G}, $opt{S} );
|
|
if ( @$result[0] ) {
|
|
return(usage( @$result[1] ));
|
|
}
|
|
####################################
|
|
# Set method to invoke
|
|
####################################
|
|
$request->{method} = $cmd;
|
|
return( \%opt );
|
|
}
|
|
|
|
|
|
|
|
##########################################################################
|
|
# Validate list of IPs
|
|
##########################################################################
|
|
sub validate_ip {
|
|
|
|
foreach (@_) {
|
|
my $ip = $_;
|
|
|
|
###################################
|
|
# Length is 4 for IPv4 addresses
|
|
###################################
|
|
my (@octets) = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
|
|
if ( scalar(@octets) != 4 ) {
|
|
return( [1,"Invalid IP address: $ip"] );
|
|
}
|
|
foreach my $octet ( @octets ) {
|
|
if (( $octet < 0 ) or ( $octet > 255 )) {
|
|
return( [1,"Invalid IP address: $ip"] );
|
|
}
|
|
}
|
|
}
|
|
return([0]);
|
|
}
|
|
|
|
|
|
##########################################################################
|
|
# IVM rnetboot
|
|
##########################################################################
|
|
sub ivm_rnetboot {
|
|
|
|
my $request = shift;
|
|
my $d = shift;
|
|
my $exp = shift;
|
|
my $name = shift;
|
|
my $opt = $request->{opt};
|
|
my $id = @$d[0];
|
|
my $profile = @$d[1];
|
|
my $fsp = @$d[2];
|
|
my $hcp = @$d[3];
|
|
my $ssh = @$exp[0];
|
|
my $userid = @$exp[4];
|
|
my $pw = @$exp[5];
|
|
my $cmd;
|
|
my $result;
|
|
|
|
#######################################
|
|
# Disconnect Expect session
|
|
#######################################
|
|
xCAT::PPCcli::disconnect( $exp );
|
|
|
|
#######################################
|
|
# Find Expect script
|
|
#######################################
|
|
$cmd = ($::XCATROOT) ? "$::XCATROOT/sbin/" : "/opt/xcat/sbin/";
|
|
$cmd .= "lpar_netboot.expect";
|
|
|
|
#######################################
|
|
# Check command installed
|
|
#######################################
|
|
if ( !-x $cmd ) {
|
|
return( [RC_ERROR,"Command not installed: $cmd"] );
|
|
}
|
|
#######################################
|
|
# Create random temporary userid/pw
|
|
# file between 1000000 and 2000000
|
|
#######################################
|
|
my $random = int( rand(1000001)) + 1000000;
|
|
my $fname = "/tmp/xCAT-$hcp-$random";
|
|
|
|
unless ( open( CRED, ">$fname" )) {
|
|
return( [RC_ERROR,"Error creating temporary password file '$fname'"]);
|
|
}
|
|
print CRED "$userid $pw\n";
|
|
close( CRED );
|
|
|
|
#######################################
|
|
# Turn on verbose and debugging
|
|
#######################################
|
|
if ( exists($request->{verbose}) ) {
|
|
$cmd.= " -v -x";
|
|
}
|
|
#######################################
|
|
# Network specified
|
|
#######################################
|
|
$cmd.= " -s auto -d auto -S $opt->{S} -G $opt->{G} -C $opt->{C}";
|
|
|
|
#######################################
|
|
# Add command options
|
|
#######################################
|
|
$cmd.= " -t ent -f \"$name\" \"$profile\" \"$fsp\" $id $hcp $fname";
|
|
|
|
#######################################
|
|
# Execute command
|
|
#######################################
|
|
if ( !open( OUTPUT, "$cmd 2>&1 |")) {
|
|
return( [RC_ERROR,"$cmd fork error: $!"] );
|
|
}
|
|
#######################################
|
|
# Get command output
|
|
#######################################
|
|
while ( <OUTPUT> ) {
|
|
$result.=$_;
|
|
}
|
|
close OUTPUT;
|
|
|
|
#######################################
|
|
# If command did not, remove file
|
|
#######################################
|
|
if ( -r $fname ) {
|
|
unlink( $fname );
|
|
}
|
|
#######################################
|
|
# Get command exit code
|
|
#######################################
|
|
my $Rc = ( $? ) ? $? >> 8 : SUCCESS;
|
|
return( [SUCCESS,$result] );
|
|
}
|
|
|
|
|
|
##########################################################################
|
|
# Get LPAR MAC addresses
|
|
##########################################################################
|
|
sub rnetboot {
|
|
|
|
my $request = shift;
|
|
my $d = shift;
|
|
my $exp = shift;
|
|
my $hwtype = @$exp[2];
|
|
my $opt = $request->{opt};
|
|
my $result;
|
|
|
|
#####################################
|
|
# Get node data
|
|
#####################################
|
|
my $type = @$d[4];
|
|
my $name = @$d[6];
|
|
|
|
#####################################
|
|
# Invalid target hardware
|
|
#####################################
|
|
if ( $type !~ /^lpar$/ ) {
|
|
return( [[$name,"Not supported"]] );
|
|
}
|
|
#########################################
|
|
# IVM does not have lpar_netboot command
|
|
# so we have to manually perform boot.
|
|
#########################################
|
|
if ( $hwtype eq "ivm" ) {
|
|
$result = ivm_rnetboot( $request, $d, $exp, $name );
|
|
}
|
|
else {
|
|
$result = xCAT::PPCcli::lpar_netboot(
|
|
$exp,
|
|
$request->{verbose},
|
|
$name,
|
|
$d,
|
|
$opt );
|
|
}
|
|
my $Rc = shift(@$result);
|
|
|
|
##################################
|
|
# Form string from array results
|
|
##################################
|
|
if ( exists($request->{verbose}) ) {
|
|
return( [[$name,join( '', @$result )]] );
|
|
}
|
|
##################################
|
|
# Return error
|
|
# lpar_netboot returns (for example):
|
|
# # Connecting to lpar1
|
|
# # Connected
|
|
# # Checking for power off.
|
|
# # Power off the node
|
|
# # Wait for power off.
|
|
# # Power off complete.
|
|
# # Power on lpar1 to Open Firmware.
|
|
# # Power on complete.
|
|
# lpar_netboot: can not find mac address 42DAB.
|
|
#
|
|
##################################
|
|
if ( $Rc != SUCCESS ) {
|
|
if ( @$result[0] =~ /lpar_netboot: (.*)/ ) {
|
|
return( [[$name,$1]] );
|
|
}
|
|
return( [[$name,join( '', @$result )]] );
|
|
}
|
|
##################################
|
|
# Split array into string
|
|
##################################
|
|
my $data = @$result[0];
|
|
if ( $hwtype eq "hmc" ) {
|
|
$data = join( '', @$result );
|
|
}
|
|
##################################
|
|
# lpar_netboot returns:
|
|
#
|
|
# # Connecting to lpar1
|
|
# # Connected
|
|
# ...
|
|
# lpar_netboot Status: network boot initiated
|
|
# # bootp sent over network.
|
|
# lpar_netboot Status: waiting for the boot image to boot up.
|
|
# # Network boot proceeding, lpar_netboot is exiting.
|
|
# # Finished.
|
|
#
|
|
#####################################
|
|
if ( $data =~ /Finished/) {
|
|
return( [[$name,"Success"]] );
|
|
}
|
|
##################################
|
|
# Can still be error w/ Rc=0:
|
|
#
|
|
# # Connecting to lpar1
|
|
# # Connected
|
|
# ...
|
|
# lpar_netboot Status: network boot initiated
|
|
# # bootp sent over network.
|
|
# lpar_netboot Status: waiting for the boot image to boot up.
|
|
# lpar_netboot: bootp operation failed.
|
|
#
|
|
#####################################
|
|
if ( $data =~ /lpar_netboot: (.*)/ ) {
|
|
return( [[$name,$1]] );
|
|
}
|
|
return( [[$name,$data]] );
|
|
}
|
|
|
|
|
|
1;
|
|
|