Setting up Linux Ethernet Bonding

From DFWLPiki
Jump to: navigation, search

I finally made a script to set up bonding, because I got tired of visiting all these config files for each and every server I built.

note, that there is an assumption that eth0 is the current configured interface, because ip configuration is copied from eth0 over to bond0. this script could be modified for em1/em2 like on more modern hardware that is loaded on RHEL6. i will probably use this to determine the first 2 interfaces on the system, regardless if they are 'eth' or 'em':

ifconfig -a|grep Link|cut -c 1-5|sed 2q >> /tmp/interfaces; cat /tmp/interfaces; rm -rf /tmp/interfaces

One challenge is to determine what the current active interface on a system is, and this might be a viable way:

ifconfig|grep Ethernet|cut -c 1-5 >> /tmp/activeinterface; cat /tmp/activeinterface; rm -rf /tmp/activeinterface

Anyway, here is the script for a system with just 'eth' interfaces:

#!/bin/bash
echo "alias bond0 bonding" >> /etc/modprobe.d/bonding.conf
echo "DEVICE=bond0" >> /etc/sysconfig/network-scripts/ifcfg-bond0
cat /etc/sysconfig/network-scripts/ifcfg-eth0 | egrep "GATEWAY|DNS1|HOSTNAME|IPADDR|NETMASK|ONBOOT" >> /etc/sysconfig/network-scripts/ifcfg-bond0
echo "BONDING_OPTS=\"mode=1 primary=eth0  miimon=100\"" >> /etc/sysconfig/network-scripts/ifcfg-bond0

cat /etc/sysconfig/network-scripts/ifcfg-eth0 | egrep "DEVICE|HWADDR" >> /tmp/ifcfg-eth0
echo "SLAVE=yes" >> /tmp/ifcfg-eth0
echo "MASTER=bond0" >> /tmp/ifcfg-eth0
echo "ONBOOT=yes" >> /tmp/ifcfg-eth0
echo "USERCTL=no" >> /tmp/ifcfg-eth0
rm -rf /etc/sysconfig/network-scripts/ifcfg-eth0
cp /tmp/ifcfg-eth0 /etc/sysconfig/network-scripts/

cat /etc/sysconfig/network-scripts/ifcfg-eth1 | egrep "DEVICE|HWADDR" >> /tmp/ifcfg-eth1
echo "SLAVE=yes" >> /tmp/ifcfg-eth1
echo "MASTER=bond0" >> /tmp/ifcfg-eth1
echo "ONBOOT=yes" >> /tmp/ifcfg-eth1
echo "USERCTL=no" >> /tmp/ifcfg-eth1
rm -rf /etc/sysconfig/network-scripts/ifcfg-eth1
cp /tmp/ifcfg-eth1 /etc/sysconfig/network-scripts/

rm -rf /tmp/ifcfg-eth*

then, just

/etc/init.d/network restart

and your done (or even add it to the end of the script if you want :)