-
-
Save YoSmudge/fe193bb7bb02db9ac167 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
# Re-order interfaces on supermicro boxes to ensure eth0/eth1 are the on-board NICs | |
# Priority given to interfaces with MAC addresses in the SUPERMICRO_ADDRESSES list below | |
RULES_FILE=$1 | |
if [ ! -f "${RULES_FILE}" ]; then | |
echo "Rules file ${RULES_FILE} does not exist! Cannot overwrite" | |
exit 1 | |
fi | |
echo "Re-ordering interfaces..." | |
HWINFO=$(lshw -class network -quiet) | |
SUPERMICRO_ADDRESSES=$(cat <<MACS | |
0c:c4:7a | |
MACS | |
) | |
IFS=$'\n' | |
SUPERMICRO_INTERFACES="" | |
OTHER_INTERFACES="" | |
for IF in $(cat /proc/net/dev | awk '{ print $1 }' | grep -Eo "^eth[[:digit:]]"); do | |
MAC=$(ip link show ${IF} | tail -n 1 | awk '{ print $2 }') | |
PCI=$(echo -e "${HWINFO}" | grep -B 1 "logical name: ${IF}" | head -n 1 | grep "bus info:" | awk '{ print $3 }') | |
echo -n "Found ${IF} with MAC ${MAC}, PCI address ${PCI}" | |
IS_SUPERMICRO=0 | |
for ADDRESS in $(echo "${SUPERMICRO_ADDRESSES}"); do | |
if [ $(echo "${MAC}" | grep -e "^${ADDRESS}") ]; then | |
IS_SUPERMICRO=1 | |
fi | |
done | |
IF_DETAILS="${IF} ${MAC} ${PCI}" | |
if [ ${IS_SUPERMICRO} -eq 1 ]; then | |
echo -n " [Supermicro]" | |
SUPERMICRO_INTERFACES="${SUPERMICRO_INTERFACES}"'\n'"${IF_DETAILS}" | |
else | |
OTHER_INTERFACES="${OTHER_INTERFACES}"'\n'"${IF_DETAILS}" | |
fi | |
echo | |
done | |
echo | |
PERSISTENT_NET_RULES="# Auto generated by reorder-interfaces.sh at $(date)" | |
IF_COUNTER=0 | |
for IF in $(echo -e "${SUPERMICRO_INTERFACES}\n${OTHER_INTERFACES}"); do | |
ETH=$(echo ${IF} | awk '{ print $1 }') | |
MAC=$(echo ${IF} | awk '{ print $2 }') | |
PCI=$(echo ${IF} | awk '{ print $3 }') | |
NEW_ETH="eth${IF_COUNTER}" | |
echo "Iterfaces ${ETH} (${MAC}) will become ${NEW_ETH} (${PCI})" | |
IF_COUNTER=$(expr ${IF_COUNTER} + 1) | |
PERSISTENT_NET_RULES="${PERSISTENT_NET_RULES}\n"'SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="'"${MAC}"'", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="'"${NEW_ETH}"'"' | |
done | |
echo -e "${PERSISTENT_NET_RULES}" > ${RULES_FILE} | |
# Now configure eth0 to DHCP (puppet will clean this up later) | |
cat > /etc/network/interfaces <<INTERFACES | |
auto lo | |
iface lo inet loopback | |
auto eth0 | |
iface eth0 inet dhcp | |
INTERFACES |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment