????

Your IP : 18.189.192.214


Current Path : /opt/microsoft/scvmmguestagent/bin/
Upload File :
Current File : //opt/microsoft/scvmmguestagent/bin/cfgnetadapter

#!/bin/bash
#
# Copyright (C) Microsoft Corporation. All rights reserved.
#
# Configure network adapter
# 

#Variables
retcode=0
thisscript=${0}
distro="unknown"
cfgAddress="false"
cfgIPv4="false"
cfgIPv6="false"
cfgDNSClient="false"
cfgGW="false"
cfgStatus="ok"
dev=""
macaddress=""
ipv4addresstype=""
ipv6address=""
ipv6prefixlen=""
ipv4address=""
ipv4prefixlen=""
ipv4netmask=""
ipv6addresstype=""
gateways=""
dnssearchsuffix=""
nameservers=""
thisdnsdomain=""

#Include utilities script
source $(dirname ${0})/utilities

function isNewElement {
    local e
        for e in "${@:2}"; do [[ "$e" = "$1" ]] && return 0; done
        return 1
}

function usage {
  WriteError "Invalid arguments provided" ${thisscript}
  echo "Usage: $0 macaddress=macaddress [ipv4addresstype=ipv4addresstype] [ipv4address=ipv4address] [ipv6addresstype=ipv6addresstype] [ipv6address=ipv6address] [gateways=gateways[]] [dnssearchsuffix=dnssearchsuffix[]] [nameservers=nameservers[]]"
}

VALID_INPUT=0
#Process arguments
while [ $# -gt 0 ]

do 
   ArgName=`echo ${1} |cut -f1 -d=`
   ArgValue=`echo ${1} |cut -f2 -d=`
   case "${ArgName}" in
   macaddress )
      macaddress=${ArgValue}
      VALID_INPUT=1
      ;;
   ipv4addresstype )
      ipv4addresstype=${ArgValue}
      ;;
   ipv4address )
      ipv4address=${ArgValue}
      ;;
   ipv6addresstype )
      ipv6addresstype=${ArgValue}
      ;;
   ipv6address )
      ipv6address=${ArgValue}
      ;;
   gateways )
      gateways=${ArgValue}
      ;;
   dnssearchsuffix )
      dnssearchsuffix=${ArgValue}
      ;;
   nameservers )
      nameservers=${ArgValue}
      ;;
   esac
   shift
done

if [ $VALID_INPUT == 0 ]
then
   usage
   exit -1
elif [ $VALID_INPUT == 1 ]
then
   WriteInfo "All inputs validated" ${thisscript}
fi

#Get Device Name
if [ -x /sbin/ip ]
then
    dev=`ip -o link show |grep -i $macaddress |awk '{print $2}' |tr -d ':'`   
elif [ -x /sbin/ifconfig ]
then 
	
	dev=`ifconfig -a |grep -i $macaddress |awk '{print $1}'`

else
		WriteError "Ethernet configuration tools (ifconfig or ip) not found. Unable to configure or validate network." ${thisscript}
		exit -1
fi

if [ -n "${dev}" -a "`echo ${dev}|egrep '^.+[0-9]$'`" = "${dev}" ]
then 
   WriteInfo "Beginning configuration for network adapter ${dev}" ${thisscript}
else
   WriteError "Unable to identify a network adapter name for hwaddr ${macaddress}" ${thisscript}
   exit -1
fi

RestartNetwork(){ 
   WriteInfo "Restarting network subsystem" ${thisscript}
   if [ "${distro}" = "DEBIAN" ] 
   then  
     cfgExec "/etc/init.d/networking stop; /etc/init.d/networking start"  warning "Errors encountered in network subsystem restart"  ${thisscript} 
   else 
     cfgExec "service network restart"  warning "Errors encountered in network subsystem restart"  ${thisscript} 
   fi 
}

ClearUnusedAdapters(){

   WriteInfo "Deleting old configuration for unused interfaces." ${thisscript}

   if [ -x /sbin/ip ]
   then
	iflist=`ip -o link show |awk '{print $2}'|tr -d ':'|tr '\n' ' '`
   else
	iflist=`ifconfig -a|grep -i "link encap" |awk '{print $1}'|tr '\n' ' '`	
   fi

   declare -a ifarr=($iflist)
   
   if [ ${#ifarr[@]} -gt 0 ]
   then 
      if [ ${distro} = "DEBIAN" ]
      then 
	 IFCfgFile="/etc/network/interfaces"
	 for ifname in `cat ${IFCfgFile} |grep iface |awk '{print $2}'`    
	 do 
	    validif=0 
	    for i in "${ifarr[@]}"
	    do
		if [ ${i} = ${ifname} ]
		then
		   validif=1
		fi
	    done	 
	    if [ $validif -eq 0 ]
	    then
	       WriteInfo "Deleting unused interface ${ifname}." ${thisscript}
	       sed -i".bak" '/^iface '${ifname}' /,/^$/{//!d}' ${IFCfgFile}
	       sed -i".bak" '/'${ifname}' /d' ${IFCfgFile}
	       sed -i".bak" '/'${ifname}'$/d' ${IFCfgFile}
	       sed -i".bak" '/^mapping '${ifname}' /,/^$/{//!d}' ${IFCfgFile}
	       sed -i".bak" '/^iface '${ifname}'\./,/^$/{//!d}' ${IFCfgFile}
	       sed -i".bak" '/'${ifname}'\./d' ${IFCfgFile}
	    fi
	 done
      else
	 if [ ${distro} = "RHEL" ]
	 then
	    cfgdir="/etc/sysconfig/network-scripts"
	 elif [ ${distro} = "SUSE" ]
	 then
	    cfgdir="/etc/sysconfig/network"
	 fi

	 for file in `ls ${cfgdir}/ifcfg-*`
	 do
	    validif=0  
	    ifname=`basename ${file} |cut -f2 -d-`
	    for i in ${iflist}
	    do
		if [ ${i} = ${ifname} ]
		then
		   validif=1
		fi
	    done
	    if [ $validif -eq 0 ]
	    then
	       WriteInfo "Deleting unused interface file ${file}." ${thisscript}
	       rm -f ${file}
	    fi
	 done
      fi
   fi
}

#Convert ipv4prefixlen to subnetmask
function calcmask {
   case "${1}" in
      0 ) ipv4netmask="0.0.0.0" ;;
      1 ) ipv4netmask="128.0.0.0" ;;
      2 ) ipv4netmask="192.0.0.0" ;;
      3 ) ipv4netmask="224.0.0.0" ;;
      4 ) ipv4netmask="240.0.0.0" ;;
      5 ) ipv4netmask="248.0.0.0" ;;
      6 ) ipv4netmask="252.0.0.0" ;;
      7 ) ipv4netmask="254.0.0.0" ;;
      8 ) ipv4netmask="255.0.0.0" ;;
      9 ) ipv4netmask="255.128.0.0" ;;
      10 ) ipv4netmask="255.192.0.0" ;;
      11 ) ipv4netmask="255.224.0.0" ;;
      12 ) ipv4netmask="255.240.0.0" ;;
      13 ) ipv4netmask="255.248.0.0" ;;
      14 ) ipv4netmask="255.252.0.0" ;;
      15 ) ipv4netmask="255.254.0.0" ;;
      16 ) ipv4netmask="255.255.0.0" ;;
      17 ) ipv4netmask="255.255.128.0" ;;
      18 ) ipv4netmask="255.255.192.0" ;;
      19 ) ipv4netmask="255.255.224.0" ;;
      20 ) ipv4netmask="255.255.240.0" ;;
      21 ) ipv4netmask="255.255.248.0" ;;
      22 ) ipv4netmask="255.255.252.0" ;;
      23 ) ipv4netmask="255.255.254.0" ;;
      24 ) ipv4netmask="255.255.255.0" ;;
      25 ) ipv4netmask="255.255.255.128" ;;
      26 ) ipv4netmask="255.255.255.192" ;;
      27 ) ipv4netmask="255.255.255.224" ;;
      28 ) ipv4netmask="255.255.255.240" ;;
      29 ) ipv4netmask="255.255.255.248" ;;
      30 ) ipv4netmask="255.255.255.252" ;;
      31 ) ipv4netmask="255.255.255.254" ;;
      32 ) ipv4netmask="255.255.255.255" ;;
   esac

   testmask=`echo $ipv4netmask |grep -c '[0-9]\{0,3\}\.[0-9]\{0,3\}\.[0-9]\{0,3\}\.[0-9]\{0,3\}'`
   if [ ${testmask} -ne 1 ]
   then
      WriteError "Failed to calculate IPv4 Network Mask" ${thisscript}
      exit -1
   fi

}

#Identify configuration to perform, from arguments
if [[ -n "${ipv4addresstype}" || -n "${ipv6addresstype}" ]]
then
   cfgAddress="true"

   if [ -n "${ipv4addresstype}" ]
   then
      cfgIPv4="true"
   fi

   if [ -n "${ipv6addresstype}" ]
   then
      cfgIPv6="true"
   fi

   if [ -n "${gateways}" ]
   then	
      cfgGW="true"
      gwlist=`echo -e ${gateways} |tr ',' ' '`
      declare -a gwarr=($gwlist)

   fi

   if [ -n "${ipv4address}" ]
   then
      ipv4prefixlen=`echo ${ipv4address} |cut -f2 -d/`
      ipv4address=`echo ${ipv4address} |cut -f1 -d/`
      calcmask "${ipv4prefixlen}"
   fi

   if [ -n "${ipv6address}" ]
   then
      ipv6prefixlen=`echo ${ipv6address} |cut -f2 -d/`
      ipv6address=`echo ${ipv6address} |cut -f1 -d/`
   fi
fi

if [ -n "${nameservers}" ]
then
   cfgDNSClient="true"
   nslist=`echo -e ${nameservers} |tr ',' ' '`
   declare -a nsarr=($nslist)
fi

if [ -n "${dnssearchsuffix}" ]
then
   cfgDNSClient="true"
   suffixlist=`echo -e ${dnssearchsuffix} |tr ',' ' '`
   declare -a suffixarr=($suffixlist)
fi

#Edit Resolv.conf for DNS Client Settings
ConfigResolv(){
   nslist=""
   searchlist=""

   #Re-write search suffix list in case it was overwritten during network restart
   if [ -n "${thissearchlist}" ]
   then
      cfgExec  "sed -i".bak" '/^search /d' /etc/resolv.conf"  warning  "Failed to delete old search entry from resolv.conf"  ${thisscript} 
      cfgExec 'echo -e ${thissearchlist} >> /etc/resolv.conf'  warning "Failed to write search suffixes to /etc/resolv.conf"  ${thisscript} 
   fi

   #get current list of dns search suffixes and append new values to suffix list
   searchlist=`cat /etc/resolv.conf |grep '^search' |cut -f2- -d ' '`
   declare -a searcharr=($searchlist)
   for search in "${suffixarr[@]}"
      do 
	    isNewElement "$search" "${searcharr[@]}"
        if [ 1 == $? ]
        then
            searchlist="${searchlist} ${search}"
        fi
   done

   #get current list of name servers, but only ones added by scvmm-agent
   currentnameservers=`cat /etc/resolv.conf |grep \#vmm|awk '{print $2}'`
   for ns in $currentnameservers
      do nslist="${nslist}nameserver ${ns} #vmm \n"
   done
   nscount=`cat /etc/resolv.conf |grep '^nameserver'|grep -c \#vmm`
		
   #For each supplied name server, append to name server list, until the total count of vmm-added name servers is 3 
   for ns in "${nsarr[@]}"
   do
      if [ $nscount -lt 3 ]
      then
	 nscount=$((nscount+1))
	 nslist="${nslist}nameserver ${ns} #vmm \n"
      fi
   done

   #Edit strings and write to resolv.conf
   cfgExec  "sed -i".bak" '/^search /d' /etc/resolv.conf"  warning  "Failed to delete old search entry from resolv.conf"  ${thisscript} 
   cfgExec  "sed -i".bak" '/^nameserver /d' /etc/resolv.conf"  warning  "Failed to delete old nameserver entries from resolv.conf"  ${thisscript}
   searchlist="search ${searchlist}"
      
   WriteInfo "Writing nameservers and search suffixes to /etc/resolv.conf" ${thisscript}
   cfgExec 'echo -e ${searchlist} >> /etc/resolv.conf'  warning "Failed to write search suffixes to /etc/resolv.conf"  ${thisscript} 
   cfgExec 'echo -e ${nslist} >> /etc/resolv.conf'  warning "Failed to write nameservers to /etc/resolv.conf"  ${thisscript} 

   #Dhclient-script may have overwritten domain entry in resolv.conf.  If so, restore it
   if [ -n "${thisdnsdomain}" ] 
   then
      cfgExec  "sed -i".bak" '/^domain /d' /etc/resolv.conf"  warning  "Failed to delete old domain entry from resolv.conf"  ${thisscript}
      cfgExec 'echo -e "domain ${thisdnsdomain}" >> "/etc/resolv.conf"'  warning  "Failed to write domain entry to resolv.conf"  ${thisscript} 
   fi
}

EnableResolvConfUpdates(){
  if [ "${ipv4addresstype}" = "dhcp" ]
    then
      if [ -x "/sbin/dhclient-script" ]
      then
	 if [ ${distro} = "DEBIAN" ]
	 then 
	    dhfile="/etc/dhcp3/dhclient-enter-hooks.d/nodnsupdate"
	 else
	    if [ -d "/etc/dhcp" ]
	    then
	       dhfile="/etc/dhcp/dhclient-enter-hooks"
	    else
	       dhfile="/etc/dhclient-enter-hooks"
	    fi
	 fi
	 WriteInfo "Removing dhclient enter hooks to enable resolv.conf overwrites in: ${dhfile}" ${thisscript}
	 if [ -e "${dhfile}" ]
	 then
	    rm -f ${dhfile}
	 fi
      fi

      if [ -e /etc/sysconfig/network/dhcp ]
      then
	 WriteInfo "Setting DHCLIENT_MODIFY_RESOLV_CONF=yes in /etc/sysconfig/network/dhcp" ${thisscript}
	 sed -i".bak" 's/^DHCLIENT_MODIFY_RESOLV_CONF=.*/DHCLIENT_MODIFY_RESOLV_CONF="yes"/g' /etc/sysconfig/network/dhcp
      fi

      if [ -x "/sbin/resolvconf" ]
      then 
	 WriteInfo "Enabling resolvconf updates to resolv.conf." ${thisscript}
	 /sbin/resolvconf --enable-updates
      fi
   fi
}

ConfigHostsEntry(){
   thishost=`hostname`
   thisdnsdomain=`cat /etc/resolv.conf |grep -e "^domain " |awk '{print $2}'`
   thissearchlist=`cat /etc/resolv.conf |grep -e "^search "`

   if [ -n "${ipv4address}" -a "${ipv4addresstype}" = "static" ]
   then
      #Remove entries currently in /etc/hosts for this IP Address
      cfgExec  "sed -i".bak" '/'${ipv4address}'/d' /etc/hosts"  warning  "Failed to remove old host entries for IP ${ipv4address} from /etc/hosts"  ${thisscript} 

      if [ ${thishost} ]
      then
	 if [ ${thisdnsdomain} ]
	 then
	    hostentry="${ipv4address}  ${thishost}.${thisdnsdomain}  ${thishost}"
	 else
	    hostentry="${ipv4address}  ${thishost}"
	 fi

	 cfgExec 'echo "${hostentry}" >> /etc/hosts' warning "Failed to write host entry for IP ${ipv4address} line to /etc/hosts"  ${thisscript} 
      fi
   fi

   if [ -n "${ipv6address}" -a "${ipv6addresstype}" = "static" ]
   then
      #Remove entries currently in /etc/hosts for this IP Address
      cfgExec  "sed -i".bak" '/'${ipv6address}'/d' /etc/hosts"  warning  "Failed to remove old host entries for IP ${ipv6address} from /etc/hosts"  ${thisscript} 

      if [ ${thishost} ]
      then
	 if [ ${thisdnsdomain} ]
	 then
	    hostentry="${ipv6address}  ${thishost}.${thisdnsdomain}  ${thishost}"
	 else
	    hostentry="${ipv6address}  ${thishost}"
	 fi

	 cfgExec 'echo "${hostentry}" >> /etc/hosts' warning "Failed to write host entry for IP ${ipv6address} line to /etc/hosts"  ${thisscript} 
      fi
   fi
}

#Adapter configuration - RHEL
NetConfigRHEL(){
   if [ ${cfgAddress} = "true" ]
   then
      WriteInfo "Configuring scripts for network adapter ${dev}" ${thisscript}
      #Make sure that networking is enabled
      cfgExec "sed -i".bak" '/NETWORKING=/d' /etc/sysconfig/network" warning "Failed to purge NETWORKING line from /etc/sysconfig/network"  ${thisscript} 
      cfgExec 'echo "NETWORKING=yes" >> /etc/sysconfig/network' error "Failed to write NETWORKING line to /etc/sysconfig/network"  ${thisscript} 
      
      if [ "${cfgIPv6}" = "true" ]
      then
	 cfgExec "sed -i".bak" '/NETWORKING_IPV6=/d' /etc/sysconfig/network" warning "Failed to purge NETWORKING_IPV6 line from /etc/sysconfig/network"  ${thisscript} 
	 cfgExec 'echo "NETWORKING_IPV6=yes" >> /etc/sysconfig/network' error "Failed to write NETWORKING_IPV6 line to /etc/sysconfig/network"  ${thisscript} 
      fi

      cfgdir="/etc/sysconfig/network-scripts"
      #remove old netconfig files for this interface. 
      WriteInfo "Deleting old ${dev} configuration" ${thisscript}
      cfgExec "rm $cfgdir/ifcfg-*$dev* --force" warning "Failed to delete old ${dev} configuration"  ${thisscript} 
      cfgExec "rm $cfgdir/route-*$dev* --force" warning "Failed to delete old ${dev} route configuration"  ${thisscript} 

      #Construct config file contents
      netcfg=""
      netcfg="${netcfg}DEVICE='${dev}'\n"
      netcfg="${netcfg}STARTMODE='auto'\n"

      #IPv4 and IPv4/IPv6 configurations
      if [ "${cfgIPv4}" = "true" ]
      then
	 if [ "${ipv4addresstype}" = "dhcp" ]
	 then
	    netcfg="${netcfg}BOOTPROTO='dhcp'\n"
	    netcfg="${netcfg}DHCP_HOSTNAME='${thishost}'\n"
	 fi
	 if [ "${ipv4addresstype}" = "static" ]
	 then
	    netcfg="${netcfg}BOOTPROTO='static'\n"
	    netcfg="${netcfg}IPADDR='$ipv4address'\n"
	    netcfg="${netcfg}NETMASK='$ipv4netmask'\n"
	    netcfg="${netcfg}PEERDNS='no'\n"
	 fi

	 if [ "${cfgIPv6}" = "true" ] 
	 then 
	    if [ "${ipv6addresstype}" = "static" ]
	    then
	       netcfg="${netcfg}IPV6INIT='yes'\n"
	       netcfg="${netcfg}IPV6_AUTOCONF='no'\n"
	       netcfg="${netcfg}IPV6ADDR='$ipv6address/$ipv6prefixlen'\n"
	    fi
	    if [ "${ipv6addresstype}" = "dhcp" ]
	    then
	       netcfg="${netcfg}IPV6INIT='yes'\n"
	       netcfg="${netcfg}IPV6_AUTOCONF='yes'\n"
	    fi
	 fi
      fi	
	
      #Handle IPv6 only configurations
      if [ "${cfgIPv6}" = "true" ] &&  [ "${cfgIPv4}" = "false" ]
      then
	 if [ "${ipv6addresstype}" = "dhcp" ]
	 then
	    netcfg="${netcfg}IPV6INIT='yes'\n"
	    netcfg="${netcfg}IPV6_AUTOCONF='yes'\n"
	 fi
	   
	 if [ "${ipv6addresstype}" = "static" ]
	 then
	    netcfg="${netcfg}IPV6INIT='yes'\n"
	    netcfg="${netcfg}IPV6_AUTOCONF='no'\n"
	    netcfg="${netcfg}IPV6ADDR='$ipv6address/$ipv6prefixlen'\n"
	    netcfg="${netcfg}PEERDNS='no'\n"
	 fi
      fi

      WriteInfo "Writing configuration to ${cfgdir}/ifcfg-${dev}" ${thisscript}
      cfgExec 'echo -e ${netcfg} > ${cfgdir}/ifcfg-${dev}'  warning "Failed to write adapter configuration to ${cfgdir}/ifcfg-${dev}"  ${thisscript} 

      #gateways - if gw defined...
      if [ "${cfgGW}" = "true" ]
      then
	    #Purge any old default gateway
	    cfgExec "sed -i".bak" '/gateway/d' /etc/sysconfig/network" warning "Failed to purge gateway line from /etc/sysconfig/network"  ${thisscript} 
	    cfgExec "sed -i".bak" '/GATEWAY/d' /etc/sysconfig/network" warning "Failed to purge GATEWAY line from /etc/sysconfig/network"  ${thisscript} 
	    #Set first supplied gateway as global default gateway
	    WriteInfo "Writing gateway ${gwarr[0]} to /etc/sysconfig/network" ${thisscript}
	    cfgExec 'echo -e "GATEWAY=${gwarr[0]}" >> /etc/sysconfig/network' error "Failed to write gateway ${gwarr[0]} to /etc/sysconfig/network"  ${thisscript} 
      fi

   fi
 
}

#Adapter configuration - DEBIAN
RemoveDebInterface(){
   #Remove adapter, and any subinterfaces from the interfaces config file
   IFCfgFile="/etc/network/interfaces"
   WriteInfo "Removing old configuration for adapter ${dev} from ${IFCfgFile}" ${thisscript}
   sed -i".bak" '/^iface '${dev}' /,/^$/{//!d}' ${IFCfgFile}
   sed -i".bak" '/'${dev}' /d' ${IFCfgFile}
   sed -i".bak" '/'${dev}'$/d' ${IFCfgFile}
   sed -i".bak" '/^mapping '${dev}' /,/^$/{//!d}' ${IFCfgFile}
   sed -i".bak" '/^iface '${dev}'\./,/^$/{//!d}' ${IFCfgFile}
   sed -i".bak" '/'${dev}'\./d' ${IFCfgFile}
   lc=`cat ${IFCfgFile} |grep -c "${dev} "`
   if [ ! $lc = 0 ]
   then
      WriteError "Failed to remove old configuration for adapter ${dev} from ${IFCfgFile}" ${thisscript}
      exit -1
   fi

}

NetConfigDEBIAN(){
   if [ ${cfgAddress} = "true" ]
   then
      cfgdir="/etc/network"
      #Remove old network interface configuration for this device
      RemoveDebInterface

      netcfg=""
      netcfg="${netcfg}\n"
      netcfg="${netcfg}#${dev}\n"
      netcfg="${netcfg}auto ${dev}\n"
      #IPv4 configuration
      if [ "${cfgIPv4}" = "true" ]
      then
	 if [ "${ipv4addresstype}" = "dhcp" ]
	 then
	    netcfg="${netcfg}iface ${dev} inet dhcp\n"
	 fi
	 if [ "${ipv4addresstype}" = "static" ]
	 then
	    netcfg="${netcfg}iface ${dev} inet static\n"
	    netcfg="${netcfg}     address ${ipv4address}\n"
	    netcfg="${netcfg}     netmask ${ipv4netmask}\n"
	    #gateway, use the first IPv4 gateway in the string array
	    for gw in "${gwarr[@]}"
	    do 
	       if [ `echo ${gw} |grep -c \.` = 1 ]
	       then
		  netcfg="${netcfg}     gateway ${gw}\n" 
		  break
	       fi
	    done
	 fi
      fi	

      #IPv6 configuration
      if [ "${cfgIPv6}" = "true" ]
      then
	 if [ "${ipv6addresstype}" = "dhcp" ]
	 then
	    netcfg="${netcfg}\n"
	    #Default to kernel level autoconf
	 fi
	 if [ "${ipv6addresstype}" = "static" ]
	 then
	    netcfg="${netcfg}\n"
	    netcfg="${netcfg}iface ${dev} inet6 static\n"
	    netcfg="${netcfg}pre-up modprobe ipv6\n"
      	    netcfg="${netcfg}     address ${ipv6address}\n"
	    netcfg="${netcfg}     netmask ${ipv6prefixlen}\n"

	    #gateway, use the first IPv6 gateway in the string array
	    for gw in ${gwarr}
	    do 
	       if [ `echo ${gw} |grep \:|wc -l` = 1 ]
	       then
		  netcfg="${netcfg}     gateway ${gw}\n" 
		  break
	       fi
	    done
	 fi
      fi	

      WriteInfo "Writing configuration for adapter ${dev} to /etc/network/interfaces" ${thisscript}
      cfgExec 'echo -e ${netcfg} >> /etc/network/interfaces' error "Failed to write configuration for adapter ${dev} to /etc/network/interfaces"  ${thisscript} 

   fi
}

#Adapter configuration - SUSE
NetConfigSUSE(){
   if [ ${cfgAddress} = "true" ]
   then
      cfgdir="/etc/sysconfig/network"
      slesver=`cat /etc/SuSE-release | grep VERSION | awk '{print $3}'`

      if [ "${slesver}" -eq 10 ]
      then
	 cfgfile="${cfgdir}/ifcfg-eth-id-${macaddress}"
	 routefile="${cfgdir}/ifroute-eth-id-${macaddress}"
	 sed -i".bak" 's/^FORCE_PERSISTENT_NAMES=.*/FORCE_PERSISTENT_NAMES=yes/g' /etc/sysconfig/network/config
      else
	 cfgfile="${cfgdir}/ifcfg-${dev}"
	 routefile="${cfgdir}/ifroute-${dev}"
      fi

      #remove old netconfig files for this interface. 
      WriteInfo "Deleting old ${dev} scripts" ${thisscript}
      cfgExec "rm $cfgdir/ifcfg-*$dev* --force" warning "Failed to delete old ${dev} scripts"  ${thisscript} 
    
      #Construct config file contents
      netcfg=""
      netcfg="${netcfg}DEVICE='${dev}'\n"
      netcfg="${netcfg}STARTMODE='auto'\n"

      #IPv4 and IPv4/IPv6 configurations
      if [ "${cfgIPv4}" = "true" ]
      then
	 if [ "${ipv4addresstype}" = "dhcp" ]
	 then
	    netcfg="${netcfg}BOOTPROTO='dhcp'\n"
	 fi
	 if [ "${ipv4addresstype}" = "static" ]
	 then
	    netcfg="${netcfg}BOOTPROTO='static'\n"
	    netcfg="${netcfg}IPADDR='${ipv4address}'\n"
	    netcfg="${netcfg}NETMASK='${ipv4netmask}'\n"
	 fi

	 if [ "${cfgIPv6}" = "true" ] 
	 then 
	    if [ "${ipv6addresstype}" = "static" ]
	    then
   	       netcfg="${netcfg}LABEL_0='0'\n"
	       netcfg="${netcfg}IPADDR_0='${ipv6address}'\n"
	       netcfg="${netcfg}PREFIXLEN_0='${ipv6prefixlen}'\n"
	    fi
	 fi

      fi	
	
      #Handle IPv6 only configurations
      if [ "${cfgIPv6}" = "true" ] &&  [ "${cfgIPv4}" = "false" ]
      then
	 #if [ "${ipv6addresstype}" = "dhcp" ]
	 #then
	    #Default to kernel level autoconf
	 #fi
	   
	 if [ "${ipv6addresstype}" = "static" ]
	 then
   	       netcfg="${netcfg}LABEL_0='0'\n"
	       netcfg="${netcfg}IPADDR_0='${ipv6address}'\n"
	       netcfg="${netcfg}PREFIXLEN_0='${ipv6prefixlen}'\n"
	 fi
      fi

      WriteInfo "Writing configuration for adapter ${dev} to ${cfgdir}/ifcfg-${dev}" ${thisscript}
      cfgExec 'echo -e ${netcfg} > ${cfgfile}' error "Failed to write configuration for adapter ${dev} to ${cfgfile}"  ${thisscript} 

      #gateways - if gw defined...
      if [ "${cfgGW}" = "true" ]
      then	
	    WriteInfo "Removing old ifroute files for interface ${dev}" ${thisscript}
	    cfgExec "rm ${cfgdir}/ifroute-*${dev}* --force" warning "Failed to remove old ifroute files for interface ${dev}"  ${thisscript} 

	    #Construct new route table for defined gateways
	    gwlist="# Destination     gateway     Netmask            Device \n"
	    gwlist="${gwlist}default	${gwarr[0]}	0.0.0.0		${dev} \n"
	
	    #Write to ifroute file
	    cfgExec 'echo -e ${gwlist} > ${routefile}' error "Failed to write default route to ${routefile}"  ${thisscript} 
	    
	    #Clean out old routing table, as it is expected to be invalid now
	    WriteInfo "Moving /etc/sysconfig/network/routes to routes.bak" ${thisscript}
	    if [ -e "/etc/sysconfig/network/routes" ]
	    then
	       cfgExec "mv /etc/sysconfig/network/routes /etc/sysconfig/network/routes.bak" warning "Failed to move /etc/sysconfig/network/routes to routes.bak"  ${thisscript} 
	    fi
      fi

   fi
}

function ValidateConfig {

	if [ -x /sbin/ip ]
	then
			ifstatus=`ip link show ${dev} |grep -c UP`
			ipv4status=`ip -o addr show ${dev} |grep -c "${ipv4address}"`   
			ipv4mask=`ip addr show ${dev} |grep -c "/${ipv4prefixlen}"`
	else
			ifstatus=`ifconfig ${dev} |grep -c UP`
			ipv4status=`ifconfig ${dev} |grep -c "inet addr:${ipv4address}"`
			ipv4mask=`ifconfig ${dev} |grep -c "Mask:${ipv4netmask}"`
	fi

   if [ ${ifstatus} = 1 ]
   then
      WriteInfo "Interface ${dev} is configured and up." ${thisscript}
   else
      WriteError "Interface ${dev} is not configured or is not up." ${thisscript}
      exit -1
   fi

   #Check static configs
   if [ "${ipv4addresstype}" = "static" ]
   then
	   if [ ${ipv4status} = 1 ]
	   then
			WriteInfo "Interface ${dev} has the correct static IPv4 address." ${thisscript}
	   else
			WriteError "Interface ${dev} does not have the correct static IPv4 address." ${thisscript}
			exit -1
	   fi
     
		if [ ${ipv4mask} = 1 ]
			then
				WriteInfo "Interface ${dev} has the correct static IPv4 netmask." ${thisscript}
			else
				WriteError "Interface ${dev} does not have the correct static IPv4 netmask." ${thisscript}
			exit -1
		fi


   fi
   
}

#Main
GetDistroFamily
ClearUnusedAdapters

ConfigHostsEntry

if [ ${distro} = "unknown" ]
then 
   WriteError "Failed to detect Linux distribution. Exiting" ${thisscript}
   exit -1
else
   if [ ${distro} = "DEBIAN" ]
   then 
      NetConfigDEBIAN
   fi

   if [ ${distro} = "RHEL" ]
   then
      NetConfigRHEL
   fi

   if [ ${distro} = "SUSE" ]
   then
      NetConfigSUSE
   fi
fi

EnableResolvConfUpdates

RestartNetwork

if [ "${cfgDNSClient}" = "true" ]
then
   ConfigResolv
fi

echo ${dev} >> $(dirname ${0})/../status/definedadapters

ValidateConfig
 
exit ${retcode}