From fd48d12d604ecd6caef42251d422413dca26d529 Mon Sep 17 00:00:00 2001 From: xq2005 Date: Fri, 2 Aug 2013 03:03:26 -0700 Subject: [PATCH] transfer the confignics and configeth to shell --- xCAT/postscripts/configeth | 993 +++++++++++++++++++++++------------- xCAT/postscripts/confignics | 380 +++++++------- xCAT/postscripts/xcatlib.sh | 17 + 3 files changed, 859 insertions(+), 531 deletions(-) create mode 100644 xCAT/postscripts/xcatlib.sh diff --git a/xCAT/postscripts/configeth b/xCAT/postscripts/configeth index 158c36a3a..22d633c5d 100755 --- a/xCAT/postscripts/configeth +++ b/xCAT/postscripts/configeth @@ -1,377 +1,666 @@ -#!/usr/bin/perl -# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html +#!/bin/bash +str_dir_name=`dirname $0` -# 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 -# 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. +. $str_dir_name/xcatlib.sh -use strict; -use Socket; - -my $nic = shift(@ARGV); -my $nicips = $ENV{NICIPS}; -my $nicnetworks = $ENV{NICNETWORKS}; -my $nicnode = $ENV{NICNODE}; -my $net_cnt = $ENV{NETWORKS_LINES}; - -my $netmask =''; -my $ipaddr = ''; -my $nic_num = ''; -my $subnet = ''; -my $gateway = ''; # this is only used for ipv6, ipv4 gateway is assigned by dhcp -my $ipv4nic = 0; -my $nic_net = ''; -my $net_name = ''; -my @nic_nets_all = (); # array of all networks for this nic -my @nic_nets4 = (); # array of ipv4 networks for this nic -my @nic_nets6 = (); # array of ipv6 networks for this nic -my @nic_ips_all =(); # array of all ip addresses for this nic -my @nic_ips4 =(); # array of ipv4 addresses for this nic -my @nic_ips6 =(); # array of ipv6 addresses 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'"); - -# 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; - $net_info =~ /gateway=([^\|]*)\|\|/; - $gateway = $1; - push @{ $networks[$cnt-1] }, ($net_name, $subnet, $netmask, $gateway); - $cnt +=1; +#tranfer the netmask to prefix for ipv4 +function v4mask2prefix(){ + local num_bits=0 + local old_ifs=$IFS + IFS=$'.' + local array_num_temp=($1) + IFS=$old_ifs + for num_dec in ${array_num_temp[@]} + do + case $num_dec in + 255) let num_bits+=8;; + 254) let num_bits+=7;; + 253) let num_bits+=6;; + 248) let num_bits+=5;; + 240) let num_bits+=4;; + 224) let num_bits+=3;; + 192) let num_bits+=2;; + 128) let num_bits+=1;; + 0) ;; + *) echo "Error: $dec is not recognised"; exit 1 + esac + done + echo "$num_bits" } -# 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 = (); - if ( $nic_networks =~ /!/ ) { - @net = split(/!/,$nic_networks); - } else { - @net = split(/:/,$nic_networks); - } - if ($net[0] eq $nic) { - @nic_nets_all = split(/\|/,$net[1]); - last; - } +function configipv4(){ + str_if_name=$1 + str_v4ip=$2 + str_v4net=$3 + str_v4mask=$4 + num_v4num=$5 + + if [ "$str_os_type" = "sles" ];then + str_conf_file="/etc/sysconfig/network/ifcfg-${str_if_name}" + if [ $num_v4num -eq 0 ];then + echo "DEVICE=${str_if_name}" > $str_conf_file + echo "BOOTPROTO=static" >> $str_conf_file + echo "BROADCAST=" >> $str_conf_file + echo "ETHTOOL_OPTIONS=" >> $str_conf_file + echo "IPADDR=${str_v4ip}" >> $str_conf_file + echo "MTU=" >> $str_conf_file + echo "NAME=" >> $str_conf_file + echo "NETMASK=${str_v4mask}" >> $str_conf_file + echo "NETWORK=${str_v4net}" >> $str_conf_file + echo "REMOTE_IPADDR=" >> $str_conf_file + echo "STARTMODE=onboot" >> $str_conf_file + echo "UNIQUE=" >> $str_conf_file + echo "USERCONTROL=no" >> $str_conf_file + echo "_nm_name=static-0" >> $str_conf_file + else + echo "IPADDR_${num_v4num}=${str_v4ip}" >> $str_conf_file + echo "NETMASK_${num_v4num}=${str_v4mask}" >> $str_conf_file + echo "NETWORK_${num_v4num}=${str_v4net}" >> $str_conf_file + echo "LABEL_${num_v4num}=${num_v4num}" >> $str_conf_file + fi + #debian ubuntu + elif [ "$str_os_type" = "debian" ];then + str_conf_file="/etc/network/interfaces.d/${str_if_name}" + if [ $num_v4num -eq 0 ];then + echo "auto ${str_if_name}" > $str_conf_file + echo "iface ${str_if_name} inet static" >> $str_conf_file + else + echo "auto ${str_if_name}:${num_v4num}" >> $str_conf_file + echo "iface ${str_if_name}:${num_v4num} inet static" >> $str_conf_file + fi + echo " address ${str_v4ip}" >> $str_conf_file + echo " netmask ${str_v4mask}" >> $str_conf_file + echo " network ${str_v4net}" >> $str_conf_file + else + # Write the info to the ifcfg file for redhat + str_conf_file="" + if [ $num_v4num -eq 0 ];then + str_conf_file="/etc/sysconfig/network-scripts/ifcfg-${str_if_name}" + echo "DEVICE=${str_if_name}" > $str_conf_file + else + str_conf_file="/etc/sysconfig/network-scripts/ifcfg-${str_if_name}:${num_v4num}" + echo "DEVICE=${str_if_name}:${num_v4num}" > $str_conf_file + fi + + echo "BOOTPROTO=none" >> $str_conf_file + echo "NM_CONTROLLED=no" >> $str_conf_file + echo "IPADDR=${str_v4ip}" >> $str_conf_file + echo "NETMASK=${str_v4mask}" >> $str_conf_file + echo "ONBOOT=yes" >> $str_conf_file + fi } -# Put all ipv4 nets into nic_nets4, -# put all ipv6 nets into nic_nets6. -my $i = 0; -for ($i=0; $i < (scalar @nic_nets_all) ; $i++ ) { - # The network name itself does not indicate ipv4 or ipv6 - # should use the subnet to determine. - # Do not use foreach (@networks), needs to keep the order of nets and ips - my $net = $nic_nets_all[$i]; - foreach my $netinfo (@networks) - { - if ($netinfo->[0] eq $net) - { - if ($netinfo->[1] =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/) - { - push @nic_nets4, $net; - } elsif ($netinfo->[1] =~ /:/) { - push @nic_nets6, $net; - } else { - system("logger -t xcat -p local4.err 'The subnet $net is not valid.'"); - } - last; - } - } -} -# 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 = (); - if ( $ips =~ /!/ ) { - @ip = split(/!/,$ips); - } else { - @ip = split(/:/,$ips); - } - if ($ip[0] eq $nic ) { - @nic_ips_all = split(/\|/,$ip[1]); - } +configipv6(){ + str_if_name=$1 + str_v6ip=$2 + str_v6net=$3 + str_v6prefix=$4 + num_v6num=$5 + num_v4num=$6 + str_v6gateway=$7 + + #remove the prefix length from the subnet + str_v6net=`echo $str_v6net | cut -d"/" -f 1` + + #remove the "/" from mask + str_v6prefix=`echo $str_v6prefix | sed 's/\///'` + + if [ "$str_os_type" = "sles" ];then + str_conf_file="/etc/sysconfig/network/ifcfg-${str_if_name}" + if [ $num_v4num -eq 0 -a $num_v6num -eq 0 ];then + echo "DEVICE=$str_if_name" > $str_conf_file + echo "BOOTPROTO=static" >> $str_conf_file + echo "NM_CONTROLLED=no" >> $str_conf_file + echo "STARTMODE=onboot" >> $str_conf_file + fi + echo "LABEL_ipv6${num_v6num}=ipv6$num_v6num" >> $str_conf_file + echo "IPADDR_ipv6${num_v6num}=${str_v6ip}" >> $str_conf_file + echo "PREFIXLEN_ipv6${num_v6num}=${str_v6prefix}" >> $str_conf_file + if [ `echo $str_v6gateway | grep -v 'xcatmaster'` ];then + grep -E "default[:space:]+${str_v6gateway}[:space:]+" /etc/sysconfig/network/routes 2>&1 1>/dev/null + if [ $? -ne 0 ];then + echo "default $str_v6gateway - -" >> /etc/sysconfig/network/routes + fi + fi + elif [ "$str_os_type" = "debian" ];then + #debian or ubuntu + str_conf_file="/etc/network/interfaces.d/${str_if_name}" + if [ $num_v4num -eq 0 -a $num_v6num -eq 0 ];then + echo "auto ${str_if_name}" > $str_conf_file + fi + if [ $num_v6num -eq 0 ];then + echo "pre-up modprobe ipv6" >> $str_conf_file + echo "iface ${str_if_name} inet6 static" >> $str_conf_file + echo " address ${str_v6ip}" >> $str_conf_file + echo " netmask ${str_v6prefix}" >> $str_conf_file + if [ $str_v6gateway ];then + echo " gateway ${str_v6gateway}" >> $str_conf_file + fi + else + echo " post-up /sbin/ifconfig ${str_if_name} inet6 add ${str_v6ip}/${str_v6prefix}" >> $str_conf_file + echo " pre-down /sbin/ifconfig ${str_if_name} inet6 del ${str_v6ip}/${str_v6prefix}" >> $str_conf_file + fi + else + #redhat + str_conf_file="/etc/sysconfig/network-scripts/ifcfg-${str_if_name}" + if [ $num_v4num -eq 0 -a $num_v6num -eq 0 ];then + echo "DEVICE=$str_if_name" > $str_conf_file + echo "BOOTPROTO=none" >> $str_conf_file + echo "NM_CONTROLLED=no" >> $str_conf_file + echo "ONBOOT=yes" >> $str_conf_file + fi + if [ $num_v6num -eq 0 ];then + echo "IPV6INIT=yes" >> $str_conf_file + echo "IPV6ADDR=${str_v6ip}/${str_v6prefix}" >> $str_conf_file + else + echo "IPV6ADDR_SECONDARIES=${str_v6ip}/${str_v6prefix}" >> $str_conf_file + fi + if [ `echo $str_v6gateway | grep -v 'xcatmaster'` ];then + echo "IPV6_DEFAULTGW=$str_v6gateway" >> $str_conf_file + fi + fi } -# Put all ipv4 addresses in @nic_ips4, -# put all ipv6 addresses in @nic_ips6. -# Do not use forach, needs to keep the order of networks and ips -for ($i=0; $i < (scalar @nic_ips_all) ; $i++ ) { - my $ip = $nic_ips_all[$i]; - # ipv4 address - if ($ip =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/) { - push @nic_ips4, $ip; - } elsif ($ip =~ /:/) { # ipv6 - push @nic_ips6, $ip; - } else { - system("logger -t xcat -p local4.err 'configeth: The ip address $ip is not valid.'"); - } +#delete all configuration file(s) on linux +function delete_nic_config_files(){ + str_temp_name=$1 + #delete the configuration files + #delete the configuration history + if [ "$str_os_type" = "debian" ];then + rm -f /etc/network/interfaces.d/$str_temp_name 2>/dev/null + sed -i "/${str_temp_name}/d" /etc/network/xcat_history_important + elif [ "$str_os_type" = "sles" ];then + rm -f /etc/sysconfig/network/ifcfg-${str_temp_name} 2>/dev/null + sed -i "/${str_temp_name}/d" /etc/sysconfig/network/xcat_history_important + else + rm -f /etc/sysconfig/network-scripts/ifcfg-${str_temp_name} 2>/dev/null + rm -f /etc/sysconfig/network-scripts/ifcfg-${str_temp_name}:* 2>/dev/null + sed -i "/${str_temp_name}/d" /etc/sysconfig/network-scripts/xcat_history_important + fi } -for ($i=0; $i < (scalar @nic_ips4) ; $i++ ) { +function add_ip_temporary(){ + local str_ip_prefix=$1 + local str_temp_name=$2 + local str_ip=`echo $str_ip_prefix | awk -F'_' '{print $1}'` + local str_mask=`echo $str_ip_prefix | awk -F'_' '{print $2}'` - # ipv6 configuration needs to know if this nic as ipv4 configured - $ipv4nic = 1; + if [ "$str_os_type" = "aix" ];then + echo $str_ip | grep ":" > /dev/null + #ipv6 + if [ $? -eq 0 ];then + lsattr -El $str_temp_name | grep netaddr6 | awk '{print $2}' | grep ":" + if [ $? -ne 0 ];then + chdev -l $str_temp_name -a netaddr6=$str_ip -a prefixlen=$str_mask + else + chdev -l $str_temp_name -a alias6=${str_ip}/${str_mask} + fi + #ipv4 + else + lsattr -El $str_temp_name | grep netaddr | awk '{print $2}' | grep '\.' + if [ $? -ne 0 ];then + chdev -l $str_temp_name -a netaddr=${str_ip} -a netmask=${str_mask} + else + chdev -l $str_temp_name -a alias4=${str_ip},${str_mask} + fi + fi + else + echo $str_ip | grep ":" > /dev/null + #ipv6 + if [ $? = 0 ];then + lsmod |grep -w 'ipv6' + if [ $? -ne 0 ];then + modprobe ipv6 + fi + ip addr add ${str_ip}/${str_mask} dev $str_temp_name + #ipv4 + else + str_label='' + ip addr show dev $str_temp_name | grep inet | grep "global" | grep -v ':' | grep "${str_temp_name}" + if [ $? -eq 0 ];then + for num_i in {1..1000} + do + ip addr show dev $str_temp_name | grep inet | grep "global" | grep ":${num_i}" + if [ $? -ne 0 ];then + str_label=${str_nic_name}:${num_i} + break + fi + done + else + str_label=$str_nic_name + fi - # 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); - } + #the label is ready, add the ip address directly + ip addr add $str_ip/${str_mask} dev $str_nic_name scope global label $str_label + fi + fi +} - $cnt = 0; - $subnet = ""; - $netmask = ""; - $net_name = ""; - while ( $cnt < $net_cnt ) { - if ( $networks[$cnt][0] eq $nic_nets4[$i] ) { - - $subnet = $networks[$cnt][1]; - $netmask = $networks[$cnt][2]; - $cnt = $net_cnt; # found match - get out. - } - else { - $cnt++; - } - } +str_nic_name='' +str_os_type=`uname | tr 'A-Z' 'a-z'` +str_cfg_dir='' +str_temp='' +if [ "$str_os_type" = "linux" ];then + str_temp=`echo $OSVER | grep -E '(sles|suse)'` + if [ -f "/etc/debian_version" ];then + debianpreconf + str_os_type="debian" + str_cfg_dir="/etc/network/" + elif [ -f "/etc/SuSE-release" -o -n "$str_temp" ];then + str_os_type="sles" + str_cfg_dir="/etc/sysconfig/network/" + else + str_os_type="redhat" + str_cfg_dir="/etc/sysconfig/network-scripts/" + fi +fi - # 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; - } +logger -t xcat -p local4.err "configeth: os type: $str_os_type" +echo "configeth on $NODE: os type: $str_os_type" +if [ "$1" = "-r" ];then + if [ $# -ne 2 ];then + logger -t xcat -p local4.err "configeth: remove nic, but the nic name is missed" + echo "configeth on $NODE: remove nic, but the nic name is missed" + exit 1 + fi + str_nic_name=$2 + logger -t xcat -p local4.err "configeth: remove nic $str_nic_name" + echo "configeth on $NODE: remove nic $str_nic_name" - if ($^O =~ /^aix/i) { - if ($i == 0) { - if ($nic_ips4[$i] =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/) { - runcmd("chdev -l '$nic' -a netaddr=$nic_ips4[$i] -a netmask=$netmask -a state='up'"); - # } else { #ipv6 - # runcmd("autoconf6 -6i en$nic_num"); - } - } else { - if ($nic_ips4[$i] =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/) { - runcmd("chdev -l '$nic' -a alias4=$nic_ips4[$i],$netmask"); - # } 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 [ "$str_os_type" = "aix" ];then + old_ifs=$IFS + IFS=$'\n' + str_temp=`lsattr -El $str_nic_name | grep alias4 | awk '{print $2}'` + array_alias4_temp=($str_temp) + IFS=$old_ifs + for str_ip_alias4 in $str_temp + do + #the alias format should be ipaddr,netmask + echo $str_ip_alias4 | grep -E , + if [ $? -eq 0 ];then + chdev -l $str_nic_name -a delalias4=$str_ip_alias4 + fi + done + str_temp=`lsattr -El $str_nic_name | grep alias6 | awk '{print $2}'` + old_ifs=$IFS + IFS=$'\n' + array_alias6_temp=($str_temp) + IFS=$old_ifs + for str_ip_alias6 in ${array_alias6_temp[@]} + do + echo $str_ip_alias6 | grep -E / + if [ $? -eq 0 ];then + chdev -l $str_nic_name -a delalias6=$str_ip_alias6 + fi + done + logger -t xcat -p local4.err "configeth run command: chdev -l $str_nic_name -a netaddr='' -a netmask='' -a netaddr6='' -a prefixlen='' -a state=down" + echo "configeth on $NODE run command: chdev -l $str_nic_name -a netaddr='' -a netmask='' -a netaddr6='' -a prefixlen='' -a state=down" + chdev -l $str_nic_name -a netaddr='' -a netmask='' -a netaddr6='' -a prefixlen='' -a state=down + else + #shut down the nic if it is on + ip link show $str_nic_name | grep -i ',up' + if [ $? -eq 0 ];then + if [ "$str_os_type" = "debian" ];then + ifdown --force $str_nic_name + else + ifdown $str_nic_name + fi + fi - 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 "NM_CONTROLLED=\'no\'\n"; - print FILE "BROADCAST=\'\'\n"; - print FILE "ETHTOOL_OPTIONS=\'\'\n"; - print FILE "IPADDR=\'".$nic_ips4[$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"; - - } else { - # on suse/sles the ip alias info goes into the same file as the base ip info. - # open ifconfig-eth file and append additional info. - 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; } + #delete the configuration files + delete_nic_config_files $str_nic_name + fi + exit 0 +fi + +#main prcess +#1. get all ip,netmask,subnet,gateway for the nic +#2. get current configurations +#3. delete the undefined ips +#4. add the new defined ips +#5. no modification, return directly +#3. on linux modify the configuration files +if [ $# -ne 3 ];then + logger -t xcat -p local4.err "configeth: paramters error currently is $@" + echo "configeth on $NODE: paramters error currently is $@" + exit 1 +fi +str_nic_name=$1 +old_ifs=$IFS +IFS=$'|' +array_nic_ips=($2) +array_nic_networks=($3) +IFS=$old_ifs + +if [ "$str_os_type" = "aix" ];then + str_temp=`lsattr -El $str_nic_name` +else + str_temp=`ip addr show dev $str_nic_name` +fi + +logger -t xcat -p local4.err "configeth: old configuration: $str_temp" +echo "configeth on $NODE: old configuration: $str_temp" + +#parse the networks tables contains +declare -a array_ip_mask +declare -a array_ip_status +declare -a array_nic_network_config +declare -a array_nic_subnet +declare -a array_nic_netmask +declare -a array_nic_gateway + +str_ip_mask_pair='' +num_index=1 +while [ $num_index -le $NETWORKS_LINES ];do + eval str_temp=\$NETWORKS_LINE$num_index + str_temp_name=`echo $str_temp | awk -F'netname=' '{print $2}' | awk -F'|' '{print $1}'` + num_i=0 + while [ $num_i -lt ${#array_nic_ips[*]} ] + do + if [ "$str_temp_name" = "${array_nic_networks[$num_i]}" ];then + array_nic_network_config[$num_i]=$str_temp + break + fi + num_i=$((num_i+1)) + done + num_index=$((num_index+1)) +done + +logger -t xcat -p local4.err "configeth: new configuration" +echo "configeth on $NODE: new configuration" +num_index=0 +str_ipv6_gateway='' +while [ $num_index -lt ${#array_nic_ips[*]} ];do + #get the ip address and network name + str_ip=${array_nic_ips[$num_index]} + str_netname=${array_nic_networks[$num_index]} + if [ ! $str_netname ];then + logger -t xcat -p local4.err "configeth: Network name is not defined on $str_nic_name for $str_ip." + echo "configeth on $NODE: Network name is not defined on $str_nic_name for $str_ip." + num_index=$((num_index+1)) + continue + fi + + #find out the network definition + str_line=${array_nic_network_config[$num_index]} + if [ ! $str_line ];then + logger -t xcat -p local4.err "configeth: Network object $str_netname is not defined." + echo "configeth on $NODE: Network object $str_netname is not defined." + num_index=$((num_index+1)) + continue + fi + + #fetch the subnet and netmask in networks definition + str_subnet=`echo $str_line | awk -F'net=' '{print $2}' | awk -F'|' '{print $1}'` + str_netmask=`echo $str_line | awk -F'mask=' '{print $2}' | awk -F'|' '{print $1}'` + str_gateway=`echo $str_line | awk -F'gateway=' '{print $2}' | awk -F'|' '{print $1}'` + + if [ ! $str_subnet -o ! $str_netmask ];then + logger -t xcat -p local4.err "configeth: subnet or netmask is not defined in network object $str_netname." + echo "configeth on $NODE: subnet or netmask is not defined in network object $str_netname." + num_index=$((num_index+1)) + continue + fi + + array_nic_subnet[$num_index]=$str_subnet + array_nic_netmask[$num_index]=$str_netmask + array_nic_gateway[$num_index]=$str_gateway + if [ -n "$str_gateway" ];then + str_ipv6_gateway=$str_gateway + fi + logger -t xcat -p local4.err "configeth: $str_ip, $str_subnet, $str_netmask, $str_gateway" + echo " $str_ip, $str_subnet, $str_netmask, $str_gateway" + #on linux, call sub rutine to define ipv4 or ipv6 address for the persitent configuration + if [ `echo $str_ip | grep -E '^([0-9]{1,3}\.){3}[0-9]{1,3}$'` ];then + if [ "$str_os_type" = "aix" ];then + hashset hash_new_config "${str_ip}_${str_netmask}" "new" + str_ip_mask_pair=$str_ip_mask_pair"${str_ip}_${str_netmask} " + else + str_prefix=$(v4mask2prefix $str_netmask) + hashset hash_new_config "${str_ip}_${str_prefix}" "new" + str_ip_mask_pair=$str_ip_mask_pair"${str_ip}_${str_prefix} " + fi + elif [ `echo $str_ip | grep -E ":"` ];then + num_ipv6_index=$((num_ipv6_index+1)) + hashset hash_new_config "${str_ip}_${str_netmask}" "new" + str_ip_mask_pair=$str_ip_mask_pair"${str_ip}_${str_netmask} " + else + logger -t xcat -p local4.err "configeth: the ipaddress( $str_ip ) for $str_nic_name is invalid." + echo "configeth on $NODE: the ipaddress( $str_ip ) for $str_nic_name is invalid." + fi + num_index=$((num_index+1)) +done + +str_ip_mask_pair=`echo "$str_ip_mask_pair" | sed -e 's/ $//'` + +str_old_conf='' +if [ "$str_os_type" = "aix" ];then + #check the netaddr + str_history=`lsattr -El $str_nic_name | grep netaddr | awk '{print $2}' | grep '\.'` + if [ $? -eq 0 ];then + str_temp=`lsattr -El $str_nic_name | grep netmask | awk '{print $2}'` + str_old_ip=${str_history}"_"${str_temp} + str_ip_status=$(hashget hash_new_config $str_old_ip) + if [ -n "$str_ip_status" ];then + hashset hash_new_config $str_old_ip "old" + else + chdev -l $str_nic_name -a netaddr='' -a netmask='' + logger -t xcat -p local4.err "configeth: delete undefined ip address $str_old_ip" + echo "configeth on $NODE: delete undefined ip address $str_old_ip" + fi + fi + + #check the netaddr6 + str_history=`lsattr -El $str_nic_name | grep netaddr6 | awk '{print $2}' | grep ':'` + if [ $? -eq 0 ];then + str_temp=`lsattr -El $str_nic_name | grep prefixlen | awk '{print $2}'` + str_old_ip=${str_history}"_"${str_temp} + str_ip_status=$(hashget hash_new_config $str_old_ip) + if [ -n "$str_ip_status" ];then + hashset hash_new_config $str_old_ip "old" + else + chdev -l $str_nic_name -a netaddr6='' -a prefixlen='' + logger -t xcat -p local4.err "configeth: delete undefined ipv6 address $str_old_ip" + echo "configeth on $NODE: delete undefined ipv6 address $str_old_ip" + fi + fi + + #check the ipv4 alias + str_history=`lsattr -El $str_nic_name | grep alias4 | awk '{print $2}' | grep '\.'` + if [ $? -eq 0 ];then + old_ifs=$IFS + IFS=$'\n' + array_alias4_temp=($str_history) + IFS=$old_ifs + for str_temp in ${array_alias4_temp[@]} + do + str_old_ip=`echo $str_temp | tr ',' '_'` + str_ip_staus=$(hashget hash_new_config $str_old_ip) + if [ -n "$str_ip_staus" ];then + hashset hash_new_config $str_old_ip "old" + else + chdev -l $str_nic_name -a delalias4=$str_temp - print FILE "IPADDR_$i=\'".$nic_ips4[$i]."\'\n"; - print FILE "NETMASK_$i=\'".$netmask."\'\n"; - print FILE "NETWORK_$i=\'".$subnet."\'\n"; - print FILE "LABEL_$i=\'".$i."\'\n"; - } - close FILE; - runcmd("ifup $nic"); - } - else { - # Write the info to the ifcfg file for redhat - 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 "NM_CONTROLLED=no\n"; - print FILE "IPADDR=$nic_ips4[$i]\n"; - print FILE "NETMASK=$netmask\n"; - #if (defined($gateway)) { print FILE "GATEWAY=$gateway\n"; } - print FILE "ONBOOT=yes\n"; - close FILE; + fi + done + fi - runcmd("$dir/ifup $specific_nic"); - } -# system("logger -t xcat -p local4.info 'configeth: successfully configured $specific_nic.'"); -} + #check the ipv6 alias + str_history=`lsattr -El $str_nic_name | grep alias6 | awk '{print $2}' | grep '\.'` + if [ $? -eq 0 ];then + old_ifs=$IFS + IFS=$'\n' + array_alias6_temp=($str_history) + IFS=$old_ifs + for str_temp in ${array_alias6_temp[@]} + do + str_old_ip=`$str_temp | tr '/' '_'` + str_ip_staus=$(hashget hash_new_config $str_old_ip) + if [ -n "$str_ip_staus" ];then + hashset hash_new_config $str_old_ip "old" + else + chdev -l $str_nic_name -a delalias6=$str_temp + fi + done + fi + #add the new configured ip address + old_ifs=$IFS + IFS=$' ' + array_ip_mask_temp=($str_ip_mask_pair) + IFS=$old_ifs + for str_new_ip in ${array_ip_mask_temp[@]} + do + str_ip_status=$(hashget hash_new_config $str_new_ip) + if [ "$str_ip_status" = "new" ];then + logger -t xcat -p local4.err "configeth: add $str_new_ip for $str_nic_name temporary." + echo "configeth on $NODE: add $str_new_ip for $str_nic_name temporary." + add_ip_temporary $str_new_ip $str_nic_name + fi + done + + #change the nic status to up + chdev -l $str_nic_name -a state=up +else + str_history='' + bool_restart_flag=0 + bool_modify_flag=0 + str_nic_status='down' + str_his_file=${str_cfg_dir}xcat_history_important -# ipv6 configuration -# ipv6 address does not use the nic alias like eth0:1, -# should use the main nic like eth0 -my $configured = 0; -for ($i=0; $i < (scalar @nic_ips6) ; $i++ ) -{ - # Get the network information: netname, subnet, netmask - my $found = 0; - my $subnet; - my $prefixlen; - my $ipv6gateway; - my $ip6addr = $nic_ips6[$i]; - my $net = $nic_nets6[$i]; - foreach my $netinfo (@networks) - { - if ($netinfo->[0] eq $net) - { - $found = 1; - $subnet = $netinfo->[1]; - $prefixlen = $netinfo->[2]; - $ipv6gateway = $netinfo->[3]; - } - # Remove the postfix like /64 from subnet - if ($subnet && ($subnet =~ /\//)) { - $subnet =~ s/\/.*$//; - } + str_history=`ip addr show dev $str_nic_name | grep inet | grep -iv dynamic | grep -iv link | awk '{print $2}'` + old_ifs=$IFS + IFS=$'\n' + array_ip_old_temp=($str_history) + IFS=$old_ifs + ip link show dev $str_nic_name | grep -i ,up + if [ $? -eq 0 ];then + str_nic_status='up' + if [ -f "${str_his_file}" ];then + cat ${str_his_file} | grep $str_nic_name + if [ $? -eq 0 ];then + str_nic_status='up' + for str_old_ip in ${array_ip_old_temp[@]} + do + str_old_ip=`echo $str_old_ip | tr '/' '_'` + str_ip_staus=$(hashget hash_new_config $str_old_ip) + if [ -n "$str_ip_staus" ];then + hashset hash_new_config $str_old_ip "old" + else + bool_modify_flag=1 + logger -t xcat -p local4.err "configeth: delete $str_old_ip for $str_nic_name temporary." + echo "configeth on $NODE: delete $str_old_ip for $str_nic_name temporary." + str_old_ip=`echo $str_old_ip | tr '_' '/'` + ip addr del $str_old_ip dev $str_nic_name + fi + done + else + bool_restart_flag=1 + bool_modify_flag=1 + fi + else + bool_restart_flag=1 + bool_modify_flag=1 + fi + else + bool_restart_flag=1 + bool_modify_flag=1 + fi - # Remove the "/" from prefixlen - if ($prefixlen && ($prefixlen =~ /^\//)) - { - $prefixlen =~ s/^\///; - } - } - if ($found == 0) - { - system("logger -t xcat -p local4.err 'configeth: Could not find network entry for ip address $ip6addr'"); - next; - } - - if ($^O =~ /^aix/i) { - if (!$configured) - { - runcmd("chdev -l en0 -a netaddr6=$ip6addr -a prefixlen=$prefixlen -a state=up"); - $configured = 1; - } else { - runcmd("chdev -l en0 -a alias6=$ip6addr/$prefixlen"); - } - } elsif (($ENV{OSVER} && ($ENV{OSVER} =~ /sles|suse/i)) || (-f "/etc/SuSE-release")) { - my $dir = "/etc/sysconfig/network"; - # If there are only ipv6 addresses on this nic, - # needs to flush the ifcfg-$nic file when configuring the first ipv6 addr, - # to avoid duplicate entries when run confignics/configeth multiple times. - if (!$ipv4nic && !$configured) - { - 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=static\n"; - print FILE "NM_CONTROLLED=no\n"; - print FILE "STARTMODE=onboot\n"; - } else { - if (!open(FILE, ">>$dir/ifcfg-$nic")) { - system("logger -t xcat -p local4.err 'configeth: cannot open $dir/ifcfg-$nic.'"); - exit 1; - } - } - # Use the label=ipv6$i in ifcfg-ethx file, like ipv60, ipv61 - print FILE "LABEL_ipv6$i=ipv6$i\n"; - print FILE "IPADDR_ipv6$i=$ip6addr\n"; - print FILE "PREFIXLEN_ipv6$i=$prefixlen\n"; - close FILE; - if ($ipv6gateway && $ipv6gateway !~ /xcatmaster/) { - # Do not add duplicate entries - `grep -E "default\\s+$ipv6gateway\\s+" /etc/sysconfig/network/routes 2>&1 1>/dev/null`; - if ($? != 0) { - `echo "default $ipv6gateway - -" >> /etc/sysconfig/network/routes`; - } - } - runcmd("ifup $nic"); - } else { - # Ubuntu TODO - my $dir = "/etc/sysconfig/network-scripts"; - # If there are only ipv6 addresses on this nic, - # needs to flush the ifcfg-$nic file when configuring the first ipv6 addr, - # to avoid duplicate entries when run confignics/configeth multiple times. - if (!$ipv4nic && !$configured) - { - 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 "NM_CONTROLLED=no\n"; - print FILE "ONBOOT=yes\n"; - } else { - if (!open(FILE, ">>$dir/ifcfg-$nic")) { - system("logger -t xcat -p local4.err 'configeth: cannot open $dir/ifcfg-$nic.'"); - exit 1; - } - } - if (!$configured) { - print FILE "IPV6INIT=yes\n"; - print FILE "IPV6ADDR=$ip6addr/$prefixlen\n"; - $configured = 1; - } else { - print FILE "IPV6ADDR_SECONDARIES=$ip6addr/$prefixlen\n"; - } - if ($ipv6gateway && $ipv6gateway !~ /xcatmaster/) { - print FILE "IPV6_DEFAULTGW=$ipv6gateway\n"; - } - close FILE; - runcmd("$dir/ifup-ipv6 $nic"); - } -} -exit 0; - -sub runcmd { - my $cmd = shift @_; - $cmd .= ' 2>&1'; - my @output = `$cmd`; - my $rc = $? >> 8; - if ($rc) { - system("logger -t xcat -p local4.err 'configeth: command $cmd failed with rc $rc: " . join('',@output) . "'"); - my $errout= "configeth: command $cmd failed with rc $rc."; - `echo $errout`; - exit $rc; - } -} + if [ $bool_restart_flag = 0 ];then + #add the new defined ip + old_ifs=$IFS + IFS=$' ' + array_ip_mask_temp=($str_ip_mask_pair) + IFS=$old_ifs + for str_new_ip in ${array_ip_mask_temp[@]} + do + str_ip_status=$(hashget hash_new_config $str_new_ip) + if [ "$str_ip_status" = "new" ];then + bool_modify_flag=1 + if [ $bool_restart_flag -eq 0 ];then + logger -t xcat -p local4.err "configeth: add $str_new_ip for $str_nic_name temporary." + echo "configeth on $NODE: add $str_new_ip for $str_nic_name temporary." + add_ip_temporary $str_new_ip $str_nic_name + fi + fi + done + fi + #configure the ipv6 default route + if [ $bool_restart_flag -eq 0 -a -n "$str_ipv6_gateway" ];then + ip -6 route | grep default | grep $str_ipv6_gateway + if [ $? -ne 0 ];then + logger -t xcat -p local4.err "configeth: the default ipv6 route changes to $str_ipv6_gateway." + echo "configeth on $NODE: the default ipv6 route changes to $str_ipv6_gateway." + ip -6 route del default + ip -6 route add default $str_ipv6_gateway dev $str_dev_name + fi + fi + #modify the configuration files + if [ $bool_modify_flag -eq 1 ];then + if [ $bool_restart_flag -eq 1 ];then + if [ "$str_nic_status" = "up" ];then + if [ "$str_os_type" = "debian" ];then + ifdown --force $str_nic_name > /dev/null + else + ifdown $str_nic_name > /dev/null + fi + fi + #delete all old ip address + for str_old_ip in ${array_ip_old_temp[@]} + do + ip addr del $str_old_ip dev $str_nic_name + done + fi + logger -t xcat -p local4.err "configeth: $str_nic_name changed, modify the configuration files" + echo "configeth on $NODE: $str_nic_name changed, modify the configuration files" + num_ipv4_index=0 + num_ipv6_index=0 + num_index=0 + cat $str_his_file | grep $str_nic_name + if [ $? -ne 0 ];then + echo "${str_nic_name}" >> $str_his_file + fi + #delete the old alias configuration files on redhat + if [ "$str_os_type" = "redhat" ];then + rm -f /etc/sysconfig/network-scripts/ifcfg-${str_nic_name}:* 2>/dev/null + fi + while [ $num_index -lt ${#array_nic_ips[*]} ];do + str_ip=${array_nic_ips[$num_index]} + str_subnet=${array_nic_subnet[$num_index]} + str_netmask=${array_nic_netmask[$num_index]} + str_gateway=${array_nic_gateway[$num_index]} + if [ ! $str_subnet -o ! $str_netmask ];then + num_index=$((num_index+1)) + continue + fi + if [ `echo $str_ip | grep -E '^([0-9]{1,3}\.){3}[0-9]{1,3}$'` ];then + configipv4 $str_nic_name $str_ip $str_subnet $str_netmask $num_ipv4_index + num_ipv4_index=$((num_ipv4_index+1)) + elif [ `echo $str_ip | grep -E ":"` ];then + configipv6 $str_nic_name $str_ip $str_subnet $str_netmask $num_ipv6_index $num_ipv4_index $str_gateway + num_ipv6_index=$((num_ipv6_index+1)) + else + num_index=$((num_index+1)) + continue + fi + num_index=$((num_index+1)) + done + else + logger -t xcat -p local4.err "configeth: $str_nic_name no changed, return directly." + echo "configeth on $NODE: $str_nic_name no changed, return directly." + fi + #restart the nic + if [ $bool_restart_flag -eq 1 ];then + if [ "$str_os_type" = "debian" ];then + ifup -a -i /etc/network/interfaces.d/$str_nic_name + else + ifup $str_nic_name + fi + fi +fi +exit 0 diff --git a/xCAT/postscripts/confignics b/xCAT/postscripts/confignics index 958173966..55aa2e749 100755 --- a/xCAT/postscripts/confignics +++ b/xCAT/postscripts/confignics @@ -1,187 +1,209 @@ -#!/usr/bin/perl -# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html +#!/bin/bash -# confignics postscript for configuring additional ethernet and ib NIC adapters. -# This module parses NIC environment variables containing data from the nics table -# for the specific node i.e.: -# NICNODE - the node name -# NICIPS - comma separated list of ips per NIC -# NICHOSTNAMESUFFIXES - comma spearated list of hostname suffixes per NIC -# NICTYPES - ethernet or infiniband -# NICCUSTOMSCRIPTS - script to configure nic, i.e. configeth or configib -# NICNETWORKS - network and subnetmask for the adapter. +str_dir_name=`dirname $0` +. $str_dir_name/xcatlib.sh -use strict; -use Socket; - -# Only two args are supported for confignics: -# "-s" to allow the install nic to be configured. If not set -# then the install nic will not be configured. -# "--ibaports=x" to specify the number of ports for an ib adapter. -# This value will set in an environment variable -# prior to calling configib. - -my $ibaports = 1; # default to one port per ib adapter. -my $cfg_inst_nic = ''; -my $arg =''; -while ($arg = shift(@ARGV)) { - if ( $arg eq "-s" ) { - $cfg_inst_nic = 1; - } elsif ( $arg =~ /--ibaports=(\d)$/) { - $ibaports = $1; - } -} - -my $ibnics = ''; -my $nicips = $ENV{NICIPS}; -my $niccustomscripts = $ENV{NICCUSTOMSCRIPTS}; -my $nictypes = $ENV{NICTYPES}; -my $installnic = $ENV{INSTALLNIC}; -my $xcatpostdir = "/xcatpost"; -my %cust_script_nics = (); # hash to save nics specified in niccustomscripts -my $type = ''; -my $nic = ''; -my $MAC = $ENV{MACADDRESS}; -my $inst_nic = ''; - -# determine which ethernet nic is the installnic. -if ($installnic =~ /(e(n|th)\d+)$/ ) { - $inst_nic = $1; -} elsif ($installnic eq "mac") { - # determine nic from mac. Get all NICs and their mac addresses from ifconfig - # and compare that with MACADDR. - my @ifcfg_info = split(/\n/,`ifconfig -a | grep HWaddr | awk '{print \$1,\$5;}'`); - foreach my $nic_mac (@ifcfg_info) { - my @nicmac = split(/ /,$nic_mac); - if (uc($nicmac[1]) eq uc($MAC)) { - $inst_nic = $nicmac[0]; - last; - } - } -} else { # INSTALLNIC not set or is not known - system("logger -t xcat -p local4.info 'confignics: install nic $inst_nic not known '"); -} - - -# niccustomscripts specifies which NICS need to be configured. -# and which script to use to configure it. -# Strip NIC and customscript and then call the custom script with the NIC -# it is up to the custom script to verify information passed in NIC env vars. -# i.e. if customscript is "eth1:configeth eth1, eth2:configeth2" -# then first get eth1 for the nic and "configeth eth1" for the command to run" -# the do the same for eth2. - -if ( defined $niccustomscripts && length $niccustomscripts > 0 ) { - system("logger -t xcat -p local4.info 'confignics: processing custom scripts: $niccustomscripts '"); - - foreach my $customscript (split(/,/,$niccustomscripts)) { - - my @script = (); - if ( $customscript =~ /!/ ) { - @script = split(/!/,$customscript); - } else { - @script = split(/:/,$customscript); - } - $cust_script_nics{$script[0]} = 1; - my @s = split(/ /,$script[1]); - - # if installnic then verify that "-s" flag was passed in. - if (($inst_nic ne $script[0]) || (($inst_nic eq $script[0]) && $cfg_inst_nic)) { - runcmd("$script[1]"); - system("logger -t xcat -p local4.info 'confignics: executed custom script: $script[1] '"); - } - } -} - -# Get nic from nicips. If nic is in cust_script_nics hash then ignore this nic. -# otherwise, get nictype if set or determine type from nic name, eth* or en* -# implies ethernet, ib* implies infiniband. -# -# configib prefers to have ib adapters configured in one call for performance -# reasons. So add ib nics to a list and call configib outside the loop. -foreach my $nic_ips (split(/,/,$nicips)) { - $type = ''; - my $type_found = 0; - $nic = ''; - my @nic_and_ips = (); - if ( $nic_ips =~ /!/ ) { - @nic_and_ips = split(/!/,$nic_ips); - } else { - @nic_and_ips = split(/:/,$nic_ips); - } - $nic = $nic_and_ips[0]; - # do not configure if nic is in the customscript hash - if ($cust_script_nics{$nic_and_ips[0]} == 1 ) { - system("logger -t xcat -p local4.info 'confignics: nic $nic_and_ips[0] already configured through custom script '"); +#the nics' information contain: +#1. ip address +#2. nic network +#3. nic type +#4. custom scripts +#all of them are saved by different variable +#this function can split the varable for each nic, and svae the information for each nic +#into an hash, the key is nic name, the value are all information, which joined by ',' +function splitconfig(){ + if [ ! "$1" ];then + return + fi + old_ifs=$IFS + IFS=$',' + array_conf_temp=($1) + IFS=$old_ifs + for i in ${array_conf_temp[@]} + do + D= + if [ `echo $i | grep "!"` ];then + D="!" + else + D=":" + fi + key=`echo $i | cut -d"$D" -f 1` + str_temp_value=`echo $i | cut -d"$D" -f 2` - } - else { - # Find matching type for this nic - foreach my $nic_type (split(/,/,$nictypes)) { - my @nic_and_type = (); - if ( $nic_type =~ /!/ ) { - @nic_and_type = split(/!/,$nic_type); - } else { - @nic_and_type = split(/:/,$nic_type); - } - if ($nic_and_type[0] eq $nic ) { - $type = $nic_and_type[1]; - # verify type is "ethernet" or "infiniband" - if ($type =~ /ethernet|infiniband/i ) { - $type_found = 1; - } - last; - } - } - # if no matching nic type then derive nic type from nic - if ( !$type_found ) { - if ( $nic =~ /(eth|en)\d+/i ) { - $type = "ethernet"; - } elsif ($nic =~ /ib\d+/i ) { - $type = "infiniband"; - } - } - - if ("ethernet" eq lc($type)) { - # Ensure to only configure the install nic if the "-s" flag was set. - if (($inst_nic ne $nic) || (($inst_nic eq $nic) && $cfg_inst_nic)) { - runcmd("configeth $nic"); - system("logger -t xcat -p local4.info 'confignics: executed script: configeth $nic '"); - } - else { - system("logger -t xcat -p local4.info 'confignics: Not configuring install nic $nic '"); - } - } elsif ("infiniband" eq lc($type)) { - if ($ibnics) { - $ibnics = $ibnics . "," . $nic; - } else { - $ibnics = $nic; - } - } else { - system("logger -t xcat -p local4.info 'confignics: unknown type $type for NIC: $nic '"); - } - } + str_temp=$(hashget hash_defined_nics $key) + if [ -n "$str_temp" ];then + str_temp=$str_temp",${str_temp_value}" + else + str_temp="$str_temp_value" + str_all_nics=$str_all_nics"$key " + fi + hashset hash_defined_nics $key $str_temp + done } -# Call configib now to configure all ib adapters in one call. -if ($ibnics) { - runcmd("NIC_IBNICS=$ibnics NIC_IBAPORTS=$ibaports configib"); - system("logger -t xcat -p local4.info 'confignics: executed script: configib for nics $ibnics '"); -} +bool_cfg_inst_nic=0 +str_inst_nic='' +str_ib_nics='' +str_os_type=`uname | tr 'A-Z' 'a-z'` +bool_remove=0 +num_iba_ports= +str_all_nics='' +for arg in "$@" +do + if [ "$arg" = "-s" ];then + bool_cfg_inst_nic=1 + elif [ "$arg" = "-r" ];then + bool_remove=1 + elif [ "${arg:0:10}" = "--ibaports" ];then + num_iba_ports=${arg#--ibaports=} + fi +done -exit 0; +logger -t xcat -p local4.info "confignics is called: config install nic:$bool_cfg_inst_nic, remove: $bool_remove, iba ports: $num_iba_ports" +echo "confignics on $NODE: config install nic:$bool_cfg_inst_nic, remove: $bool_remove, iba ports: $num_iba_ports" -sub runcmd { - my $cmd = shift @_; - $cmd .= ' 2>&1'; - my @output = `$cmd`; - my $rc = $? >> 8; - if ($rc) { - system("logger -t xcat -p local4.err 'confignics: command $cmd failed with rc $rc: " . join('',@output) . "'"); - my $errout= "confignics: command $cmd failed with rc $rc.\n"; - print $errout; - exit $rc; - } - print join("\n",@output),"\n"; -} +str_temp='' +if [ ! $INSTALLNIC ];then + str_temp="mac" +else + str_temp=$INSTALLNIC +fi +if [ "$str_temp" = "mac" ];then + if [ "$str_os_type" = "aix" ];then + old_ifs=$IFS + IFS=$' ' + str_temp=`ifconfig -l` + array_nicnames_temp=($str_temp) + IFS=$old_ifs + for temp_nic in ${array_nicnames_temp[@]} + do + entstat -t $temp_nic | grep -i "$MACADDRESS" + if [ $? -eq 0 ];then + str_inst_nic=$temp_nic + break + fi + done + else + str_inst_nic=`ifconfig -a | grep -i "$MACADDRESS" | awk '{print $1;}'` + fi +elif [ `echo $str_temp | grep -E "e(n|th)[0-9]+"` ];then + str_inst_nic=$str_temp +fi + + + +splitconfig $NICIPS +splitconfig $NICTYPES +splitconfig $NICNETWORKS +splitconfig $NICCUSTOMSCRIPTS + +#get state of nic in "UP" status +#If bonded redhat then "SLAVE" or "MASTER" will be in the first line of stanza +#do not configure the loopback nic +if [ $bool_remove -eq 1 ];then + if [ "$str_os_type" = "aix" ];then + str_temp=`ifconfig -a | grep flags | grep -vi loopback | grep -v SALVE | grep -v MASTER | awk -F: {'print $1'}` + else + str_temp=`ip link show | grep -v link | grep -vi loopback | grep -v SALVE | grep -v MASTER | awk {'print $2'} | sed s/://` + fi + old_ifs=$IFS + IFS=$'\n' + array_nics_temp=($str_temp) + IFS=$old_ifs + for str_temp_nic in ${array_nics_temp[@]} + do + #the nic type should be ethernet + echo $str_temp_nic | grep -E "e(n|th)[0-9]+" + if [ $? -ne 0 ];then + continue + fi + if [ "$str_os_type" != "aix" ];then + brctl show 2>/dev/null | grep $str_temp_nic + #the nic belongs to a bridge, go to next + if [ $? -eq 0 ];then + continue + fi + fi + #the nic is defined this time + str_temp=$(hashget hash_defined_nics $str_temp_nic) + if [ -n "$str_temp" ];then + continue + fi + + if [ "$str_temp_nic" = "$str_inst_nic" -a $bool_cfg_inst_nic -eq 0 ];then + continue + fi + + #ignore the vlan interface + echo $str_temp_nic | grep "\." + if [ $? -eq 0 ];then + continue + fi + + logger -t xcat -p local4.info "confignics: remove nic $str_temp_nic" + echo "confignics on $NODE: remove nic $str_temp_nic" + configeth -r $str_temp_nic + done +fi + +old_ifs=$IFS +IFS=$' ' +array_nics_temp=($str_all_nics) +IFS=$old_ifs +for key in ${array_nics_temp[@]} +do + key=`echo $key | sed 's/^ \+//' | sed 's/ \+$//'` + str_nic_type= + str_value=$(hashget hash_defined_nics $key) + if [ "$key" = "$str_inst_nic" -a $bool_cfg_inst_nic -eq 0 ];then + continue + fi + old_ifs=$IFS + IFS=$',' + array_temp=($str_value) + IFS=$old_ifs + + if [ "${array_temp[3]}" ];then + logger -t xcat -p local4.info "confignics: processing custom scripts: ${array_temp[3]} for interface $key" + echo "confignics on $NODE: processing custom scripts: ${array_temp[3]} for interface $key" + ${array_temp[3]} + else + if [ "${array_temp[1]}" ];then + str_nic_type=`echo ${array_temp[1]} | tr "[A-Z]" "[a-z]"` + else + if [ `echo $key | grep -E '(eth|en)[0-9]+'` ];then + str_nic_type="ethernet" + elif [ `echo $KEY | grep -E 'ib[0-9]+'` ];then + str_nic_type="infiniband" + fi + fi + + if [ $str_nic_type = "ethernet" ];then + logger -t xcat -p local4.info "confignics: call 'configeth $key ${array_temp[0]} ${array_temp[2]}'" + echo "confignics on $NODE: call 'configeth $key ${array_temp[0]} ${array_temp[2]}'" + configeth $key ${array_temp[0]} ${array_temp[2]} + elif [ $str_nic_type = "infiniband" ];then + if [ $str_ib_nics ];then + str_ib_nics=$str_ib_nics","$key + else + str_ib_nics=$key + fi + else + logger -t xcat -p local4.info "confignics: unknown type $str_nic_type for NIC: $key" + echo "confignics on $NODE: unknown type $str_nic_type for NIC: $key" + fi + fi +done + +if [ -n "$str_ib_nics" ];then + logger -t xcat -p local4.info "confignics: executed script: configib for nics: $str_ib_nics, ports: $num_iba_ports" + echo "confignics on $NODE: executed script: configib for nics: $str_ib_nics, ports: $num_iba_ports" + NIC_IBNICS=$str_ib_nics NIC_IBAPORTS=$num_iba_ports configib +else + if [ $bool_remove -eq 1 ];then + logger -t xcat -p local4.info "confignics: executed script: 'configib -u' to remove all ib nics and configuration files" + echo "confignics on $NODE: executed script: 'configib -r' to remove all ib nics and configuration files" + configib + fi +fi diff --git a/xCAT/postscripts/xcatlib.sh b/xCAT/postscripts/xcatlib.sh new file mode 100644 index 000000000..e5b5da8fd --- /dev/null +++ b/xCAT/postscripts/xcatlib.sh @@ -0,0 +1,17 @@ +function hashencode(){ + local map="$1" + echo `echo $map | sed 's/\./xDOTx/g' | sed 's/:/xCOLONx/g' | sed 's/,/:xCOMMAx/g'` +} + +function hashset(){ + local hashname="hash${1}${2}" + local value=$3 + hashname=$(hashencode $hashname) + eval "${hashname}='${value}'" +} + +function hashget(){ + local hashname="hash${1}${2}" + hashname=$(hashencode $hashname) + eval echo "\$${hashname}" +}