Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1648331
  • 博文数量: 82
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 0
  • 用 户 组: 普通用户
  • 注册时间: 2017-12-09 12:58
文章分类

全部博文(82)

文章存档

2011年(7)

2010年(3)

2009年(11)

2008年(4)

2007年(57)

我的朋友

分类: LINUX

2011-12-09 17:40:29

很奇怪呀,一群研发要做bond0绑定br0,在分配剩余ETH*,居然让用shell去写,我晕, 而且需求相当恶心,给了我一个DOC文档,自己设计逻辑做,还要2天搞定。发上来大家研究吧,不管如何我做了。
 
先看看需求吧。
 

只装前台:

1、一块网卡,不处理,直接为这个网卡分配ip

2、两块网卡,把两块网卡做一个bond0,分给portal(管理)

 

portal_ip(管理)地址写入/etc/ivic/vswitch_netcard.yaml,格式为portal_ip=172.30.39.61

 

只装后台:

1、一块网卡,将eth0绑定到br0(网桥),存储和vm(业务)共用eth0

2、两块网卡,将eth0eth1绑定到bond0,将bond0绑定到br0,存储和vm(业务)共用bond0

3、三块网卡,将eth0eth1绑定到bond0,将bond0绑定到br0vm(业务)使用bond0,存储使用eth2

4、四块网卡,将eth0eth1绑定到bond0,将bond0绑定到br0vm(业务)使用bond0

eth2eth3绑定到bond1,存储使用bond1

 

管理)vstore_ip(存储)地址写入/etc/ivic/vswitch_netcard.yaml,格式为portal_ip=172.30.39.61, vstore_ip=172.30.39.62

 

同时装前台和后台:

1、一块网卡,将eth0绑定到br0(网桥) vm(业务)、存储和portal(管理)共用eth0

2、两块网卡,将eth0eth1绑定到bond0,将bond0绑定到br0vm(业务)、存储和portal(管理)共用bond0

3、三块网卡,将eth0eth1绑定到bond0,将bond0绑定到br0vm(业务)和存储使用bond0portal使用eth2

4、四块网卡,将eth0eth1绑定到bond0,将bond0绑定到br0vm(业务)使用bond0,如果用户选择绑定eth2eth3,存储和portal(管理)共用bond1,如果不绑定,存储使用eth2portal使用eth3

4、五块网卡,将eth0eth1绑定到bond0,将bond0绑定到br0vm(业务)使用bond0,如果用户选择绑定eth2eth3,存储使用bond1portal使用eth4,如果不绑定,存储使用eth2portal使用eth3 eth4不配置

5、六块网卡,将eth0eth1绑定到bond0,将bond0绑定到br0vm(业务)使用bond0;如果用户选择绑定eth2eth3,存储使用bond1,如果不绑定eth2eth3,存储使用eth2

如果用户选择绑定eth3eth4portal使用bond2 portal5不配置,如果不绑定eth3eth4portal使用eth3eth4eth5不配置

 

portal_ip(管理)、service_ip(业务)vstore_ip(存储)地址写入/etc/ivic/vswitch_netcard.yaml,格式为portal_ip=172.30.39.61, vstore_ip=172.30.39.62service_ip=172.30.39.63

 

 

 

下面是我写的shell, 只因需求总变、还断断续续给我,靠,弄的脚本没有模块化,全部模块化就要大动作。大家讲究看吧。

 

#!/bin/bash
# By Nick.Ma
# DATE:2011-12-07
# The progarm for bonding test with interface.

APT_DIR="/etc/ivic"
API_FILE="/etc/ivic/vswitch_netcard.yaml"
INIT_CHK () {
  if [ $UID -ne "0" ] ; then
    echo -e "Please Login with root."
  fi

  if [  -z /etc/udev/rules.d/70-persistent-net.rules ];then
    echo "The file no exist."
  fi

  dpkg -l | grep ifenslave > /dev/null
  if [ $? -ne "0" ]; then
    `which apt-get` install -y ifenslave
  fi
  dpkg -l | grep "bridge-utils" > /dev/null
  if [ $? -ne "0" ]; then
    `which apt-get` install -y  bridge-utils
  fi
  if [ -z "$API_FILE" ] ;then
    touch $API_FILE
    chmod +x $API_FILE
  fi

}
INIT_CHK
`which clear`

NET_CONF_END () {
  cp -rf /root/interfaces /etc/network/interfaces
  if [ $(cat /etc/modules | grep -c "bonding mode=6 miimon=100")  -lt "1" ] ; then
    sh -c "echo bonding mode=6 miimon=100 >> /etc/modules"
  fi
 # if [ $(cat /etc/rc.local | grep -c "bonding mode=6 miimon=100")] ; then
  #  echo -e ""modprobe bonding mode=6 miimon=100 " >> /etc/rc.local"
  #fi
 # /etc/init.d/networking restart > /dev/null
  echo -n "Ohyee ! finish .  Now do you want to reboot your machine ? "
  read -p  "If you want reboot machine ,Please enter to "yes".Enter any key to nothing."  yes
  case $yes in
        yes )
          echo "System reboot starting......"
        ;;
        * )
          echo "Enter any key to nothing."
        ;;
  esac
}

ETH0_BR0_CONF () {
        echo "Please enter br0 IP address:  "
        read BR0_IP
        echo "Please enter br0 netmask:  "
        read BR0_NETMASK
        echo "Please enter br0 network:  "
        read BR0_GATEWAY
}

ETH0_CONF () {
        echo "Please enter eth0 IP address:  "
        read eth0_IP
        echo "Please enter eth0 netmask:  "
        read eth0_NETMASK
        echo "Please enter eth0 network:  "
        read eth0_GATEWAY
 echo -e "auto eth0\niface eth0 inet static\n\taddress $eth0_IP\n\tnetmask $eth0_NETMASK\n\tnetwork $eth0_NETWORK\n" >> /root/interfaces
}


BOND0_CONF () {
    echo "Please enter bond0 IP address:  "
    read BOND_IP
    echo "Please enter bond0 netmask:  "
    read BOND_NETMASK
    echo "Please enter bond0 network:  "
    read BOND_NETWORK
    echo -e "auto bond0\niface bond0 inet static\n\taddress $BOND_IP\n\tnetmask $BOND_NETMASK\n\tnetwork $BOND_NETWORK\n" >> /root/interfaces
#    read -p  "Please enter bond0 interaction interfaces.Example:[eth0] [eth1] : " ETHA ETHB
    echo -e "\tpost-up ifenslave bond0 eth0 eth1\n\tpre-down ifenslave -d bond0 eth0 eth1 \n" >> /root/interfaces
}


BOND0_BR0_CONF () {
    echo "Please enter br0 IP address:  "
    read BR0_IP
    echo "Please enter br0 netmask:  "
    read BR0_NETMASK
    echo "Please enter br0 network:  "
    read BR0_NETWORK
    echo -e "auto bond0\niface bond0 inet manual\n\tpost-up ifenslave bond0 eth2 eth3\n\tpre-down ifenslave -d bond0 eth2 eth3 \n\nauto br0\niface br0 inet static\n\taddress $BR0_IP\n\tnetmask $BR0_NETMASK\n\tnetwork $BR0_NETWORK\n\tbridge_ports bond0\n\tbridge_maxwait 0\n\tbridge_fd 0" >> /root/interfaces
    sh -c "echo bonding mode=6 miimon=100 >> /etc/modules"

#    read -p  "Please enter bond0 interaction interfaces.Example:[eth0] [eth1] : " ETHA ETHB
    #echo -e "\tpost-up ifenslave bond0 eth0 eth1\n\tpre-down ifenslave -d bond0 eth0 eth1 \n" >> /root/interfaces
}

ETH2_CONF () {
        echo "Please enter eth2 IP address:  "
        read ETH2_IP
        echo "Please enter eth2 netmask:  "
        read ETH2_NETMASK
        echo "Please enter eth0 network:  "
        read eth2_NETWORK
        echo -e "auto eth2\niface eth2 inet static\n\taddress $ETH2_IP\n\tnetmask $ETH2_NETMASK\n\tnetwork $ETH2_NETWORK\n" >> /root/interfaces

}

ETH5_CONF () {
        echo "Please enter eth5 IP address:  "
        read ETH5_IP
        echo "Please enter eth5 netmask:  "
        read ETH5_NETMASK
        echo "Please enter eth5 network:  "
        read eth5_NETWORK
        echo -e "auto eth5\niface eth5 inet static\n\taddress $ETH5_IP\n\tnetmask $ETH5_NETMASK\n\tnetwork $ETH5_NETWORK\n" >> /root/interfaces

}
ETH3_CONF () {
        echo "Please enter eth3 IP address:  "
        read ETH3_IP
        echo "Please enter eth3 netmask:  "
        read ETH3_NETMASK
        echo "Please enter eth3 network:  "
        read eth3_GATEWAY
        echo -e "auto eth3\niface eth3 inet static\n\taddress $ETH3_IP\n\tnetmask $ETH3_NETMASK\n\tnetwork $ETH3_NETWORK\n" >> /root/interfaces

}

FLUSH_API_FILE () {
   mv $API_FILE $API_FILE.OLD
   touch $API_FILE
   echo -e "portal_ip=\nvstore_ip=\nservice_ip=\n" > $API_FILE
}
FLUSH_API_FILE

cp -rf /etc/network/interfaces /root/
ETH_MAX=$(cat /etc/udev/rules.d/70-persistent-net.rules | grep -v "^#" | grep -c "eth*")
#echo $ETH_MAX
ETH_NAME=$(cat /etc/udev/rules.d/70-persistent-net.rules | grep -v "^#" | awk -F "," '{print $8}' | cut -c 8-11 | grep -v "^$"^C)

if [ $ETH_MAX -gt "2" ] && [ $ETH_MAX -lt "6" ]; then
  echo -e "Dear account. If interface have more than two,So you must configure bond0.\n"
  BOND0_BR0_CONF
  sed -i 's/portal_ip=/portal_ip='"$BR0_IP"'/g' $API_FILE
  ACT_NUM=1
  STA_NUM=1
  if [ $ETH_MAX -gt "3" ] ;then
    while [[ $STA_NUM -le $ACT_NUM ]]
      do
     STA_NUM=$STA_NUM
#     echo "true.it is $STA_NUM"
   echo -e "This machine have $ETH_MAX Nic interface.\n"
   echo -n "Are you sure configure bond${STA_NUM} interface ?"
   read -p "Do you want enter any key go on with jobs. If you want exit .  enter "yes": "  SURE
   case $SURE in
     yes )
       #break  # 255 code is choose exit.
       echo -e "\n"
       ETH2_CONF
              sed -i 's/vstore_ip=/vstore_ip='"$ETH2_IP"'/g' $API_FILE
       echo -e "\n"
       ETH3_CONF
              sed -i 's/service_ip=/service_ip='"$ETH3_IP"'/g' $API_FILE
       break
     ;;
     * )
       echo "Nothing." >> /dev/null
     ;;
   esac
     echo "Please enter bond${STA_NUM} IP address:  "
   read BOND_IP
     echo "Please enter bond${STA_NUM} netmask:  "
   read BOND_NETMASK
     echo "Please enter bond$STA_NUM network:  "
   read BOND_NETWORK
     echo -e "auto bond${STA_NUM}\niface bond${STA_NUM} inet static\n\taddress $BOND_IP\n\tnetmask $BOND_NETMASK\n\tnetwork $BOND_NETWORK\n" >> /root/interfaces
   read -p  "Please enter bond${STA_NUM} interaction interfaces.Example:[eth2] [eth3] : " ETHA ETHB
   echo -e "\tpost-up ifenslave bond${STA_NUM} $ETHA $ETHB\n\tpre-down ifenslave -d bond${STA_NUM} $ETHA $ETHB\n" >> /root/interfaces
   sleep 1
   let STA_NUM=$STA_NUM+1
       sed -i 's/vstore_ip=/vstore_ip='"$BOND_IP"'/g' $API_FILE
   sleep 1
   ETH5_CONF
       sed -i 's/service_ip=/service_ip='"$ETH5_IP"'/g' $API_FILE
 
    done
  else
        echo "Please enter eth2 IP address:  "
        read ETH2_IP
        echo "Please enter eth2 netmask:  "
        read ETH2_NETMASK
        echo "Please enter eth0 network:  "
        #read eth2_GATEWAY
        echo -e "auto eth2\niface eth2 inet static\n\taddress $ETH2_IP\n\tnetmask $ETH2_NETMASK\n\tnetwork\n" >> /root/interfaces
 #sed -i 's//portal_ip='"$ETH2_IP"'/g' $API_FILE
 PORTAL_IP=$(grep "portal_ip" /etc/ivic/vswitch_netcard.yaml)
 sed -i 's/'"$PORTAL_IP"'/portal_ip='"$ETH2_IP"'/g' /etc/ivic/vswitch_netcard.yaml
 sleep 1
 # 3 ethN choose
 sed -i 's/service_ip=/service_ip='"$BR0_IP"'/g' $API_FILE
 sed -i 's/vstore_ip=/vstore_ip='"$BR0_IP"'/g' $API_FILE
  fi
  if [ $(cat /etc/modules | grep -c "bonding mode=6 miimon=100")  -lt "1" ] ; then
    sh -c "echo bonding mode=6 miimon=100 >> /etc/modules"
  fi
  NET_CONF_END
 # if [ $(cat /etc/rc.local | grep -c "bonding mode=6 miimon=100")] ; then
  #  echo -e ""modprobe bonding mode=6 miimon=100 " >> /etc/rc.local"
  #fi
 # /etc/init.d/networking restart > /dev/null
 # echo -n "Ohyee ! finish .  Now do you want to reboot your machine ? "
 # read -p  "If you want reboot machine ,Please enter to "yes".Enter any key to nothing."  yes
 # case $yes in
# yes )
#      echo "System reboot starting......"
# ;;
# * )
#   echo "Enter any key to nothing."
# ;;
#  esac
  cp -rf /root/interfaces /etc/network/interfaces
else
  if [ $ETH_MAX -eq "2" ] ; then
    echo "Please enter bond${STA_NUM} IP address:  "
    read BOND_IP
    echo "Please enter bond${STA_NUM} netmask:  "
    read BOND_NETMASK
    echo "Please enter bond$STA_NUM network:  "
    read BOND_NETWORK
    echo -e "auto bond0\niface bond0 inet static\n\taddress $BOND_IP\n\tnetmask $BOND_NETMASK\n\tnetwork $BOND_NETWORK\n" >> /root/interfaces
#    read -p  "Please enter bond0 interaction interfaces.Example:[eth0] [eth1] : " ETHA ETHB
    echo -e "\tpost-up ifenslave bond0 eth0 eth1\n\tpre-down ifenslave -d bond0 eth0 eth1 \n" >> /root/interfaces 
    sed -i 's/portal_ip=/portal_ip='"$BOND_IP"'/g' $API_FILE
    sed -i 's/vstore_ip=/vstore_ip='"$BOND_IP"'/g' $API_FILE
    sed -i 's/service_ip=/service_ip='"$BOND_IP"'/g' $API_FILE
    NET_CONF_END
  else
    ETH0_IP=$(cat /etc/network/interfaces  | sed '/iface eth0/{n;s/address//p}' -n | grep -v "^$")
    if [ -z $ETH_IP ] ;then
      if [ -n "`cat /etc/network/interfaces | grep "auto eth0"`" ] ; then
        ETH0_BR0_CONF
 echo -e  "auto eth0\niface eth0 inet manual\n\taddress $BR0_IP\n\tnetmask $BR0_NETMASK\n\tbridge_ports bond0\n" >> /boot/interfaces 
        sed -i 's/portal_ip=/portal_ip='"$BR0_IP"'/g' $API_FILE
        sed -i 's/vstore_ip=/vstore_ip='"$BR0_IP"'/g' $API_FILE
        sed -i 's/service_ip=/service_ip='"$BR0_IP"'/g' $API_FILE
      fi
      NET_CONF_END
    else
      ETH0_BR0_CONF
      #cat /etc/network/interfaces | sed ''
      #sed -i '/case/{n;s/yes/aa/}'
      echo -e "auto br0\niface br0 inet static\n\taddress $BR0_IP\n\tnetmask $BR0_NETMASK\n\tbridge_ports bond0\n" > /root/interfaces
      sed -i 's/portal_ip=/portal_ip='"$BR0_IP"'/g' $API_FILE
      sed -i 's/vstore_ip=/vstore_ip='"$BR0_IP"'/g' $API_FILE
      sed -i 's/service_ip=/service_ip='"$BR0_IP"'/g' $API_FILE
      NET_CONF_END 
    fi
  fi
  cp -rf /root/interfaces /etc/network/interfaces
fi

 

 

# Foreground config
FOREROUND_CONF () {
  case $ETH_MAX in
    2 )
    BOND_CONF
    NET_CONF_END
    cp -rf /root/interfaces /etc/network/interfaces
    sed -i 's/portal_ip=/portal_ip='"$BOND0_IP"'/g' $API_FILE
    ;;
    1 )
    ETH0_CONF   
    NET_CONF_END
    cp -rf /root/interfaces /etc/network/interfaces
    sed -i 's/portal_ip=/portal_ip='"$ETH0_IP"'/g' $API_FILE
    ;;
  esac
# >> /etc/ivic/vswitch_netcard.yaml
}
TIME=`date -R | awk '{print $5}'`
#mv /root/interfaces /tmp/interfaces_$TIME.tmp


#trap "cp -rf /root/interfaces /etc/network/interfaces ; mv /etc/ivic/vswitch_netcard.yaml.OLD /etc/ivic/vswitch_netcard.yaml" EXIT

阅读(1149) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~