Skip to content

Commit 22bcb4a

Browse files
committed
Support PCI path style for rd.qubes.* kernel param
Support stable identifiers (as listed by qvm-pci) also for hiding specific devices manually. Classic BDF format is (and will remain) still supported. This covers rd.qubes.hide_pci and rd.qubes.dom0_usb options. To properly handle all the cases (like old identifier in one one option but new in the other) translate all devices to BDF before comparing. QubesOS/qubes-issues#8681
1 parent 9ce4abf commit 22bcb4a

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

dracut/modules.d/90qubes-pciback/qubes-pciback.sh

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,38 @@
33
type getarg >/dev/null 2>&1 || . /lib/dracut-lib.sh
44
unset re HIDE_PCI usb_in_dom0 dev skip exposed
55

6+
resolve_dev() {
7+
local dev="$1"
8+
local segment="0000"
9+
local bridge bus bus_offset
10+
11+
case "$dev" in
12+
# device path to resolve
13+
[0-9a-f][0-9a-f][0-9a-f][0-9a-f]_*_*.*)
14+
segment=${dev%%_*}
15+
dev="${dev#*_}"
16+
;;
17+
*_*.*) ;;
18+
# (S)BDF directly
19+
*:*:*.*) echo "$dev"; return ;;
20+
*:*.*) echo "0000:$dev"; return;;
21+
*) echo "Invalid device format: $dev" >&2; return ;;
22+
esac
23+
24+
while [[ "$dev" = *-* ]]; do
25+
bridge="${dev%%-*}"
26+
bridge="${bridge//_/:}"
27+
dev="${dev#*-}"
28+
# this is in hex
29+
bus="${dev%%_*}"
30+
# this is in dec
31+
bus_offset=$(<"/sys/bus/pci/devices/$segment:$bridge/secondary_bus_number") || return
32+
bus=$(printf "%02x" $(( "0x$bus" + bus_offset )) )
33+
dev="${bus}_${dev#*_}"
34+
done
35+
echo "$segment:${dev//_/:}"
36+
}
37+
638
usb_in_dom0=false
739

840
if getargbool 0 rd.qubes.hide_all_usb; then
@@ -22,17 +54,17 @@ HIDE_PCI=$(set -o pipefail; { lspci -mm -n | awk "/^[^ ]* \"$re/ {print \$1}";})
2254

2355
manual_pcidevs=$(getarg rd.qubes.hide_pci)
2456
case $manual_pcidevs in
25-
(*[!0-9a-f.:,]*) warn 'Bogus rd.qubes.hide_pci option - fix your kernel command line!';;
57+
(*[!0-9a-f_.:,-]*) warn 'Bogus rd.qubes.hide_pci option - fix your kernel command line!';;
2658
esac
2759
HIDE_PCI="$HIDE_PCI ${manual_pcidevs//,/ }"
2860

2961
# XXX should this be fatal?
3062
ws=$' \n'
31-
[[ $HIDE_PCI =~ ^[0-9a-f.:$ws]+$ ]] ||
63+
[[ $HIDE_PCI =~ ^[0-9a-f_.:$ws-]+$ ]] ||
3264
die 'Bogus PCI device list - fix your kernel command line!'
3365
modprobe xen-pciback 2>/dev/null || :
3466
dom0_usb=$(getarg rd.qubes.dom0_usb)
35-
if [[ "$dom0_usb" == *[!0-9a-f.:,]* ]] ; then
67+
if [[ "$dom0_usb" == *[!0-9a-f_.:,-]* ]] ; then
3668
warn 'Bogus rd.qubes.dom0_usb option - fix your kernel command line!'
3769
dom0_usb=""
3870
elif [ -n "$dom0_usb" ] ; then
@@ -44,12 +76,16 @@ fi
4476
set -e
4577
# ... and hide them so that Dom0 doesn't load drivers for them
4678
for dev in $HIDE_PCI; do
79+
BDF=$(resolve_dev "$dev")
80+
if [ -z "$BDF" ]; then
81+
die "Cannot find device '$dev' to hide"
82+
fi
4783
skip=false
4884
for exposed in $dom0_usb; do
49-
if [ "$dev" = "$exposed" ]; then skip=true; fi
85+
exposed=$(resolve_dev "$exposed")
86+
if [ "$BDF" = "$exposed" ]; then skip=true; fi
5087
done
5188
if [ "$skip" = true ]; then continue; fi
52-
BDF=0000:$dev
5389
if [ -e "/sys/bus/pci/devices/$BDF/driver" ]; then
5490
echo -n "$BDF" > "/sys/bus/pci/devices/$BDF/driver/unbind"
5591
fi

0 commit comments

Comments
 (0)