bondingの設定がめんどうなのでスクリプト化した

Linuxのbonding設定がめんどうなのでスクリプト化しました。


エラー処理のない乱暴なスクリプトなので、初期インストール時以外で使わないようにー。
PXEブートインストールに組み込んでしまえると楽かなぁ。

使い方

[root@cent ~]# ./set_bonding.sh
Usage: ./set_bonding.sh bond ip mask eth...
[root@cent ~]# ./set_bonding.sh bond0 192.168.0.1 255.255.255.0 eth0 eth2
[root@cent ~]# reboot
[root@cent ~]# ifconfig
bond0     Link encap:Ethernet  HWaddr 00:01:80:60:6B:84
          inet addr:192.168.0.1  Bcast:192.168.0.255  Mask:255.255.255.0
...
eth0      Link encap:Ethernet  HWaddr 00:01:80:60:6B:84
...
eth2      Link encap:Ethernet  HWaddr 00:01:80:60:6B:84
...

ソースコード

#!/bin/bash
if [ $# -lt 5 ];then
  echo "Usage: $0 bond ip mask eth..."
  exit
fi

bond=$1;shift
ip=$1;shift
mask=$1;shift
eths=("$@")

#/etc/modprobe.conf
cat <<END >>/etc/modprobe.conf

alias $bond bonding
options $bond mode=1
options $bond miimon=100
END

#ifcfg files
cd /etc/sysconfig/network-scripts
backup_dir=backup.`date +\%Y\%m\%d`
mkdir $backup_dir

cat <<END > ifcfg-$bond
DEVICE=$bond
BOOTPROTO=none
IPADDR=$ip
NETMASK=$mask
ONBOOT=yes
END

for eth in ${eths[@]};do
  ifcfg=ifcfg-$eth
  /bin/mv $ifcfg $backup_dir
  egrep 'DEVICE|HWADDR' $backup_dir/$ifcfg > $ifcfg
  cat <<END >>$ifcfg
ONBOOT=yes
BOOTPROTO=none
MASTER=$bond
SLAVE=yes
END
done