Merge minimal scripts

This commit is contained in:
Risingprism 2020-12-22 03:14:44 +00:00
parent fc2769db05
commit 4d9866cc15
2 changed files with 44 additions and 110 deletions

View File

@ -8,46 +8,42 @@ short_delay=1
echo "Beginning of startup!" echo "Beginning of startup!"
function stop_display_manager_if_running { function stop_display_manager_if_running {
if systemctl is-active --quiet $1 ; then # Stop dm using systemd
echo $1 >> /tmp/vfio-store-display-manager if command -v systemctl; then
systemctl stop $1 if systemctl is-active --quiet "$1.service" ; then
echo $1 >> /tmp/vfio-store-display-manager
systemctl stop "$1.service"
fi
while systemctl is-active --quiet "$1.service" ; do
sleep "${short_delay}"
done
return
fi fi
while systemctl is-active --quiet $1 ; do # Stop dm using runit
sleep "${short_delay}" if command -v sv; then
done if sv status $1 ; then
} echo $1 >> /tmp/vfio-store-display-manager
sv stop $1
function unload_module_if_loaded { fi
if lsmod | grep $1 &> /dev/null ; then
modprobe -r $1
echo $1 >> /tmp/vfio-loaded-gpu-modules
fi fi
while lsmod | grep $1 &> /dev/null ; do
sleep 1
done
} }
function get_virsh_id {
python -c "print('pci_0000_'+'$1'.split(':')[0] + '_' + '$1'.split(':')[1].split('.')[0] + '_' + '$1'.split(':')[1].split('.')[1])"
}
function get_pci_id_from_device_id {
lspci -nn | grep $1 | awk '{print $1}'
}
# Stop currently running display manager # Stop currently running display manager
if test -e "/tmp/vfio-store-display-manager" ; then if test -e "/tmp/vfio-store-display-manager" ; then
rm -f /tmp/vfio-store-display-manager rm -f /tmp/vfio-store-display-manager
fi fi
stop_display_manager_if_running sddm.service
stop_display_manager_if_running gdm.service
stop_display_manager_if_running lightdm.service
stop_display_manager_if_running lxdm.service
stop_display_manager_if_running xdm.service
stop_display_manager_if_running mdm.service
stop_display_manager_if_running display-manager.service
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 VTconsoles if currently bound (adapted from https://www.kernel.org/doc/Documentation/fb/fbcon.txt) # Unbind VTconsoles if currently bound (adapted from https://www.kernel.org/doc/Documentation/fb/fbcon.txt)
if test -e "/tmp/vfio-bound-consoles" ; then if test -e "/tmp/vfio-bound-consoles" ; then
@ -65,53 +61,14 @@ do
fi fi
done done
# According to kernel documentation (https://www.kernel.org/doc/Documentation/fb/fbcon.txt), # Unbind EFI-Framebuffer
# specifically unbinding efi-framebuffer is not necessary after all consoles if test -e "/tmp/vfio-is-nvidia" ; then
# are unbound (and often times harmful in my experience), so it was omitted here rm -f /tmp/vfio-is-nvidia
# I leave it here for reference in case anyone needs it.
#Unbind EFI-Framebuffer if currently bound
# if test -e "/sys/bus/platform/drivers/efi-framebuffer/unbind" ; then
# echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/unbind
# else
# echo "Could not find framebuffer to unload!"
sleep "${long_delay}"
# Unload loaded GPU drivers
if test -e "/tmp/vfio-loaded-gpu-modules" ; then
rm -f /tmp/vfio-loaded-gpu-modules
fi fi
unload_module_if_loaded amdgpu-pro if lsmod | grep "nvidia" &> /dev/null ; then
unload_module_if_loaded amdgpu echo "true" >> /tmp/vfio-is-nvidia
unload_module_if_loaded nvidia_drm echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/unbind
unload_module_if_loaded nvidia_modeset
unload_module_if_loaded nvidia_uvm
unload_module_if_loaded nvidia
unload_module_if_loaded ipmi_devintf
unload_module_if_loaded nouveau
unload_module_if_loaded i915
# Unbind the GPU from display driver
if test -e "/tmp/vfio-virsh-ids" ; then
rm -f /tmp/vfio-virsh-ids
fi fi
gpu_device_id=$(modprobe -c vfio-pci | grep 'options vfio_pci ids' | cut -d '=' -f2 | cut -d ',' -f 1)
gpu_audio_device_id=$(modprobe -c vfio-pci | grep 'options vfio_pci ids' | cut -d '=' -f2 | cut -d ',' -f 2)
gpu_pci_id=$(get_pci_id_from_device_id ${gpu_device_id})
gpu_audio_pci_id=$(get_pci_id_from_device_id ${gpu_audio_device_id})
virsh_gpu_id=$(get_virsh_id ${gpu_pci_id})
virsh_gpu_audio_id=$(get_virsh_id ${gpu_audio_pci_id})
echo ${virsh_gpu_audio_id} >> /tmp/vfio-virsh-ids
echo ${virsh_gpu_id} >> /tmp/vfio-virsh-ids
virsh nodedev-detach "${virsh_gpu_id}"
virsh nodedev-detach "${virsh_gpu_audio_id}"
# Load VFIO kernel module
modprobe vfio-pci
echo "End of startup!" echo "End of startup!"

View File

@ -3,15 +3,16 @@ set -x
echo "Beginning of teardown!" echo "Beginning of teardown!"
# Unload VFIO-PCI Kernel Driver # Restart Display Manager
modprobe -r vfio-pci input="/tmp/vfio-store-display-manager"
modprobe -r vfio_iommu_type1 while read displayManager; do
modprobe -r vfio if command -v systemctl; then
systemctl start "$displayManager.service"
# Re-Bind GPU to AMD Driver else
input="/tmp/vfio-virsh-ids" if command -v sv; then
while read virshId; do sv start $displayManager
virsh nodedev-reattach "$virshId" fi
fi
done < "$input" done < "$input"
# Rebind VT consoles (adapted from https://www.kernel.org/doc/Documentation/fb/fbcon.txt) # Rebind VT consoles (adapted from https://www.kernel.org/doc/Documentation/fb/fbcon.txt)
@ -26,33 +27,9 @@ while read consoleNumber; do
fi fi
done < "$input" done < "$input"
# Hack that magically makes nvidia gpus work :) # Rebind framebuffer for nvidia
if command -v nvidia-xconfig ; then if test -e "/tmp/vfio-is-nvidia" ; then
nvidia-xconfig --query-gpu-info > /dev/null 2>&1 echo "efi-framebuffer.0" > /sys/bus/platform/drivers/efi-framebuffer/bind
fi fi
# According to kernel documentation (https://www.kernel.org/doc/Documentation/fb/fbcon.txt),
# specifically unbinding efi-framebuffer is not necessary after all consoles
# are unbound (and often times harmful in my experience), so it was omitted here
# I leave it here for reference in case anyone needs it.
# Re-Bind EFI-Framebuffer
# if test -e "/sys/bus/platform/drivers/efi-framebuffer/bind" ; then
# echo "efi-framebuffer.0" > /sys/bus/platform/drivers/efi-framebuffer/bind
# else
# echo "Could not find framebuffer to bind!"
# fi
#Load amd driver
input="/tmp/vfio-loaded-gpu-modules"
while read gpuModule; do
modprobe "$gpuModule"
done < "$input"
# Restart Display Manager
input="/tmp/vfio-store-display-manager"
while read displayManager; do
systemctl start "$displayManager"
done < "$input"
echo "End of teardown!" echo "End of teardown!"