2008-04-24 13:04:01 +00:00
#!/usr/bin/perl
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
2013-01-09 16:24:11 +00:00
# xCAT post script for configuring additional ethernet nics. Information is
# retreieved from nics table. Environment variables are set in the postscript
# The nic (i.e. eth1, en1) is passed as the only argument.
# Environment variables are set in the postscript in the mypostscript.tmpl
# file on the management node:
#
2013-01-02 19:48:07 +00:00
#
# NICNODE - the name of the node minus the NICHOSTNAMESUFFIXES
# NICIPS - the ip address for this nic
# NICTYPES - for configeth - this must be ethernet
# NICCUSTOMSCRIPTS - parsed in confignics to invoke this script if set. (not used here)
# NICNETWORKS - the network this nic is attached to. Must also verify this network is
# set in the networks table.
2008-04-24 13:04:01 +00:00
2011-11-01 20:26:01 +00:00
use strict;
2008-04-24 13:04:01 +00:00
use Socket;
2013-01-02 19:48:07 +00:00
my $nic = shift(@ARGV);
my $nicips = $ENV{NICIPS};
my $nicnetworks = $ENV{NICNETWORKS};
my $nicnode = $ENV{NICNODE};
2013-01-04 21:55:30 +00:00
my $net_cnt = $ENV{NETWORKS_LINES};
2009-03-29 18:39:34 +00:00
2013-01-09 16:24:11 +00:00
my $netmask ='';
2013-01-02 19:48:07 +00:00
my $ipaddr = '';
my $nic_num = '';
2013-01-09 16:24:11 +00:00
my $subnet = '';
my $nic_net = '';
my $net_name = '';
my @nic_nets = (); # array of networks for this nic
my @nic_ips =(); # array of ipaddresses for this nic
my @networks = (); # array of all networks from networks table.
# { network_name, subnet, netmask }
2010-05-10 08:05:31 +00:00
2013-01-04 21:55:30 +00:00
system("logger -t xcat -p local4.err 'configeth: NIC: $nic'");
system("logger -t xcat -p local4.err 'configeth: NICNETWORKS: $nicnetworks'");
system("logger -t xcat -p local4.err 'configeth: NICIPS: $nicips'");
2013-01-09 16:24:11 +00:00
# create array of network info. Needed in case where there are
# more than one ip address per nic and shouldn't be many networks.
2013-01-04 21:55:30 +00:00
my $net_info;
2013-01-09 16:24:11 +00:00
my $cnt = 1;
2013-01-04 21:55:30 +00:00
while ( $cnt <= $net_cnt ) {
$net_info = $ENV{"NETWORKS_LINE$cnt"};
2013-01-09 16:24:11 +00:00
$net_info =~ /^netname=([^\|]*)\|\|/;
$net_name = $1;
$net_info =~ /net=([^\|]*)\|\|/;
$subnet = $1;
$net_info =~ /mask=([^\|]*)\|\|/;
$netmask = $1;
push @{ $networks[$cnt-1] }, ($net_name, $subnet, $netmask);
$cnt +=1;
2013-01-02 19:48:07 +00:00
}
2010-05-10 08:05:31 +00:00
2013-01-09 16:24:11 +00:00
# get network or networks for this nic from NICNETWORKS:
# eth0:1_0_0_0-255_255_0_0|network2,eth1:1_1_0_0
# create array of networks for this nic
foreach my $nic_networks (split(/,/,$nicnetworks)) {
2013-01-09 20:25:08 +00:00
my @net = ();
if ( $nic_networks =~ /!/ ) {
@net = split(/!/,$nic_networks);
} else {
@net = split(/:/,$nic_networks);
}
2013-01-09 16:24:11 +00:00
if ($net[0] eq $nic) {
@nic_nets = split(/\|/,$net[1]);
last;
}
2013-01-04 21:55:30 +00:00
}
2013-01-09 16:24:11 +00:00
# get all nic ipaddress from $nicips: i.e. eth0:1.0.0.1|2.0.0.1,eth1:1.1.1.1
# Then get all ips for this specific nic, i.e. eth0.
2013-01-02 19:48:07 +00:00
foreach my $ips (split(/,/,$nicips)) {
2013-01-09 20:25:08 +00:00
my @ip = ();
if ( $ips =~ /!/ ) {
@ip = split(/!/,$ips);
} else {
@ip = split(/:/,$ips);
}
2013-01-04 21:55:30 +00:00
if ($ip[0] eq $nic ) {
2013-01-09 16:24:11 +00:00
@nic_ips = split(/\|/,$ip[1]);
2010-05-10 08:05:31 +00:00
}
}
2013-01-09 16:24:11 +00:00
my $i;
for ($i=0; $i < (scalar @nic_ips) ; $i++ ) {
2013-01-02 19:48:07 +00:00
2013-01-09 16:24:11 +00:00
# Time to create the interfaces.
# loop through the nic networks, find the matching networks to get the
# subnet and netmask and then create the appropriate ifcfg file for linux
# or invoke correct AIX command.
my $specific_nic = $nic;
if ($i > 0) {
$specific_nic = $nic . ":" . ($i);
}
2013-01-02 19:48:07 +00:00
2013-01-09 16:24:11 +00:00
$cnt = 0;
$subnet = "";
$netmask = "";
$net_name = "";
while ( $cnt < $net_cnt ) {
if ( $networks[$cnt][0] eq $nic_nets[$i] ) {
$subnet = $networks[$cnt][1];
$netmask = $networks[$cnt][2];
$cnt = $net_cnt; # found match - get out.
}
else {
$cnt++;
2010-05-10 08:05:31 +00:00
}
2013-01-09 16:24:11 +00:00
}
2009-02-12 14:14:05 +00:00
2013-01-09 16:24:11 +00:00
# check that there is a subnet and netmask set
if ( !(length($subnet) > 0) || !(length($netmask) > 0) ) {
system("logger -t xcat -p local4.err 'configeth: network subnet or netmask not set.'");
exit 1;
}
2013-01-02 19:48:07 +00:00
2013-01-09 16:24:11 +00:00
if ($^O =~ /^aix/i) {
2013-02-01 04:25:18 +00:00
if ($i == 0) {
if ($nic_ips[$i] =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/) {
runcmd("chdev -l '$nic' -a netaddr=$nic_ips[$i] -a netmask=$netmask -a state='up'");
# } else { #ipv6
# runcmd("autoconf6 -6i en$nic_num");
2013-01-09 16:24:11 +00:00
}
2013-02-01 04:25:18 +00:00
} else {
if ($nic_ips[$i] =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/) {
runcmd("chdev -l '$nic' -a alias4=$nic_ips[$i],$netmask");
# } else { #ipv6
# runcmd("autoconf6 -6i en$nic_num");
}
}
2013-01-09 16:24:11 +00:00
}
elsif (($ENV{OSVER} && ($ENV{OSVER} =~ /sles|suse/i)) || (-f "/etc/SuSE-release")) {
# Write the info to the ifcfg file
my $dir = "/etc/sysconfig/network";
2013-01-31 15:17:09 +00:00
if ($i == 0 ) {
if (!open(FILE, ">$dir/ifcfg-$nic")) { system("logger -t xcat -p local4.err 'configeth: cannot open $dir/ifcfg-$nic.'"); exit 1; }
# Not sure what is really REQUIRED from below -- copied the eth file from
# the system
print FILE "DEVICE=\'$nic\'\n";
print FILE "BOOTPROTO=\'static\'\n";
print FILE "BROADCAST=\'\'\n";
print FILE "ETHTOOL_OPTIONS=\'\'\n";
print FILE "IPADDR=\'".$nic_ips[$i]."\'\n";
print FILE "MTU=\'\'\n";
print FILE "NAME=\'\'\n";
print FILE "NETMASK=\'".$netmask."\'\n";
print FILE "NETWORK=\'".$subnet."\'\n";
print FILE "REMOTE_IPADDR=\'\'\n";
print FILE "STARTMODE=\'onboot\'\n";
print FILE "UNIQUE=\'\'\n";
print FILE "USERCONTROL=\'no\'\n";
print FILE "_nm_name=\'static-0\'\n";
2013-01-09 16:24:11 +00:00
2013-01-31 15:17:09 +00:00
} else {
# on suse/sles the ip alias info goes into the same file as the base ip info.
2013-02-01 04:25:18 +00:00
# open ifconfig-eth file and append additional info.
2013-01-31 15:17:09 +00:00
if (!open(FILE, ">>$dir/ifcfg-$nic")) { system("logger -t xcat -p local4.err 'configeth: cannot open $dir/ifcfg-$nic for appending ip alias info'"); exit 1; }
print FILE "IPADDR_$i=\'".$nic_ips[$i]."\'\n";
print FILE "NETMASK_$i=\'".$netmask."\'\n";
print FILE "NETWORK_$i=\'".$subnet."\'\n";
print FILE "LABEL_$i=\'".$i."\'\n";
}
2013-01-09 16:24:11 +00:00
close FILE;
runcmd("ifup $nic");
}
else {
2013-01-31 15:17:09 +00:00
# Write the info to the ifcfg file for redhat
2013-01-09 16:24:11 +00:00
my $dir = "/etc/sysconfig/network-scripts";
if (!open(FILE, ">$dir/ifcfg-$specific_nic")) { system("logger -t xcat -p local4.err 'configeth: cannot open $dir/ifcfg-$specific_nic.'"); exit 1; }
print FILE "DEVICE=$specific_nic\n";
print FILE "BOOTPROTO=none\n";
print FILE "IPADDR=$nic_ips[$i]\n";
print FILE "NETMASK=$netmask\n";
#if (defined($gateway)) { print FILE "GATEWAY=$gateway\n"; }
print FILE "ONBOOT=yes\n";
close FILE;
runcmd("$dir/ifup $specific_nic");
}
# system("logger -t xcat -p local4.info 'configeth: successfully configured $specific_nic.'");
2009-02-12 14:14:05 +00:00
}
2008-04-24 13:04:01 +00:00
exit 0;
sub runcmd {
my $cmd = shift @_;
$cmd .= ' 2>&1';
my @output = `$cmd`;
my $rc = $? >> 8;
if ($rc) {
2012-05-15 03:48:00 +00:00
system("logger -t xcat -p local4.err 'configeth: command $cmd failed with rc $rc: " . join('',@output) . "'");
2011-10-31 19:53:30 +00:00
my $errout= "configeth: command $cmd failed with rc $rc.";
2013-01-31 15:17:09 +00:00
echo $errout;
2008-04-24 13:04:01 +00:00
exit $rc;
}
}