forked from claudio-walser/egpu-setup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathegpu
84 lines (68 loc) · 2.19 KB
/
egpu
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
#!/bin/bash
set -o errexit # exit script if a command fails
set -o nounset # exit when trying to use undeclared variables
# define the configs you manualy put there for xorg
declare INTERNAL=/etc/X11/xorg.conf.internal
declare EXTERNAL=/etc/X11/xorg.conf.egpu
# xorg default config file will be linked to one of the above
# ensure you do not have any nvidia config in /etc/X11/xorg.conf.d/ at this moment.
# this cost me half an hour
declare LINK=/etc/X11/xorg.conf
# egpu device state
declare egpu_connected=0
# returns 1 if egpu is connected, 0 if not
function is_egpu_connected() {
# read pci id from xorg.conf.egpu
declare egpu_pci_id=$(cat $EXTERNAL | grep -Ei "BusID" | grep -oEi '[0-9]+\:[0-9]+\:[0-9]+')
# create an array by splitting the BUS-ID on ':'
declare busArray=(${egpu_pci_id//:/ })
declare bus1d=${busArray[0]}
declare bus2d=${busArray[1]}
declare bus3d=${busArray[2]}
# convert dec to hex
declare bus1h=$(printf "%02x" $bus1d)
declare bus2h=$(printf "%02x" $bus2d)
declare bus3h=$(printf "%01x" $bus3d)
if [ $(lspci | grep -iEc "$bus1h:$bus2h.$bus3h") -eq 1 ]; then
echo "EGPU is connected."
egpu_connected=1
else
echo "EGPU is disconnected."
egpu_connected=0
fi
}
function unlink_config() {
if test -L $LINK; then
sudo unlink $LINK
fi
}
function link_config() {
unlink_config
sudo ln -s $1 $LINK
}
# first and only argument needs to be start or stop
if [ "$1" != "start" ] && [ "$1" != "stop" ]; then
echo "Argument Error: first argument needs to be start/stop"
exit 1
fi
if [ "$1" = "start" ] ; then
echo "Setup configuration for external gpu"
# only allow usage if an egpu is currently attachted
is_egpu_connected
if [ ${egpu_connected} != 1 ] ; then
echo "Configuration Error: Plug in or change configuration"
exit 1
fi
link_config $EXTERNAL
else
# let always work using the internal gpu
echo "Setup configuration for internal gpu"
link_config $INTERNAL
fi
echo "configuration done"
echo "I will now restart your x-session to the desired gpu."
echo "If anthing goes wrong, you can still login to a different tty without a desktop environment and fix it."
echo "I will restart X in 3 seconds"
sleep 3
sudo systemctl restart display-manager.service
exit 0