[PATCH 2/2] vfio: selftests: Automate setting up devices for testing
From: Josh Hilke
Date: Wed Apr 08 2026 - 21:54:23 EST
Update the VFIO selftest setup script to find a device that has a VFIO selftest
driver on the system, and set it up (i.e unbind that device from its driver, and
then bind that device to VFIO).
This makes the process of setting up a device to VFIO selftests much easier, as
users don't have to manually figure out which device to use. Though, users can
still manually setup a device by passing the -d argument with a
segment:bus:device.function number.
Example of automated device setup:
$ ./tools/testing/selftests/vfio/scripts/setup.sh./setup.sh
+ echo "0000:6a:01.0" > /sys/bus/pci/drivers/idxd/unbind
+ echo "vfio-pci" > /sys/bus/pci/devices/0000:6a:01.0/driver_override
+ echo "0000:6a:01.0" > /sys/bus/pci/drivers/vfio-pci/bind
Successfully set up 0000:6a:01.0
Example of automated device setup where no compatible devices exist:
$ . ./tools/testing/selftests/vfio/scripts/setup.sh/setup.sh
No available supported devices found on the system.
Example of manual device setup:
$ ./tools/testing/selftests/vfio/scripts/setup.sh -d 0000:6a:01.0
+ echo "0000:6a:01.0" > /sys/bus/pci/drivers/idxd/unbind
+ echo "vfio-pci" > /sys/bus/pci/devices/0000:6a:01.0/driver_override
+ echo "0000:6a:01.0" > /sys/bus/pci/drivers/vfio-pci/bind
Successfully set up 0000:6a:01.0
Signed-off-by: Josh Hilke <jrhilke@xxxxxxxxxx>
---
tools/testing/selftests/vfio/scripts/setup.sh | 22 +++++++++++++++++--
1 file changed, 20 insertions(+), 2 deletions(-)
diff --git a/tools/testing/selftests/vfio/scripts/setup.sh b/tools/testing/selftests/vfio/scripts/setup.sh
index 2cde07eb9c8b..6d4492b211ea 100755
--- a/tools/testing/selftests/vfio/scripts/setup.sh
+++ b/tools/testing/selftests/vfio/scripts/setup.sh
@@ -19,11 +19,30 @@ function print_supported_devices() {
done
}
+function pick_device() {
+ local bdf
+
+ while read -r bdf; do
+ if [ -n "${bdf}" ]; then
+ if [ -d "${DEVICES_DIR}/${bdf}" ]; then
+ echo "${bdf} has already been set up, exiting." >&2
+ exit 0
+ fi
+ echo "${bdf}"
+ return 0
+ fi
+ done <<< "$(print_supported_devices)"
+
+ echo "No available supported devices found on the system." >&2
+ exit 1
+}
+
function usage() {
echo "usage: $0 [-l] [-d <segment:bus:device.function>]" >&2
echo "" >&2
echo " -l List segment:bus:device.function numbers of supported devices." >&2
echo " -d segment:bus:device.function to set up." >&2
+ echo " If -d is not specified, a device will be automatically picked." >&2
}
function main() {
@@ -46,8 +65,7 @@ function main() {
done
if [ ${#bdf_list[@]} -eq 0 ]; then
- usage
- exit 1
+ bdf_list=($(pick_device))
fi
for device_bdf in "${bdf_list[@]}"; do
--
2.53.0.1213.gd9a14994de-goog