|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -ex |
| 4 | + |
| 5 | +if [ "$1" != "--vendor" ]; then |
| 6 | + echo "No vendor provided" |
| 7 | + exit 1 |
| 8 | +fi |
| 9 | +vendor=$2 |
| 10 | + |
| 11 | +function get_device_driver() { |
| 12 | + local dev_driver=$(readlink $driver_path) |
| 13 | + echo "${dev_driver##*/}" |
| 14 | +} |
| 15 | + |
| 16 | +# find the PCI address of the device by vendor_id:product_id |
| 17 | +pci_address=(`lspci -D -d ${vendor}`) |
| 18 | +pci_address="${pci_address[0]}" |
| 19 | +dev_sysfs_path="/sys/bus/pci/devices/$pci_address" |
| 20 | + |
| 21 | +if [[ ! -d $dev_sysfs_path ]]; then |
| 22 | + echo "Error: PCI address ${pci_address} does not exist!" 1>&2 |
| 23 | + exit 1 |
| 24 | +fi |
| 25 | + |
| 26 | +if [[ ! -d "$dev_sysfs_path/iommu/" ]]; then |
| 27 | + echo "Error: No vIOMMU found in the VM" 1>&2 |
| 28 | + exit 1 |
| 29 | +fi |
| 30 | + |
| 31 | +# set device driver path |
| 32 | +driver_path="${dev_sysfs_path}/driver" |
| 33 | +driver_override="${dev_sysfs_path}/driver_override" |
| 34 | + |
| 35 | +# load the vfio-pci module |
| 36 | +modprobe -i vfio-pci |
| 37 | + |
| 38 | + |
| 39 | +driver=$(get_device_driver) |
| 40 | + |
| 41 | +if [[ "$driver" != "vfio-pci" ]]; then |
| 42 | + |
| 43 | + # unbind from the original device driver |
| 44 | + echo ${pci_address} > "${driver_path}/unbind" |
| 45 | + # bind the device to vfio-pci driver |
| 46 | + echo "vfio-pci" > ${driver_override} |
| 47 | + echo $pci_address > /sys/bus/pci/drivers/vfio-pci/bind |
| 48 | +fi |
| 49 | + |
| 50 | +# The device should now be using the vfio-pci driver |
| 51 | +new_driver=$(get_device_driver) |
| 52 | +if [[ $new_driver != "vfio-pci" ]]; then |
| 53 | + echo "Error: Failed to bind to vfio-pci driver" 1>&2 |
| 54 | + exit 1 |
| 55 | +fi |
0 commit comments