#!/bin/bash
#
# script runs simple bonding services for eth1 to bond0 and also does reversion back to just eth1
# tested and created in fedora core 4
#

PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin
export PATH

case $1 in
  'bond0')
    # recopy bond0 files back to network scripts
     cp --reply yes /etc/sysconfig/network-scripts/bak/ifcfg-bond0-eth1 /etc/sysconfig/network-scripts/ifcfg-eth1 
     cp --reply yes /etc/sysconfig/network-scripts/bak/ifcfg-bond0 /etc/sysconfig/network-scripts/
     cp --reply yes /etc/sysconfig/network-scripts/bak/ifcfg-eth0 /etc/sysconfig/network-scripts/
     cp --reply yes /etc/sysconfig/network-scripts/bak/ifcfg-eth2 /etc/sysconfig/network-scripts/
     /sbin/route add default gw 172.16.1.1
     /etc/rc.d/init.d/network restart 
     /etc/rc.d/init.d/network status ; ifconfig bond0 ; ifconfig eth1 ; ifconfig eth0 ; ifconfig eth2 ; cat /proc/net/bonding/bond0 
     ip link show eth1 ; ip link show bond0 ; ip link show eth2
      ;;
  'original')
    #  recopy original files back to network scripts and drop bond0
     cp --reply yes /etc/sysconfig/network-scripts/bak/ifcfg-original-eth1 /etc/sysconfig/network-scripts/ifcfg-eth1
     rm /etc/sysconfig/network-scripts/ifcfg-bond0
     rm /etc/sysconfig/network-scripts/ifcfg-eth0
     rm /etc/sysconfig/network-scripts/ifcfg-eth2
     ifenslave -d bond0 eth1 
     ifconfig bond0 down
     /etc/rc.d/init.d/network restart
     /etc/rc.d/init.d/network status ; ifconfig eth1 ; ifconfig bond0 ; ifconfig eth2 ; cat /proc/net/bonding/bond0     
     ip link show eth1 ; ip link show bond0 ; iplink show eth0 ; ip link show eth2
      ;;
  'additional')
    # adding additional ifaces from command line
     if [ ! -f /etc/sysconfig/network-scripts/ifcfg-bond0 ]; then
         cp /etc/sysconfig/network-scripts/bak/bond_bond /etc/sysconfig/network-scripts/ifcfg-bond0
         echo BONDING_SLAVE=$2 >> /etc/sysconfig/network-scripts/ifcfg-bond0 
       else
         rm /etc/sysconfig/network-scripts/ifcfg-bond0
         cp /etc/sysconfig/network-scripts/bak/bond_bond /etc/sysconfig/network-scripts/ifcfg-bond0
         echo BONDING_SLAVE=$2 >> /etc/sysconfig/network-scripts/ifcfg-bond0
     fi     

     if [ ! -f /etc/sysconfig/network-scripts/ifcfg-eth1 ]; then
         cp /etc/sysconfig/network-scripts/bak/ifcfg-bond0-eth1 /etc/sysconfig/network-scripts/ifcfg-eth1
       else
         cp --reply yes /etc/sysconfig/network-scripts/bak/ifcfg-bond0-eth1 /etc/sysconfig/network-sscripts/ifcfg-eth1
     fi

     if [ ! -f /etc/sysconfig/network-scripts/ifcfg-$2 ]; then
         echo DEVICE=$2 >> /etc/sysconfig/network-scripts/ifcfg-$2
         cat /etc/sysconfig/network-scripts/bak/bond_eth >> /etc/sysconfig/network-scripts/ifcfg-$2
       else 
         echo ifcfg-$2 already exist - did not recreate
     fi
      ;;
  'status')
    # status to see if bond is up or not
     /etc/rc.d/init.d/network status ; ifconfig eth1 ; ifconfig bond0 ; ifconfig eth0 ; ifconfig eth2 ; cat /proc/net/bonding/bond0
     ip link show eth1 ; ip link show bond0 ; ip link show eth2
      ;;
  *) echo "usage: $0 <bond0|original|status|additional (ethx)>"
esac