quickpassthrough/lib/set_MODULES.sh

107 lines
3.1 KiB
Bash
Raw Normal View History

2022-02-27 11:19:22 -05:00
#!/bin/bash
2022-03-10 09:34:45 -05:00
function insert_MODULES() {
# Get the header and enabled modules separately from the /etc/modules file
2022-03-10 09:34:45 -05:00
local MODULES_HEADER
local MODULES_ENABLED
local VENDOR_RESET
MODULES_HEADER=$(head -n $1 "$2" | grep -P "^#" | grep -v "# Added by quickpassthrough")
MODULES_ENABLED=$(cat "$2" | grep -vP "^#" | grep -v "vendor-reset")
VENDOR_RESET=0
# If vendor-reset is present
if grep -q "vendor-reset" "$2" ;
then
VENDOR_RESET=1
fi
2022-02-27 11:19:22 -05:00
# Write header
echo "$MODULES_HEADER" > "$2"
# If vendor-reset existed from before
if [ $VENDOR_RESET == 1 ];
then
# Write vendor-reset as the first module!
echo "vendor-reset" >> "$2"
fi
# Append vfio
printf "
# Added by quickpassthrough #
vfio
2022-02-27 11:19:22 -05:00
vfio_iommu_type1
vfio_pci
2022-02-27 11:19:22 -05:00
vfio_virqfd
#############################
" >> "$2"
2022-02-27 11:19:22 -05:00
# Write the previously enabled modules under vfio in the load order
echo "$MODULES_ENABLED" >> "$2"
}
function set_MODULES () {
# Get the config paths
source "$SCRIPTDIR/lib/paths.sh"
2022-03-10 09:36:27 -05:00
# Insert modules in the correct locations as early as possible without
# conflicting with vendor-reset module if it is enabled
insert_MODULES 4 "$SCRIPTDIR/$ETCMODULES"
insert_MODULES 11 "$SCRIPTDIR/$INITRAMFS/modules"
2022-02-27 11:19:22 -05:00
# Assign the GPU device ids to a variable
GPU_DEVID="$1"
# Get the kernel_args file content
2022-02-27 13:18:09 -05:00
CMDLINE=$(cat "$SCRIPTDIR/config/kernel_args")
2022-02-27 11:19:22 -05:00
# Ask if we shall disable video output on this card
2022-03-11 17:43:29 -05:00
echo "
Disabling video output in Linux for the card you want to use in a VM
will make it easier to successfully do the passthrough without issues."
2022-02-27 13:06:18 -05:00
read -p "Do you want to force disable video output in linux on this card? [Y/n]: " DISABLE_VGA
2022-02-27 11:19:22 -05:00
case "${DISABLE_VGA}" in
[Yy]*)
# Update kernel_args file
echo "${CMDLINE} vfio_pci.ids=${GPU_DEVID} vfio_pci.disable_vga=1" > "$SCRIPTDIR/config/kernel_args"
# Update GPU_DEVID
GPU_DEVID="$GPU_DEVID disable_vga=1"
2022-02-27 11:19:22 -05:00
;;
[Nn]*)
echo ""
;;
*)
# Update kernel_args file
echo "${CMDLINE} vfio_pci.ids=${GPU_DEVID} vfio_pci.disable_vga=1" > "$SCRIPTDIR/config/kernel_args"
# Update GPU_DEVID
GPU_DEVID="$GPU_DEVID disable_vga=1"
2022-02-27 11:19:22 -05:00
;;
esac
2022-02-27 11:19:22 -05:00
# Write the vfio modprobe config
printf "## This is an autogenerated file that stubs your graphic card for use with vfio
## This file should be placed inside /etc/modprobe.d/
# Uncomment the line below to \"hardcode\" your graphic card to be bound to the vfio-pci driver.
# In most cases this should not be neccessary, it will also prevent you from turning off vfio in the bootloader.
#options vfio_pci ids=%s
2022-03-01 03:02:29 -05:00
# Make sure vfio_pci is loaded before these modules: nvidia, nouveau, amdgpu and radeon
2022-02-27 11:19:22 -05:00
softdep nvidia pre: vfio vfio_pci
softdep nouveau pre: vfio vfio_pci
softdep amdgpu pre: vfio vfio_pci
softdep radeon pre: vfio vfio_pci
" "${GPU_DEVID}" > "$SCRIPTDIR/$MODPROBE/vfio.conf"
2022-02-27 11:19:22 -05:00
exec "$SCRIPTDIR/lib/get_USB_CTL.sh"
}
function main () {
SCRIPTDIR=$(dirname "$(which $0)" | perl -pe "s/\/\.\.\/lib//" | perl -pe "s/\/lib$//")
set_MODULES "$1"
2022-02-27 11:19:22 -05:00
}
main "$1"