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
10 # - After configured vSwitch and vNIC with SRIOV, start Linux virtual
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.
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.
21 netvsc_cls
={f8615163-df3e-46c5-913f-f2d2f965ed0e
}
25 if [ -f /etc
/redhat-release
];
27 cfgdir
=/etc
/sysconfig
/network-scripts
29 elif grep -q 'Ubuntu' /etc
/issue
33 elif grep -q 'SUSE' /etc
/issue
35 cfgdir
=/etc
/sysconfig
/network
38 echo "Unsupported Distro"
42 echo Detected Distro
: $distro, or compatible
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[@]}
48 echo List of net devices
:
50 # Get the MAC addresses
51 for (( i
=0; i
< $eth_cnt; i
++ ))
53 list_mac
[$i]=`cat $sysdir/${list_eth[$i]}/address`
54 echo ${list_eth[$i]}, ${list_mac[$i]}
57 # Find NIC with matching MAC
58 for (( i
=0; i
< $eth_cnt-1; i
++ ))
60 for (( j
=i
+1; j
< $eth_cnt; j
++ ))
62 if [ "${list_mac[$i]}" = "${list_mac[$j]}" ]
64 list_match
[$i]=${list_eth[$j]}
70 function create_eth_cfg_redhat
{
71 local fn
=$cfgdir/ifcfg-
$1
75 echo TYPE
=Ethernet
>>$fn
76 echo BOOTPROTO
=none
>>$fn
77 echo UUID
=`uuidgen` >>$fn
79 echo PEERDNS
=yes >>$fn
80 echo IPV6INIT
=yes >>$fn
85 function create_eth_cfg_pri_redhat
{
86 create_eth_cfg_redhat
$1 $2
89 function create_bond_cfg_redhat
{
90 local fn
=$cfgdir/ifcfg-
$1
95 echo BOOTPROTO
=dhcp
>>$fn
96 echo UUID
=`uuidgen` >>$fn
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
104 function del_eth_cfg_ubuntu
{
105 local mainfn
=$cfgdir/interfaces
106 local fnlist
=( $mainfn )
108 local dirlist
=(`awk '/^[ \t]*source/{print $2}' $mainfn`)
111 for i
in "${dirlist[@]}"
113 fnlist
+=(`ls $i 2>/dev/null`)
116 local tmpfl
=$
(mktemp
)
118 local nic_start
='^[ \t]*(auto|iface|mapping|allow-.*)[ \t]+'$1
119 local nic_end
='^[ \t]*(auto|iface|mapping|allow-.*|source)'
122 for fn
in "${fnlist[@]}"
124 awk "/$nic_end/{x=0} x{next} /$nic_start/{x=1;next} 1" \
133 function create_eth_cfg_ubuntu
{
134 local fn
=$cfgdir/interfaces
136 del_eth_cfg_ubuntu
$1
138 echo $
'\n'auto
$1 >>$fn
139 echo iface
$1 inet manual
>>$fn
140 echo bond-master
$2 >>$fn
143 function create_eth_cfg_pri_ubuntu
{
144 local fn
=$cfgdir/interfaces
146 create_eth_cfg_ubuntu
$1 $2
147 echo bond-primary
$1 >>$fn
150 function create_bond_cfg_ubuntu
{
151 local fn
=$cfgdir/interfaces
153 del_eth_cfg_ubuntu
$1
155 echo $
'\n'auto
$1 >>$fn
156 echo iface
$1 inet dhcp
>>$fn
157 echo bond-mode active-backup
>>$fn
158 echo bond-miimon
100 >>$fn
159 echo bond-slaves none
>>$fn
162 function create_eth_cfg_suse
{
163 local fn
=$cfgdir/ifcfg-
$1
166 echo BOOTPROTO
=none
>>$fn
167 echo STARTMODE
=auto
>>$fn
170 function create_eth_cfg_pri_suse
{
171 create_eth_cfg_suse
$1
174 function create_bond_cfg_suse
{
175 local fn
=$cfgdir/ifcfg-
$1
178 echo BOOTPROTO
=dhcp
>>$fn
179 echo STARTMODE
=auto
>>$fn
180 echo BONDING_MASTER
=yes >>$fn
181 echo BONDING_SLAVE_0
=$2 >>$fn
182 echo BONDING_SLAVE_1
=$3 >>$fn
183 echo BONDING_MODULE_OPTS
=\'mode
=active-backup miimon
=100 primary
=$2\' >>$fn
186 function create_bond
{
187 local bondname
=bond
$bondcnt
191 local class_id1
=`cat $sysdir/$1/device/class_id 2>/dev/null`
192 local class_id2
=`cat $sysdir/$2/device/class_id 2>/dev/null`
194 if [ "$class_id1" = "$netvsc_cls" ]
198 elif [ "$class_id2" = "$netvsc_cls" ]
206 echo $
'\nBond name:' $bondname
208 echo configuring
$primary
209 create_eth_cfg_pri_
$distro $primary $bondname
211 echo configuring
$secondary
212 create_eth_cfg_
$distro $secondary $bondname
214 echo creating
: $bondname with primary slave
: $primary
215 create_bond_cfg_
$distro $bondname $primary $secondary
217 let bondcnt
=bondcnt
+1
220 for (( i
=0; i
< $eth_cnt-1; i
++ ))
222 if [ -n "${list_match[$i]}" ]
224 create_bond
${list_eth[$i]} ${list_match[$i]}