Skip to content

Instantly share code, notes, and snippets.

@tahuang1991
Created December 13, 2019 15:51
Show Gist options
  • Save tahuang1991/abe20e277885a542224adcefa51e0eaf to your computer and use it in GitHub Desktop.
Save tahuang1991/abe20e277885a542224adcefa51e0eaf to your computer and use it in GitHub Desktop.
#!/bin/zsh
function interfaceNames(){
# Implicit (default) interface naming scheme
export __ETH0__=em1
export __ETH1__=em2
export __ETH2__=p1p1
export __ETH3__=p1p2
export __ETH4__=p2p1
export __ETH5__=p2p2
# If an explicit naming scheme is defined for the network interfaces, take that instead of the implicit one.
[[ -f ${DRIVERS_DIR}/ifnames_igb_emu.sh ]] && source ${DRIVERS_DIR}/ifnames_igb_emu.sh || print "${DRIVERS_DIR}/ifnames_igb_emu.sh not found. Falling back to default interface naming scheme."
}
function module_parameters(){
# Print module parameters (same value for all ports) in a comma separated list ready to be passed to modprobe.
# argument 1: number of ports
# argument 2: value
PARAMS=$2
for ((I=1;I<$1;I++)); do
PARAMS=${PARAMS}",$2"
done
echo -n $PARAMS
}
function load_igb_emu(){
# To be invoked with the list of hook names, e.g.
# load_igb_emu eth_hook_2_vme eth_hook_3_ddu eth_hook_4_dmb eth_hook_5_dmb
# Bring down the interfaces
echo "Bringing down the interfaces"
for N in 2 3 4 5; do
/sbin/ifconfig ${IF_NAME[$N]} down
done
# Unload the modules if they've been loaded already
echo "Unloading the modules if they've been loaded already"
echo "lsmod | grep igb"
/sbin/lsmod | grep igb
/sbin/lspci -k | grep -A 3 Ethernet
if [[ $(/sbin/lsmod | grep -c 'igb_emu ') -gt 0 ]]; then
echo "/sbin/modprobe -r igb_emu"
/sbin/modprobe -r igb_emu
fi
if [[ $(/sbin/lsmod | grep -c 'igb ') -gt 0 ]]; then
echo "/sbin/modprobe -r igb"
/sbin/modprobe -r igb
fi
echo "lsmod | grep igb"
/sbin/lsmod | grep igb
/sbin/lspci -k | grep -A 3 Ethernet
# Create schar devices if they don't yet exist
echo "Creating schar devices if they don't yet exist"
for N in 2 3 4 5
do
[[ -c /dev/schar${N} ]] || ( mknod /dev/schar${N} c 23${N} 0 && chmod 777 /dev/schar${N} )
done
# Copy the driver and the requested hooks
echo "Copying the driver and the requested hooks"
[[ -d /lib/modules/$(uname -r)/kernel/drivers/net/igb ]] && MODULES_DIR=/lib/modules/$(uname -r)/kernel/drivers/net/igb
[[ -d /lib/modules/$(uname -r)/kernel/drivers/net/ethernet/intel/igb ]] && MODULES_DIR=/lib/modules/$(uname -r)/kernel/drivers/net/ethernet/intel/igb
rm -f ${MODULES_DIR}/igb_emu.ko
rm -f ${MODULES_DIR}/eth_hook_*.ko(N)
rm -f /lib/modules/$(uname -r)/kernel/drivers/net/eth_hook_*.ko(N)
cp ${DRIVERS_DIR}/igb_emu.ko ${MODULES_DIR}
for HOOK in "$@"; do
cp ${DRIVERS_DIR}/${HOOK}.ko ${MODULES_DIR}
done
NPORTS=4
# Module aliases. Note that, in the case of a built-in Intel NIC with module igb, it will also be replaced with igb_emu.
if [[ $SLC_MAJOR -eq 5 ]]; then
echo "Updating /etc/modprobe.conf"
[[ -f /etc/modprobe.conf ]] && sed -i.bak -e 's/^alias eth\([012345]\) igb$/alias eth\1 igb_emu/g' /etc/modprobe.conf
# Count the number of on-board ports to be served by igb_emu
[[ -f /etc/modprobe.conf ]] && (( NPORTS+=$(grep -c '^alias eth[01] igb' /etc/modprobe.conf) ))
elif [[ $SLC_MAJOR -eq 6 ]]; then
echo "Updating /etc/modprobe.d"
sed -i.bak -e 's:^\(alias p[12]p[12] [^ ]\+\)$:# \1 # commented out by '${0}':g' -e 's:^alias \(em[12]\) \(igb[^ ]*\)$:# alias \1 \2 # commented out by '${0}'\nalias \1 igb_emu:g' /etc/modprobe.d/*.conf
rm -f /etc/modprobe.d/net-igb_emu.conf
for N in 2 3 4 5; do
print "alias ${IF_NAME[$N]} igb_emu" >> /etc/modprobe.d/net-igb_emu.conf
done
# Count the number of on-board ports to be served by igb_emu
(( NPORTS+=$( grep -c "^alias em[12] igb" =(cat /etc/modprobe.d/*.conf) ) ))
elif [[ $SLC_MAJOR -eq 7 ]]; then
echo "Updating /etc/modprobe.d"
sed -i.bak -e 's:^\(alias enp[0-9]s[0-9]f[0-9] [^ ]\+\)$:# \1 # commented out by '${0}':g' -e 's:^alias \(eno[12]\) \(igb[^ ]*\)$:# alias \1 \2 # commented out by '${0}'\nalias \1 igb_emu:g' /etc/modprobe.d/*.conf
rm -f /etc/modprobe.d/net-igb_emu.conf
for N in 2 3 4 5; do
print "alias ${IF_NAME[$N]} igb_emu" >> /etc/modprobe.d/net-igb_emu.conf
done
# Count the number of on-board ports to be served by igb_emu
(( NPORTS+=$( grep -c "^alias eno[12] igb" =(cat /etc/modprobe.d/*.conf) ) ))
fi
# Update module dependencies
echo "Updating module dependencies"
/sbin/depmod
# Load new modules
echo "Loading igb_emu"
echo "/sbin/modprobe igb_emu InterruptThrottleRate=$(module_parameters $NPORTS 0) QueuePairs=$(module_parameters $NPORTS 0) EEE=$(module_parameters $NPORTS 0) MDD=$(module_parameters $NPORTS 0)"
/sbin/modprobe igb_emu InterruptThrottleRate=$(module_parameters $NPORTS 0) QueuePairs=$(module_parameters $NPORTS 0) EEE=$(module_parameters $NPORTS 0) MDD=$(module_parameters $NPORTS 0)
echo "lsmod | grep igb"
/sbin/lsmod | grep igb
/sbin/lspci -k | grep -A 3 Ethernet
# Remove igb module, just in case it's still loaded
if [[ $(/sbin/lsmod | grep -c 'igb ') -gt 0 ]]; then
echo "Removing tenacious igb module"
echo "rmmod igb"
/usr/sbin/rmmod igb
echo "lsmod | grep igb"
/sbin/lsmod | grep igb
fi
# Disable automatic start on boot and network manager for the plugged-in interfaces
for N in 2 3 4 5; do
echo "Disable automatic start on boot and network manager for the plugged-in interfaces"
echo "/etc/sysconfig/network-scripts/ifcfg-${IF_NAME[$N]}"
if [[ -f /etc/sysconfig/network-scripts/ifcfg-${IF_NAME[$N]} ]]; then
sed -i.bak -e '/^BOOTPROTO=/d' -e '/^ONBOOT=/d' -e '/^NM_CONTROLLED=/d' -e '/^TYPE=/d' /etc/sysconfig/network-scripts/ifcfg-${IF_NAME[$N]}
print "BOOTPROTO=static" >> /etc/sysconfig/network-scripts/ifcfg-${IF_NAME[$N]}
print "ONBOOT=no" >> /etc/sysconfig/network-scripts/ifcfg-${IF_NAME[$N]}
print "NM_CONTROLLED=no" >> /etc/sysconfig/network-scripts/ifcfg-${IF_NAME[$N]}
print "TYPE=Ethernet" >> /etc/sysconfig/network-scripts/ifcfg-${IF_NAME[$N]}
fi
done
sleep 2;
# Restart the built-in interfaces, too, in case they're also Intel supported by igb.
for N in 0 1; do
if [[ $SLC_MAJOR -eq 5 && -f /etc/modprobe.conf && $( grep -c "^alias ${IF_NAME[$N]} igb" /etc/modprobe.conf ) -gt 0 ]] || \
[[ $SLC_MAJOR -eq 6 && $( grep -c "^alias ${IF_NAME[$N]} igb" =(cat /etc/modprobe.d/*.conf) ) -gt 0 ]] || \
[[ $SLC_MAJOR -eq 7 && $( grep -c "^alias ${IF_NAME[$N]} igb" =(cat /etc/modprobe.d/*.conf) ) -gt 0 ]] ; then
sleep 2
echo "Restarting the built-in interface ${IF_NAME[$N]}"
/etc/sysconfig/network-scripts/ifdown ${IF_NAME[$N]}
sleep 5
/etc/sysconfig/network-scripts/ifup-eth ${IF_NAME[$N]}
fi
done
# Trigger kernel events for the udev rules in /etc/udev/rules.d/70-persistent-net.rules to be applied
print "udevadm trigger"
udevadm trigger
sleep 1
# Bring up the interfaces for the plugged-in NICs
echo "Bringing up the interfaces for the plugged-in NICs"
# Take the position within the rack from the host name (e.g. 15 from ctrl-s2g18-15-01) and add it to 100:
HOSTNUMBER="1${${$(hostname -s)%-*}##*-}"
# Check if it makes sense
[[ ${HOSTNUMBER} == <0-255> ]] || HOSTNUMBER=100
for N in 2 3 4 5; do
print "/sbin/ifconfig ${IF_NAME[$N]} down"
/sbin/ifconfig ${IF_NAME[$N]} down
sleep 1
echo "select the driver type ${(P)$((N-1))} hostnumber ",$HOSTNUMBER," TAMU used hostnumber 240"
print /sbin/ifconfig ${IF_NAME[$N]} promisc mtu 8192 192.168.240.${N}
/sbin/ifconfig ${IF_NAME[$N]} promisc mtu 8192 192.168.240.${N}
done
}
####################################################################################################
# Kernel object files are installed in DRIVERS_DIR, from where we copy only those required on the current host to /lib/modules/$(uname -r)/kernel/drivers
# Let it be the same directory this script is in:
#DRIVERS_DIR=${0%/*}
#Tao, add cmslab1 to the list
DRIVERS_DIR=/usr/local/bin/igb_emu/
# Mapping from arbitrary old-fashioned eth<N> to consistent physical p<slot>p<port> interface name, if necessary
typeset -A IF_NAME
if [[ $(uname -r | grep -c '^3\.10\.0-') -gt 0 ]]; then
interfaceNames
IF_NAME=(
0 ${__ETH0__}
1 ${__ETH1__}
2 ${__ETH2__}
3 ${__ETH3__}
4 ${__ETH4__}
5 ${__ETH5__}
)
SLC_MAJOR=7
elif [[ $(uname -r | grep -c '^2\.6\.32-') -gt 0 ]]; then
IF_NAME=(
0 em1
1 em2
2 p1p1
3 p1p2
4 p2p1
5 p2p2
)
SLC_MAJOR=6
elif [[ $(uname -r | grep -c '^2\.6\.18-') -gt 0 ]]; then
IF_NAME=(
0 eth0
1 eth1
2 eth2
3 eth3
4 eth4
5 eth5
)
SLC_MAJOR=5
else
print "Cannot determine network interface naming scheme. Exiting."
exit 1
fi
print "Seems to be SLC${SLC_MAJOR}. Assuming interface names ${IF_NAME}."
# Only load the drivers on hosts in this list of aliases:
# add cmslab1 to the list, Tao
for ALIAS in cmslab1 emu42fastprod01 emu-me11-step{1,2,3,4} ctrl-s2g18-{15..18}-01 srv-c2d08-25-01 vmepc-s2g18-20-01; do
if [[ $(host $ALIAS | grep -i -c $(hostname -s)) -ge 1 ]]; then
load_igb_emu eth_hook_2_vme eth_hook_3_vme eth_hook_4_vme eth_hook_5_vme
exit 0
fi
done
for ALIAS in cmslab1 vmepc-e1x07-21-01 vmepc-e1x07-26-01 emusx5-systest1 emusx50{5,6,7,8}; do
if [[ $(host $ALIAS | grep -i -c $(hostname -s)) -ge 1 ]]; then
load_igb_emu eth_hook_2_vme eth_hook_3_dmb eth_hook_4_vme eth_hook_5_vme
exit 0
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment