single-gpu-passthrough/hooks/vfio-startup.sh

157 lines
5.8 KiB
Bash
Raw Normal View History

2020-04-25 04:32:07 +00:00
#!/bin/bash
2022-07-03 20:13:18 +00:00
#############################################################################
## ______ _ _ _______ _ _ ##
## (_____ \(_) | |(_______) | | | | ##
## _____) )_ _ _ _____ | | _ _ _ _ | |__ _____ __| | ##
## | ____/| |( \ / )| ___ || || | | | | | | || _ \ | ___ | / _ | ##
## | | | | ) X ( | ____|| || |__| | | |_| || |_) )| ____|( (_| | ##
## |_| |_|(_/ \_)|_____) \_)\______)|____/ |____/ |_____) \____| ##
## ##
#############################################################################
###################### Credits ###################### ### Update PCI ID'S ###
## Lily (PixelQubed) for editing the scripts ## ## ##
## RisingPrisum for providing the original scripts ## ## update-pciids ##
## Void for testing and helping out in general ## ## ##
## .Chris. for testing and helping out in general ## ## Run this command ##
## WORMS for helping out with testing ## ## if you dont have ##
##################################################### ## names in your ##
2022-07-03 20:13:18 +00:00
## The VFIO community for using the scripts and ## ## lspci feedback ##
## testing them for us! ## ## in your terminal ##
##################################################### #######################
################################# Variables #################################
## Adds current time to var for use in echo for a cleaner log and script ##
DATETIME=$(date +"%m/%d/%Y %R:%S :")
2022-07-03 20:13:18 +00:00
## Sets dispmgr var as null ##
DISPMGR="null"
################################## Script ###################################
echo "$DATETIME Beginning of Startup!"
2022-07-03 20:13:18 +00:00
2020-04-25 04:32:07 +00:00
2020-10-04 01:05:49 +00:00
function stop_display_manager_if_running {
2022-07-03 20:13:18 +00:00
## Get display manager on systemd based distros ##
if [[ -x /run/systemd/system ]] && echo "$DATETIME Distro is using Systemd"; then
2022-07-03 20:13:18 +00:00
DISPMGR="$(grep 'ExecStart=' /etc/systemd/system/display-manager.service | awk -F'/' '{print $(NF-0)}')"
echo "$DATETIME Display Manager = $DISPMGR"
2022-07-03 20:13:18 +00:00
## Stop display manager using systemd ##
if systemctl is-active --quiet "$DISPMGR.service"; then
grep -qsF "$DISPMGR" "/tmp/vfio-store-display-manager" || echo "$DISPMGR" >/tmp/vfio-store-display-manager
systemctl stop "$DISPMGR.service"
systemctl isolate multi-user.target
2020-12-22 03:14:44 +00:00
fi
2022-07-03 20:13:18 +00:00
while systemctl is-active --quiet "$DISPMGR.service"; do
sleep "1"
2020-12-22 03:14:44 +00:00
done
return
2022-07-03 20:13:18 +00:00
2020-10-04 01:05:49 +00:00
fi
2022-07-03 20:13:18 +00:00
}
function kde-clause {
echo "$DATETIME Display Manager = display-manager"
2022-07-03 20:13:18 +00:00
## Stop display manager using systemd ##
if systemctl is-active --quiet "display-manager.service"; then
grep -qsF "display-manager" "/tmp/vfio-store-display-manager" || echo "display-manager" >/tmp/vfio-store-display-manager
systemctl stop "display-manager.service"
2020-10-04 01:05:49 +00:00
fi
2022-07-03 20:13:18 +00:00
while systemctl is-active --quiet "display-manager.service"; do
sleep 2
done
return
2020-10-04 01:05:49 +00:00
}
2022-07-03 20:13:18 +00:00
####################################################################################################################
## Checks to see if your running KDE. If not it will run the function to collect your display manager. ##
## Have to specify the display manager because kde is weird and uses display-manager even though it returns sddm. ##
####################################################################################################################
2020-10-09 00:30:22 +00:00
2022-07-03 20:13:18 +00:00
if pgrep -l "plasma" | grep "plasmashell"; then
echo "$DATETIME Display Manager is KDE, running KDE clause!"
2022-07-03 20:13:18 +00:00
kde-clause
else
echo "$DATETIME Display Manager is not KDE!"
2022-07-03 20:13:18 +00:00
stop_display_manager_if_running
fi
2022-07-03 20:13:18 +00:00
## Unbind EFI-Framebuffer ##
if test -e "/tmp/vfio-is-nvidia"; then
rm -f /tmp/vfio-is-nvidia
else
test -e "/tmp/vfio-is-amd"
rm -f /tmp/vfio-is-amd
fi
sleep "1"
2020-04-25 04:32:07 +00:00
2022-07-03 20:13:18 +00:00
##############################################################################################################################
## Unbind VTconsoles if currently bound (adapted and modernised from https://www.kernel.org/doc/Documentation/fb/fbcon.txt) ##
##############################################################################################################################
if test -e "/tmp/vfio-bound-consoles"; then
2020-10-09 00:30:22 +00:00
rm -f /tmp/vfio-bound-consoles
fi
2020-10-09 00:30:22 +00:00
for (( i = 0; i < 16; i++))
do
2022-07-03 20:13:18 +00:00
if test -x /sys/class/vtconsole/vtcon"${i}"; then
if [ "$(grep -c "frame buffer" /sys/class/vtconsole/vtcon"${i}"/name)" = 1 ]; then
echo 0 > /sys/class/vtconsole/vtcon"${i}"/bind
echo "$DATETIME Unbinding Console ${i}"
2022-07-03 20:13:18 +00:00
echo "$i" >> /tmp/vfio-bound-consoles
2020-10-09 00:30:22 +00:00
fi
fi
done
2022-07-03 20:13:18 +00:00
sleep "1"
if lspci -nn | grep -e VGA | grep -s NVIDIA ; then
echo "$DATETIME System has an NVIDIA GPU"
2022-07-03 20:13:18 +00:00
grep -qsF "true" "/tmp/vfio-is-nvidia" || echo "true" >/tmp/vfio-is-nvidia
echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/unbind
## Unload NVIDIA GPU drivers ##
modprobe -r nvidia_uvm
modprobe -r nvidia_drm
modprobe -r nvidia_modeset
modprobe -r nvidia
modprobe -r i2c_nvidia_gpu
modprobe -r drm_kms_helper
modprobe -r drm
echo "$DATETIME NVIDIA GPU Drivers Unloaded"
fi
2020-10-04 01:05:49 +00:00
2022-07-03 20:13:18 +00:00
if lspci -nn | grep -e VGA | grep -s AMD ; then
echo "$DATETIME System has an AMD GPU"
2022-07-03 20:13:18 +00:00
grep -qsF "true" "/tmp/vfio-is-amd" || echo "true" >/tmp/vfio-is-amd
2020-12-22 03:14:44 +00:00
echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/unbind
2022-07-03 20:13:18 +00:00
## Unload AMD GPU drivers ##
modprobe -r drm_kms_helper
modprobe -r amdgpu
modprobe -r radeon
modprobe -r drm
echo "$DATETIME AMD GPU Drivers Unloaded"
2020-10-09 00:30:22 +00:00
fi
2022-07-03 20:13:18 +00:00
## Load VFIO-PCI driver ##
modprobe vfio
modprobe vfio_pci
modprobe vfio_iommu_type1
echo "$DATETIME End of Startup!"