Last active
February 9, 2024 21:12
-
-
Save drbild/955b6fcec69307ac1ab3417a5223578f to your computer and use it in GitHub Desktop.
Generate udev rules for persistent interface names by bus path, not mac address
This file contains 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/sh -e | |
# Copyright (C) 2006 Marco d'Itri <[email protected]> | |
# Copyright (C) 2007 Kay Sievers <[email protected]> | |
# Copyright (C) 2018 David R. Bild <[email protected]> | |
# | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 2 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
RULES_FILE='50-custom-persistent-net.rules' | |
if [ "$1" ]; then | |
RULES_FILE=$1 | |
fi | |
write_header() { | |
{ | |
echo "# This file was automatically generated by the $0" | |
echo "# program." | |
echo "" | |
} > $RULES_FILE | |
} | |
write_rule() { | |
local subsystem="$SUBSYSTEM" | |
local path="${ID_PATH#pci-}" | |
local interface="$INTERFACE" | |
local subclass_name="$ID_PCI_SUBCLASS_FROM_DATABASE" | |
local vendor_id="$ID_VENDOR_ID" | |
local vendor_name="$ID_VENDOR_FROM_DATABASE" | |
local device_id="$ID_MODEL_ID" | |
local device_name="$ID_MODEL_FROM_DATABASE" | |
{ | |
echo "# $path $subclass_name: $vendor_name $device_name was $interface" | |
echo "SUBSYSTEM==\"$subsystem\", ACTION==\"add\", KERNELS==\"$path\", ATTRS{vendor}==\"$vendor_id\", ATTRS{device}==\"$device_id\", NAME:=\"$interface\"" | |
echo "" | |
} >> $RULES_FILE | |
} | |
load_properties() | |
{ | |
local device_path=$1 | |
eval $(udevadm info -p $device_path -q property --export) | |
} | |
find_pci_net_devices() { | |
find /sys/devices -type d -wholename '/sys/devices/pci*/net' -print | |
} | |
write_header | |
for device in $(find_pci_net_devices); do | |
$(load_properties "$device/*" && write_rule) | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment