quickpassthrough/vfio-setup

148 lines
5 KiB
Text
Raw Normal View History

2022-02-25 08:31:45 -05:00
#!/bin/bash
2022-02-26 18:10:21 -05:00
# Get the scripts directory
SCRIPTDIR=$(dirname `which $0`)
cd $SCRIPTDIR
2022-02-27 11:18:59 -05:00
# Get the config paths
source "$SCRIPTDIR/lib/paths.sh"
# Make sure all the scripts are executable
chmod +x "$SCRIPTDIR/lib/*"
2022-02-25 08:31:45 -05:00
# Clear the screen
clear
# Show the user a warning before we start
printf "Welcome to the VFIO enabler script!
The setup done by this script is quite complex and is prone to human error or hardware incompatibilities.
It is HIGHLY RECOMMENDED to make a backup/snapshot of your system using something like timeshift or snapper before starting.
2022-02-26 18:10:21 -05:00
Once everything is configured, your 2nd graphic card will hopefully be dedicated for use inside a virtual machine.
2022-02-25 08:31:45 -05:00
Press ENTER to continue once you have made a backup of your system.
"
read ENTER
clear
printf "This script assumes a few things:
* You have already enabled IOMMU, VT-d and/or AMD-v inside your UEFI/BIOS advanced settings.
2022-02-27 11:18:59 -05:00
* You have already added \"iommu=pt intel_iommu=on\" or \"iommu=pt amd_iommu=on\" to your
kernel boot arguments and booted your system with these kernel arguments active.
2022-02-25 08:31:45 -05:00
* You are comfortable with navigating and changing settings in your UEFI/BIOS.
* You know how edit your bootloader configuration.
* Your Linux distribution is an EFI installation (important to get VFIO working).
NOTE: If your computer no longer fully shut down after enabling IOMMU, then there is possibly a bug
with your motherboard and a piece of hardware in your system, it only prevents you from using
2022-02-26 18:10:21 -05:00
the system in a headless mode with working shutdown and is otherwise just an annoying
quirk with IOMMU on some boards.
2022-02-25 08:31:45 -05:00
This is a list of prerequisites you will be needing before starting with VFIO:
2022-02-27 11:18:59 -05:00
* 2 very different GPUs (iGPU/APU included), the easiest combination is to have 2 from different vendors (amd/intel/nvidia)
if both cards share the same device id (ex: both are identified as 1022:145c), then passthrough will most likely
not be possible unless you swap out one of the cards.
2022-02-26 18:10:21 -05:00
* A \"ghost display\" dummy plug for your second graphic card (or having it hooked to a separate input on your monitor).
2022-02-25 08:31:45 -05:00
* If you are planning to use the inegrated GPU on your CPU, make sure your monitor is connected to it before continuing.
* Preferably a motherboard verified to work with IOMMU and with good IOMMU groups.
https://reddit.com/r/vfio is a good resource for this info.
(If you are unsure, you will find out while using this script)
* It is also highly recommended to have access to your VM through VNC (RDP will not work) as once you pass through
a graphic card, it will most likely not possible to interact with it through spice and the normal qemu display window!
2022-02-27 11:18:59 -05:00
Press ENTER to start creating your config from scratch.
NOTE: continuing will delete the contents of \"$SCRIPTDIR/config\"
2022-02-25 08:31:45 -05:00
"
read ENTER
2022-02-26 18:10:21 -05:00
clear
2022-02-27 11:18:59 -05:00
if [ -d "$SCRIPTDIR/config" ];
then
rm -r "$SCRIPTDIR/config"
fi
# Get the CPU Vendor
CPU_VENDOR=$(cat /proc/cpuinfo | grep vendor | head -1 | cut -f 2 | cut -d " " -f 2)
CMDLINE="iommu=pt"
# Adjust our kernel_args based on cpu vendor
if [ "$CPU_VENDOR" == "GenuineIntel" ];
then
CMDLINE="$CMDLINE intel_iommu=on"
elif [ "$CPU_VENDOR" == "AuthenticAMD" ];
then
CMDLINE="$CMDLINE amd_iommu=on"
fi
2022-02-27 11:18:59 -05:00
# Make the directories
mkdir -p "$SCRIPTDIR/$MODPROBE"
mkdir -p "$SCRIPTDIR/$DEFAULT"
mkdir -p "$SCRIPTDIR/$INITRAMFS"
mkdir -p "$SCRIPTDIR/$QUICKEMU"
# Write the cmdline file
echo "$CMDLINE" > "$SCRIPTDIR/config/kernel_args"
2022-02-27 11:18:59 -05:00
# Copy system configs into our config folder so we can safely edit them
if [ -f "/etc/modules" ];
then
# This copies /etc/modules without the vfio module lines
grep -v "vfio" "/etc/modules" > "$SCRIPTDIR/$MODULES"
else
touch "$SCRIPTDIR/$MODULES"
fi
if [ -f "/etc/default/grub" ];
then
# Currently we do not modify bootloaders, will ask users to do it instead
#cp "/etc/default/grub" "$SCRIPTDIR/$DEFAULT/grub"
echo ""
2022-02-27 11:18:59 -05:00
fi
if [ -f "/etc/initramfs-tools/modules" ];
then
# This copies /etc/initramfs-tools/modules without the vfio modules
grep -v "vfio" "/etc/initramfs-tools/modules" > "$SCRIPTDIR/$INITRAMFS/modules"
2022-02-27 11:18:59 -05:00
else
touch "$SCRIPTDIR/$INITRAMFS"
fi
# Run ls-iommu so we can verify that IOMMU properly working
2022-03-01 07:07:56 -05:00
LS_IOMMU=$($SCRIPTDIR/utils/ls-iommu)
2022-02-26 18:10:21 -05:00
2022-03-01 07:07:56 -05:00
if [[ $LS_IOMMU =~ ^IOMMU[[:space:]]Group[[:space:]]+\*: ]];
then
printf "IOMMU IS NOT ENABLED!
Please enable IOMMU, VT-d or AMD-v inside your UEFI/BIOS and add \"$CMDLINE\"
to your kernel boot arguments and reboot your system, then re-run this script!
2022-02-26 18:10:21 -05:00
"
2022-03-01 07:07:56 -05:00
exit 1
else
echo "$LS_IOMMU"
fi
echo ""
read -p "Is there more than 1 group in the output above? [y/N]: " YESNO
2022-02-26 18:10:21 -05:00
case "${YESNO}" in
[Yy]*)
clear
;;
[Nn]*)
2022-03-01 07:07:56 -05:00
printf "Please enable IOMMU, VT-d or AMD-v inside your UEFI/BIOS and add \"$CMDLINE\"
to your kernel boot arguments and reboot your system, then re-run this script!
"
2022-02-26 18:10:21 -05:00
exit 1
;;
*)
2022-03-01 07:07:56 -05:00
printf "Please enable IOMMU, VT-d or AMD-v inside your UEFI/BIOS and add \"$CMDLINE\"
to your kernel boot arguments and reboot your system, then re-run this script!
"
2022-02-26 18:10:21 -05:00
exit 1
;;
esac
2022-02-27 11:18:59 -05:00
exec "$SCRIPTDIR/lib/get_GPU.sh"