|
|
- #!/bin/bash
- #双网卡绑定使用方法
- #将bond脚本复制到/etc/init.d/下,确保权限为755
- #确保在Yast里最好不要对需要绑定的网卡作设置,如果有,将设备删除,使之处于未设置的状态。
- #根据自身需要修改以下变量,然后用chkconfig命令将bond服务在3,5运行级别启用。
- #然后到/etc/init.d/rc3.d,和/etc/init.d/rc5.d/下检查bond服务是否运行在network
- #之后,如果不是,则需要手工链接。
- #bond_ip 需要设置的IP
- #bond_netmask 子网掩码
- #bond_gw 缺省网关
- #bond_mode 绑定的模式
- #host_route_ip 主机路由的IP,如果没有,留空
- #host_route_dev 主机路由的设备
- #dev_list 需要绑定的设备名称,用空格分开
- #primary_eth 主设备名称
- bond_ip=192.168.1.18
- bond_netmask=255.255.255.0
- bond_gw=192.168.1.1
- bond_mode=active-backup
- #bond_mode=balance_rr
- host_route_ip=192.168.2.1
- host_route_dev=bond0
- dev_list="eth0 eth1"
- primary_eth="eth0"
- mii_val=100
- dev_num=`echo $dev_list | awk '{print NF}'`
- function wait_real_interfaces() {
- local max_wait=120
- local wait_count=0
- local chk_dev_count=0
- local dev=""
- while [ 0 ];do
- chk_dev_count=0
- #Check each device in the list
- for dev in $dev_list;do
- chk_eth=`cat /proc/net/dev | awk '{print $1}' | grep "$dev:" | sed 's/:[0-9]*//' | wc -l`
- if [ $chk_eth -eq 1 ];then
- chk_dev_count=$[chk_dev_count+1]
- fi
- done
- if [ $wait_count -eq $max_wait ];then
- echo "Max waits reached,abort to wait real interfaces!"
- exit 1
- fi
- if [ $chk_dev_count -eq $dev_num ];then
- return 0
- fi
- wait_count=$[wait_count+1]
- echo "Bonding network : waiting for real interfaces,retry at $wait_count,max waits is $max_wait"
- sleep 1
- done
- }
- function check_real_interfaces() {
- local chk_eth=0
- local chk_dev_count=0
- for dev in $dev_list;do
- chk_eth=`ifconfig -a $dev | grep $dev | awk '{print $1}' | wc -l`
- if [ $chk_eth -eq 1 ];then
- chk_dev_count=$[chk_dev_count+1]
- fi
- done
- if [ $chk_dev_count -eq $dev_num ];then
- return 0
- fi
- return 1
- }
- function check_bond_device() {
- local chk_num=$[dev_num+1]
- chk_bond=`ifconfig | grep "$bond_ip" | wc -l`
- if [ $chk_bond -eq $chk_num ];then
- echo "--------------------------------------------------------------------------------"
- echo "Bond IP : $bond_ip , Netmask : $bond_netmask , Gateway : $bond_gw"
- echo "Bonding network devices are successfully setup!"
- echo "--------------------------------------------------------------------------------"
- return 0
- else
- echo "Bonding network device is NOT setup correctly!"
- return 1
- fi
- }
- function init_bond_device() {
- local dev=""
- modprobe bonding mode=$bond_mode miimon=$mii_val primary=$primary_eth
- ifconfig bond0 inet $bond_ip netmask $bond_netmask
- if [ -n "$host_route_ip" -a -n "$host_route_dev" ];then
- route add -host $host_route_ip dev $host_route_dev
- fi
- route add default gw $bond_gw
- stop_real_devices
- for dev in $dev_list;do
- ifconfig $dev up
- ifenslave bond0 $dev
- done
- ifconfig bond0 up
- }
- function start_bond_device() {
- local max_attempt=30
- local attempt_count=0
- local interval=3
-
- while [ 0 ];do
- wait_real_interfaces
- if [ $? -eq 0 ];then
- echo "All real network devices are initialized!"
- fi
- check_real_interfaces
- if [ $? -eq 1 ];then
- continue
- fi
- echo "Wait for $interval seconds to bind real interface..."
- sleep $interval
- init_bond_device
- check_bond_device
- if [ $? -eq 0 ];then
- break
- else
- stop_bond_device
- attempt_count=$[attempt_count+1]
- echo "Binding real interfaces,retry at $attempt_count,max attempts is $max_attempt"
- if [ $attempt_count -eq $max_attempt ];then
- echo "Max attempt retry reached,abort to setup bonding network!"
- exit 1
- fi
- continue
- fi
- done
- }
- function stop_real_devices() {
- local dev=""
- #stop all devices in the list
- for dev in $dev_list;do
- ifdown $dev 2>&1 > /dev/null
- done
- #stop every real devices that may be configured as the same ip address
- for dev in `cat /proc/net/dev | grep eth | sed 's/:.*//'`;do
- chk_ip=`ifconfig $dev | grep $bond_ip | wc -l`
- if [ $chk_ip -eq 1 ];then
- ifdown $dev 2>&1 > /dev/null
- fi
- chk_ip=0
- done
- }
- function stop_bond_device() {
- local dev=""
- chk_bond=`ifconfig | grep bond`
- if [ -z "$chk_bond" ];then
- echo "Bonding device is not up,no need to stop"
- return 0
- fi
- echo "Stopping bonding devices..."
- for dev in $dev_list;do
- ifenslave -d bond0 $dev
- done
- stop_real_devices
- ifconfig bond0 down
- rmmod bonding
- }
- case $1 in
- #Initialize bonding device,try every times until everything is OK
- start)
- start_bond_device
- ;;
- #Unbind and stop the bonding & real network interfaces
- stop)
- stop_bond_device
- ;;
- #Reset the bonding devices
- restart)
- stop_bond_device
- start_bond_device
- ;;
- #Show status for bonding device
- status)
- check_bond_device
- ;;
- *)
- echo "Usage:`basename $0` [ start | stop | restart | status]"
- exit 0
- ;;
- esac
- exit 0
复制代码 |
|