Skip to content

Instantly share code, notes, and snippets.

@redneck-f25
Last active June 17, 2024 23:21
Show Gist options
  • Save redneck-f25/bafffa76c68228f686ba00490bb61187 to your computer and use it in GitHub Desktop.
Save redneck-f25/bafffa76c68228f686ba00490bb61187 to your computer and use it in GitHub Desktop.
Stable names for multiple network interfaces in GNU/Linux VM on Hyper-V
# /etc/udev/rules.d/70-hvnetdev.rules
SUBSYSTEM=="net", ACTION=="add", PROGRAM=="/etc/udev/hvnetdev_program", NAME="$result"
#!/bin/bash
# /etc/udev/hvnetdev_map
# the names are inspired by OpenBSD (on Hyper-V)
# call hvnetdev_program --list and compare with Get-VMNetworkAdapter -VMName <name> (PowerShell) to create the map
hvnetdev_map['56b619fe-c897-4306-af0b-ed1e53acb47a']='hvn0'
hvnetdev_map['f3a0a6a6-14eb-4b07-8732-51762015f1bf']='hvn1'
hvnetdev_map['246753d2-6a7f-4d2d-aa3a-2ea173b49c2d']='hvn2'
hvnetdev_map['d0c1b0f0-7373-4ea9-a0ee-9b514e6436a8']='hvn3'
hvnetdev_map['56804368-d379-433d-86a7-7a369e10e67a']='hvn4'
hvnetdev_map['689254ce-f7ab-4eb3-8045-bbec141db8b7']='hvn5'
#!/bin/bash
# /etc/udev/hvnetdev_program
# we need to rename network devices since kernel names are not stable
# @see: https://github.com/systemd/systemd/issues/8652
# @see: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=810247
# @see: https://wiki.debian.org/NetworkInterfaceNames
set -euo pipefail
if [[ "${1:-}" == '--list' || -z "${DEVPATH:-}" ]]; then
while read -r _ kname _; do
kname="${kname%:}"
read -r _ mac_addr _
path="$( readlink -f "$( find -L /sys/bus/vmbus/drivers/hv_netvsc -mindepth 3 -maxdepth 3 -name "${kname}" 2> /dev/null )" )"
uuid=${path%/*/*}
uuid=${uuid##*/}
printf '%s %s %s\n' "${kname}" "${mac_addr}" "${uuid}"
done < <( ip link show | grep -EA1 '^[^[:space:]:]*:[[:space:]](eth|hvn)' | grep -v '^--' )
exit
fi
if [[ "${ACTION}" != "add" || "${SUBSYSTEM}" != "net" ]]; then
exit 1
fi
uuid=${DEVPATH%/*/*}
uuid=${uuid##*/}
if [[ ! -d /sys/bus/vmbus/drivers/hv_netvsc/${uuid}/net/${INTERFACE} ]]; then
exit 1
fi
declare -A hvnetdev_map
. "${0%_program}_map"
printf '%s\n' "${hvnetdev_map[${uuid}]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment