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

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

    Bond mode 参数的详细解释请看我的另一篇文章(http://rolandqu.blog.51cto.com/3477736/943269

 
  
  1. #!/bin/bash 
  2.  
  3. # Program:
  4. # This program is create bond interface and bond + bridge interface.
  5. # History:
  6. # 2012/06/21 RolandQu First release 
  1. export PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin" 
  2. confDir="/etc/sysconfig/network-scripts" 
  3. backConfDir="/etc/sysconfig/ifcfg.bak.$(date +%Y%m%d)" 
  4.  
  5. # 打印帮助信息
  6. # vm 参数创建bond+bridge接口 并创建私网IP子接口
  7. # pm 参数只创建bond接口 并创建私网IP子接口
  8. # ip mask参数为空时从网卡配置中读取信息
  9. help() { 
  10.     cat >> /dev/stdout <<EOF 
  11. Usage: $(basename $0) { vm | pm }[ public ip address ] [ network mask ] 
  12. Example: ./$(basename $0) vm 112.112.112.2 255.255.255.248 
  13. Example: ./$(basename $0) pm 10.168.1.1 255.255.255.0 
  14. Example: ./$(basename $0) vm 
  15. EOF 
  16.     exit 1 
  17.  
  18. # 检查IP地址和子网掩码是否符合规范
  19. checkIp() { 
  20.     if ! echo $pubIp | grep '[0-9]\{1,3\}\(\.[0-9]\{1,3\}\)\{3\}' > /dev/nullthen 
  21.         echo -e "\n public ip address input error! \n" 
  22.         exit 1 
  23.     fi 
  24.     if ! echo $pubMask | grep '[0-9]\{1,3\}\(\.[0-9]\{1,3\}\)\{3\}' > /dev/nullthen 
  25.         echo -e "\n public netmask input error! \n" 
  26.         exit 1 
  27.     fi 
  28.  
  29. # 修改eth*网卡配置文件
  30. create_eth() { 
  31.     mv -f $confDir/ifcfg-eth* $backConfDir 
  32.     cat >> $confDir/ifcfg-eth0 <<EOF 
  33. DEVICE=eth0 
  34. ONBOOT=yes 
  35. BOOTPROTO=static 
  36. USERCTL=no 
  37. MASTER=bond0 
  38. SLAVE=yes 
  39. EOF 
  40.     cat >> $confDir/ifcfg-eth1 <<EOF 
  41. DEVICE=eth1 
  42. ONBOOT=yes 
  43. BOOTPROTO=static 
  44. USERCTL=no 
  45. MASTER=bond0 
  46. SLAVE=yes 
  47. EOF 
  48.  
  49. # 创建bond配置文件 包括公网IP和私网IP子接口
  50. create_bond() { 
  51.     create_eth 
  52.     mv -f $confDir/ifcfg-bond* $backConfDir 
  53.     cat >> $confDir/ifcfg-bond0 <<EOF 
  54. DEVICE=bond0 
  55. ONBOOT=yes 
  56. BOOTPROTO=static 
  57. IPADDR=$pubIp 
  58. NETMASK=$pubMask 
  59. USERCTL=no 
  60. IPV6INIT=no  
  61. EOF 
  62.     cat >> $confDir/ifcfg-bond0:1 <<EOF 
  63. DEVICE=bond0:1 
  64. ONBOOT=yes 
  65. BOOTPROTO=static 
  66. IPADDR=$priIp 
  67. NETMASK=$priMask 
  68. USERCTL=no 
  69. IPV6INIT=no 
  70. EOF 
  71.     cat >> /etc/modprobe.d/bonding.conf <<EOF 
  72. alias bond0 bonding 
  73. options bond0 miimon=100 mode=4 
  74. EOF 
  75.  
  76. #创建bridge配置文件 修改bond配置文件 包括公网IP和私网IP子接口
  77. create_bridge() { 
  78.     create_eth 
  79.     mv -f $confDir/ifcfg-bond* $backConfDir 1>/dev/null 2>&1 
  80.     mv -f $confDir/ifcfg-br* $backConfDir 1>/dev/null 2>&1 
  81.     rm -f /etc/libvirt/qemu/networks/autostart/default.xml 
  82.     cat >> $confDir/ifcfg-bond0 <<EOF 
  83. DEVICE=bond0 
  84. ONBOOT=yes 
  85. BOOTPROTO=static 
  86. USERCTL=no 
  87. BRIDGE=cloudbr0 
  88. EOF 
  89.     cat >> /etc/modprobe.d/bonding.conf <<EOF 
  90. alias bond0 bonding 
  91. options bond0 miimon=100 mode=4 
  92. EOF 
  93.     cat >> $confDir/ifcfg-cloudbr0 <<EOF 
  94. DEVICE=cloudbr0 
  95. ONBOOT=yes 
  96. BOOTPROTO=static 
  97. IPADDR=$pubIp 
  98. NETMASK=$pubMask 
  99. USERCTL=no 
  100. TYPE=Bridge 
  101. IPV6INIT=no 
  102. EOF 
  103.     cat >> $confDir/ifcfg-cloudbr0:1 <<EOF 
  104. DEVICE=cloudbr0:1 
  105. ONBOOT=yes 
  106. BOOTPROTO=static 
  107. IPADDR=$priIp 
  108. NETMASK=$priMask 
  109. USERCTL=no 
  110. TYPE=Bridge 
  111. IPV6INIT=no 
  112. EOF 
  113.  
  114. # 输入参数判断
  115. if [ "$1" != "vm" ] && [ "$1" != "pm" ]; then 
  116.     help 
  117. fi 
  118. if [ -n "$2" ] && [ -z "$3" ]; then 
  119.     help 
  120. fi 
  121. if [ -n "$2" ] && [ -n "$3" ]; then 
  122.     pubIp=$2 
  123.     pubMask=$3 
  124.     checkIp 
  125. # 定义私网IP地址和子网掩码
  126.     priIp=192.168.$(echo $pubIp | awk -F "." '{print $3"."$4}'
  127.     priMask=255.255.0.0 
  128. fi 
  129.  
  130. # 如果IP和子网掩码参数没有输入 将从ifcfg-eth0配置文件中读取
  131. if [ -z "$2" ] && [ -z "$3" ]; then 
  132.     pubIp=$(grep 'IPADDR' $confDir/ifcfg-eth0 | awk -F "=" '{print $2}'
  133.     pubMask=$(grep 'NETMASK' $confDir/ifcfg-eth0 | awk -F "=" '{print $2}'
  134.     checkIp 
  135. # 定义私网IP地址和子网掩码
  136.     priIp=192.168.$(echo $pubIp | awk -F "." '{print $3"."$4}'
  137.     priMask=255.255.0.0 
  138. fi 
  139.  
  140. if [ -n "$pubIp" ] && [ -n "$pubMask" ]; then 
  141.     test -d $backConfDir || mkdir -p $backConfDir 
  142.     if [ "$1" == "vm" ]; then 
  143.         create_bridge 
  144.     elif [ "$1" == "pm" ]; then 
  145.         create_bond 
  146.     fi 
  147.     echo -e "\n create bonding succeed! \n please manual reboot network or server! \n" 
  148. else 
  149.     echo -e "\n program error! please retry. \n" 
  150.     exit 1 
  151. fi