Linux bonding config – bondit.sh

This is a quick script I wrote to configure files for NIC bonding. The first time I did this was for a 12 node cluster with both public and private network (so 24 bonds). After the first 5 servers I decided I needed a quicker safer way. Thanks KevinD,  BryanT,  BobyM, and JerryS for helping me out.

Note: This is rather simple and assumes the config of the file. You should verify the parameters satisfy your needs as I only include the basic needs.

RedHat 6 (change to bonding config)

#!/bin/bash

# bondit.sh <bondname> <first eth> <second eth>

echo;echo
echo Developed by Jed S. Walker - September 9th, 2011
echo version=20110912.b

echo
echo Warning!! If you have modified the original ifcfg files
echo this could cause issues. Be sure you have the original versions
echo hit enter to continue, or ctl-c to exit
read continue

export bond_val=$1
export eth1_val=$2
export eth2_val=$3

echo 
echo Backup files                      
cd /etc/modprobe.d
cp bonding.conf _bonding.conf.orig     
ls -l *bonding.conf*                   
cd /etc/sysconfig/network-scripts
cp "ifcfg-"$eth1_val "_ifcfg-"$eth1_val".orig"
cp "ifcfg-"$eth2_val "_ifcfg-"$eth2_val".orig"
ls -l *ifcfg*

echo If the backups look good then hit enter to continue or ctl-c to exit
read continue

echo 
echo Build ifcfg-$bond_val             
echo DEVICE=$bond_val                     > "ifcfg-"$bond_val
echo ONBOOT=yes                          >> "ifcfg-"$bond_val
echo BOOTPROTO=none                      >> "ifcfg-"$bond_val
echo USERCTL=no                          >> "ifcfg-"$bond_val
echo BONDING_OPTS=\"mode=1 miimon=100\"  >> "ifcfg-"$bond_val
grep IPADDR "_ifcfg-"$eth1_val".orig"    >> "ifcfg-"$bond_val
grep NETMASK "_ifcfg-"$eth1_val".orig"   >> "ifcfg-"$bond_val
grep GATEWAY "_ifcfg-"$eth1_val".orig"   >> "ifcfg-"$bond_val

echo
echo Add $bond_val to bonding.conf
echo "alias "$bond_val" bonding" >> /etc/modprobe.d/bonding.conf
echo now running "modprobe bonding..."
modprobe bonding

echo
echo Build ifcfg-$eth1_val
grep "^#" "_ifcfg-"$eth1_val".orig"     > "ifcfg-"$eth1_val
echo DEVICE=$eth1_val                  >> "ifcfg-"$eth1_val
echo USERCTL=no                        >> "ifcfg-"$eth1_val
echo ONBOOT=yes                        >> "ifcfg-"$eth1_val
echo BOOTPROTO=none                    >> "ifcfg-"$eth1_val
echo MASTER=$bond_val                  >> "ifcfg-"$eth1_val
echo SLAVE=yes                         >> "ifcfg-"$eth1_val
grep HWADDR "_ifcfg-"$eth1_val".orig"  >> "ifcfg-"$eth1_val

echo 
echo Build ifcfg-$eth2_val
grep "^#" "_ifcfg-"$eth2_val".orig"     > "ifcfg-"$eth2_val
echo DEVICE=$eth2_val                   > "ifcfg-"$eth2_val
echo USERCTL=no                        >> "ifcfg-"$eth2_val
echo ONBOOT=yes                        >> "ifcfg-"$eth2_val
echo BOOTPROTO=none                    >> "ifcfg-"$eth2_val
echo MASTER=$bond_val                  >> "ifcfg-"$eth2_val
echo SLAVE=yes                         >> "ifcfg-"$eth2_val
grep HWADDR "_ifcfg-"$eth2_val".orig"  >> "ifcfg-"$eth2_val

echo
echo Files by mod time
#cd /etc/
ls -lrt modprobe.d/bonding.*
#cd /etc/sysconfig/network-scripts
ls -lrt *ifcfg*

echo
echo Verify that the files above contain correct bond entries
echo The bond config file should have DEVICE=
echo The eth configs should have MASTER=
grep $bond_val ifcfg*

echo
echo Now running "service network restart"
echo If things lock up here you'll need to use iLO or console to reboot the server
service network restart
sleep 1
service network status

echo
echo When you have finished all bonding then run
echo ifconfig -a
echo It is then a good idea to reboot to make sure everything is clean

echo
echo Complete.

RedHat 5

#!/bin/bash

# bondit.sh <bondname> <first eth> <second eth>

echo;echo
echo Developed by Jed S. Walker - September 9th, 2011
echo version=20110912.b

echo
echo Warning!! If you have modified the original ifcfg files
echo this could cause issues. Be sure you have the original versions
echo hit enter to continue, or ctl-c to exit
read continue

export bond_val=$1
export eth1_val=$2
export eth2_val=$3

echo
echo Backup files
cd /etc/
cp modprobe.conf _modprobe.conf.orig
ls -l *modprobe.conf*
cd /etc/sysconfig/network-scripts
cp "ifcfg-"$eth1_val "_ifcfg-"$eth1_val".orig"
cp "ifcfg-"$eth2_val "_ifcfg-"$eth2_val".orig"
ls -l *ifcfg*

echo If the backups look good then hit enter to continue or ctl-c to exit
read continue

echo
echo Build ifcfg-$bond_val
echo DEVICE=$bond_val                     > "ifcfg-"$bond_val
echo ONBOOT=yes                          >> "ifcfg-"$bond_val
echo BOOTPROTO=none                      >> "ifcfg-"$bond_val
echo USERCTL=no                          >> "ifcfg-"$bond_val
echo BONDING_OPTS=\"mode=1 miimon=100\"  >> "ifcfg-"$bond_val
grep IPADDR "_ifcfg-"$eth1_val".orig"    >> "ifcfg-"$bond_val
grep NETMASK "_ifcfg-"$eth1_val".orig"   >> "ifcfg-"$bond_val
grep GATEWAY "_ifcfg-"$eth1_val".orig"   >> "ifcfg-"$bond_val

echo
echo Add $bond_val to modprobe.conf
echo "alias "$bond_val" bonding" >> /etc/modprobe.conf
echo now running "modprobe bonding..."
modprobe bonding

echo
echo Build ifcfg-$eth1_val
grep "^#" "_ifcfg-"$eth1_val".orig"     > "ifcfg-"$eth1_val
echo DEVICE=$eth1_val                  >> "ifcfg-"$eth1_val
echo USERCTL=no                        >> "ifcfg-"$eth1_val
echo ONBOOT=yes                        >> "ifcfg-"$eth1_val
echo BOOTPROTO=none                    >> "ifcfg-"$eth1_val
echo NM_CONTROLLED=no                  >> "ifcfg-"$eth1_val
echo MASTER=$bond_val                  >> "ifcfg-"$eth1_val
echo SLAVE=yes                         >> "ifcfg-"$eth1_val
grep HWADDR "_ifcfg-"$eth1_val".orig"  >> "ifcfg-"$eth1_val

echo
echo Build ifcfg-$eth2_val
grep "^#" "_ifcfg-"$eth2_val".orig"     > "ifcfg-"$eth2_val
echo DEVICE=$eth2_val                   > "ifcfg-"$eth2_val
echo USERCTL=no                        >> "ifcfg-"$eth2_val
echo ONBOOT=yes                        >> "ifcfg-"$eth2_val
echo BOOTPROTO=none                    >> "ifcfg-"$eth2_val
echo NM_CONTROLLED=no                  >> "ifcfg-"$eth2_val
echo MASTER=$bond_val                  >> "ifcfg-"$eth2_val
echo SLAVE=yes                         >> "ifcfg-"$eth2_val
grep HWADDR "_ifcfg-"$eth2_val".orig"  >> "ifcfg-"$eth2_val

echo
echo Files by mod time
#cd /etc/
ls -lrt *modprobe*
#cd /etc/sysconfig/network-scripts
ls -lrt *ifcfg*

echo
echo Verify that the files above contain correct bond entries
echo The bond config file should have DEVICE=
echo The eth configs should have MASTER=
grep $bond_val ifcfg*

echo
echo Now running "service network restart"
echo If things lock up here you will need to use iLO or console to reboot the server
service network restart
sleep 1
service network status

echo
echo When you have finished all bonding then run
echo ifconfig -a
echo It is then a good idea to reboot to make sure everything is clean

echo
echo Complete.

2 responses to “Linux bonding config – bondit.sh

  1. Pingback: NIC/Link bonding/teaming/trunking/aggregation | Jed's

  2. Pingback: BONDING SCRIPT | Linux – IDEA

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s