2007-11-16 19:47:00 +00:00
|
|
|
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
|
|
|
|
package xCAT::PPCmac;
|
|
|
|
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],
|
|
|
|
"getmacs -h|--help",
|
|
|
|
"getmacs -v|--version",
|
2008-03-19 12:44:22 +00:00
|
|
|
"getmacs [-V|--verbose] noderange [-c][-w][-S server -G gateway -C client]",
|
2007-11-16 19:47:00 +00:00
|
|
|
" -h writes usage information to standard output",
|
|
|
|
" -v displays command version",
|
2007-12-19 19:21:30 +00:00
|
|
|
" -c colon seperated output",
|
2007-11-16 19:47:00 +00:00
|
|
|
" -C IP of the partition",
|
|
|
|
" -G Gateway IP of the partition specified",
|
|
|
|
" -S Server IP to ping",
|
2008-03-19 12:44:22 +00:00
|
|
|
" -V verbose output",
|
|
|
|
" -w writes first adapter MAC to the xCAT database"]);
|
2007-11-16 19:47:00 +00:00
|
|
|
};
|
|
|
|
#############################################
|
|
|
|
# Process command-line arguments
|
|
|
|
#############################################
|
|
|
|
if ( !defined( $args )) {
|
|
|
|
$request->{method} = $cmd;
|
|
|
|
return( \%opt );
|
|
|
|
}
|
|
|
|
#############################################
|
|
|
|
# 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" );
|
|
|
|
|
2008-03-19 12:44:22 +00:00
|
|
|
if ( !GetOptions( \%opt,qw(h|help V|Verbose v|version C=s G=s S=s c w))) {
|
2007-11-16 19:47:00 +00:00
|
|
|
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]" ));
|
|
|
|
}
|
|
|
|
####################################
|
|
|
|
# If one specified, all required
|
|
|
|
####################################
|
|
|
|
my @network;
|
|
|
|
foreach ( qw(C G S) ) {
|
|
|
|
if ( exists($opt{$_}) ) {
|
|
|
|
push @network, $_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( @network ) {
|
|
|
|
if ( scalar(@network) != 3 ) {
|
|
|
|
return( usage() );
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
|
2007-12-19 19:21:30 +00:00
|
|
|
foreach (@_) {
|
|
|
|
my $ip = $_;
|
|
|
|
|
2007-11-16 19:47:00 +00:00
|
|
|
###################################
|
|
|
|
# Length is 4 for IPv4 addresses
|
|
|
|
###################################
|
|
|
|
my (@octets) = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
|
|
|
|
if ( scalar(@octets) != 4 ) {
|
2007-12-19 19:21:30 +00:00
|
|
|
return( [1,"Invalid IP address1: $ip"] );
|
2007-11-16 19:47:00 +00:00
|
|
|
}
|
|
|
|
foreach my $octet ( @octets ) {
|
|
|
|
if (( $octet < 0 ) or ( $octet > 255 )) {
|
2007-12-19 19:21:30 +00:00
|
|
|
return( [1,"Invalid IP address2: $ip"] );
|
2007-11-16 19:47:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return([0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##########################################################################
|
|
|
|
# IVM get LPAR MAC addresses
|
|
|
|
##########################################################################
|
|
|
|
sub ivm_getmacs {
|
|
|
|
|
2008-03-20 18:32:18 +00:00
|
|
|
my $request = shift;
|
|
|
|
my $d = shift;
|
|
|
|
my $exp = shift;
|
|
|
|
my $name = shift;
|
|
|
|
my $opt = $request->{opt};
|
|
|
|
my $ssh = @$exp[0];
|
|
|
|
my $userid = @$exp[4];
|
|
|
|
my $pw = @$exp[5];
|
2008-02-14 18:49:44 +00:00
|
|
|
my $cmd;
|
2007-12-19 19:21:30 +00:00
|
|
|
my $result;
|
|
|
|
|
|
|
|
#######################################
|
|
|
|
# Disconnect Expect session
|
|
|
|
#######################################
|
|
|
|
xCAT::PPCcli::disconnect( $exp );
|
|
|
|
|
2008-03-20 18:32:18 +00:00
|
|
|
#######################################
|
|
|
|
# Get node data
|
|
|
|
#######################################
|
|
|
|
my $id = @$d[0];
|
|
|
|
my $pprofile = @$d[1];
|
|
|
|
my $fsp = @$d[2];
|
|
|
|
my $hcp = @$d[3];
|
|
|
|
|
2008-02-14 18:49:44 +00:00
|
|
|
#######################################
|
|
|
|
# Find Expect script
|
|
|
|
#######################################
|
|
|
|
$cmd = ($::XCATROOT) ? "$::XCATROOT/sbin/" : "/opt/xcat/sbin/";
|
|
|
|
$cmd .= "lpar_netboot.expect";
|
|
|
|
|
2007-12-19 19:21:30 +00:00
|
|
|
#######################################
|
|
|
|
# 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";
|
|
|
|
}
|
|
|
|
#######################################
|
|
|
|
# Colon seperated output
|
|
|
|
#######################################
|
|
|
|
if ( exists($opt->{c}) ) {
|
|
|
|
$cmd.= " -c";
|
|
|
|
}
|
|
|
|
#######################################
|
|
|
|
# Network specified (-D ping test)
|
|
|
|
#######################################
|
|
|
|
if ( exists( $opt->{S} )) {
|
|
|
|
$cmd.= " -D -s auto -d auto -S $opt->{S} -G $opt->{G} -C $opt->{C}";
|
|
|
|
}
|
|
|
|
#######################################
|
|
|
|
# Add command options
|
|
|
|
#######################################
|
2008-03-20 18:32:18 +00:00
|
|
|
$cmd.= " -t ent -f -M -A -n \"$name\" \"$pprofile\" \"$fsp\" $id $hcp $fname";
|
2007-12-19 19:21:30 +00:00
|
|
|
|
|
|
|
#######################################
|
|
|
|
# Execute command
|
|
|
|
#######################################
|
|
|
|
if ( !open( OUTPUT, "$cmd 2>&1 |")) {
|
|
|
|
return( [RC_ERROR,"$cmd fork error: $!"] );
|
|
|
|
}
|
|
|
|
#######################################
|
|
|
|
# Get command output
|
|
|
|
#######################################
|
|
|
|
while ( <OUTPUT> ) {
|
|
|
|
$result.=$_;
|
|
|
|
}
|
|
|
|
close OUTPUT;
|
2007-11-16 19:47:00 +00:00
|
|
|
|
2007-12-19 19:21:30 +00:00
|
|
|
#######################################
|
|
|
|
# If command did not, remove file
|
|
|
|
#######################################
|
|
|
|
if ( -r $fname ) {
|
|
|
|
unlink( $fname );
|
|
|
|
}
|
2008-03-03 19:55:49 +00:00
|
|
|
#######################################
|
|
|
|
# Get command exit code
|
|
|
|
#######################################
|
2008-03-04 20:01:02 +00:00
|
|
|
my $Rc = SUCCESS;
|
|
|
|
|
|
|
|
foreach ( split /\n/, $result ) {
|
|
|
|
if ( /^lpar_netboot: / ) {
|
|
|
|
$Rc = RC_ERROR;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
2008-03-19 12:44:22 +00:00
|
|
|
######################################
|
|
|
|
# Split results into array
|
|
|
|
######################################
|
|
|
|
return( [$Rc, split( /\n/, $result)] );
|
2007-11-16 19:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
##########################################################################
|
|
|
|
# Get LPAR MAC addresses
|
|
|
|
##########################################################################
|
|
|
|
sub getmacs {
|
|
|
|
|
|
|
|
my $request = shift;
|
|
|
|
my $d = shift;
|
|
|
|
my $exp = shift;
|
2007-12-19 19:21:30 +00:00
|
|
|
my $opt = $request->{opt};
|
2007-11-16 19:47:00 +00:00
|
|
|
my $hwtype = @$exp[2];
|
2008-03-19 12:44:22 +00:00
|
|
|
my $delim = ( exists( $opt->{c} )) ? ":" : " ";
|
2007-12-19 19:21:30 +00:00
|
|
|
my $result;
|
2008-03-06 16:11:40 +00:00
|
|
|
my $node;
|
2007-11-16 19:47:00 +00:00
|
|
|
|
|
|
|
#########################################
|
|
|
|
# Get node data
|
|
|
|
#########################################
|
2008-03-06 16:11:40 +00:00
|
|
|
my $lparid = @$d[0];
|
|
|
|
my $mtms = @$d[2];
|
|
|
|
my $type = @$d[4];
|
|
|
|
my $name = @$d[6];
|
2007-11-16 19:47:00 +00:00
|
|
|
|
|
|
|
#########################################
|
|
|
|
# Invalid target hardware
|
|
|
|
#########################################
|
|
|
|
if ( $type ne "lpar" ) {
|
2008-03-06 16:11:40 +00:00
|
|
|
return( [[$name,"Node must be LPAR",RC_ERROR]] );
|
|
|
|
}
|
|
|
|
#########################################
|
|
|
|
# Get name known by HCP
|
|
|
|
#########################################
|
|
|
|
my $filter = "name,lpar_id";
|
|
|
|
my $values = xCAT::PPCcli::lssyscfg( $exp, $type, $mtms, $filter );
|
|
|
|
my $Rc = shift(@$values);
|
|
|
|
|
|
|
|
#########################################
|
|
|
|
# Return error
|
|
|
|
#########################################
|
|
|
|
if ( $Rc != SUCCESS ) {
|
|
|
|
return( [[$name,@$values[0],$Rc]] );
|
|
|
|
}
|
|
|
|
#########################################
|
|
|
|
# Find LPARs by lpar_id
|
|
|
|
#########################################
|
|
|
|
foreach ( @$values ) {
|
|
|
|
if ( /^(.*),$lparid$/ ) {
|
|
|
|
$node = $1;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#########################################
|
|
|
|
# Node not found by lpar_id
|
|
|
|
#########################################
|
|
|
|
if ( !defined( $node )) {
|
|
|
|
return( [[$name,"Node not found, lparid=$lparid",RC_ERROR]] );
|
2007-11-16 19:47:00 +00:00
|
|
|
}
|
|
|
|
#########################################
|
|
|
|
# IVM does not have lpar_netboot command
|
|
|
|
# so we have to manually collect MAC
|
|
|
|
# addresses.
|
|
|
|
#########################################
|
|
|
|
if ( $hwtype eq "ivm" ) {
|
2008-03-06 16:11:40 +00:00
|
|
|
$result = ivm_getmacs( $request, $d, $exp, $node );
|
2007-11-16 19:47:00 +00:00
|
|
|
}
|
2007-12-19 19:21:30 +00:00
|
|
|
else {
|
|
|
|
$result = xCAT::PPCcli::lpar_netboot(
|
|
|
|
$exp,
|
|
|
|
$request->{verbose},
|
2008-03-06 16:11:40 +00:00
|
|
|
$node,
|
2007-11-16 19:47:00 +00:00
|
|
|
$d,
|
2007-12-19 19:21:30 +00:00
|
|
|
$opt );
|
|
|
|
}
|
2007-11-16 19:47:00 +00:00
|
|
|
my $Rc = shift(@$result);
|
2008-03-04 20:01:02 +00:00
|
|
|
|
2007-12-19 19:21:30 +00:00
|
|
|
##################################
|
|
|
|
# Form string from array results
|
|
|
|
##################################
|
|
|
|
if ( exists($request->{verbose}) ) {
|
2008-03-19 12:44:22 +00:00
|
|
|
if ( $Rc == SUCCESS ) {
|
|
|
|
if ( exists( $opt->{w} )) {
|
|
|
|
writemac( $name, $delim, $result );
|
|
|
|
}
|
|
|
|
}
|
2008-03-05 18:39:02 +00:00
|
|
|
return( [[$name,join( '', @$result ),$Rc]] );
|
2007-12-19 19:21:30 +00:00
|
|
|
}
|
2007-11-16 19:47:00 +00:00
|
|
|
##################################
|
|
|
|
# Return error
|
|
|
|
##################################
|
|
|
|
if ( $Rc != SUCCESS ) {
|
2008-03-04 20:01:02 +00:00
|
|
|
if ( @$result[0] =~ /lpar_netboot: (.*)/ ) {
|
2008-03-05 18:39:02 +00:00
|
|
|
return( [[$name,$1,$Rc]] );
|
2008-03-04 20:01:02 +00:00
|
|
|
}
|
2008-03-05 18:39:02 +00:00
|
|
|
return( [[$name,join( '', @$result ),$Rc]] );
|
2007-11-16 19:47:00 +00:00
|
|
|
}
|
2008-03-19 12:44:22 +00:00
|
|
|
#####################################
|
2007-11-16 19:47:00 +00:00
|
|
|
# lpar_netboot returns:
|
|
|
|
#
|
2007-12-20 15:28:28 +00:00
|
|
|
# # Connecting to lpar4\n
|
|
|
|
# # Connected\n
|
|
|
|
# # Checking for power off.\n
|
|
|
|
# # Power off complete.\n
|
|
|
|
# # Power on lpar4 to Open Firmware.\n
|
|
|
|
# # Power on complete.\n
|
|
|
|
# # Getting adapter location codes.\n
|
|
|
|
# # Type\t Location Code\t MAC Address\t Full Path Name\tPing Result\n
|
|
|
|
# ent U9117.MMA.10F6F3D-V5-C3-T1 1e0e122a930d /vdevice/l-lan@30000003
|
2007-12-19 19:21:30 +00:00
|
|
|
#
|
2007-11-16 19:47:00 +00:00
|
|
|
#####################################
|
|
|
|
my $values;
|
2008-03-19 12:44:22 +00:00
|
|
|
|
2007-12-19 19:21:30 +00:00
|
|
|
foreach ( @$result ) {
|
2008-03-04 20:01:02 +00:00
|
|
|
if ( /^#\s?Type/ ) {
|
2007-12-20 15:28:28 +00:00
|
|
|
$values.= "\n$_\n";
|
2008-03-19 12:44:22 +00:00
|
|
|
} elsif ( /^ent$delim/ ) {
|
2007-12-20 15:28:28 +00:00
|
|
|
$values.= "$_\n";
|
|
|
|
}
|
2007-11-16 19:47:00 +00:00
|
|
|
}
|
2008-03-19 12:44:22 +00:00
|
|
|
#####################################
|
|
|
|
# Write first adapter MAC to database
|
|
|
|
#####################################
|
|
|
|
if ( exists( $opt->{w} )) {
|
|
|
|
writemac( $name, $delim, $result );
|
|
|
|
}
|
2008-03-05 18:39:02 +00:00
|
|
|
return( [[$name,$values,$Rc]] );
|
2007-11-16 19:47:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-19 12:44:22 +00:00
|
|
|
##########################################################################
|
|
|
|
# Writes the first adapter MAC to the database
|
|
|
|
##########################################################################
|
|
|
|
sub writemac {
|
|
|
|
|
|
|
|
my $name = shift;
|
|
|
|
my $delim = shift;
|
|
|
|
my $data = shift;
|
|
|
|
my $values;
|
|
|
|
|
|
|
|
#####################################
|
|
|
|
# Find first adapter
|
|
|
|
#####################################
|
|
|
|
foreach ( @$data ) {
|
|
|
|
if ( /^ent$delim/ ) {
|
|
|
|
$values = $_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#####################################
|
|
|
|
# Get adapter mac
|
|
|
|
#####################################
|
|
|
|
my ($k,$u);
|
|
|
|
my @fields = split $delim, $values;
|
|
|
|
my $mac = $fields[2];
|
|
|
|
|
|
|
|
#####################################
|
|
|
|
# Write adapter mac to database
|
|
|
|
#####################################
|
|
|
|
my $tab = xCAT::Table->new( "mac", -create=>1, -autocommit=>1 );
|
|
|
|
if ( !$tab ) {
|
|
|
|
return( [[$name,"Error opening 'mac'",RC_ERROR]] );
|
|
|
|
}
|
|
|
|
$k->{node} = $name;
|
|
|
|
$u->{mac} = $mac;
|
|
|
|
my $d = $tab->setAttribs( $k,$u );
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-16 19:47:00 +00:00
|
|
|
1;
|
2007-12-19 19:21:30 +00:00
|
|
|
|
2008-02-14 18:49:44 +00:00
|
|
|
|
2008-03-04 20:01:02 +00:00
|
|
|
|
2008-03-06 16:11:40 +00:00
|
|
|
|
2008-03-19 12:44:22 +00:00
|
|
|
|
2008-03-20 18:32:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
|