updated configeth for nic ip alias
git-svn-id: https://svn.code.sf.net/p/xcat/code/xcat-core/trunk@14827 8638fb3e-16cb-4fca-ae20-7b5d299a9bcd
This commit is contained in:
		| @@ -1,20 +1,19 @@ | ||||
| #!/usr/bin/perl | ||||
| # IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html | ||||
|  | ||||
| # Sample xCAT post script for configuring eth1 based on eth0 settings and | ||||
| # some conventions.  | ||||
| # This scripts works for both diskfull installs, diskless boots on AIX or Linux. | ||||
| # the nic is passed as the only argument. All other required attributes are | ||||
| # passed in as environment variables. These are set in mypostscript.tmpl as follows: | ||||
| # 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: | ||||
| #  | ||||
| # | ||||
| # NICNODE - the name of the node minus the NICHOSTNAMESUFFIXES | ||||
| # NICIPS  - the ip address for this nic | ||||
| # NICHOSTNAMESUFFIXES - the suffix to combine with the NICNODE for the hostname | ||||
| # 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. | ||||
| #  | ||||
|  | ||||
| use strict; | ||||
| use Socket; | ||||
| @@ -22,129 +21,142 @@ use Socket; | ||||
| my $nic = shift(@ARGV); | ||||
| my $nicips = $ENV{NICIPS}; | ||||
| my $nicnetworks = $ENV{NICNETWORKS}; | ||||
| my $nichostnamesuffixes = $ENV{NICHOSTNAMESUFFIXES}; | ||||
| my $nicnode = $ENV{NICNODE}; | ||||
| my $net_cnt = $ENV{NETWORKS_LINES}; | ||||
|  | ||||
| my $suffix = ''; | ||||
| my $netmask; | ||||
| my $netmask =''; | ||||
| my $ipaddr = ''; | ||||
| my $nic_num = ''; | ||||
| my $subnet; | ||||
| my $nic_net; | ||||
| 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 } | ||||
|  | ||||
| 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'"); | ||||
| system("logger -t xcat -p local4.err  'configeth: NICHOSTNAMESUFFIXES:  $nichostnamesuffixes'"); | ||||
|  | ||||
| # get network or networks for this nic from NICNETWORKS: eth0:1_0_0_0-255_255_0_0,eth1:1_1_0_0 | ||||
| foreach my $networks (split(/,/,$nicnetworks)) { | ||||
|     my @net = split(/:/,$networks); | ||||
| # create array of network info. Needed in case where there are  | ||||
| # more than one ip address per nic and shouldn't be many networks. | ||||
| my $net_info; | ||||
| my $cnt = 1; | ||||
|  | ||||
| while ( $cnt <= $net_cnt ) { | ||||
|     $net_info = $ENV{"NETWORKS_LINE$cnt"}; | ||||
|     $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;  | ||||
| } | ||||
|  | ||||
| # 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)) { | ||||
|     my @net = split(/:/,$nic_networks); | ||||
|     if ($net[0] eq $nic) { | ||||
|         $nic_net = $net[1]; | ||||
|         @nic_nets = split(/\|/,$net[1]); | ||||
|         last; | ||||
|     } | ||||
| } | ||||
|  | ||||
|  | ||||
| # Find the matching network for the nic | ||||
| my $cnt = 1; | ||||
| my $net_info; | ||||
|  | ||||
| while ( $cnt <= $net_cnt ) { | ||||
|     $net_info = $ENV{"NETWORKS_LINE$cnt"}; | ||||
|     if ( $net_info =~ /^netname=$nic_net\|\|/ ) { | ||||
|         $net_info =~ /net=([^\|]*)\|\|/; | ||||
|         $subnet = $1; | ||||
|         $net_info =~ /mask=([^\|]*)\|\|/; | ||||
|         $netmask = $1; | ||||
| 	$cnt = $net_cnt + 1; | ||||
|     }  | ||||
|     else { | ||||
|         $cnt +=1 ;  | ||||
|     } | ||||
| } | ||||
|  | ||||
| # check that there is a subnet and netmask set | ||||
| if ( !(length($subnet) > 0) || !(length($subnet) > 0) ) { | ||||
|     system("logger -t xcat -p local4.err  'configeth: network subnet or netmask not set.'"); | ||||
|     exit 1; | ||||
| } | ||||
|  | ||||
| # get ipaddress from $nicips: eth0:1.0.0.1,eth1:1.1.1.1 | ||||
| # 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. | ||||
| foreach my $ips (split(/,/,$nicips)) { | ||||
|     my @ip = split(/:/,$ips); | ||||
|     if ($ip[0] eq $nic ) { | ||||
|         $ipaddr = $ip[1]; | ||||
|         @nic_ips = split(/\|/,$ip[1]); | ||||
|     } | ||||
| } | ||||
|  | ||||
| # get nic suffix to create hostname: eth0:-eth0,eth1:-eth1 | ||||
| foreach my $suffixes (split(/,/,$nichostnamesuffixes)) { | ||||
|     my @nic_suffix = split(/:/,$suffixes); | ||||
|     if ($nic_suffix[0] eq $nic ) { | ||||
|         $suffix = $nic_suffix[1]; | ||||
| my $i; | ||||
| for ($i=0; $i < (scalar @nic_ips) ; $i++ ) { | ||||
|  | ||||
|     # 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); | ||||
|     } | ||||
| } | ||||
|  | ||||
| #  Do we really need to set gateway or is there a route that needs to be set | ||||
| my $gateway; | ||||
| #$gateway = '1.2.3.254'; | ||||
|  | ||||
|  | ||||
| my $hostname = "$nicnode$suffix"; | ||||
| #my $ip = &getipaddr($hostname); | ||||
| #if (!$ip) { system("logger -t xcat -p local4.err  'configeth: cannot resolve $hostname.'"); exit 1; } | ||||
|  | ||||
| if ($^O =~ /^aix/i) {  | ||||
|         if ($ipaddr =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/) { | ||||
|             runcmd("chdev -l '$nic' -a netaddr=$ipaddr -a netmask=$netmask -a state='up'"); | ||||
| #        } else { #ipv6 | ||||
| #            runcmd("autoconf6 -6i en$nic_num"); | ||||
|     $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++;  | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     # 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; | ||||
|     } | ||||
|  | ||||
|     if ($^O =~ /^aix/i) { | ||||
|             if ($ipaddr =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/) { | ||||
|                 runcmd("chdev -l '$specific_nic' -a netaddr=$nic_ips[$i] -a netmask=$netmask -a state='up'"); | ||||
|     #        } else { #ipv6 | ||||
|     #            runcmd("autoconf6 -6i en$nic_num"); | ||||
|             } | ||||
|     } | ||||
|     elsif (($ENV{OSVER} && ($ENV{OSVER} =~ /sles|suse/i)) || (-f "/etc/SuSE-release")) {  | ||||
|         # Write the info to the ifcfg file | ||||
|         my $dir = "/etc/sysconfig/network"; | ||||
|         if (!open(FILE, ">$dir/ifcfg-$specific_nic")) { system("logger -t xcat -p local4.err 'configeth: cannot open $dir/ifcfg-$specific_nic.'"); exit 1; } | ||||
|         # Not sure what is really REQUIRED from below -- copied the eth file from | ||||
|         # the system | ||||
|         print FILE "DEVICE=\'$specific_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"; | ||||
|          | ||||
|         close FILE; | ||||
|         runcmd("ifup $nic"); | ||||
|     } | ||||
|     else { | ||||
|         # Write the info to the ifcfg file | ||||
|         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.'"); | ||||
| } | ||||
| elsif (($ENV{OSVER} && ($ENV{OSVER} =~ /sles|suse/i)) || (-f "/etc/SuSE-release")) {  | ||||
| 	# Write the info to the ifcfg file | ||||
| 	my $dir = "/etc/sysconfig/network"; | ||||
| 	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 "BOOTPROTO=\'static\'\n"; | ||||
| 	print FILE "BROADCAST=\'\'\n"; | ||||
| 	print FILE "ETHTOOL_OPTIONS=\'\'\n"; | ||||
| 	print FILE "IPADDR=\'".$ipaddr."\'\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"; | ||||
|  | ||||
| 	close FILE; | ||||
| 	runcmd("ifup $nic"); | ||||
|  | ||||
| } | ||||
| else { | ||||
| 	# Write the info to the ifcfg file | ||||
| 	my $dir = "/etc/sysconfig/network-scripts"; | ||||
| 	if (!open(FILE, ">$dir/ifcfg-$nic")) { system("logger -t xcat -p local4.err 'configeth: cannot open $dir/ifcfg-$nic.'"); exit 1; } | ||||
| 	print FILE "DEVICE=$nic\n"; | ||||
| 	print FILE "BOOTPROTO=none\n"; | ||||
| 	print FILE "IPADDR=$ipaddr\n"; | ||||
| 	print FILE "NETMASK=$netmask\n"; | ||||
| 	if (defined($gateway)) { print FILE "GATEWAY=$gateway\n"; } | ||||
| 	print FILE "ONBOOT=yes\n"; | ||||
| 	close FILE; | ||||
|  | ||||
| 	runcmd("$dir/ifup $nic"); | ||||
|  | ||||
| } | ||||
| system("logger -t xcat -p local4.info 'configeth: successfully configured $nic.'"); | ||||
|  | ||||
| exit 0; | ||||
|  | ||||
| sub runcmd { | ||||
| @@ -160,51 +172,6 @@ sub runcmd { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| #$master=$ENV{MASTER}; | ||||
| #if ($^O =~ /^aix/i) { } | ||||
| #elsif (($ENV{OSVER} && ($ENV{OSVER} =~ /fedora/i)) || (-f "/etc/fedora-release")) { } | ||||
| #elsif (($ENV{OSVER} && ($ENV{OSVER} =~ /sles|suse/i)) || (-f "/etc/SuSE-release")) { } | ||||
| #$result=`grep "^SYSLOG_DAEMON=" $sysconfig 2>&1`; | ||||
| #`logger -t xcat "Install: syslog setup"`; | ||||
|  | ||||
|  | ||||
| # Take primary node name, add "-eth1" for linux and "-en1" for AIX  and  | ||||
| #	then reverse resolve to get what ip should be | ||||
| sub getipaddr() | ||||
| { | ||||
|     my ($iporhost) = @_; | ||||
|  | ||||
|     my $socket6support = eval { require Socket6 }; | ||||
|  | ||||
|     if (($iporhost =~ /\d+\.\d+\.\d+\.\d+/) || ($iporhost =~ /:/)) | ||||
|     { | ||||
|         #pass in an ip and only want an ip?? | ||||
|         return $iporhost; | ||||
|     } | ||||
|  | ||||
|     if ($socket6support) # the getaddrinfo and getnameinfo supports both IPv4 and IPv6 | ||||
|     { | ||||
|         my ($family, $socket, $protocol, $ip, $name) = Socket6::getaddrinfo($iporhost,0); | ||||
|         if ($ip) | ||||
|         { | ||||
|             return (Socket6::getnameinfo($ip, Socket6::NI_NUMERICHOST()))[0]; | ||||
|         } | ||||
|        return undef; | ||||
|     } | ||||
|     else | ||||
|     { | ||||
|          return inet_ntoa(inet_aton($iporhost)) | ||||
|     } | ||||
| } | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user