2014-09-08 12:15:09 +00:00
#!/bin/sh
2011-09-16 18:53:32 +00:00
# pmatch determines if 1st argument string is matched by 2nd argument pattern
pmatch ()
{
case $1 in
$2) return 0;; # zero return code means string matched by pattern
esac
return 1 # non-zero return code means string not matched by pattern
}
2014-05-14 12:01:41 +00:00
# converts netmask CIDR fromat to x.x.x.x mask value
maskfromprefix ()
{
prefixlen=$1
maskval=$((0xffffffff>>(32-prefixlen)<<(32-prefixlen)))
mask1=$((maskval >> 24))
mask2=$((maskval >> 16 & 0xff))
mask3=$((maskval >> 8 & 0xff))
mask4=$((maskval & 0xff))
echo $mask1.$mask2.$mask3.$mask4
NETMASK=$mask1.$mask2.$mask3.$mask4
}
2011-09-16 18:53:32 +00:00
network_ipv4calc ()
{
# Returns the network value needed for Ubuntu /etc/network/interface file
# $1 must be the IP Address
# $2 must be the Netmask value
IP_1=`echo $1 | cut -d . -f 1`
IP_2=`echo $1 | cut -d . -f 2`
IP_3=`echo $1 | cut -d . -f 3`
IP_4=`echo $1 | cut -d . -f 4`
NETM_1=`echo $2 | cut -d . -f 1`
NETM_2=`echo $2 | cut -d . -f 2`
NETM_3=`echo $2 | cut -d . -f 3`
NETM_4=`echo $2 | cut -d . -f 4`
NET_1=$(($IP_1 & $NETM_1))
NET_2=$(($IP_2 & $NETM_2))
NET_3=$(($IP_3 & $NETM_3))
NET_4=$(($IP_4 & $NETM_4))
NETWORK="$NET_1.$NET_2.$NET_3.$NET_4"
echo $NETWORK
}
2010-02-24 08:27:58 +00:00
defgw=`ip route | grep default | awk '{print $3}'`
2011-09-16 18:53:32 +00:00
if ( pmatch $OSVER "ubuntu*" )
then
echo `hostname` >/etc/hostname
mv /etc/network/interfaces /etc/network/interfaces.old # this file will be filled up next
elif [ -f /etc/SuSE-release ]
2009-09-28 02:59:07 +00:00
then
#SLES9 and SLES10, uses /etc/sysconfig/network/ifcfg-eth-id-<mac>
#SLES11, uses /etc/sysconfig/network/ifcfg-eth<x>
NICFILEPRE="/etc/sysconfig/network/ifcfg-"
echo `hostname` > /etc/HOSTNAME
2010-02-24 08:27:58 +00:00
if [ ! -z "$defgw" ]; then
echo "default $defgw - -" > /etc/sysconfig/routes
fi
2009-09-28 02:59:07 +00:00
else
#RedHat uses /etc/sysconfig/network-scripts/ifcfg-eth<x>
NICFILEPRE="/etc/sysconfig/network-scripts/ifcfg-"
2014-05-28 14:27:41 +00:00
if [ -f "/etc/hostname" ]; then # for rh7
echo `hostname` >/etc/hostname
else
sed -i "s/HOSTNAME.*/HOSTNAME=`hostname`/" /etc/sysconfig/network
fi
2010-02-24 08:27:58 +00:00
if [ ! -z "$defgw" ]; then
echo "GATEWAY=$defgw" >> /etc/sysconfig/network
fi
2009-09-28 02:59:07 +00:00
fi
2014-05-14 12:01:41 +00:00
for nic in `ip link |grep "BROADCAST" |awk '{print $2}' | sed s/://`; do
IPADDRMASK=`ip addr show dev $nic | grep inet | grep -v inet6 | awk '{print $2}' | head -n 1`
IPADDR=`echo $IPADDRMASK | awk -F'/' '{print $1}'`
2015-03-16 04:54:56 +00:00
[ -z $IPADDR ] && continue
2014-05-14 12:01:41 +00:00
PREFIXMASK=`echo $IPADDRMASK | awk -F'/' '{print $2}'`
# converts to x.x.x.x mask value
maskfromprefix $PREFIXMASK
2011-09-16 18:53:32 +00:00
if ( pmatch $OSVER "ubuntu*" )
then
NETWORK=`network_ipv4calc $IPADDR $NETMASK`
2014-06-13 14:37:18 +00:00
#BROADCAST=`ifconfig $nic | grep Bcast | awk '{print $3}' | awk -F: '{print $2}'`
BROADCAST=`ip -4 -oneline addr show $nic|grep brd| awk -F ' ' '{print $6}'`
2011-09-16 18:53:32 +00:00
if [ ! -z "$defgw" ]; then
gateway_line="gateway $defgw"
else
gateway_line=""
fi
2014-05-14 12:01:41 +00:00
# add info to interfaces file on ubuntu, TBD does unbuntu change to systemd, this will not exist
2011-09-16 18:53:32 +00:00
cat >>/etc/network/interfaces <<EOF
auto $nic
iface $nic inet static
address $IPADDR
network $NETWORK
netmask $NETMASK
broadcast $BROADCAST
$gateway_line
EOF
2014-05-14 12:01:41 +00:00
# not ubuntu
2011-09-16 18:53:32 +00:00
else
if [ -f ${NICFILEPRE}${nic} ]
then
NICFILE=${NICFILEPRE}${nic}
else
2014-05-14 12:01:41 +00:00
#mac=`ifconfig $nic|grep HWaddr|awk '{print $5}'|tr "[A-Z]" "[a-z]"`
2015-03-16 04:54:56 +00:00
mac=`ip link show $nic | grep "ether\|infiniband" | awk '{print $2}'`
[ -z $mac ] && continue
2011-09-16 18:53:32 +00:00
NICFILE=${NICFILEPRE}eth-id-${mac}
fi
sed -i s/BOOTPROTO=dhcp/BOOTPROTO=static/ $NICFILE
sed -i s/BOOTPROTO=\'dhcp\'/BOOTPROTO=static/ $NICFILE
sed -i s/BOOTPROTO=\"dhcp\"/BOOTPROTO=static/ $NICFILE
2014-11-07 02:27:43 +00:00
grep ^IPADDR= $NICFILE >/dev/null
if [ $? -eq 0 ]
then
sed -i '/IPADDR=/d' $NICFILE
fi
2011-09-16 18:53:32 +00:00
echo IPADDR=$IPADDR >> $NICFILE
2014-11-07 02:27:43 +00:00
grep ^NETMASK= $NICFILE >/dev/null
if [ $? -eq 0 ]
then
sed -i '/NETMASK=/d' $NICFILE
fi
echo NETMASK=$NETMASK >> $NICFILE
2011-09-16 18:53:32 +00:00
fi
2012-05-21 20:10:32 +00:00
#for netboot/statelite case, restart the network interface. For diskful installation, it is not necessary because the restart of the network will happen at the first boot.
if [ "$NODESETSTATE" = "netboot" ] || [ "$NODESETSTATE" = "statelite" ]
then
ifdown $nic
ifup $nic
fi
2008-03-07 21:24:09 +00:00
done
2011-09-16 18:53:32 +00:00
if ( pmatch $OSVER "ubuntu*")
then
cat >>/etc/network/interfaces <<EOF
auto lo
iface lo inet loopback
EOF
fi
2012-05-21 20:10:32 +00:00
2012-11-23 02:34:18 +00:00
exit 0