#!/bin/bash
#write: davis
# description: build dubble netcard bond0 on CentOS 6.4 mode 1
# args: $0 eth0 eth1
#[ -z $2 ] && [ -z $1 ] && echo "please input two network name"&&exit 0 ||echo "args ok"
#### check args
#if [ -z $2 ]
#then
#echo -e "please input two network name \n for example:11.sh eth0 eth1"
#exit 1
#fi
patth="/etc/sysconfig/network-scripts"
### test netcard
#cd $patth && cp ifcfg-$1{,bak} && cp ifcfg-$2{,bak}
cd $patth
grep -Eq "(em1|em2)" ifcfg-*
if [ $? -eq 0 ]
then
i1=em1
i2=em2
else
i1=eth0
i2=eth1
fi
echo $i1 $i2
# 0 bak
cp ifcfg-${i1} /opt/ && cp ifcfg-${i2} /opt/
##1 add bonding file
[ -f /etc/modprobe.d/bonding.conf ] && echo "bonding file exist" ||
cat > /etc/modprobe.d/bonding.conf << EOF
alias bond0 bonding
options bond0 miimon=100 mode=1
EOF
##2 bond0 configure file
aip=`cat $patth/ifcfg-$i1|grep IPADDR`
agw=`cat $patth/ifcfg-$i1|grep GATEWAY`
amk=`cat $patth/ifcfg-$i1|grep NETMASK`
cat > /etc/sysconfig/network-scripts/ifcfg-bond0 << GOF
DEVICE=bond0
BOOTPROTO=static
ONBOOT=yes
TYPE=Ethernet
USERCTL=no
GOF
echo -e "$aip\n$agw\n$amk" >> $patth/ifcfg-bond0
##3 eth0 eth1 configure file
for j in $i1 $i2
do
cat > $patth/ifcfg-$j << AOF
ONBOOT=yes
BOOTPROTO=none
MASTER=bond0
SLAVE=yes
USERCTL=no
AOF
echo "DEVICE=$j" >> $patth/ifcfg-$j
done
#4 modprobe
modprobe bonding
#5 network restart
/etc/init.d/network restart
#6 check bond0
num=`cat /proc/net/bonding/bond0 |grep "Slave Interface"|wc -l`
if [ $num -eq 2 ]
then
echo "bond0 success!"
else
echo $num
echo "bond0 failed"
fi
##7 reboot
#reboot
bond mode 请自行查阅。 我用的是mode=1 active-backup mode=0 LB rr
brctl addif br100 eth0
brctl delif br0 eth0
bond之后,可以选择reboot或/etc/init.d/network restart
如果涉及到宿主机,必须要重启,如果重启network 有可能由于异常问题,如arp刷新、广播风暴等导致宿主机网络中断,仍需要重启。亲身体会呀。还是以系统稳定为大局。bond重启吧。
不涉及到宿主机的,也尽量重启一下。 稳定是我们的根本。