您的位置:首页 > 运维架构 > Shell

CentOS6.2 创建Bond和Bridge接口ShellScript实例

2012-07-26 15:41 281 查看
在配置KVM或者XEN虚拟化环境时均要使用到Bridge接口为虚拟机提供网络;在生产环境中为了提高网络可用性一般会采取双网卡Bond技术;下面脚本是自动配置Bond接口或者Bond+Brideg接口的应用实例,有需要的朋友可以借鉴。

此shell脚本中将bond配置为mode=4模式,此模式需要交换机支持LACP并配置链路聚合组后才能和操作系统配合生效,如果交换机不支持此功能可以将脚本中的bond配置改为mode=0或mode=1模式。

Bond mode 参数的详细解释请看我的另一篇文章(/article/4316144.html

#!/bin/bash

# Program:
#     This program is create bond interface and bond + bridge interface.
# History:
#     2012/06/21 RolandQu First release
export PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"
confDir="/etc/sysconfig/network-scripts"
backConfDir="/etc/sysconfig/ifcfg.bak.$(date +%Y%m%d)"

# 打印帮助信息
# vm 参数创建bond+bridge接口 并创建私网IP子接口
# pm 参数只创建bond接口 并创建私网IP子接口
# ip mask参数为空时从网卡配置中读取信息
help() {
cat >> /dev/stdout <<EOF

Usage: $(basename $0) { vm | pm }[ public ip address ] [ network mask ]
Example: ./$(basename $0) vm 112.112.112.2 255.255.255.248
Example: ./$(basename $0) pm 10.168.1.1 255.255.255.0

Example: ./$(basename $0) vm
EOF
exit 1
}

# 检查IP地址和子网掩码是否符合规范
checkIp() {
if ! echo $pubIp | grep '[0-9]\{1,3\}\(\.[0-9]\{1,3\}\)\{3\}' > /dev/null; then
echo -e "\n public ip address input error! \n"
exit 1
fi
if ! echo $pubMask | grep '[0-9]\{1,3\}\(\.[0-9]\{1,3\}\)\{3\}' > /dev/null; then
echo -e "\n public netmask input error! \n"
exit 1
fi
}

# 修改eth*网卡配置文件
create_eth() {
mv -f $confDir/ifcfg-eth* $backConfDir
cat >> $confDir/ifcfg-eth0 <<EOF
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
USERCTL=no
MASTER=bond0
SLAVE=yes
EOF
cat >> $confDir/ifcfg-eth1 <<EOF
DEVICE=eth1
ONBOOT=yes
BOOTPROTO=static
USERCTL=no
MASTER=bond0
SLAVE=yes
EOF
}

# 创建bond配置文件 包括公网IP和私网IP子接口
create_bond() {
create_eth
mv -f $confDir/ifcfg-bond* $backConfDir
cat >> $confDir/ifcfg-bond0 <<EOF
DEVICE=bond0
ONBOOT=yes
BOOTPROTO=static
IPADDR=$pubIp
NETMASK=$pubMask
USERCTL=no
IPV6INIT=no

EOF
cat >> $confDir/ifcfg-bond0:1 <<EOF
DEVICE=bond0:1
ONBOOT=yes
BOOTPROTO=static
IPADDR=$priIp
NETMASK=$priMask
USERCTL=no
IPV6INIT=no

EOF
cat >> /etc/modprobe.d/bonding.conf <<EOF
alias bond0 bonding
options bond0 miimon=100 mode=4
EOF
}

#创建bridge配置文件 修改bond配置文件 包括公网IP和私网IP子接口
create_bridge() {
create_eth
mv -f $confDir/ifcfg-bond* $backConfDir 1>/dev/null 2>&1
mv -f $confDir/ifcfg-br* $backConfDir 1>/dev/null 2>&1
rm -f /etc/libvirt/qemu/networks/autostart/default.xml
cat >> $confDir/ifcfg-bond0 <<EOF
DEVICE=bond0
ONBOOT=yes
BOOTPROTO=static
USERCTL=no
BRIDGE=cloudbr0
EOF
cat >> /etc/modprobe.d/bonding.conf <<EOF
alias bond0 bonding
options bond0 miimon=100 mode=4
EOF
cat >> $confDir/ifcfg-cloudbr0 <<EOF
DEVICE=cloudbr0
ONBOOT=yes
BOOTPROTO=static
IPADDR=$pubIp
NETMASK=$pubMask
USERCTL=no
TYPE=Bridge
IPV6INIT=no

EOF
cat >> $confDir/ifcfg-cloudbr0:1 <<EOF
DEVICE=cloudbr0:1
ONBOOT=yes
BOOTPROTO=static
IPADDR=$priIp
NETMASK=$priMask
USERCTL=no
TYPE=Bridge
IPV6INIT=no

EOF
}

# 输入参数判断
if [ "$1" != "vm" ] && [ "$1" != "pm" ]; then
help
fi
if [ -n "$2" ] && [ -z "$3" ]; then
help
fi
if [ -n "$2" ] && [ -n "$3" ]; then
pubIp=$2
pubMask=$3
checkIp
# 定义私网IP地址和子网掩码
priIp=192.168.$(echo $pubIp | awk -F "." '{print $3"."$4}')
priMask=255.255.0.0
fi

# 如果IP和子网掩码参数没有输入 将从ifcfg-eth0配置文件中读取
if [ -z "$2" ] && [ -z "$3" ]; then
pubIp=$(grep 'IPADDR' $confDir/ifcfg-eth0 | awk -F "=" '{print $2}')
pubMask=$(grep 'NETMASK' $confDir/ifcfg-eth0 | awk -F "=" '{print $2}')
checkIp
# 定义私网IP地址和子网掩码
priIp=192.168.$(echo $pubIp | awk -F "." '{print $3"."$4}')
priMask=255.255.0.0
fi

if [ -n "$pubIp" ] && [ -n "$pubMask" ]; then
test -d $backConfDir || mkdir -p $backConfDir
if [ "$1" == "vm" ]; then
create_bridge
elif [ "$1" == "pm" ]; then
create_bond
fi

echo -e "\n create bonding succeed! \n please manual reboot network or server! \n"
else
echo -e "\n program error! please retry. \n"
exit 1
fi


本文出自 “运维进行时” 博客,请务必保留此出处http://rolandqu.blog.51cto.com/3477736/943224
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: