ATAE SuSE系统绑定双网卡Shell脚本

Posted by Harid六月 - 15 - 2013 Leave comments

ATAE单板因为高可靠的需求经常需要进行双网卡绑定,而且ATAE单板中的网卡存在Fabric平面、Base平面、Updata平面网卡之分,其中Fabric平面网卡一般用来做业务层面的网卡,Base平面网卡一般用来做管理层面的网卡,同一个机框中各业务板Base平面通过背板连至交换板实现互通。各个网卡分属于哪个平面可以通过/var/adm/autoinstall/scripts/eth_alias.sh脚本获取。

下面是一个的ATAE单板中实现双网卡绑定的Shell脚本,基于已经配置好Fabric、Base平面IP的基础上,将实现bond0网卡绑定Fabric平面的两个网卡,bond1网卡绑定Base平面两个网卡。

具体内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/bash
####
##
## Author: http://www.timepoet.com
## Date: 2013-05-30
##
####
 
### ###
function get_char(){
	echo ""
	echo "Press any key to continue, CTRL+C to quit..."
	SAVEDSTTY=`stty -g`
	stty -echo
	stty cbreak
	dd if=/dev/tty bs=1 count=1 2> /dev/null
	stty -raw
	stty echo
	stty $SAVEDSTTY
}
 
function bondNIC(){
	if [ -e /etc/sysconfig/network/ifcfg-bond0 ] || [ -e /etc/sysconfig/network/ifcfg-bond1 ];then
		echo "There already exist some bond NIC."
		ls -l /etc/sysconfig/network/ifcfg-bond*
		echo ""
		get_char
	fi
	clear
	echo "Start to bond the NIC..."
	workDir=`pwd`
	/var/adm/autoinstall/scripts/eth_alias.sh > ${workDir}/NICInfo.out
	cat ${workDir}/NICInfo.out | awk '{print $1}' > ${workDir}/NIC.out
	/var/adm/autoinstall/scripts/eth_alias.sh | grep "Fabric" | awk '{print $1}' > ${workDir}/fabric.out
	/var/adm/autoinstall/scripts/eth_alias.sh | grep "Base" | awk '{print $1}' > ${workDir}/base.out
	if [ -e ${workDir}/IP.out ];then
		rm -rf ${workDir}/IP.out
	fi
	if [ -e ${workDir}/eth.out ];then
		rm -rf ${workDir}/eth.out
	fi
	for name in `cat ${workDir}/NIC.out`
	do
		IP=`ifconfig ${name} | grep "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" | awk '{print $2}' | cut -d ":" -f2`
		if [ "X${IP}" != "X" ];then
			echo ${IP} >> ${workDir}/IP.out
			echo ${name} >> ${workDir}/eth.out
			if [ `cat ${workDir}/fabric.out | grep ${name} | wc -l` -gt 0 ];then
				F_IP="${IP}"
				F_NIC="${name}"
			elif [ `cat ${workDir}/base.out | grep ${name} | wc -l` -gt 0 ];then
				B_IP="${IP}"
				B_NIC="${name}"
			else
				F_IP=""
				F_NIC=""
				B_IP=""
				B_NIC=""
			fi
		fi
	done
 
	echo "Fabric IP is  : ${F_IP}"
	echo "Fabric NIC is : ${F_NIC}"
	echo "Base IP is    : ${B_IP}"
	echo "Base NIC is   : ${B_NIC}"
 
	if [ "X${F_IP}" == "X" ];then
		echo "Pls check fabric level IP configuration."
		get_char
	fi
	if [ "X${B_IP}" == "X" ];then
		echo "Pls check base level IP configuration."
		get_char
	fi
 
	cd /etc/sysconfig/network
	ls ifcfg-eth* > ${workDir}/config_t.out
	cat ${workDir}/config_t.out | grep -v "bak" > ${workDir}/config.out
	rm -f ${workDir}/config_t.out
	for name in `cat ${workDir}/config_t.out`
	do
		mv ${name} ${name}.bak
	done
	rm -f ${workDir}/config_t.out
 
	for ((i=0;i<=1;i=i+1))
	do
		touch ifcfg-bond${i}
		if [ $i == 0 ];then
			MAINIP=`echo ${F_IP}`
			NIC_M=`echo {F_NIC}`
			NIC_S=`cat ${workDir}/fabric.out | grep -v "${F_NIC}"`
		else
			MAINIP=`echo ${B_IP}`
			NIC_M=`echo {B_NIC}`
			NIC_S=`cat ${workDir}/base.out | grep -v "${B_NIC}"`
		fi
		echo "OTPROTO='static'" >> ifcfg-bond${i}
		echo "IPADDR='`echo ${MAINIP}`'" >> ifcfg-bond${i}
		echo "NETMASK='255.255.255.0'" >> ifcfg-bond${i}
		echo "STARTMODE='auto'" >> ifcfg-bond${i}
		echo "BONDING_MASTER='yes'" >> ifcfg-bond${i}
		echo "BONDING_MODULE_OPTS='mode=1 millmon=200 use_carrier=1'" >> ifcfg-bond${i}
		echo "BONDING_SLAVE0='`echo ${NIC_M}`'" >> ifcfg-bond${i}
		echo "BONDING_SLAVE1='`echo ${NIC_S}`'" >> ifcfg-bond${i}
		echo "BROADCAST=''" >> ifcfg-bond${i}
		echo "ETHTOOL_OPTIONS=''" >> ifcfg-bond${i}
		echo "MTU=''" >> ifcfg-bond${i}
		echo "NAME=''" >> ifcfg-bond${i}
		echo "NETWORK=''" >> ifcfg-bond${i}
		echo "REMOTE_IPADDR=''" >> ifcfg-bond${i}
		echo "USERCONTROL='no'" >> ifcfg-bond${i}
	done
 
	cd ${workDir}
	rm -f ${workDir}/IP.out ${workDir}/NIC.out ${workDir}/NICInfo.out ${workDir}/fabric.out ${workDir}/base.out ${workDir}/eth.out ${workDir}/config.out
	echo "Already complete bonding NIC. Do you wanna continue to restart network service?"
	get_char
	rcnetwork restart > /dev/null 2>&1
}
 
bondNIC
exit 0

   声明:本文采用 BY-NC-SA 协议进行授权 | 星期九
   原创文章转载请注明:转自《ATAE SuSE系统绑定双网卡Shell脚本


分享按钮