]>
Commit | Line | Data |
---|---|---|
178cd55f HZ |
1 | #!/bin/bash |
2 | ||
3 | # This example script creates bonding network devices based on synthetic NIC | |
4 | # (the virtual network adapter usually provided by Hyper-V) and the matching | |
5 | # VF NIC (SRIOV virtual function). So the synthetic NIC and VF NIC can | |
6 | # function as one network device, and fail over to the synthetic NIC if VF is | |
7 | # down. | |
8 | # | |
9 | # Usage: | |
10 | # - After configured vSwitch and vNIC with SRIOV, start Linux virtual | |
11 | # machine (VM) | |
12 | # - Run this scripts on the VM. It will create configuration files in | |
13 | # distro specific directory. | |
14 | # - Reboot the VM, so that the bonding config are enabled. | |
15 | # | |
16 | # The config files are DHCP by default. You may edit them if you need to change | |
17 | # to Static IP or change other settings. | |
18 | # | |
19 | ||
20 | sysdir=/sys/class/net | |
21 | netvsc_cls={f8615163-df3e-46c5-913f-f2d2f965ed0e} | |
22 | bondcnt=0 | |
23 | ||
24 | # Detect Distro | |
25 | if [ -f /etc/redhat-release ]; | |
26 | then | |
27 | cfgdir=/etc/sysconfig/network-scripts | |
28 | distro=redhat | |
29 | elif grep -q 'Ubuntu' /etc/issue | |
30 | then | |
31 | cfgdir=/etc/network | |
32 | distro=ubuntu | |
33 | elif grep -q 'SUSE' /etc/issue | |
34 | then | |
35 | cfgdir=/etc/sysconfig/network | |
36 | distro=suse | |
37 | else | |
38 | echo "Unsupported Distro" | |
39 | exit 1 | |
40 | fi | |
41 | ||
42 | echo Detected Distro: $distro, or compatible | |
43 | ||
44 | # Get a list of ethernet names | |
45 | list_eth=(`cd $sysdir && ls -d */ | cut -d/ -f1 | grep -v bond`) | |
46 | eth_cnt=${#list_eth[@]} | |
47 | ||
48 | echo List of net devices: | |
49 | ||
50 | # Get the MAC addresses | |
51 | for (( i=0; i < $eth_cnt; i++ )) | |
52 | do | |
53 | list_mac[$i]=`cat $sysdir/${list_eth[$i]}/address` | |
54 | echo ${list_eth[$i]}, ${list_mac[$i]} | |
55 | done | |
56 | ||
57 | # Find NIC with matching MAC | |
58 | for (( i=0; i < $eth_cnt-1; i++ )) | |
59 | do | |
60 | for (( j=i+1; j < $eth_cnt; j++ )) | |
61 | do | |
62 | if [ "${list_mac[$i]}" = "${list_mac[$j]}" ] | |
63 | then | |
64 | list_match[$i]=${list_eth[$j]} | |
65 | break | |
66 | fi | |
67 | done | |
68 | done | |
69 | ||
70 | function create_eth_cfg_redhat { | |
71 | local fn=$cfgdir/ifcfg-$1 | |
72 | ||
73 | rm -f $fn | |
74 | echo DEVICE=$1 >>$fn | |
75 | echo TYPE=Ethernet >>$fn | |
76 | echo BOOTPROTO=none >>$fn | |
fd7aabb0 | 77 | echo UUID=`uuidgen` >>$fn |
178cd55f | 78 | echo ONBOOT=yes >>$fn |
178cd55f HZ |
79 | echo PEERDNS=yes >>$fn |
80 | echo IPV6INIT=yes >>$fn | |
81 | echo MASTER=$2 >>$fn | |
82 | echo SLAVE=yes >>$fn | |
83 | } | |
84 | ||
85 | function create_eth_cfg_pri_redhat { | |
86 | create_eth_cfg_redhat $1 $2 | |
87 | } | |
88 | ||
89 | function create_bond_cfg_redhat { | |
90 | local fn=$cfgdir/ifcfg-$1 | |
91 | ||
92 | rm -f $fn | |
93 | echo DEVICE=$1 >>$fn | |
94 | echo TYPE=Bond >>$fn | |
95 | echo BOOTPROTO=dhcp >>$fn | |
fd7aabb0 | 96 | echo UUID=`uuidgen` >>$fn |
178cd55f | 97 | echo ONBOOT=yes >>$fn |
178cd55f HZ |
98 | echo PEERDNS=yes >>$fn |
99 | echo IPV6INIT=yes >>$fn | |
100 | echo BONDING_MASTER=yes >>$fn | |
101 | echo BONDING_OPTS=\"mode=active-backup miimon=100 primary=$2\" >>$fn | |
102 | } | |
103 | ||
104 | function create_eth_cfg_ubuntu { | |
105 | local fn=$cfgdir/interfaces | |
106 | ||
107 | echo $'\n'auto $1 >>$fn | |
108 | echo iface $1 inet manual >>$fn | |
109 | echo bond-master $2 >>$fn | |
110 | } | |
111 | ||
112 | function create_eth_cfg_pri_ubuntu { | |
113 | local fn=$cfgdir/interfaces | |
114 | ||
115 | create_eth_cfg_ubuntu $1 $2 | |
116 | echo bond-primary $1 >>$fn | |
117 | } | |
118 | ||
119 | function create_bond_cfg_ubuntu { | |
120 | local fn=$cfgdir/interfaces | |
121 | ||
122 | echo $'\n'auto $1 >>$fn | |
123 | echo iface $1 inet dhcp >>$fn | |
124 | echo bond-mode active-backup >>$fn | |
125 | echo bond-miimon 100 >>$fn | |
126 | echo bond-slaves none >>$fn | |
127 | } | |
128 | ||
129 | function create_eth_cfg_suse { | |
130 | local fn=$cfgdir/ifcfg-$1 | |
131 | ||
132 | rm -f $fn | |
133 | echo BOOTPROTO=none >>$fn | |
134 | echo STARTMODE=auto >>$fn | |
135 | } | |
136 | ||
137 | function create_eth_cfg_pri_suse { | |
138 | create_eth_cfg_suse $1 | |
139 | } | |
140 | ||
141 | function create_bond_cfg_suse { | |
142 | local fn=$cfgdir/ifcfg-$1 | |
143 | ||
144 | rm -f $fn | |
145 | echo BOOTPROTO=dhcp >>$fn | |
146 | echo STARTMODE=auto >>$fn | |
147 | echo BONDING_MASTER=yes >>$fn | |
148 | echo BONDING_SLAVE_0=$2 >>$fn | |
149 | echo BONDING_SLAVE_1=$3 >>$fn | |
150 | echo BONDING_MODULE_OPTS=\'mode=active-backup miimon=100 primary=$2\' >>$fn | |
151 | } | |
152 | ||
153 | function create_bond { | |
154 | local bondname=bond$bondcnt | |
155 | local primary | |
156 | local secondary | |
157 | ||
158 | local class_id1=`cat $sysdir/$1/device/class_id 2>/dev/null` | |
159 | local class_id2=`cat $sysdir/$2/device/class_id 2>/dev/null` | |
160 | ||
161 | if [ "$class_id1" = "$netvsc_cls" ] | |
162 | then | |
163 | primary=$2 | |
164 | secondary=$1 | |
165 | elif [ "$class_id2" = "$netvsc_cls" ] | |
166 | then | |
167 | primary=$1 | |
168 | secondary=$2 | |
169 | else | |
170 | return 0 | |
171 | fi | |
172 | ||
173 | echo $'\nBond name:' $bondname | |
174 | ||
175 | echo configuring $primary | |
176 | create_eth_cfg_pri_$distro $primary $bondname | |
177 | ||
178 | echo configuring $secondary | |
179 | create_eth_cfg_$distro $secondary $bondname | |
180 | ||
181 | echo creating: $bondname with primary slave: $primary | |
182 | create_bond_cfg_$distro $bondname $primary $secondary | |
183 | ||
184 | let bondcnt=bondcnt+1 | |
185 | } | |
186 | ||
187 | for (( i=0; i < $eth_cnt-1; i++ )) | |
188 | do | |
189 | if [ -n "${list_match[$i]}" ] | |
190 | then | |
191 | create_bond ${list_eth[$i]} ${list_match[$i]} | |
192 | fi | |
193 | done |