2014年新的一年春天来了,又是一年春来到,烟花三月好风光,好久没发表文章了,今天在这里记录一下KVM网卡绑定安装脚本,供大家参考。
一、实施环境:
硬件设备:H3C S5500三层交换机+DELLR720服务器
虚拟化软件:KVM虚拟化
二、脚本内容:
脚本基于服务器四块网卡绑定,网卡流量聚合叠加,总和为4G,模拟测试任意down掉一块网卡、两块网卡对物理机上的虚拟机集群没有任何影响。直接上脚本如下:
#!/bin/sh #Auto Make KVM Virtualization #Author wugk 2013-12-06 bond0+br0 #Auto config bond scripts eth_bond() { NETWORK=( HWADDR=`ifconfig eth0 |egrep "HWaddr|Bcast" |tr "\n" " "|awk '{print $5,$7,$NF}'|sed -e 's/addr://g' -e 's/Mask://g'|awk '{print $1}'` IPADDR=`ifconfig eth0 |egrep "HWaddr|Bcast" |tr "\n" " "|awk '{print $5,$7,$NF}'|sed -e 's/addr://g' -e 's/Mask://g'|awk '{print $2}'` NETMASK=`ifconfig eth0 |egrep "HWaddr|Bcast" |tr "\n" " "|awk '{print $5,$7,$NF}'|sed -e 's/addr://g' -e 's/Mask://g'|awk '{print $3}'` GATEWAY=`route -n|grep "UG"|awk '{print $2}'` ) DIR=/etc/sysconfig/network-scripts/ BAK=/data/backup/`date +%Y%m%d` if [ ! -d $BAK ];then mkdir -p $BAK cp $DIR/ifcfg-eth* $BAK else cp $DIR/ifcfg-eth* $BAK fi cat >ifcfg-bond0<<EOF DEVICE=bond0 BOOTPROTO=static ${NETWORK[1]} ${NETWORK[2]} ${NETWORK[3]} ONBOOT=yes TYPE=Ethernet NM_CONTROLLED=no EOF yes|cp ifcfg-bond0 ifcfg-eth0 ifcfg-eth1 ifcfg-eth2 ifcfg-eth3 $DIR yes|cp modprobe.conf /etc/modprobe.d/ echo "The Server bond0 config success !!" } #Auto install kvm and config br0 scripts kvm_install() { cat <<EOF ++++++++++++++++Welcome To Use Auto Install KVM Scripts ++++++++++++++++++ +++++++++++++++++++++++++This KVM Install Virtual ++++++++++++++++++++++++ +++++++++++++++++++++++++2013-12-06 Author wugk ++++++++++++++++++++++++++ EOF KVM_SOFT=( kvm python-virtinst libvirt bridge-utils virt-manager qemu-kvm-tools virt-viewer virt-v2v libguestfs-tools ) NETWORK=( HWADDR=`ifconfig bond0 |egrep "HWaddr|Bcast" |tr "\n" " "|awk '{print $5,$7,$NF}'|sed -e 's/addr://g' -e 's/Mask://g'|awk '{print $1}'` IPADDR=`ifconfig bond0 |egrep "HWaddr|Bcast" |tr "\n" " "|awk '{print $5,$7,$NF}'|sed -e 's/addr://g' -e 's/Mask://g'|awk '{print $2}'` NETMASK=`ifconfig bond0 |egrep "HWaddr|Bcast" |tr "\n" " "|awk '{print $5,$7,$NF}'|sed -e 's/addr://g' -e 's/Mask://g'|awk '{print $3}'` GATEWAY=`route -n|grep "UG"|awk '{print $2}'` ) #Check whether the system supports virtualization egrep 'vmx|svm' /proc/cpuinfo >>/dev/null if [ "$?" -eq "0" ];then echo 'Congratulations, your system success supports virtualization !' else echo -e 'OH,your system does not support virtualization !\nPlease modify the BIOS virtualization options (Virtualization Technology)' exit 0 fi if [ -e /usr/bin/virsh ];then echo "Virtualization is already installed ,Please exit ...." ;exit 0 fi yum -y install ${KVM_SOFT[@]} /sbin/modprobe kvm ln -s /usr/libexec/qemu-kvm /usr/bin/qemu-kvm lsmod | grep kvm >>/dev/null if [ "$?" -eq "0" ];then echo 'KVM installation is successful !' else echo 'KVM installation is falis,Please check ......' exit 1 fi cd /etc/sysconfig/network-scripts/ mkdir -p /data/backup/`date +%Y%m%d-%H:%M` yes|cp ifcfg-eth* /data/backup/`date +%Y%m%d-%H:%M`/ if [ -e /etc/sysconfig/network-scripts/ifcfg-br0 ];then echo "The ifcfg-br0 already exist ,Please wait exit ......" exit 2 else cat >ifcfg-bond0 <<EOF DEVICE=bond0 BOOTPROTO=none #${NETWORK[0]} NM_CONTROLLED=no ONBOOT=yes TYPE=Ethernet BRIDGE="br0" ${NETWORK[1]} ${NETWORK[2]} ${NETWORK[3]} USERCTL=no EOF cat >ifcfg-br0 <<EOF DEVICE="br0" BOOTPROTO=none #${NETWORK[0]} IPV6INIT=no NM_CONTROLLED=no ONBOOT=yes TYPE="Bridge" ${NETWORK[1]} ${NETWORK[2]} ${NETWORK[3]} USERCTL=no EOF fi echo 'Your can restart Ethernet Service: /etc/init.d/network restart !' echo '---------------------------------------------------------' sleep 1 echo 'Your can restart KVM Service : /etc/init.d/libvirtd restart !' echo echo -e "You can create a KVM virtual machine: \nvirt-install --name=centos01 --ram 512 --vcpus=1 --disk path=/data/kvm/centos01.img,size=7,bus=virtio --accelerate --cdrom /data/iso/centos58.iso --vnc --vncport=5910 --vnclisten=0.0.0.0 --network bridge=br0,model=virtio --noautoconsole" } case $1 in eth_bond ) eth_bond ;; kvm_install ) kvm_install ;; * ) echo "Usage: { $0 eth_bond|kvm_install|help}" esac
三、网卡内容:
修改之后ifcfg-eth0配置内容如下,其他三块网卡依次修改为对应的网卡名称,内容跟eth0一致:
DEVICE=eth0 BOOTPROTO=none ONBOOT=yes MASTER=bond0 SLAVE=yes TYPE=Ethernet NM_CONTROLLED=no
四、H3C S5500交换机配置:
服务器端聚合配置完毕后,还需要在交换机把相应的4个端口做网络聚合,命令如下:
system-view interface Bridge-Aggregation 100 link-aggregation mode dynamic quit interface GigabitEthernet1/0/1 port link-aggregation group 100 quit interface GigabitEthernet1/0/2 port link-aggregation group 100 quit interface GigabitEthernet1/0/3 port link-aggregation group 100 quit interface GigabitEthernet1/0/4 port link-aggregation group 100 quit
参考文章:
http://navyaijm.blog.51cto.com/4647068/1316910
http://wgkgood.blog.51cto.com/1192594/1337628