For what it may be worth to others, I became sick of using a manual process to find the PCI controller (BDF) associated with a USB device that is connected via an internal or external hub, including those in a KVM
The below script should work as-is and will add an additional field to the output of lsusb
that provides the BDF for the device - it will be the actual PCI controller that the device ultimately connects to the system through
This is most useful when working with a fresh install on new hardware, especially when some devices are connected via hubs (thanks @apparatus for helping to determine the subtlety with lsusb
output for hub-connected devices)
Example use/output (run in dom0 only):
[user@dom0 ~]$ lsbdf
BDF: f4:00.4 Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
BDF: f4:00.4 Bus 001 Device 002: ID 0b95:772b ASIX Electronics Corp. AX88772B
...
Disclaimer: This is provided “as-is”. It works on my computer, but may not work on yours. This, and that, and yada blablabla. Shortened version:
#!/bin/bash
# Based on manual steps @ https://www.qubes-os.org/doc/how-to-use-usb-devices/
set -eu
lsusb | while IFS= read -r l; do
b=$(printf "%d" $(echo $l | grep -Po '(?<=Bus )\d+' | sed -e 's|0||g'))
dv=$(printf "%d" $(echo $l | grep -Po '(?<=Device )\d+' | sed -e 's|0||g'))
ds=$(echo $l | grep -Po '(?<=[0-9a-f]{4}:[0-9a-f]{4} ).*')
f=$(readlink /sys/bus/usb/devices/usb$b)
p=$(dirname "$f")
bf=$(basename $p | cut -d ':' -f 2-)
printf "BDF: %s %s\n" "$bf" "$l"
done
If this is not appropriate or belongs in another forum section, please notify me and I will do my best to resolve the issue
Thank you