qemu-vm/pci

33 lines
830 B
Bash
Executable file

#!/usr/bin/env bash
set -euo pipefail
BASE_PATH=/sys/bus/pci/
# sanitize variables
if [[ "${BASE_PATH: -1}" = '/' ]]; then
BASE_PATH="${BASE_PATH::${#BASE_PATH}-1}"
fi
vfio_rebind_device() {
local pci_id
local override
pci_id="$1"
override="$2"
if [[ "$override" != 'true' ]]; then
override=false
fi
if [[ -e "$BASE_PATH/devices/$pci_id" ]]; then
if "$override"; then
echo "vfio-pci" | sudo tee \
"$BASE_PATH/devices/$pci_id/driver_override" > /dev/null
fi
[[ -e "$BASE_PATH/devices/$pci_id/driver/unbind" ]] \
&& echo "$pci_id" | sudo tee \
"$BASE_PATH/devices/$pci_id/driver/unbind" >/dev/null
echo "$pci_id" | sudo tee \
"$BASE_PATH/drivers/vfio-pci/bind" > /dev/null
fi
}
"$@"