-
Notifications
You must be signed in to change notification settings - Fork 1
/
gpuset.sh
82 lines (72 loc) · 2.92 KB
/
gpuset.sh
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
#!/bin/bash
# gpuset, part of GPU Pit Crew by actuallyasriel
set -e
# Debugging
# These lines output Bash trace info to a file on the desktop when uncommented.
# Because I can't be sure that QEMU hooks have tilde expansion, you'll have to replace <USER>. I hope you can forgive me.
# exec 19> /home/<USER>/Desktop/startlogfile
# BASH_XTRACEFD=19
# set -x
# user variables. TODO: make these a config file?
###
# look ma i can sed!
displayManager=$(realpath "/etc/systemd/system/display-manager.service" | sed -r "s/.+\/(.+)\..+/\1/")
# if you're on an amd card, use lsmod to find the modules related to your gpu and replace them accordingly. the fuser lines in switch_vfio() may also need tweaking.
# word to the wise: the order of the modules matters because they may depend on each other.
gpumodules="drm drm_kms_helper i2c_nvidia_gpu nvidia nvidia_modeset nvidia_drm"
vfiomodules="vfio_pci vfio_iommu_type1 vfio"
# determine what GPU driver is currently loaded
if [ ! -z "$(lspci -vnn | grep vfio)" ]; then
mode="vfio"
else
mode="nvidia"
fi
#echo "mode: $mode"
# determine whether passthrough has been requested or not. the request file is generated by the provided qemu hooks.
if [ -f /run/libvirt/request-passthrough ]; then
passthrough="true"
else
passthrough="false"
fi
#echo "passthrough requested: $passthrough"
# TODO: use a second flag file to allow graceful unloading of modules (without using -f.) probably safer, might also just not work.
#if [ -f /run/libvirt/passthrough-gentle ]; then
# flags="-ra"
#else
# flags="-rfa"
#fi
flags="-rfa"
# function defs.
# lightdm will work correctly when disabling it from the display-manager symlink, but not when re-enabling it.
# so setting the variable above lets us work around this. it's only lightdm that does this that i'm aware of!!
# there's 2 ways this might be improved. the definition of pitcrew.service could obviate the need for the systemctl calls.
# stopping the display manager might shut down all the necessary processes on its own, obviating the fuser calls.
# if both are true, then we basically don't need to do any of that here. experimentation is needed.
function switch_nvidia() {
echo "Unloading vfio..."
modprobe $flags $vfiomodules
echo "Loading nvidia..."
modprobe -a $gpumodules
}
function switch_vfio() {
echo "Killing processes using the discrete card..."
fuser -k /dev/nvidiactl
fuser -k /dev/nvidia0 # both these device files are binding to x right now because nvidia is clingy af
echo "Stopping display manager."
systemctl disable --now $displaymanager
echo "Unloading nvidia..."
modprobe $flags $gpumodules
echo "Loading vfio..."
modprobe -a $vfiomodules
echo "Bringing X back...hopefully..."
systemctl enable --now $displaymanager
}
if [ $mode = "nvidia" ] && [ $passthrough = "true" ] ; then
switch_vfio
elif [ $mode = "vfio" ] && [ $passthrough = "true" ] ; then
switch_vfio
elif [ $mode = "vfio" ] && [ $passthrough = "false" ] ; then
switch_nvidia
else
exit 0
fi