Skip to content

Instantly share code, notes, and snippets.

@maierru
Created August 13, 2024 12:59
Show Gist options
  • Save maierru/954c670918c0db62155b3812d4797666 to your computer and use it in GitHub Desktop.
Save maierru/954c670918c0db62155b3812d4797666 to your computer and use it in GitHub Desktop.
GPU Passthrough proxmox setup
#!/bin/bash
# https://www.reddit.com/r/homelab/comments/b5xpua/the_ultimate_beginners_guide_to_gpu_passthrough/
# intel based script
apt-get update
apt-get upgrade -y
echo "current value of GRUB_CMDLINE_LINUX_DEFAULT"
grep GRUB_CMDLINE_LINUX_DEFAULT /etc/default/grub
sed -i 's/GRUB_CMDLINE_LINUX_DEFAULT=.*/GRUB_CMDLINE_LINUX_DEFAULT="quiet intel_iommu=on"/g' /etc/default/grub
echo "new value"
grep GRUB_CMDLINE_LINUX_DEFAULT /etc/default/grub
update-grub
modules=(vfio vfio_iommu_type1 vfio_pci vfio_virqfd)
for module in "${modules[@]}"; do
if ! grep -q "^$module" /etc/modules; then
echo "$module" | tee -a /etc/modules
fi
done
lines=( "options vfio_iommu_type1 allow_unsafe_interrupts=1" )
for line in "${lines[@]}"; do
if ! grep -q "^$line" /etc/modprobe.d/iommu_unsafe_interrupts.conf; then
echo "$line" | tee -a /etc/modprobe.d/iommu_unsafe_interrupts.conf
fi
done
lines=("options kvm ignore_msrs=1" )
for line in "${lines[@]}"; do
if ! grep -q "^$line" /etc/modprobe.d/kvm.conf; then
echo "$line" | tee -a /etc/modprobe.d/kvm.conf
fi
done
drivers=(nouveau nvidia radeon)
for driver in "${drivers[@]}"; do
if ! grep -q "^blacklist $driver" /etc/modprobe.d/blacklist.conf; then
echo "blacklist $driver" | tee -a /etc/modprobe.d/blacklist.conf
fi
done
gpu_address=$(lspci -nn | grep -i "vga\|3d\|2d" | grep -v "Audio" | head -n 1 | cut -d ' ' -f 1 | cut -d '.' -f 1)
ids=""
# getting vendors and devices id for vga and audio from address
for device in $(lspci -n -s $gpu_address | cut -d ' ' -f 3); do
vendor=$(echo $device | cut -d ':' -f 1)
device=$(echo $device | cut -d ':' -f 2)
if [ -z "$ids" ]; then
ids="$vendor:$device"
else
ids="$ids,$vendor:$device"
fi
done
# exclude from vfio-pci
if ! grep -q "^options vfio-pci ids=$ids disable_vga=1" /etc/modprobe.d/vfio.conf; then
echo "options vfio-pci ids=$ids disable_vga=1" | tee -a /etc/modprobe.d/vfio.conf
fi
update-initramfs -u
echo "Do you want to reboot now? (y/n)"
read answer
if [ "$answer" != "${answer#[Yy]}" ] ;then
reboot
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment