Skip to content

Instantly share code, notes, and snippets.

@drbild
Last active February 9, 2024 21:12
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save drbild/955b6fcec69307ac1ab3417a5223578f to your computer and use it in GitHub Desktop.
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
#!/bin/sh -e
# Copyright (C) 2006 Marco d'Itri <md@Linux.IT>
# Copyright (C) 2007 Kay Sievers <kay.sievers@vrfy.org>
# Copyright (C) 2018 David R. Bild <david.bild@xaptum.com>
#
# 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