initial commit to prepare for rewrite into golang for actual use

This commit is contained in:
HikariKnight 2023-04-05 13:32:38 +02:00
parent 0d4f63f371
commit 5da1a88296
21 changed files with 129 additions and 1 deletions

View file

@ -0,0 +1,152 @@
#!/bin/bash
# shellcheck disable=SC1091
function make_BACKUP () {
local BACKUPDIR
BACKUPDIR="$SCRIPTDIR/backup"
if [ ! -d "$BACKUPDIR" ];
then
# Make the backup directories and backup the files
if [ -d "/etc/initramfs-tools" ];
then
mkdir -p "$BACKUPDIR/etc/initramfs-tools"
cp -v "/etc/initramfs-tools/modules" "$BACKUPDIR/etc/initramfs-tools/modules"
cp -v "/etc/modules" "$BACKUPDIR/etc/modules"
elif [ -d "/etc/dracut.conf" ];
then
mkdir -p "$BACKUPDIR/etc/dracut.conf.d"
if [ -f "/etc/dracut.conf.d/10-vfio.conf" ];
then
cp -v "/etc/dracut.conf.d/10-vfio.conf" "$BACKUPDIR/etc/dracut.conf.d/10-vfio.conf"
fi
elif [ -f "/etc/mkinitcpio.conf" ];
then
mkdir -p "$BACKUPDIR/etc"
cp -v "/etc/mkinitcpio.conf" "$BACKUPDIR/etc/mkinitcpio.conf"
fi
if [ -f "/etc/default/grub" ];
then
mkdir -p "$BACKUPDIR/etc/default"
cp -v "/etc/default/grub" "$BACKUPDIR/etc/default/grub"
fi
if [ -d "/etc/modprobe.d" ];
then
mkdir -p "$BACKUPDIR/etc/modprobe.d"
# If a vfio.conf file exists, backup that too
if [ -f "/etc/modprobe.d/vfio.conf" ];
then
cp -v "/etc/modprobe.d/vfio.conf" "$BACKUPDIR/etc/modprobe.d/vfio.conf"
fi
fi
printf "Backup completed!\n"
else
echo "
A backup already exists!
backup skipped.
"
fi
}
function copy_FILES () {
echo "Starting copying files to the system!"
if [ -d "/etc/modprobe.d" ];
then
sudo cp -v "$SCRIPTDIR/$MODPROBE/vfio.conf" "/etc/modprobe.d/vfio.conf"
fi
if [ -d "/etc/initramfs-tools" ];
then
sudo cp -v "$SCRIPTDIR/$ETCMODULES" "/etc/modules"
sudo cp -v "$SCRIPTDIR/$INITRAMFS/modules" "/etc/initramfs-tools/modules"
echo "
Rebuilding initramfs"
sudo update-initramfs -u
elif [ -f "/etc/dracut.conf" ];
then
cp -v "$SCRIPTDIR/$DRACUT/10-vfio.conf" "/etc/dracut.conf.d/10-vfio.conf"
echo "
Rebuilding initramfs"
sudo dracut -f -v --kver "$(uname -r)"
elif [ -f "/etc/mkinitcpio.conf" ];
then
cp -v "$SCRIPTDIR/$MKINITCPIO" "/etc/mkinitcpio.conf"
echo "
Rebuilding initramfs"
sudo mkinitcpio -P
else
echo "
Unsupported initramfs infrastructure
In order to make vfio work, please add these modules to your
initramfs and make them load early, then rebuild initramfs.
vfio
vfio_iommu_type1
vfio_pci
vfio_virqfd
Press ENTER to continue once you have done the above."
read -r
fi
}
function apply_CHANGES () {
clear
# Get the config paths
source "$SCRIPTDIR/lib/paths.sh"
echo "Configuration is now complete and these files have been generated for your system:
$SCRIPTDIR/$ETCMODULES
$SCRIPTDIR/$INITRAMFS/modules
$SCRIPTDIR/$MODPROBE/vfio.conf
By proceeding, a backup of your system's version of these files will be placed in
$SCRIPTDIR/backup
unless a backup already exist.
Then the files above will be copied to your system followed by running followed by updating your
initramfs and then attempt adding new kernel arguments to your bootloader."
read -r -p "Do you want to proceed with the installation of the files? (no=quit) [Y/n]: " YESNO
case "${YESNO}" in
[Nn]*)
exit 1
;;
*)
make_BACKUP
copy_FILES
exec "$SCRIPTDIR/lib/set_CMDLINE.sh"
;;
esac
}
function main () {
SCRIPTDIR=$(dirname "$(realpath "$0")" | perl -pe "s/\/\.\.\/lib//" | perl -pe "s/\/lib$//")
apply_CHANGES
}
main

41
old_bash_version/lib/get_GPU.sh Executable file
View file

@ -0,0 +1,41 @@
#!/bin/bash
function get_GPU () {
clear
printf "These are your graphic cards, they have to be in separate groups.
The graphic card you want to passthrough cannot be in a group with other devices that
does not belong to itself. Both cards must also have unique hardware ids [xxxx:yyyy]!:
"
echo "#------------------------------------------#"
"$SCRIPTDIR/utils/ls-iommu" -g -F name,device_id,optional_revision
echo "#------------------------------------------#"
printf "
Press q to quit
"
read -r -p "Which group number do you want to check?: " IOMMU_GROUP
case "${IOMMU_GROUP}" in
[1-9]*)
exec "$SCRIPTDIR/lib/get_GPU_GROUP.sh" "$IOMMU_GROUP"
;;
[Qq]*)
echo "Aborted, your setup is incomplete!
DO NOT use any of the files from $SCRIPTDIR/config !
"
;;
*)
exec "$SCRIPTDIR/lib/get_GPU.sh"
;;
esac
}
function main () {
SCRIPTDIR=$(dirname "$(realpath "$0")" | perl -pe "s/\/\.\.\/lib//" | perl -pe "s/\/lib$//")
get_GPU
}
main

View file

@ -0,0 +1,84 @@
#!/bin/bash
# shellcheck disable=SC1091
function get_GPU_GROUP () {
clear
# Get the config paths
source "$SCRIPTDIR/lib/paths.sh"
printf "For this card to be passthrough-able, it must contain only:
* The GPU/Graphic card
* The GPU Audio Controller
Optionally it may also include:
* GPU USB Host Controller
* GPU Serial Port
* GPU USB Type-C UCSI Controller
* PCI Bridge (if they are in their own IOMMU groups)
NOTE: If the audio controller is missing, it might be in a separate IOMMU group, please run
\"%s/utils/ls-iommu\" -i %s -rr -F \"pciaddr,subclass_name:,name,device_id,optional_revision\"
and add the PCI address and deviceID to the config manually (this is not done automatically because the -rr flag is unreliable if the system has 2 cards from the same vendor)
" "$SCRIPTDIR" "$1"
echo "#------------------------------------------#"
"$SCRIPTDIR/utils/ls-iommu" -i "$1" -r -F subclass_name:,name,device_id,optional_revision # | cut -d " " -f 1-5,6- | perl -pe "s/\[[0-9a-f]{4}\]: //"
echo "#------------------------------------------#"
printf "
To use any of these devices for passthrough ALL of them (except PCI bridges in their own IOMMU groups) has to be passed through to the VMs\
To return to the previous page just press ENTER without typing in anything.
"
read -r -p "Do you want to use these devices for passthrough? [y/N]: " YESNO
case "${YESNO}" in
[Yy]*)
# Get the hardware ids from the selected group
local GPU_DEVID
GPU_DEVID=$("$SCRIPTDIR/utils/ls-iommu" -i "$1" -r --id | perl -pe "s/\n/,/" | perl -pe "s/,$/\n/")
# Get the PCI ids
local PCI_ID
PCI_ID=$("$SCRIPTDIR/utils/ls-iommu" -i "$1" -r --pciaddr | perl -pe "s/([0-9a-f]{2}:[0-9a-f]{2}.[0-9a-f]{1})\n/\"\1\" /" | perl -pe "s/\s$//")
# Write the GPU_PCI_IDs to the config that quickemu might make use of in the future
echo "GPU_PCI_ID=($PCI_ID)
USB_CTL_ID=()" > "$SCRIPTDIR/$QUICKEMU/qemu-vfio_vars.conf"
# Get the GPU ROM
"$SCRIPTDIR/lib/get_GPU_ROM.sh" "$1"
# Start setting up modules
if [ -d "/etc/initramfs-tools" ];
then
exec "$SCRIPTDIR/lib/set_INITRAMFSTOOLS.sh" "$GPU_DEVID"
elif [ -d "/etc/dracut.conf" ];
then
exec "$SCRIPTDIR/lib/set_DRACUT.sh" "$GPU_DEVID"
elif [ -f "/etc/mkinitcpio.conf" ];
then
exec "$SCRIPTDIR/lib/set_MKINITCPIO.sh" "$GPU_DEVID"
else
# Bind GPU to VFIO
"$SCRIPTDIR/lib/set_VFIO.sh" "$GPU_DEVID"
# Configure modprobe
"$SCRIPTDIR/lib/set_MODPROBE.sh" "$GPU_DEVID"
fi
;;
*)
exec "$SCRIPTDIR/lib/get_GPU.sh"
;;
esac
}
function main () {
SCRIPTDIR=$(dirname "$(realpath "$0")" | perl -pe "s/\/\.\.\/lib//" | perl -pe "s/\/lib$//")
get_GPU_GROUP "$1"
}
main "$1"

View file

@ -0,0 +1,70 @@
#!/bin/bash
# shellcheck disable=SC1091,SC2024
function get_GPU_ROM () {
clear
# Get the config paths
source "$SCRIPTDIR/lib/paths.sh"
VBIOS_PATH=$("$SCRIPTDIR/utils/ls-iommu" -g -i "$1" --rom)
echo "We will now attempt to dump the vbios of your selected GPU.
Passing a VBIOS rom to the card used for passthrough is required for some cards, but not all.
Some cards also requires you to patch your VBIOS romfile, check online if this is neccessary for your card.
The VBIOS will be read from $VBIOS_PATH
This process will require the use of sudo and will run the following commands:
echo 1 | sudo tee $VBIOS_PATH
sudo cat $VBIOS_PATH > $SCRIPTDIR/$QUICKEMU/vfio_card.rom
echo 0 | sudo tee $VBIOS_PATH
"
read -r -p "Do you want to dump the VBIOS, choosing N will skip this step [y/N]: " YESNO
case "${YESNO}" in
[Yy]*)
echo 1 | sudo tee "$VBIOS_PATH"
sudo cat "$VBIOS_PATH" > "$SCRIPTDIR/$QUICKEMU/vfio_card.rom"
sudo md5sum "$VBIOS_PATH" | cut -d " " -f 1 > "$SCRIPTDIR/$QUICKEMU/vfio_card.rom.md5"
local ROM_MD5
ROM_MD5=$(sudo md5sum "$VBIOS_PATH" | cut -d " " -f 1)
echo 0 | sudo tee "$VBIOS_PATH"
local ROMFILE_MD5
ROMFILE_MD5=$(md5sum "$SCRIPTDIR/$QUICKEMU/vfio_card.rom" | cut -d " " -f 1)
if [ -f "$SCRIPTDIR/$QUICKEMU/vfio_card.rom" ];
then
if [ "$ROM_MD5" == "$ROMFILE_MD5" ];
then
echo "Checksums match!"
echo "Dumping of VBIOS successful!"
echo 'GPU_ROMFILE="vfio_card.rom"' >> "$SCRIPTDIR/$QUICKEMU/qemu-vfio_vars.conf"
read -r -p "Press ENTER to continue."
else
echo "Checksums does not match!"
echo "Dumping of VBIOS failed, skipping romfile"
mv "$SCRIPTDIR/$QUICKEMU/vfio_card.rom" "$SCRIPTDIR/$QUICKEMU/vfio_card.rom.fail"
echo 'GPU_ROMFILE=""' >> "$SCRIPTDIR/$QUICKEMU/qemu-vfio_vars.conf"
read -r -p "Press ENTER to continue."
fi
else
echo 'GPU_ROMFILE=""' >> "$SCRIPTDIR/$QUICKEMU/qemu-vfio_vars.conf"
fi
;;
[Nn]*)
echo 'GPU_ROMFILE=""' >> "$SCRIPTDIR/$QUICKEMU/qemu-vfio_vars.conf"
;;
*)
echo 'GPU_ROMFILE=""' >> "$SCRIPTDIR/$QUICKEMU/qemu-vfio_vars.conf"
;;
esac
}
function main () {
SCRIPTDIR=$(dirname "$(realpath "$0")" | perl -pe "s/\/\.\.\/lib//" | perl -pe "s/\/lib$//")
get_GPU_ROM "$1"
}
main "$1"

View file

@ -0,0 +1,39 @@
#!/bin/bash
function get_USB_CTL () {
clear
printf "THIS STEP IS OPTIONAL IF YOU DO NOT PLAN TO USE ANYTHING OTHER THAN MOUSE AND KEYBOARD!
The USB Controller you want to passthrough cannot be in a group with other devices.
Passing through a whole USB Controller (a set of hardwired 1-4 usb ports on the motherboard)
is only needed if you intend to use other devices than just mouse and keyboard with the VFIO enabled VM.
"
echo "#------------------------------------------#"
"$SCRIPTDIR/utils/ls-iommu" -u -F name,device_id,optional_revision
echo "#------------------------------------------#"
printf "
Press q to quit
"
read -r -p "Which group number do you want to check?: " IOMMU_GROUP
case "${IOMMU_GROUP}" in
[1-9]*)
exec "$SCRIPTDIR/lib/get_USB_CTL_GROUP.sh" "$IOMMU_GROUP"
;;
[Qq]*)
exec "$SCRIPTDIR/lib/apply_CHANGES.sh"
;;
*)
exec "$SCRIPTDIR/lib/apply_CHANGES.sh"
;;
esac
}
function main () {
SCRIPTDIR=$(dirname "$(realpath "$0")" | perl -pe "s/\/\.\.\/lib//" | perl -pe "s/\/lib$//")
get_USB_CTL
}
main

View file

@ -0,0 +1,48 @@
#!/bin/bash
# shellcheck disable=SC1091
function get_USB_CTL_GROUP () {
clear
# Get the config paths
source "$SCRIPTDIR/lib/paths.sh"
printf "
For this USB controller device to be passthrough-able, it must be the ONLY device in this group!
Passing through more than just the USB controller can in some cases cause system issues
if you do not know what you are doing.
"
echo "#------------------------------------------#"
"$SCRIPTDIR/utils/ls-iommu" -i "$1" -F subclass_name:,name,device_id,optional_revision
echo "#------------------------------------------#"
printf "
To use any of the devices shown for passthrough, all of them have to be passed through
To return to the previous page just press ENTER.
"
read -r -p "Do you want to use the displayed devices for passthrough? [y/N]: " YESNO
case "${YESNO}" in
[Yy]*)
# Get the PCI ids
local PCI_ID
PCI_ID=$("$SCRIPTDIR/utils/ls-iommu" -i "$1" --pciaddr | perl -pe "s/([0-9a-f]{4}:[0-9a-f]{2}:[0-9a-f]{2}.[0-9a-f]{1})\n/\"\1\" /" | perl -pe "s/\s$//")
# Replace the blank USB_CTL_ID with the PCI_ID for the usb controller the user wants to pass through
perl -pi -e "s/USB_CTL_ID=\(\)/USB_CTL_ID=\($PCI_ID\)/" "$SCRIPTDIR/$QUICKEMU/qemu-vfio_vars.conf"
exec "$SCRIPTDIR/lib/apply_CHANGES.sh"
;;
*)
exec "$SCRIPTDIR/lib/get_USB_CTL.sh"
;;
esac
}
function main () {
SCRIPTDIR=$(dirname "$(realpath "$0")" | perl -pe "s/\/\.\.\/lib//" | perl -pe "s/\/lib$//")
get_USB_CTL_GROUP "$1"
}
main "$1"

18
old_bash_version/lib/paths.sh Executable file
View file

@ -0,0 +1,18 @@
#!/bin/bash
# shellcheck disable=SC2034
MODPROBE="config/etc/modprobe.d"
INITRAMFS="config/etc/initramfs-tools"
ETCMODULES="config/etc/modules"
DEFAULT="config/etc/default"
QUICKEMU="config/quickemu"
DRACUT="config/etc/dracut.conf.d"
MKINITCPIO="config/etc/mkinitcpio.conf"
READAPI="wget -O-"
DOWNLOAD="wget -0 \"$SCRIPTDIR/utils/ls-iommu.tar.gz\""
# Get the tool to use for downloading
if [ -f "/usr/bin/curl" ];
then
READAPI="curl"
DOWNLOAD="curl -JLo ls-iommu.tar.gz"
fi

View file

@ -0,0 +1,188 @@
#!/bin/bash
# shellcheck disable=SC1091
# Function to configure systemd-boot using kernelstub
function set_KERNELSTUB () {
# Separator
printf "
############################################################
"
# Tell what we are going to do
echo "Adding vfio kernel arguments to systemd-boot using kernelstub"
# Get the config paths
source "${SCRIPTDIR}/lib/paths.sh"
# Check if systemd-boot already has vfio parameters from before
KERNELSTUB_TEST=$(sudo kernelstub -p 2>&1 | grep "Kernel Boot Options" | perl -pe "s/.+Kernel Boot Options:\..+(vfio_pci.ids=.+ ).+/\1/")
# If there are already vfio_pci parameters in kernelstub
if [[ "$KERNELSTUB_TEST" =~ vfio_pci.ids ]] ;
then
# Remove the old parameters
sudo kernelstub -d "$KERNELSTUB_TEST"
sudo kernelstub -d "vfio_pci.disable_vga=1"
sudo kernelstub -d "vfio_pci.disable_vga=0"
fi
# Apply new parameters
CMDLINE=$(cat "${SCRIPTDIR}/config/kernel_args")
sudo kernelstub -a "$CMDLINE"
}
# Function to configure grub
function set_GRUB () {
# Separator
printf "
############################################################
"
# Get the config paths
source "$SCRIPTDIR/lib/paths.sh"
local CMDLINE
CMDLINE=$(cat "$SCRIPTDIR/config/kernel_args")
# HIGHLY EXPERIMENTAL!
local GRUB_CMDLINE
local GRUB_CMDLINE_LINUX
# Check if there is a GRUB_CMDLINE_LINUX_DEFAULT line in grub config
if grep -q "GRUB_CMDLINE_LINUX_DEFAULT=" "$SCRIPTDIR/$DEFAULT/grub" ;
then
# Update the GRUB_CMDLINE_LINUX_DEFAULT line
GRUB_CMDLINE=$(grep -P "^GRUB_CMDLINE_LINUX_DEFAULT" "/etc/default/grub" | perl -pe "s/GRUB_CMDLINE_LINUX_DEFAULT=\"(.+)\"/\1/" | perl -pe "s/(amd|intel)_iommu=(on|1)|iommu=(pt|on)|vfio_pci.ids=.+|vfio_pci.disable_vga=\d{1}//g" | perl -pe "s/(^\s+|\s+$)//g")
GRUB_CMDLINE_LINUX=$(grep -P "^GRUB_CMDLINE_LINUX_DEFAULT" "/etc/default/grub")
perl -pi -e "s/${GRUB_CMDLINE_LINUX}/GRUB_CMDLINE_LINUX_DEFAULT=\"${GRUB_CMDLINE} ${CMDLINE}\"/" "${SCRIPTDIR}/$DEFAULT/grub"
else
# Update the GRUB_CMDLINE_LINUX line
GRUB_CMDLINE=$(grep -P "^GRUB_CMDLINE_LINUX" "/etc/default/grub" | perl -pe "s/GRUB_CMDLINE_LINUX=\"(.+)\"/\1/" | perl -pe "s/(amd|intel)_iommu=(on|1)|iommu=(pt|on)|vfio_pci.ids=.+|vfio_pci.disable_vga=\d{1}//g" | perl -pe "s/(^\s+|\s+$)//g")
GRUB_CMDLINE_LINUX=$(grep -P "^GRUB_CMDLINE_LINUX" "/etc/default/grub")
perl -pi -e "s/${GRUB_CMDLINE_LINUX}/GRUB_CMDLINE_LINUX=\"${GRUB_CMDLINE} ${CMDLINE}\"/" "${SCRIPTDIR}/$DEFAULT/grub"
fi
echo "The script will now replace your default grub file with a new one.
Then attempt to update grub and generate a new grub.cfg.
If generating the grub.cfg file fails, you can find a backup of your grub default file here:
$SCRIPTDIR/backup/etc/default/grub
"
read -r -p "Press ENTER to continue"
sudo cp -v "$SCRIPTDIR/$DEFAULT/grub" "/etc/default/grub"
# Generate grub.cfg
if [ -d "/boot/grub" ];
then
sudo grub-mkconfig -o "/boot/grub/grub.cfg"
else
sudo grub-mkconfig -o "/boot/grub2/grub.cfg"
fi
echo ""
read -r -p "Please verify there were no errors generating the grub.cfg file, then press ENTER"
}
function show_FINISH () {
# Separator
printf "
############################################################
"
# Get the config paths
source "$SCRIPTDIR/lib/paths.sh"
local CMDLINE
CMDLINE=$(cat "$SCRIPTDIR/config/kernel_args")
echo "Configuration is now complete!"
if [ "$1" == 0 ];
then
printf "For VFIO to work properly you need to make sure these kernel parameters are in your bootloader entry:
#-----------------------------------------------#
%s
#-----------------------------------------------#
" "$CMDLINE"
fi
echo "Restart your system and run
\"$SCRIPTDIR/vfio-verify\"
to check if your GPU is properly set up.
If the graphic card is bound to vfio-pci then you can
proceed to add it to your virtual machines.
A backup the files we replaced on your system can be found inside
$SCRIPTDIR/backup/
In order to restore these files just copy them back to your system and
rebuild your initramfs image.
You can remove the the vfio_pci kernel arguments from the linux line in your bootloader
to disable/unbind the graphic card from the vfio driver on boot.
The files inside \"$SCRIPTDIR/$QUICKEMU\" are currently unused files, however they provide
the required information that the QuickEMU project can hook into and use to add support for VFIO enabled VMs.
The PCI Devices with these IDs are what you should add to your VMs using Virt Manager:
NOTE: Some AMD GPUs will require the vendor-reset kernel module from https://github.com/gnif/vendor-reset to be installed!"
source "${SCRIPTDIR}/config/quickemu/qemu-vfio_vars.conf"
for dev in "${GPU_PCI_ID[@]}"
do
echo "* $dev"
done
for dev in "${USB_CTL_ID[@]}"
do
echo "* $dev"
done
echo "
To add the graphic card to your VM using qemu directly, use the following arguments:"
for dev in "${GPU_PCI_ID[@]}"
do
echo -n "-device vfio-pci,host=$dev "
done
printf "\n"
echo "
For performance tuning and advanced configuration look at:
https://github.com/HikariKnight/vfio-setup-docs/wiki"
}
function set_CMDLINE () {
# Make a variable to tell if
local BOOTLOADER_AUTOCONFIG
BOOTLOADER_AUTOCONFIG=0
# If kernelstub is detected (program to manage systemd-boot)
if which kernelstub > /dev/null 2>&1 ;
then
# Configure kernelstub
set_KERNELSTUB
BOOTLOADER_AUTOCONFIG=1
fi
# If grub exists
if which grub-mkconfig > /dev/null 2>&1 ;
then
# Configure grub
set_GRUB
BOOTLOADER_AUTOCONFIG=1
fi
show_FINISH $BOOTLOADER_AUTOCONFIG
}
function main () {
SCRIPTDIR=$(dirname "$(realpath "$0")" | perl -pe "s/\/\.\.\/lib//" | perl -pe "s/\/lib$//")
set_CMDLINE
}
main

View file

@ -0,0 +1,30 @@
#!/bin/bash
# shellcheck disable=SC1091
function set_DRACUT () {
# Get the config paths
source "$SCRIPTDIR/lib/paths.sh"
# Write the dracut config
echo "add_drivers+=\" vfio_pci vfio vfio_iommu_type1 vfio_virqfd \"" > "$SCRIPTDIR/$DRACUT/10-vfio.conf"
# Get the kernel_args file content
CMDLINE=$(cat "$SCRIPTDIR/config/kernel_args")
# Update kernel_args to load the vfio_pci module early in dracut (as dracut uses kernel arguments for early loading)
echo "$CMDLINE rd.driver.pre=vfio_pci" > "$SCRIPTDIR/config/kernel_args"
# Bind GPU to VFIO
"$SCRIPTDIR/lib/set_VFIO.sh" "$1"
# Configure modprobe
exec "$SCRIPTDIR/lib/set_MODPROBE.sh" "$1"
}
function main () {
SCRIPTDIR=$(dirname "$(realpath "$0")" | perl -pe "s/\/\.\.\/lib//" | perl -pe "s/\/lib$//")
set_DRACUT "$1"
}
main "$1"

View file

@ -0,0 +1,66 @@
#!/bin/bash
# shellcheck disable=SC1091
function insert_INITRAMFSTOOLS() {
# Get the header and enabled modules separately from the /etc/modules file
local MODULES_HEADER
local MODULES_ENABLED
local VENDOR_RESET
MODULES_HEADER=$(head -n "$1" "$2" | grep -P "^#" | grep -v "# Added by quickpassthrough")
MODULES_ENABLED=$(grep -vP "^#" "$2" | grep -v "vendor-reset" | perl -pe "s/^\n//")
VENDOR_RESET=0
# If vendor-reset is present
if grep -q "vendor-reset" "$2" ;
then
VENDOR_RESET=1
fi
# 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
vfio_iommu_type1
vfio_pci
vfio_virqfd
#############################
" >> "$2"
# Write the previously enabled modules under vfio in the load order
echo "$MODULES_ENABLED" >> "$2"
}
function set_INITRAMFSTOOLS () {
# Get the config paths
source "$SCRIPTDIR/lib/paths.sh"
# Insert modules in the correct locations as early as possible without
# conflicting with vendor-reset module if it is enabled
insert_INITRAMFSTOOLS 4 "$SCRIPTDIR/$ETCMODULES"
insert_INITRAMFSTOOLS 11 "$SCRIPTDIR/$INITRAMFS/modules"
# Bind GPU to VFIO
"$SCRIPTDIR/lib/set_VFIO.sh" "$1"
# Configure modprobe
exec "$SCRIPTDIR/lib/set_MODPROBE.sh" "$1"
}
function main () {
SCRIPTDIR=$(dirname "$(realpath "$0")" | perl -pe "s/\/\.\.\/lib//" | perl -pe "s/\/lib$//")
set_INITRAMFSTOOLS "$1"
}
main "$1"

View file

@ -0,0 +1,35 @@
#!/bin/bash
# shellcheck disable=SC1091
function set_MKINITCPIO () {
# Get the config paths
source "$SCRIPTDIR/lib/paths.sh"
# Grab the current modules but exclude vfio and vendor-reset
CURRENTMODULES=$(grep -P "^MODULES" "$SCRIPTDIR/$MKINITCPIO" | perl -pe "s/MODULES=\((.+)\)/\1/")
MODULES="$(grep -P "^MODULES" "$SCRIPTDIR/$MKINITCPIO" | perl -pe "s/MODULES=\((.+)\)/\1/" | perl -pe "s/\s?(vfio_iommu_type1|vfio_pci|vfio_virqfd|vfio|vendor-reset)\s?//g")"
# Check if vendor-reset is present
if [[ $CURRENTMODULES =~ "vendor-reset" ]];
then
# Inject vfio modules with vendor-reset
perl -pi -e "s/MODULES=\(${CURRENTMODULES}\)/MODULES=\(vendor-reset vfio vfio_iommu_type1 vfio_pci vfio_virqfd ${MODULES}\)/" "$SCRIPTDIR/$MKINITCPIO"
else
# Inject vfio modules
perl -pi -e "s/MODULES=\(${CURRENTMODULES}\)/MODULES=\(vfio vfio_iommu_type1 vfio_pci vfio_virqfd ${MODULES}\)/" "$SCRIPTDIR/$MKINITCPIO"
fi
# Bind GPU to VFIO
"$SCRIPTDIR/lib/set_VFIO.sh" "$1"
# Configure modprobe
exec "$SCRIPTDIR/lib/set_MODPROBE.sh" "$1"
}
function main () {
SCRIPTDIR=$(dirname "$(realpath "$0")" | perl -pe "s/\/\.\.\/lib//" | perl -pe "s/\/lib$//")
set_MKINITCPIO "$1"
}
main "$1"

View file

@ -0,0 +1,41 @@
#!/bin/bash
# shellcheck disable=SC1091
function set_MODPROBE () {
# Get the config paths
source "$SCRIPTDIR/lib/paths.sh"
# Assign the GPU device ids to a variable
GPU_DEVID="$1"
# If VGA is disabled
if grep -q "vfio_pci.disable_vga=1" "$SCRIPTDIR/config/kernel_args" ;
then
# Modify our GPU_DEVID line to containe disable_vga=1
GPU_DEVID="${GPU_DEVID} disable_vga=1"
fi
# 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
# Make sure vfio_pci is loaded before these modules: nvidia, nouveau, amdgpu and radeon
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"
exec "$SCRIPTDIR/lib/get_USB_CTL.sh"
}
function main () {
SCRIPTDIR=$(dirname "$(realpath "$0")" | perl -pe "s/\/\.\.\/lib//" | perl -pe "s/\/lib$//")
set_MODPROBE "$1"
}
main "$1"

View file

@ -0,0 +1,47 @@
#!/bin/bash
# shellcheck disable=SC1091
function set_VFIO () {
# Get the config paths
source "$SCRIPTDIR/lib/paths.sh"
# Assign the GPU device ids to a variable
GPU_DEVID="$1"
# Get the kernel_args file content
CMDLINE=$(cat "$SCRIPTDIR/config/kernel_args")
# Ask if we shall disable video output on this card
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."
read -r -p "Do you want to force disable video output in linux on this card? [Y/n]: " DISABLE_VGA
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"
;;
[Nn]*)
# Update kernel_args file
echo "${CMDLINE} vfio_pci.ids=${GPU_DEVID}" > "$SCRIPTDIR/config/kernel_args"
;;
*)
# 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"
;;
esac
}
function main () {
SCRIPTDIR=$(dirname "$(realpath "$0")" | perl -pe "s/\/\.\.\/lib//" | perl -pe "s/\/lib$//")
set_VFIO "$1"
}
main "$1"