This repository has been archived by the owner on Apr 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
optimus.sh
executable file
·143 lines (112 loc) · 2.79 KB
/
optimus.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
#!/usr/bin/bash
confdir="/etc/optimus"
confpath="$confdir/optimus.conf"
# Check if we are running as root
root_needed() {
if [ "$EUID" != '0' ]; then
echo "Run $0 with root permissions to continue"
exit 1
fi
}
# Check if the config file is present
if [ -f "$confpath" ]; then
source "$confpath"
else
echo "Could not find the config file at $confpath. Getting a new config file from the installer will fix this problem"
exit 1
fi
# Indicates if the script is currently running as a daemon
: "${daemon:=false}"
# Command to daemonize the script
# basically detach it, so it doesn't
# get killed by X11
daemonize="env -i --block-signal=SIGHUP --block-signal=SIGTERM daemon=true $0 $@"
# Stop X11 gracefully
x11_exit() {
systemctl stop display-manager
sleep 1
}
# Used to unload drivers
unload_drivers() {
# Continue running until all modules
# with nvidia in their name are unloaded
while lsmod | grep -q "nvidia"; do
m=$(lsmod | awk '{print $1}' | grep nvidia)
for i in $m; do
modprobe -r "$i"
done
sleep 1
done
sleep 1
}
# Used to load drivers
# $1 - specify parameters to the nvidia driver
load_drivers() {
modprobe nvidia_drm
modprobe nvidia_modeset
modprobe nvidia "$1"
}
set_card_state() {
if [ "$1" == "ON" ] || [ "$1" == "OFF" ]; then
tee /proc/acpi/bbswitch <<<"$1"
sleep 1
else
echo "Invalid card state: $1"
fi
}
if [ "$daemon" == "true" ]; then
if [ "$1" == 'igpu' ]; then
x11_exit
# Remove the X11 config file
# that loads the nvidia drivers
rm "$X11conf"
# Add a file in /etc/modprobe.d so the
# nvidia drivers don't load on boot
cp "$confdir/other/blacklist_nvidia.conf" "$modprobeconf"
# Disable the GPU on boot
systemctl enable optimus.service
# Unload the nvidia drivers
unload_drivers
# Power off the card
set_card_state OFF
# Start X11
systemctl start display-manager
exit 0
elif [[ $1 == 'dgpu' ]]; then
x11_exit
# Add an X11 config file that loads
# the nvidia drivers on X11 start
cp "$confdir/other/X11.conf" "$X11conf"
# Autoload the nvidia drivers on boot
cp "$confdir/other/load_nvidia.conf" "$modprobeconf"
cp "$confdir/other/nvidia_modules.conf" "$modulesloadconf"
# Don't disable the GPU on boot
systemctl disable optimus.service
# Power on the card
set_card_state ON
# Load the drivers
load_drivers "NVreg_DynamicPowerManagement=0x02"
# Start X11
systemctl start display-manager
exit 0
fi
else
if [ "$1" == "igpu" ] || [ "$1" == "dgpu" ]; then
root_needed
$daemonize
elif [ "$1" == "state" ]; then
if lsmod | grep -q "nvidia"; then
echo 1
else
echo 0
fi
exit 0
else
echo "Usage: $0 [igpu/dgpu]"
echo ""
echo "igpu | Turn off the discrete GPU"
echo "dgpu | Turn on the discrete GPU"
echo "state | Get state of discrete GPU (0 = OFF, 1 = ON)"
exit 0
fi
fi