-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvfio-startup
More file actions
executable file
·141 lines (111 loc) · 3.77 KB
/
Copy pathvfio-startup
File metadata and controls
executable file
·141 lines (111 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/bin/bash
# Credits:
# Original scripts:
# https://github.com/ledisthebest/LEDs-single-gpu-passthrough
# https://gitlab.com/risingprismtv/single-gpu-passthrough/-/blob/master/hooks/vfio-startup
#
# I have modified the script especially for KDE Plasma and Nvidia.
#
# Helpful to read output when debugging
set -x
long_delay=10
medium_delay=5
short_delay=1
echo "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"
fi
while systemctl is-active --quiet "$1.service" ; do
sleep "${medium_delay}"
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
}
# Stop currently running display manager
if test -e "/tmp/vfio-store-display-manager" ; then
rm -f /tmp/vfio-store-display-manager
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
sleep "${medium_delay}"
# Unbind VTconsoles if currently bound (adapted 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
fi
fi
done
if lsmod | grep "nvidia" &> /dev/null ; then
echo "true" >> /tmp/vfio-is-nvidia
echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/unbind
fi
## 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"
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 ##
echo "$DATE Checking for processes using NVIDIA modules..."
lsof /dev/nvidia* > "/tmp/vfio-nvidia-processes.log" 2>&1
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
# Log any errors during module unloading
lsmod | grep -E "nvidia|drm" > "/tmp/vfio-remaining-modules.log"
echo "$DATE NVIDIA GPU Drivers Unloaded"
fi
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 ##
echo "$DATE Checking for processes using AMD modules..."
lsof /dev/dri/* > "/tmp/vfio-amd-processes.log" 2>&1
modprobe -r drm_kms_helper
modprobe -r amdgpu
modprobe -r radeon
modprobe -r drm
# Log any errors during module unloading
lsmod | grep -E "amdgpu|radeon|drm" > "/tmp/vfio-remaining-modules.log"
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!"