From 56c3ba4ae64741b134a0c5069e68ee10d5b803ca Mon Sep 17 00:00:00 2001 From: Lily Freeman Date: Sun, 3 Jul 2022 20:13:18 +0000 Subject: [PATCH] New Scripts --- hooks/vfio-startup.sh | 172 ++++++++++++++++++++++++++++++----------- hooks/vfio-teardown.sh | 98 +++++++++++++++++++---- 2 files changed, 208 insertions(+), 62 deletions(-) diff --git a/hooks/vfio-startup.sh b/hooks/vfio-startup.sh index 9d40d1b..71ee32b 100755 --- a/hooks/vfio-startup.sh +++ b/hooks/vfio-startup.sh @@ -1,74 +1,156 @@ #!/bin/bash -# Helpful to read output when debugging -set -x -long_delay=10 -medium_delay=5 -short_delay=1 -echo "Beginning of startup!" +############################################################################# +## ______ _ _ _______ _ _ ## +## (_____ \(_) | |(_______) | | | | ## +## _____) )_ _ _ _____ | | _ _ _ _ | |__ _____ __| | ## +## | ____/| |( \ / )| ___ || || | | | | | | || _ \ | ___ | / _ | ## +## | | | | ) 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 you're ## +## 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 ## +DATE=$(date +"%m/%d/%Y %R:%S :") + +## Sets dispmgr var as null ## +DISPMGR="null" + +################################## Script ################################### + +echo "$DATE Beginning of Startup!" + function stop_display_manager_if_running { - # Stop dm using systemd - if command -v systemctl; then - if systemctl is-active --quiet "$1.service" ; then - echo $1 >> /tmp/vfio-store-display-manager - systemctl stop "$1.service" + ## Get display manager on systemd based distros ## + if [[ -x /run/systemd/system ]] && echo "$DATE Distro is using Systemd"; then + DISPMGR="$(grep 'ExecStart=' /etc/systemd/system/display-manager.service | awk -F'/' '{print $(NF-0)}')" + echo "$DATE Display Manager = $DISPMGR" + + ## 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 fi - while systemctl is-active --quiet "$1.service" ; do - sleep "${short_delay}" + while systemctl is-active --quiet "$DISPMGR.service"; do + sleep "1" done return + fi - # Stop dm using runit - if command -v sv; then - if sv status $1 ; then - echo $1 >> /tmp/vfio-store-display-manager - sv stop $1 - fi - fi } +function kde-clause { -# Stop currently running display manager -if test -e "/tmp/vfio-store-display-manager" ; then - rm -f /tmp/vfio-store-display-manager + echo "$DATE Display Manager = display-manager" + + ## 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" + fi + + while systemctl is-active --quiet "display-manager.service"; do + sleep 2 + done + + return + +} + +#################################################################################################################### +## 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. ## +#################################################################################################################### + +if pgrep -l "plasma" | grep "plasmashell"; then + echo "$DATE Display Manager is KDE, running KDE clause!" + kde-clause + else + echo "$DATE Display Manager is not KDE!" + stop_display_manager_if_running fi -stop_display_manager_if_running sddm -stop_display_manager_if_running gdm -stop_display_manager_if_running lightdm -stop_display_manager_if_running lxdm -stop_display_manager_if_running xdm -stop_display_manager_if_running mdm -stop_display_manager_if_running display-manager +## 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 -# Unbind VTconsoles if currently bound (adapted from https://www.kernel.org/doc/Documentation/fb/fbcon.txt) -if test -e "/tmp/vfio-bound-consoles" ; then +sleep "1" + +############################################################################################################################## +## 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 rm -f /tmp/vfio-bound-consoles fi for (( i = 0; i < 16; i++)) do - if test -x /sys/class/vtconsole/vtcon${i}; then - if [ `cat /sys/class/vtconsole/vtcon${i}/name | grep -c "frame buffer"` \ - = 1 ]; then - echo 0 > /sys/class/vtconsole/vtcon${i}/bind - echo "Unbinding console ${i}" - echo $i >> /tmp/vfio-bound-consoles + 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 "$DATE Unbinding Console ${i}" + echo "$i" >> /tmp/vfio-bound-consoles fi fi done -# Unbind EFI-Framebuffer -if test -e "/tmp/vfio-is-nvidia" ; then - rm -f /tmp/vfio-is-nvidia -fi +sleep "1" -if lsmod | grep "nvidia" &> /dev/null ; then - echo "true" >> /tmp/vfio-is-nvidia +if lspci -nn | grep -e VGA | grep -s NVIDIA ; then + echo "$DATE System has an NVIDIA GPU" + 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 "$DATE NVIDIA GPU Drivers Unloaded" fi -echo "End of startup!" +if lspci -nn | grep -e VGA | grep -s AMD ; then + echo "$DATE System has an AMD GPU" + grep -qsF "true" "/tmp/vfio-is-amd" || echo "true" >/tmp/vfio-is-amd + echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/unbind + + ## Unload AMD GPU drivers ## + modprobe -r drm_kms_helper + modprobe -r amdgpu + modprobe -r radeon + modprobe -r drm + + echo "$DATE AMD GPU Drivers Unloaded" +fi + +## Load VFIO-PCI driver ## +modprobe vfio +modprobe vfio_pci +modprobe vfio_iommu_type1 + +echo "$DATE End of Startup!" diff --git a/hooks/vfio-teardown.sh b/hooks/vfio-teardown.sh index 2bcdb7d..5df281b 100755 --- a/hooks/vfio-teardown.sh +++ b/hooks/vfio-teardown.sh @@ -1,35 +1,99 @@ #!/bin/bash -set -x -echo "Beginning of teardown!" +############################################################################# +## ______ _ _ _______ _ _ ## +## (_____ \(_) | |(_______) | | | | ## +## _____) )_ _ _ _____ | | _ _ _ _ | |__ _____ __| | ## +## | ____/| |( \ / )| ___ || || | | | | | | || _ \ | ___ | / _ | ## +## | | | | ) 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 you're ## +## The VFIO community for using the scripts and ## ## lspci feedback ## +## testing them for us! ## ## in your terminal ## +##################################################### ####################### -# Restart Display Manager +################################# Variables ################################# + +## Adds current time to var for use in echo for a cleaner log and script ## +DATE=$(date +"%m/%d/%Y %R:%S :") + +################################## Script ################################### + +echo "$DATE Beginning of Teardown!" + +## Unload VFIO-PCI driver ## +modprobe -r vfio_pci +modprobe -r vfio_iommu_type1 +modprobe -r vfio + +if grep -q "true" "/tmp/vfio-is-nvidia" ; then + + ## Load NVIDIA drivers ## + echo "$DATE Loading NVIDIA GPU Drivers" + + modprobe drm + modprobe drm_kms_helper + modprobe i2c_nvidia_gpu + modprobe nvidia + modprobe nvidia_modeset + modprobe nvidia_drm + modprobe nvidia_uvm + + echo "$DATE NVIDIA GPU Drivers Loaded" +fi + +if grep -q "true" "/tmp/vfio-is-amd" ; then + + ## Load NVIDIA drivers ## + echo "$DATE Loading AMD GPU Drivers" + + modprobe drm + modprobe amdgpu + modprobe radeon + modprobe drm_kms_helper + + echo "$DATE AMD GPU Drivers Loaded" +fi + +## Restart Display Manager ## input="/tmp/vfio-store-display-manager" -while read displayManager; do +while read -r DISPMGR; do if command -v systemctl; then - systemctl start "$displayManager.service" + + ## Make sure the variable got collected ## + echo "$DATE Var has been collected from file: $DISPMGR" + + systemctl start "$DISPMGR.service" + else if command -v sv; then - sv start $displayManager + sv start "$DISPMGR" fi fi done < "$input" -# Rebind VT consoles (adapted from https://www.kernel.org/doc/Documentation/fb/fbcon.txt) +############################################################################################################ +## Rebind VT consoles (adapted and modernised from https://www.kernel.org/doc/Documentation/fb/fbcon.txt) ## +############################################################################################################ + input="/tmp/vfio-bound-consoles" -while read consoleNumber; do - if test -x /sys/class/vtconsole/vtcon${consoleNumber}; then - if [ `cat /sys/class/vtconsole/vtcon${consoleNumber}/name | grep -c "frame buffer"` \ +while read -r consoleNumber; do + if test -x /sys/class/vtconsole/vtcon"${consoleNumber}"; then + if [ "$(grep -c "frame buffer" "/sys/class/vtconsole/vtcon${consoleNumber}/name")" \ = 1 ]; then - echo "Rebinding console ${consoleNumber}" - echo 1 > /sys/class/vtconsole/vtcon${consoleNumber}/bind + echo "$DATE Rebinding console ${consoleNumber}" + echo 1 > /sys/class/vtconsole/vtcon"${consoleNumber}"/bind fi fi done < "$input" -# Rebind framebuffer for nvidia -if test -e "/tmp/vfio-is-nvidia" ; then - echo "efi-framebuffer.0" > /sys/bus/platform/drivers/efi-framebuffer/bind -fi -echo "End of teardown!" +echo "$DATE End of Teardown!"