-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart-egpu-igpu.sh
67 lines (49 loc) · 1.56 KB
/
start-egpu-igpu.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
#!/bin/bash
# Display each command after execution
set -x
# Treat undefined variables as error
set -u
# Load the config file
source "/etc/libvirt/hooks/kvm.conf"
# Exit if already in correct state
if [[ -e "/sys/bus/pci/drivers/vfio-pci/$VIRSH_GPU_VIDEO" ]]; then
echo "GPUs already bound to correct drivers, nothing to do"
exit 0
fi
# Kill display manager
systemctl stop gdm.service
# Avoid framebuffer still being used while unbinding
sleep 2
# Unbind framebuffer
declare -a bound_framebuffers
for vtcon in /sys/class/vtconsole/vtcon*; do
if [[ $(cat "$vtcon/bind") == 1 ]]; then
echo 0 > "$vtcon/bind"
bound_framebuffers+=("$vtcon")
fi
done
# Avoid framebuffer still being bound while GPU is unbinding
sleep 2
# Unbind primary GPU
driver-rebind "$VIRSH_GPU_VIDEO" vfio-pci
modprobe -r amdgpu
sleep 2
# Set bar sizes
modprobe -r vfio-pci # GPU must be completely unused
echo 13 > "/sys/bus/pci/devices/$VIRSH_GPU_VIDEO/resource0_resize" # 8 MB
echo 3 > "/sys/bus/pci/devices/$VIRSH_GPU_VIDEO/resource2_resize" # 8 GB
modprobe vfio-pci
# Avoid GPU not being initialized before rebinding framebuffer
sleep 2
# Rebind framebuffer
for vtcon in ${bound_framebuffers[@]}; do
echo 1 > "$vtcon/bind"
done
# Isolate CPU cores from host
systemctl set-property --runtime -- user.slice AllowedCPUs=12-31
systemctl set-property --runtime -- system.slice AllowedCPUs=12-31
systemctl set-property --runtime -- init.scope AllowedCPUs=12-31
# Avoid Framebuffer not being bound before gdm is started
sleep 2
# Start display manager
systemctl restart gdm.service