Last active
November 4, 2022 13:41
-
-
Save cseelye/e30fd2dad82b69484f5199d9e29a0b25 to your computer and use it in GitHub Desktop.
Create persistent device names for 1G and 10G interfaces using systemd
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 | |
set -euo pipefail | |
BASENAME_1G=mgmt | |
BASENAME_10G=data | |
OUTPUT_PATH=/etc/systemd/network | |
current_nic_names=$(for iface in $(ls -1 /sys/class/net); do | |
# Skip virtual devices, we only want physical | |
[[ ! -e /sys/class/net/${iface}/device ]] && continue | |
echo "${iface}" | |
done | tr '\n' ' ' || true) | |
index_1g=0 | |
index_10g=0 | |
for ifname in ${current_nic_names}; do | |
port=$(ethtool ${ifname} | grep "Supported ports:" || true) | |
if [[ ${port} =~ "TP" ]]; then | |
cat <<-EOF > ${OUTPUT_PATH}/10-${BASENAME_1G}${index_1g}.link | |
[Match] | |
Path=pci-$(ethtool -i ${ifname} | grep --color=none bus-info | awk '{print $2}') | |
[Link] | |
Name=${BASENAME_1G}${index_1g} | |
EOF | |
(( index_1g++ )) || true | |
elif [[ ${port} =~ "FIBRE" ]]; then | |
cat <<-EOF > ${OUTPUT_PATH}/10-${BASENAME_10G}${index_10g}.link | |
[Match] | |
Path=pci-$(ethtool -i ${ifname} | grep --color=none bus-info | awk '{print $2}') | |
[Link] | |
Name=${BASENAME_10G}${index_10g} | |
EOF | |
(( index_10g++ )) || true | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment