Blame SOURCES/bondvf.sh

f22315
#!/bin/bash
f22315
f22315
# This example script creates bonding network devices based on synthetic NIC
f22315
# (the virtual network adapter usually provided by Hyper-V) and the matching
f22315
# VF NIC (SRIOV virtual function). So the synthetic NIC and VF NIC can
f22315
# function as one network device, and fail over to the synthetic NIC if VF is
f22315
# down.
f22315
#
f22315
# Usage:
f22315
# - After configured vSwitch and vNIC with SRIOV, start Linux virtual
f22315
#   machine (VM)
f22315
# - Run this scripts on the VM. It will create configuration files in
f22315
#   distro specific directory.
f22315
# - Reboot the VM, so that the bonding config are enabled.
f22315
#
f22315
# The config files are DHCP by default. You may edit them if you need to change
f22315
# to Static IP or change other settings.
f22315
#
f22315
f22315
sysdir=/sys/class/net
f22315
netvsc_cls={f8615163-df3e-46c5-913f-f2d2f965ed0e}
f22315
bondcnt=0
f22315
f22315
# Detect Distro
f22315
if [ -f /etc/redhat-release ];
f22315
then
f22315
	cfgdir=/etc/sysconfig/network-scripts
f22315
	distro=redhat
f22315
elif grep -q 'Ubuntu' /etc/issue
f22315
then
f22315
	cfgdir=/etc/network
f22315
	distro=ubuntu
f22315
elif grep -q 'SUSE' /etc/issue
f22315
then
f22315
	cfgdir=/etc/sysconfig/network
f22315
	distro=suse
f22315
else
f22315
	echo "Unsupported Distro"
f22315
	exit 1
f22315
fi
f22315
f22315
echo Detected Distro: $distro, or compatible
f22315
f22315
# Get a list of ethernet names
f22315
list_eth=(`cd $sysdir && ls -d */ | cut -d/ -f1 | grep -v bond`)
f22315
eth_cnt=${#list_eth[@]}
f22315
f22315
echo List of net devices:
f22315
f22315
# Get the MAC addresses
f22315
for (( i=0; i < $eth_cnt; i++ ))
f22315
do
f22315
	list_mac[$i]=`cat $sysdir/${list_eth[$i]}/address`
f22315
	echo ${list_eth[$i]}, ${list_mac[$i]}
f22315
done
f22315
f22315
# Find NIC with matching MAC
f22315
for (( i=0; i < $eth_cnt-1; i++ ))
f22315
do
f22315
	for (( j=i+1; j < $eth_cnt; j++ ))
f22315
	do
f22315
		if [ "${list_mac[$i]}" = "${list_mac[$j]}" ]
f22315
		then
f22315
			list_match[$i]=${list_eth[$j]}
f22315
			break
f22315
		fi
f22315
	done
f22315
done
f22315
f22315
function create_eth_cfg_redhat {
f22315
	local fn=$cfgdir/ifcfg-$1
f22315
f22315
	rm -f $fn
f22315
	echo DEVICE=$1 >>$fn
f22315
	echo TYPE=Ethernet >>$fn
f22315
	echo BOOTPROTO=none >>$fn
f22315
	echo UUID=`uuidgen` >>$fn
f22315
	echo ONBOOT=yes >>$fn
f22315
	echo PEERDNS=yes >>$fn
f22315
	echo IPV6INIT=yes >>$fn
f22315
	echo MASTER=$2 >>$fn
f22315
	echo SLAVE=yes >>$fn
f22315
}
f22315
f22315
function create_eth_cfg_pri_redhat {
f22315
	create_eth_cfg_redhat $1 $2
f22315
}
f22315
f22315
function create_bond_cfg_redhat {
f22315
	local fn=$cfgdir/ifcfg-$1
f22315
f22315
	rm -f $fn
f22315
	echo DEVICE=$1 >>$fn
f22315
	echo TYPE=Bond >>$fn
f22315
	echo BOOTPROTO=dhcp >>$fn
f22315
	echo UUID=`uuidgen` >>$fn
f22315
	echo ONBOOT=yes >>$fn
f22315
	echo PEERDNS=yes >>$fn
f22315
	echo IPV6INIT=yes >>$fn
f22315
	echo BONDING_MASTER=yes >>$fn
f22315
	echo BONDING_OPTS=\"mode=active-backup miimon=100 primary=$2\" >>$fn
f22315
}
f22315
f22315
function create_eth_cfg_ubuntu {
f22315
	local fn=$cfgdir/interfaces
f22315
f22315
	echo $'\n'auto $1 >>$fn
f22315
	echo iface $1 inet manual >>$fn
f22315
	echo bond-master $2 >>$fn
f22315
}
f22315
f22315
function create_eth_cfg_pri_ubuntu {
f22315
	local fn=$cfgdir/interfaces
f22315
f22315
	create_eth_cfg_ubuntu $1 $2
f22315
	echo bond-primary $1 >>$fn
f22315
}
f22315
f22315
function create_bond_cfg_ubuntu {
f22315
	local fn=$cfgdir/interfaces
f22315
f22315
	echo $'\n'auto $1 >>$fn
f22315
	echo iface $1 inet dhcp >>$fn
f22315
	echo bond-mode active-backup >>$fn
f22315
	echo bond-miimon 100 >>$fn
f22315
	echo bond-slaves none >>$fn
f22315
}
f22315
f22315
function create_eth_cfg_suse {
f22315
        local fn=$cfgdir/ifcfg-$1
f22315
f22315
        rm -f $fn
f22315
	echo BOOTPROTO=none >>$fn
f22315
	echo STARTMODE=auto >>$fn
f22315
}
f22315
f22315
function create_eth_cfg_pri_suse {
f22315
	create_eth_cfg_suse $1
f22315
}
f22315
f22315
function create_bond_cfg_suse {
f22315
	local fn=$cfgdir/ifcfg-$1
f22315
f22315
	rm -f $fn
f22315
	echo BOOTPROTO=dhcp >>$fn
f22315
	echo STARTMODE=auto >>$fn
f22315
	echo BONDING_MASTER=yes >>$fn
f22315
	echo BONDING_SLAVE_0=$2 >>$fn
f22315
	echo BONDING_SLAVE_1=$3 >>$fn
f22315
	echo BONDING_MODULE_OPTS=\'mode=active-backup miimon=100 primary=$2\' >>$fn
f22315
}
f22315
f22315
function create_bond {
f22315
	local bondname=bond$bondcnt
f22315
	local primary
f22315
	local secondary
f22315
f22315
	local class_id1=`cat $sysdir/$1/device/class_id 2>/dev/null`
f22315
	local class_id2=`cat $sysdir/$2/device/class_id 2>/dev/null`
f22315
f22315
	if [ "$class_id1" = "$netvsc_cls" ]
f22315
	then
f22315
		primary=$2
f22315
		secondary=$1
f22315
	elif [ "$class_id2" = "$netvsc_cls" ]
f22315
	then
f22315
		primary=$1
f22315
		secondary=$2
f22315
	else
f22315
		return 0
f22315
	fi
f22315
f22315
	echo $'\nBond name:' $bondname
f22315
f22315
	echo configuring $primary
f22315
	create_eth_cfg_pri_$distro $primary $bondname
f22315
f22315
	echo configuring $secondary
f22315
	create_eth_cfg_$distro $secondary $bondname
f22315
f22315
	echo creating: $bondname with primary slave: $primary
f22315
	create_bond_cfg_$distro $bondname $primary $secondary
f22315
f22315
	let bondcnt=bondcnt+1
f22315
}
f22315
f22315
for (( i=0; i < $eth_cnt-1; i++ ))
f22315
do
f22315
        if [ -n "${list_match[$i]}" ]
f22315
        then
f22315
		create_bond ${list_eth[$i]} ${list_match[$i]}
f22315
        fi
f22315
done