工作中经常要为客户调整网卡绑定,为此制作了此脚本(比较粗糙,请见谅)。使用时请查看脚本帮助
bond.sh
#!/bin/bash usage() { cat << EOF create by [email protected] usage: $0 options OPTIONS: -h Show this message -i Bonding IP,NETMASK,GATEWAY(optional) -q Show Bonding Parameter Info -d Define NIC Name: eth1,eth2 or em1,em2(default primary is first,only mode 1) -m Bonding Mode:0,1,2,3,4,5,6 EOF } show() { cat << EOF Do not place parameters for the bonding kernel module in the /etc/modprobe.conf file. Instead, specify them as a space-separated list in the BONDING_OPTS="<bonding parameters>" directive in the ifcfg-bond<N> interface file. The only exception is the debug parameter, which cannot be used on a per-device basis, and which should therefore be specified in /etc/modprobe.conf as follows: options bonding debug=1 For further instructions and advice on configuring the bonding module, as well as to view the list of bonding parameters Parameters for the bonding kernel module must be specified as a space-separated list in the BONDING_OPTS="bonding parameters" directive in the ifcfg-bondN interface file. Do not specify options for the bonding device in /etc/modprobe.d/bonding.conf, or in the deprecated /etc/modprobe.conf file. For further instructions and advice on configuring the bonding module and to view the list of bonding parameters mode=<value> Allows you to specify the bonding policy. The <value> can be one of: balance-rr or 0 — Sets a round-robin policy for fault tolerance and load balancing. Transmissions are received and sent out sequentially on each bonded slave interface beginning with the first one available. active-backup or 1 — Sets an active-backup policy for fault tolerance. Transmissions are received and sent out via the first available bonded slave interface. Another bonded slave interface is only used if the active bonded slave interface fails. balance-xor or 2 — Sets an XOR (exclusive-or) policy for fault tolerance and load balancing. Using this method, the interface matches up the incoming request‘s MAC address with the MAC address for one of the slave NICs. Once this link is established, transmissions are sent out sequentially beginning with the first available interface. broadcast or 3 — Sets a broadcast policy for fault tolerance. All transmissions are sent on all slave interfaces. 802.3ad or 4 — Sets an IEEE 802.3ad dynamic link aggregation policy. Creates aggregation groups that share the same speed and duplex settings. Transmits and receives on all slaves in the active aggregator. Requires a switch that is 802.3ad compliant. balance-tlb or 5 — Sets a Transmit Load Balancing (TLB) policy for fault tolerance and load balancing. The outgoing traffic is distributed according to the current load on each slave interface. Incoming traffic is received by the current slave. If the receiving slave fails, another slave takes over the MAC address of the failed slave. balance-alb or 6 — Sets an Active Load Balancing (ALB) policy for fault tolerance and load balancing. Includes transmit and receive load balancing for IPV4 traffic. Receive load balancing is achieved through ARP negotiation. MASTER=bond-interface where bond-interface is the channel bonding interface to which the Ethernet interface is linked. This directive is used in conjunction with the SLAVE directive. NM_CONTROLLED=answer where answer is one of the following: yes — NetworkManager is permitted to configure this device. This is the default behavior and can be omitted. no — NetworkManager is not permitted to configure this device. SLAVE=answer where answer is one of the following: yes — This device is controlled by the channel bonding interface specified in the MASTER directive. no — This device is not controlled by the channel bonding interface specified in the MASTER directive. This directive is used in conjunction with the MASTER directive. USERCTL=answer where answer is one of the following: yes — Non-root users are allowed to control this device. no — Non-root users are not allowed to control this device. primary=<interface_name> Specifies the interface name, such as eth0, of the primary device. The primary device is the first of the bonding interfaces to be used and is not abandoned unless it fails. This setting is particularly useful when one NIC in the bonding interface is faster and, therefore, able to handle a bigger load. This setting is only valid when the bonding interface is in active-backup mode. Refer to /usr/share/doc/kernel-doc-<kernel-version>/Documentation/networking/bonding.txt for more information. primary_reselect=<value> Specifies the reselection policy for the primary slave. This affects how the primary slave is chosen to become the active slave when failure of the active slave or recovery of the primary slave occurs. This parameter is designed to prevent flip-flopping between the primary slave and other slaves. Possible values are: always or 0 (default) — The primary slave becomes the active slave whenever it comes back up. better or 1 — The primary slave becomes the active slave when it comes back up, if the speed and duplex of the primary slave is better than the speed and duplex of the current active slave. failure or 2 — The primary slave becomes the active slave only if the current active slave fails and the primary slave is up. The primary_reselect setting is ignored in two cases: If no slaves are active, the first slave to recover is made the active slave. When initially enslaved, the primary slave is always made the active slave. Changing the primary_reselect policy via sysfs will cause an immediate selection of the best active slave according to the new policy. This may or may not result in a change of the active slave, depending upon the circumstances miimon=<time_in_milliseconds> Specifies (in milliseconds) how often MII link monitoring occurs. This is useful if high availability is required because MII is used to verify that the NIC is active. EOF } set() { IP1=`echo $IP|cut -d "," -f1` MASK=`echo $IP|cut -d "," -f2` GATE=`echo $IP|cut -d "," -f3` if [ -z $GATE ] ; then GATE="" else GATE="GATEWAY=`echo $IP|cut -d "," -f3`" fi IF1=`echo $IF|cut -d "," -f1` IF2=`echo $IF|cut -d "," -f2` IF3=`echo $IF|cut -d "," -f3` IF4=`echo $IF|cut -d "," -f4` if [ $MODE = 1 ] ;then primary="primary=$IF1" fi echo " DEVICE=bond0 ONBOOT=yes BOOTPROTO=none USERCTL=no IPADDR=$IP1 NETMASK=$MASK $GATE BONDING_OPTS="miimon=100 mode=$MODE $primary" ">/etc/sysconfig/network-scripts/ifcfg-bond0 sed -i "s/BOOTPROTO.*$/BOOTPROTO=\"none\"/" /etc/sysconfig/network-scripts/ifcfg-$IF1 sed -i "s/BOOTPROTO.*$/BOOTPROTO=\"none\"/" /etc/sysconfig/network-scripts/ifcfg-$IF2 sed -i "s/ONBOOT.*$/ONBOOT=\"yes\"/" /etc/sysconfig/network-scripts/ifcfg-$IF1 sed -i "s/ONBOOT.*$/ONBOOT=\"yes\"/" /etc/sysconfig/network-scripts/ifcfg-$IF2 echo " MASTER=bond0 SLAVE=yes USERCTL=no ">>/etc/sysconfig/network-scripts/ifcfg-$IF1 echo " MASTER=bond0 SLAVE=yes USERCTL=no ">>/etc/sysconfig/network-scripts/ifcfg-$IF2 if [ -n $IF3 ] ;then sed -i "s/BOOTPROTO.*$/BOOTPROTO=\"none\"/" /etc/sysconfig/network-scripts/ifcfg-$IF3 sed -i "s/ONBOOT.*$/ONBOOT=\"yes\"/" /etc/sysconfig/network-scripts/ifcfg-$IF3 echo " MASTER=bond0 SLAVE=yes USERCTL=no ">>/etc/sysconfig/network-scripts/ifcfg-$IF3 fi if [ -n $IF4 ] ;then sed -i "s/BOOTPROTO.*$/BOOTPROTO=\"none\"/" /etc/sysconfig/network-scripts/ifcfg-$IF4 sed -i "s/ONBOOT.*$/ONBOOT=\"yes\"/" /etc/sysconfig/network-scripts/ifcfg-$IF4 echo " MASTER=bond0 SLAVE=yes USERCTL=no ">>/etc/sysconfig/network-scripts/ifcfg-$IF4 fi service network restart cat /proc/net/bonding/bond0 } if [ $# = 0 ] ;then usage exit fi while getopts hqi:d:m: OPTION do case $OPTION in h) usage exit 1 ;; q) show exit 1 ;; i) IP=$OPTARG ;; d) IF=$OPTARG ;; m) MODE=$OPTARG ;; ?) usage exit 1 ;; *) usage exit 1 ;; esac done #shift $((OPTIND-1)) #echo "opts [email protected]" if [ -z $IP ] ;then echo " Please Input IP,NETMASK,GATEWAY Example:bond.sh -i 192.168.1.2,255.255.255.0,192.168.1.1 -d eth0,eth1,eth3,eth4 " exit 1 fi if [ -z $IF ] ;then echo " Please Input NIC Name Example:bond.sh -d eth0,eth1,eth2,eth3 or em1,em2,em3,em4 " exit 1 fi if [ -z $MODE ] ;then MODE=1 fi set echo "cat /proc/net/bonding/bond0 show bond status"
CentOS 7 可以通过三种方式来实现
- nmtui #在界面里配置
- nmcli #请通过man nmcli查看帮助
- 通过修改网卡配置文件实现(理论上此脚本也可以,未做测试)
时间: 2024-10-29 09:13:46