一个bonding的脚本

#!/bin/bash
bond_ip=192.168.59.9                            #bond0的ip地址
bond_netmask=255.255.255.0                      #bond0的子网掩码
bond_gw=192.168.65.62                           #网关
bond_mode=active-backup                         #bonding的工作模式,active-backup为互备模式,balance_rr为负载均衡模式
dev_list="eth1 eth2"                            #bonding绑定的网络接口
primary_eth="eth1"                              #bonding绑定的的主网络接口
mii_val=100
dev_num=`echo $dev_list | awk '{print NF}'`

function wait_real_interfaces()
{
        local max_wait=180
        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
                for dev in $dev_list ; do
                ifenslave bond0 $dev
        done
        ifconfig bond0 up
    route add default gw $bond_gw

}

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_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
                ifdown $dev
        done
        #stop the other 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
                fi
                chk_ip=0
        done
        ifconfig bond0 down
        rmmod bonding
}

case $1 in
start)          #Initialize bonding device,try every times until everything is OK
        start_bond_device       ;;
stop)           #Unbind and stop the bonding & real network interfaces
        stop_bond_device        ;;
status)         #Show status for bonding device
        check_bond_device       ;;
*)
        echo "Usage:`basename $0` [ start | stop | restart | status]"
        exit 1  ;;
esac
exit 0

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/16566331/viewspace-670108/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/16566331/viewspace-670108/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值