Make setup.sh refuse to set up a device that is currently bound to a driver other than vfio-pci unless -f is passed. Devices that are unbound or already bound to vfio-pci do not require -f. Validate all device arguments before modifying any device so a refused device does not leave partial state behind. Also add -h to print usage and exit 0, and document -f in Documentation/driver-api/vfio-selftests.rst. Suggested-by: David Matlack Signed-off-by: Josh Hilke --- Documentation/driver-api/vfio-selftests.rst | 9 ++-- tools/testing/selftests/vfio/scripts/setup.sh | 41 +++++++++++++++++-- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/Documentation/driver-api/vfio-selftests.rst b/Documentation/driver-api/vfio-selftests.rst index 9d39a22519d6..cfe7a3a06602 100644 --- a/Documentation/driver-api/vfio-selftests.rst +++ b/Documentation/driver-api/vfio-selftests.rst @@ -48,12 +48,13 @@ Setting up Devices .. code-block:: sh - tools/testing/selftests/vfio/scripts/setup.sh 0000:01:00.0 + tools/testing/selftests/vfio/scripts/setup.sh [-f] 0000:01:00.0 The script handles unbinding the device from its current driver and binding it -to ``vfio-pci``. Metadata about this device is stored in -``/tmp/vfio-selftests-devices`` so that the device can be cleaned up later -(bound back to its original driver). +to ``vfio-pci``. If a device is currently bound to a driver other than +``vfio-pci``, ``setup.sh`` refuses to unbind it unless ``-f`` is passed. +Metadata about this device is stored in ``/tmp/vfio-selftests-devices`` so that +the device can be cleaned up later (bound back to its original driver). Execution --------- diff --git a/tools/testing/selftests/vfio/scripts/setup.sh b/tools/testing/selftests/vfio/scripts/setup.sh index 49a499e51cbe..7eacd301f0c7 100755 --- a/tools/testing/selftests/vfio/scripts/setup.sh +++ b/tools/testing/selftests/vfio/scripts/setup.sh @@ -3,20 +3,55 @@ set -e source $(dirname -- "${BASH_SOURCE[0]}")/lib.sh +function usage() { + cat >&2 <&2 + while getopts "fh" opt; do + case "${opt}" in + f) force="true" ;; + h) usage; exit 0 ;; + *) usage; exit 1 ;; + esac + done + shift $((OPTIND - 1)) + + device_bdfs=("$@") + if [ ${#device_bdfs[@]} -eq 0 ]; then + usage exit 1 fi - for device_bdf in "$@"; do + for device_bdf in "${device_bdfs[@]}"; do test -d /sys/bus/pci/devices/${device_bdf} + if [ "${force}" ]; then + continue + fi + + driver=$(get_driver ${device_bdf}) + if [ "${driver}" ] && [ "${driver}" != "vfio-pci" ]; then + echo "${device_bdf} is bound to ${driver}, pass -f to unbind it." >&2 + exit 1 + fi + done + + for device_bdf in "${device_bdfs[@]}"; do device_dir=${DEVICES_DIR}/${device_bdf} if [ -d "${device_dir}" ]; then echo "${device_bdf} has already been set up, exiting." -- 2.56.1.839.g22a8290db9-goog