-
Notifications
You must be signed in to change notification settings - Fork 2
/
gpuswitch.sh
executable file
·237 lines (219 loc) · 7.12 KB
/
gpuswitch.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/bin/bash
#BEGIN Variables#
gpu=01:00.0
xsession_stop="systemctl stop lxdm"
xsession_start="systemctl start lxdm"
#END Variables#
red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`
echo " _____ _____ _ _ _______ _______ _______ _____ _ _ "
echo " / ____| __ \| | | |/ ____\ \ / /_ _|__ __/ ____| | | |"
echo "| | __| |__) | | | | (___ \ \ /\ / / | | | | | | | |__| |"
echo "| | |_ | ___/| | | |\___ \ \ \/ \/ / | | | | | | | __ |"
echo "| |__| | | | |__| |____) | \ /\ / _| |_ | | | |____| | | |"
echo " \_____|_| \____/|_____/ \/ \/ |_____| |_| \_____|_| |_|"
echo ""
echo "Pascal Maximilian Bremer"
echo "Easily switch between nvidia and vfio-drivers"
echo ""
if [[ $EUID -ne 0 ]]
then
echo ${red}"ERROR: This script must be run as root"${reset}
exit 1
fi
# Only works for vfio-pci <--> nouveau
# Parameter 1 = {vfio-pci | nouveau}
switch(){
modprobe $1
echo 0 > /sys/bus/pci/drivers_autoprobe
# Gather relevant information of the device in BUS
VENDOR=$(cat /sys/bus/pci/devices/0000\:${gpu}/vendor)
DEVICE=$(cat /sys/bus/pci/devices/0000\:${gpu}/device)
if [ -e /sys/bus/pci/devices/0000\:${gpu}/driver ]; then
echo 0000:${gpu} > /sys/bus/pci/devices/0000\:${gpu}/driver/unbind
fi
echo $VENDOR $DEVICE > /sys/bus/pci/drivers/$1/new_id
echo $VENDOR $DEVICE > /sys/bus/pci/drivers/$1/remove_id
echo 1 > /sys/bus/pci/drivers_autoprobe
}
showinfo(){
echo "Detected following graphic cards"
lspci -v | perl -ne '/VGA/../^$/ and /VGA|Kern/ and print'
}
read_driver(){
lspci -v -s ${gpu} | grep "Kernel driver in use:" | awk -F ": " '{print $2}'
}
switch_menu(){
echo "Select the new driver"
echo "[1] nvidia"
echo "[2] vfio-pci"
echo "[3] nouveau"
read -s -n 1 key_gpu
}
using_info(){
echo ${green}"You are currently using the driver : "$(read_driver)${reset}
}
already_using(){
echo "You are already using the driver"
}
nvidia_to_nouveau(){
rmmod nvidia
sleep 1
modprobe nouveau
sleep 1
}
nouveau_to_nvidia(){
rmmod nouveau
sleep 1
modprobe nvidia
sleep 1
}
about(){
echo " #@; "
echo " @,,:'; "
echo " @,,:::+ "
echo " @,,:::::. "
echo " @,,::::::' "
echo " @,,::::::: "
echo " @+,:::::::# "
echo " #:::::::: "
echo " +::::::: "
echo " ::::::,: "
echo " +:::::,' "
echo " :::::,, "
echo " #::::,,; "
echo " ,::::,,# "
echo " ::::,,: "
echo " ::::,,, "
echo " ;:::,,, "
echo " +:::,,, "
echo " .#@@####@#. "
echo " '@@@#########++#+ "
echo " @@@@@#########++++++@ "
echo " #@@@@##########+++++++++@ "
echo " @@@@@;,+#######++++@;,+#+'+' "
echo " .@@@# '####+++# '+'': "
echo " :@@@, ##+++, +''' "
echo " .@@@+ #+++ '''; "
echo " @@@@ ;+# ;'';' "
echo " @@@@' +' ';;' "
echo " +@@@@ # #;;;@ "
echo " @@@@# + +;;;; "
echo "#@@### ; ;;;;;@ "
echo "@@#### ' ';;;;; + :. "
echo "@##### @ @;;;;:, @ + "
echo "######: +: ';;;::@ ; ; "
echo "####### #@@# ''+ @@@, ';;;:::; ' ; "
echo "#######: @@@@@@ +'': @@@@@' ';;::::: + @ "
echo "######++ @@@@@@ @'''' ;@@@@@@ @;;:::::: @ .. '@"
echo "#####++++.@@ @@ @''''''.@' .@# @;:::::::: , "
echo "####++++++#@#@@#'''''''';#@+#@#;;::::::::: , "
echo "###++++++++++'''''''''';;;;;;;;;:::::::::: + ' ,#"
echo "##++++++++++'''''''''';;;;;;;;;::::::::::, @ "
echo "#++++++ ''' '''''',,,,,,:::.::::,; @ '"
echo "+++++++''''''''''''''''''''''''''''':::,,@ @ :#+@ "
echo "+++++++''''''''''''''''''''''''''''':,,,,, @ "
echo "#++++++.''''''''''''''''''''''''''',,,,,, ; "
echo "++++++'.'''''''''''''''''''''''''';,,,,,@ # ++,@"
echo " ++++'''''''''''''''''''''''''''''',,,,, #. + "
echo " +++''''''''''''''''''''''''''''':,,,,,@ ,@';'+@@# "
echo " #'''''',''''''''''''''''''''''',,,,,: "
echo " ''''''';'''''''''''''';'' ,''.,,,,,' "
echo " .'''''';;''''''''''''.,,,,, ,,,,,,; "
echo " :'''';;;'''''''''''',,,,,,,,,,,,' "
echo " .'';;;;;::''''''''',,,,.,,,,,,: "
echo " +;;;;;;;: :'''''', ,,,,,,,:' "
echo " #;;;;;;;:::::::::,,,,,,,@ "
echo " @;;;;:::::::::,,,,,,@ "
echo " '':::::::::,,,,;+ "
echo " .+#';;;'##. "
echo ""
echo "Scripted by Pascal Maximilian Bremer <https://github.com/Bomberus>"
}
switch_helper(){
using_info
switch_menu
${xsession_stop}
case "$(read_driver)" in
"")
case "${key_gpu}" in
1)
modprobe nvidia
;;
2)
switch vfio-pci
;;
3)
switch nouveau
;;
esac
;;
nvidia)
case "${key_gpu}" in
1)
already_using
;;
2)
rmmod nvidia_modeset
nvidia_to_nouveau
switch vfio-pci
;;
3)
rmmod nvidia_modeset
nvidia_to_nouveau
;;
esac
;;
vfio-pci)
case "${key_gpu}" in
1)
switch nouveau
nouveau_to_nvidia
;;
2)
already_using
;;
3)
switch nouveau
;;
esac
;;
nouveau)
case "${key_gpu}" in
1)
nouveau_to_nvidia
;;
2)
switch vfio-pci
;;
3)
already_using
;;
esac
;;
esac
using_info
${xsession_start}
}
echo "Menu overview please press key"
echo "[1] Show Systeminfo"
echo "[2] Switch GPU (will restart XServer)"
echo "[3] About"
echo "[4] Exit"
read -s -n 1 key
case "${key}" in
1)
showinfo
;;
2)
switch_helper
;;
3)
about
;;
4)
echo ${green}"Goodbye" ${reset}
;;
esac
exit 0