qusal/salt/dom0/files/bin/qvm-pci-regain

42 lines
1.3 KiB
Plaintext
Raw Normal View History

2023-11-13 09:33:28 -05:00
#!/bin/sh
2024-01-29 10:49:54 -05:00
# SPDX-FileCopyrightText: 2023 - 2024 Benjamin Grande M. S. <ben.grande.b@gmail.com
2023-11-13 09:33:28 -05:00
#
# SPDX-License-Identifier: GPL-3.0-or-later
## Credits: https://www.qubes-os.org/doc/how-to-use-pci-devices/#bringing-pci-devices-back-to-dom0
##
## After attaching a PCI device and detaching it, the device is not available
## in Dom0 anymore. This is an intended feature. A device which was previously
## assigned to a less trusted qube could attack dom0 if it were automatically
## reassigned there. In order to re-enable the device in dom0, either reboot
## physical machine or run the following script.
##
## This is useful for testing purposes when testing PCI devices, not
## recommended for users to attach a device to Dom0.
set -eu
usage(){
echo "Usage: ${0##*/} --i-like-danger <DEVICE>"
echo "Example: ${0##*/} --i-like-danger 0000:00:1b.0"
echo "Warning: It is strongly discouraged to reattach PCI devices to dom0, especially if they don't support resetting!"
2023-11-13 09:33:28 -05:00
exit 1
}
case "${1-}" in
--i-like-danger) ;;
*) usage;;
esac
case "${2-}" in
"") usage;;
2023-12-31 01:49:25 -05:00
*) device="${2}"
2023-11-13 09:33:28 -05:00
esac
test "$(id -u)" = "0" || exec sudo "${0}"
2023-12-31 01:49:25 -05:00
echo "${device}" | tee /sys/bus/pci/drivers/pciback/unbind
modalias="$(cat "/sys/bus/pci/devices/${device}/modalias")"
module="$(modprobe -R "${modalias}" | head -n 1)"
echo "${device}" | tee "/sys/bus/pci/drivers/${module}/bind"