LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 847|回复: 0

SLES9下的双网卡绑定

[复制链接]
发表于 2005-12-22 13:56:19 | 显示全部楼层 |阅读模式

  1. #!/bin/bash

  2. #双网卡绑定使用方法

  3. #将bond脚本复制到/etc/init.d/下,确保权限为755
  4. #确保在Yast里最好不要对需要绑定的网卡作设置,如果有,将设备删除,使之处于未设置的状态。
  5. #根据自身需要修改以下变量,然后用chkconfig命令将bond服务在3,5运行级别启用。
  6. #然后到/etc/init.d/rc3.d,和/etc/init.d/rc5.d/下检查bond服务是否运行在network
  7. #之后,如果不是,则需要手工链接。
  8. #bond_ip    需要设置的IP
  9. #bond_netmask    子网掩码
  10. #bond_gw    缺省网关
  11. #bond_mode    绑定的模式
  12. #host_route_ip    主机路由的IP,如果没有,留空
  13. #host_route_dev    主机路由的设备

  14. #dev_list    需要绑定的设备名称,用空格分开
  15. #primary_eth    主设备名称

  16. bond_ip=192.168.1.18
  17. bond_netmask=255.255.255.0
  18. bond_gw=192.168.1.1
  19. bond_mode=active-backup
  20. #bond_mode=balance_rr
  21. host_route_ip=192.168.2.1
  22. host_route_dev=bond0

  23. dev_list="eth0 eth1"
  24. primary_eth="eth0"
  25. mii_val=100
  26. dev_num=`echo $dev_list | awk '{print NF}'`

  27. function wait_real_interfaces() {
  28.     local max_wait=120
  29.     local wait_count=0
  30.     local chk_dev_count=0
  31.     local dev=""

  32.     while [ 0 ];do
  33.         chk_dev_count=0
  34.         #Check each device in the list
  35.         for dev in $dev_list;do
  36.             chk_eth=`cat /proc/net/dev | awk '{print $1}' | grep "$dev:" | sed 's/:[0-9]*//' | wc -l`
  37.             if [ $chk_eth -eq 1 ];then
  38.                 chk_dev_count=$[chk_dev_count+1]
  39.             fi
  40.         done

  41.         if [ $wait_count -eq $max_wait ];then
  42.             echo "Max waits reached,abort to wait real interfaces!"
  43.             exit 1
  44.         fi

  45.         if [ $chk_dev_count -eq $dev_num ];then
  46.             return 0
  47.         fi

  48.         wait_count=$[wait_count+1]
  49.         echo "Bonding network : waiting for real interfaces,retry at $wait_count,max waits is $max_wait"
  50.         sleep 1
  51.     done
  52. }

  53. function check_real_interfaces() {
  54.     local chk_eth=0
  55.     local chk_dev_count=0

  56.     for dev in $dev_list;do
  57.         chk_eth=`ifconfig -a $dev | grep $dev | awk '{print $1}' | wc -l`
  58.         if [ $chk_eth -eq 1 ];then
  59.             chk_dev_count=$[chk_dev_count+1]
  60.         fi
  61.     done

  62.     if [ $chk_dev_count -eq $dev_num ];then
  63.         return 0
  64.     fi

  65.     return 1
  66. }

  67. function check_bond_device() {
  68.     local chk_num=$[dev_num+1]

  69.     chk_bond=`ifconfig | grep "$bond_ip" | wc -l`
  70.     if [ $chk_bond -eq $chk_num ];then
  71.         echo "--------------------------------------------------------------------------------"
  72.         echo "Bond IP : $bond_ip , Netmask : $bond_netmask , Gateway : $bond_gw"
  73.         echo "Bonding network devices are successfully setup!"
  74.         echo "--------------------------------------------------------------------------------"
  75.         return 0
  76.     else
  77.         echo "Bonding network device is NOT setup correctly!"
  78.         return 1
  79.     fi
  80. }

  81. function init_bond_device() {
  82.     local dev=""
  83.     modprobe bonding mode=$bond_mode miimon=$mii_val primary=$primary_eth
  84.     ifconfig bond0 inet $bond_ip netmask $bond_netmask
  85.     if [ -n "$host_route_ip" -a -n "$host_route_dev" ];then
  86.         route add -host $host_route_ip dev $host_route_dev
  87.     fi
  88.     route add default gw $bond_gw

  89.     stop_real_devices

  90.     for dev in $dev_list;do
  91.         ifconfig $dev up
  92.         ifenslave bond0 $dev
  93.     done

  94.     ifconfig bond0 up
  95. }

  96. function start_bond_device() {
  97.     local max_attempt=30
  98.     local attempt_count=0
  99.     local interval=3
  100.    
  101.     while [ 0 ];do
  102.         wait_real_interfaces
  103.         if [ $? -eq 0 ];then
  104.             echo "All real network devices are initialized!"
  105.         fi

  106.         check_real_interfaces
  107.         if [ $? -eq 1 ];then
  108.             continue
  109.         fi

  110.         echo "Wait for $interval seconds to bind real interface..."
  111.         sleep $interval
  112.         init_bond_device

  113.         check_bond_device
  114.         if [ $? -eq 0 ];then
  115.             break
  116.         else
  117.             stop_bond_device
  118.             attempt_count=$[attempt_count+1]
  119.             echo "Binding real interfaces,retry at $attempt_count,max attempts is $max_attempt"
  120.             if [ $attempt_count -eq $max_attempt ];then
  121.                 echo "Max attempt retry reached,abort to setup bonding network!"
  122.                 exit 1
  123.             fi
  124.             continue
  125.         fi
  126.     done
  127. }

  128. function stop_real_devices() {
  129.     local dev=""

  130.     #stop all devices in the list
  131.     for dev in $dev_list;do
  132.        ifdown $dev 2>&1 > /dev/null
  133.     done

  134.     #stop every real devices that may be configured as the same ip address
  135.     for dev in `cat /proc/net/dev | grep eth | sed 's/:.*//'`;do
  136.         chk_ip=`ifconfig $dev | grep $bond_ip | wc -l`
  137.         if [ $chk_ip -eq 1 ];then
  138.             ifdown $dev 2>&1 > /dev/null
  139.         fi
  140.         chk_ip=0
  141.     done
  142. }

  143. function stop_bond_device() {
  144.     local dev=""

  145.     chk_bond=`ifconfig | grep bond`
  146.     if [ -z "$chk_bond" ];then
  147.         echo "Bonding device is not up,no need to stop"
  148.         return 0
  149.     fi

  150.     echo "Stopping bonding devices..."
  151.     for dev in $dev_list;do
  152.         ifenslave -d bond0 $dev
  153.     done

  154.     stop_real_devices

  155.     ifconfig bond0 down
  156.     rmmod bonding
  157. }


  158. case $1 in
  159. #Initialize bonding device,try every times until everything is OK
  160.     start)
  161.         start_bond_device
  162.         ;;

  163. #Unbind and stop the bonding & real network interfaces
  164.     stop)
  165.         stop_bond_device
  166.         ;;

  167. #Reset the bonding devices
  168.     restart)
  169.         stop_bond_device
  170.         start_bond_device
  171.         ;;
  172. #Show status for bonding device
  173.     status)
  174.         check_bond_device
  175.         ;;
  176.     *)
  177.         echo "Usage:`basename $0` [ start | stop | restart | status]"
  178.         exit 0
  179.         ;;
  180. esac

  181. exit 0
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表