This repository has been archived by the owner on Jul 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nvman
executable file
·499 lines (413 loc) · 16.3 KB
/
nvman
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#!/bin/bash
# ================================================================================
# Colorize output.
# ================================================================================
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
CYAN=$(tput setaf 6)
BOLD=$(tput bold)
RESET=$(tput sgr0)
# ================================================================================
# ================================================================================
# nvman variables.
# ================================================================================
version="1.3.2"
config_path="/etc/nvman"
config_file="${config_path}/config"
# ================================================================================
# ================================================================================
# Utility function, run the following command as root, pretty print when needing
# privelege scalation.
# ================================================================================
sudo_run() {
if [[ $(id -u) -eq 0 ]]; then
eval "$@"
return
fi
sudo -n true &> /dev/null
[[ $? -eq 1 ]] &&
echo "${GREEN}==>${RESET} Privilege scalation needed"
eval "sudo $@"
}
# ================================================================================
# Check if $1 is a valid value for service (bumblebee or optimus).
# ================================================================================
valid_service() {
case $1 in
'bumblebee' | 'optimus')
echo 1
;;
*)
echo 0
;;
esac
}
valid_gpu() {
case $1 in
'intel' | 'nvidia' | 'auto')
echo 1
;;
*)
echo 0
;;
esac
}
# ================================================================================
# Get the service file descriptor:
# optimus -> optimus-manager.service
# bumbleblee -> bumblebeed.service
# ================================================================================
service_descriptor() {
local service=$1
case $service in
'bumblebee')
service='bumblebeed.service'
;;
'optimus')
service='optimus-manager.service'
;;
esac
echo $service
}
# ================================================================================
# Prints active or inactive, depending on the service status.
# ================================================================================
service_status() {
echo "$(systemctl status $(service_descriptor $1) | awk '/Active/ {print $2}')"
}
# ================================================================================
# Prints enabled or disabled, depending on the service status.
# ================================================================================
service_enabled() {
local status=$(systemctl status $(service_descriptor $1) | awk '/Loaded: loaded/ { print $4}')
echo "${status::-1}"
}
# ================================================================================
# Utility function to enable a service, check if willing to enable conflictive
# services, if that's the case, toggle them.
# ================================================================================
enable_service() {
local service=$1
# Toggle service depending on input
case $service in
'bumblebee')
disable_service optimus
;;
'optimus')
disable_service bumblebee
;;
esac
# If the service is already enabled, exit
[[ $(service_enabled $service) = 'enabled' ]] && return 0
sudo_run "systemctl enable $(service_descriptor $service) &> /dev/null"
}
# ================================================================================
# Utility function to disable a service, no special checks, it's okay to have both
# disabled.
# ================================================================================
disable_service() {
local service=$1
# If the service is already disabled, exit
[[ $(service_enabled $service) = 'disabled' ]] && return 0
sudo_run "systemctl disable $(service_descriptor $service) &> /dev/null"
}
# ================================================================================
# Utility function to start a service, check if willing to start conflictive
# services, if that's the case, toggle them.
# ================================================================================
start_service() {
local service=$1
# If the service is already active, exit
[[ $(service_status $service) = 'active' ]] && return 0
# Check if willing to activate conflictive services
case $service in
'bumblebee')
[[ $(service_status optimus) = 'active' ]] &&
print_error "Trying to activate conflicting services" && return 1
;;
'optimus')
[[ $(service_status bumblebee) = 'active' ]] &&
print_error "Trying to activate conflicting services" && return 1
;;
esac
# Start the service
sudo_run "systemctl start $(service_descriptor $service)"
}
# ================================================================================
# Utility function to stop a service, no special checks, it's okay to have both
# stopped.
# ================================================================================
stop_service() {
local service=$1
# If the service is already inactive, exit
[[ $(service_status $service) = 'inactive' ]] && return 0
# Stop the service
sudo_run "systemctl stop $(service_descriptor $service)"
}
# ================================================================================
# Print with 'Error:'
# ================================================================================
print_error() {
echo "${RED}${BOLD}Error:${RESET} $@"
}
# ================================================================================
# Utility function to read NVIDIA GPU status, prints on or off
# ================================================================================
get_nvidia_status() {
local status=$(cat /proc/acpi/bbswitch | awk '{printf $2}')
echo "$(echo -n $status | tr '[:upper:]' '[:lower:]')"
}
# ================================================================================
# ================================================================================
# Print status utility functions
# ================================================================================
print_optimus() {
first=$(echo -n "${BOLD}${WHITE}Optimus :${RESET} ${optimus_color}${optimus_status}${RESET} ")
second=$(echo -n "${YELLOW}($(service_enabled optimus))${RESET}")
echo "$first $second"
}
print_bumblebee() {
first=$(echo -n "${BOLD}${WHITE}Bumblebee :${RESET} ${bumblebee_color}${bumblebee_status}${RESET} ")
second=$(echo -n "${YELLOW}($(service_enabled bumblebee))${RESET}")
echo "$first $second"
}
print_nvidia() {
echo "${BOLD}${WHITE}NVIDIA GPU :${RESET} $(get_nvidia_status)"
}
print_default() {
echo "${BOLD}${WHITE}Default service :${RESET} $(get_default_service)"
}
print_optimus_mode() {
echo "${BOLD}${WHITE}Optimus mode :${RESET} $(get_optimus_mode)"
}
print_optimus_startup() {
echo "${BOLD}${WHITE}Optimus startup :${RESET} $(get_optimus_startup)"
}
print_status() {
local optimus_color=$RED
local bumblebee_color=$RED
local optimus_status=$(service_status optimus)
local bumblebee_status=$(service_status bumblebee)
[[ $optimus_status = 'active' ]] && optimus_color=$GREEN
[[ $bumblebee_status = 'active' ]] && bumblebee_color=$GREEN
# Print optimus overall status
print_optimus | awk '{printf "%-20s %s %-20s %s\n", $1, $2, $3, $4 }'
# Print bumblebee overall status
print_bumblebee | awk '{printf "%-20s %s %-20s %s\n", $1, $2, $3, $4 }'
# Print NVIDIA GPU status
print_nvidia | awk '{printf "%s %-9s %s %s\n", $1, $2, $3, $4 }'
# Print default service
print_default | awk '{printf "%s %-8s %s %s\n", $1, $2, $3, $4 }'
# Print optimus mode (if active)
[[ $(service_status optimus) = 'active' ]] &&
print_optimus_mode | awk '{printf "%s %-8s %s %s\n", $1, $2, $3, $4 }'
# Print optimus startup (if enabled)
[[ $(get_default_service) = 'optimus' ]] &&
print_optimus_startup | awk '{printf "%s %-8s %s %s\n", $1, $2, $3, $4 }'
}
# ================================================================================
# ================================================================================
# Write to config_file the default service to be enabled at boot.
# ================================================================================
set_default_service() {
if [[ ! -d $config_path ]]; then
sudo_run "mkdir -p $config_path"
fi
echo "default = $1" | sudo_run "tee $config_file &> /dev/null"
}
# ================================================================================
# Read from config_file the default service to be enabled at boot.
# If there's no config_file, call set_default_service optimus.
# ================================================================================
get_default_service() {
if [[ ! -e $config_file ]]; then
set_default_service optimus
fi
echo "$(cat $config_file | grep '\<default\>' | tr '=' ' ' | awk '{print $2}')"
}
# ================================================================================
# Utility function to read optimus mode, prints intel or nvidia
# ================================================================================
get_optimus_mode() {
echo "$(optimus-manager --print-mode | awk '/mode/ {print $5}')"
}
# ================================================================================
# Utility function to read optimus startup, prints intel or nvidia
# ================================================================================
get_optimus_startup() {
echo "$(optimus-manager --print-startup | awk '/startup/ {print $7}')"
}
# ================================================================================
# Check if using gdm, suggest wiki read
# ================================================================================
check_gdm_prime() {
local gdm_name=$(pacman -Qq gdm)
[[ $(service_enabled 'gdm.service') = 'disabled' ]] && return
local bad_flag=false
if [[ $gdm_name != 'gdm-prime' ]]; then
print_error "Seems like you're using GDM, but you don't have gdm-prime installed."
bad_flag=true
fi
local xorg_gdm=$(cat /etc/gdm/custom.conf | grep '\<WaylandEnable\>' | cut -dW -f 1)
if [[ $xorg_gdm = '#' ]]; then
print_error "Please disable wayland for GDM /etc/gdm/custom.conf"
bad_flag=true
fi
$bad_flag && exit 1
}
# ================================================================================
# ================================================================================
# Print usage (help)
# ================================================================================
print_usage() {
echo "Usage: $0 command"
echo "Version $version"
echo ""
echo "Commands:"
echo " nvidia <on|off> Turn on/off NVIDIA GPU, useful for CUDA"
echo " run <cmd> Run any command with primusrun"
echo " switch <nvidia|intel|auto> Switch gpu using optimus-manager"
echo " startup <nvidia|intel> Set startup gpu for optimus service"
echo " default <bumblebee|optimus> Set default service at boot"
echo " start <bumblebee|optimus> Manually start the service"
echo " stop <bumblebee|optimus> Manually stop the service"
echo ""
echo " help Show this help"
echo " status Show the current status of both services"
}
# ================================================================================
# Main section
# ================================================================================
# Read arguments
cmd=$1
arg=$2
shift
shift
# Check if the script is being run as root, the only exception is if it's using the
# command 'inspector'
if [[ $cmd != 'inspector' ]] && [[ $(id -u) -eq 0 ]]; then
print_error "Don't run this script as root"
exit 1
fi
case $cmd in
'nvidia')
# Check if optimus service is active and it's using nvidia, fatal error
optimus_status=$(service_status optimus)
if [[ $optimus_status = 'active' ]] && [[ $(get_optimus_mode) = 'nvidia' ]]; then
print_error "Currently using nvidia with optimus!"
exit 1
fi
case $arg in
'on')
sudo_run "tee /proc/acpi/bbswitch <<< ON &> /dev/null"
;;
'off')
sudo_run "rmmod nvidia_uvm &> /dev/null | true"
sudo_run "rmmod nvidia_modeset &> /dev/null | true"
sudo_run "rmmod nvidia &>/dev/null | true"
sudo_run "tee /proc/acpi/bbswitch <<< OFF &> /dev/null"
;;
*)
print_error "Invalid value $arg"
;;
esac
;;
'run')
# Check if optimus service is active and it's using nvidia, fatal error
optimus_status=$(service_status optimus)
if [[ $optimus_status = 'active' ]] && [[ $(get_optimus_mode) = 'nvidia' ]]; then
print_error "Currently using nvidia with optimus!"
exit 1
fi
echo "${CYAN}==>${RESET} Running $arg $@"
# Toggle services
stop_service optimus
start_service bumblebee
# Run with primusrun
eval "primusrun $arg $@"
;;
'switch')
gpu=$arg
if [[ $(valid_gpu $gpu) = '0' ]]; then
print_error "Invalid GPU value '$gpu'!"
exit 1
fi
check_gdm_prime
# Toggle services
stop_service bumblebee
start_service optimus
# Switch modes
echo "${CYAN}==>${RESET} Switching to $gpu"
sudo_run "true" # Do nothing but prompt message
optimus-manager --switch $gpu --no-confirm &> /dev/null
;;
'start')
if [[ $(valid_service $arg) = '0' ]]; then
print_error "Unknown service '$arg'!"
exit 1
fi
echo "${CYAN}==>${RESET} Starting service $arg"
start_service $arg
;;
'stop')
if [[ $(valid_service $arg) = '0' ]]; then
print_error "Unknown service '$arg'!"
exit 1
fi
echo "${CYAN}==>${RESET} Stopping service $arg"
stop_service $arg
;;
'status' | '')
print_status
;;
'default')
if [[ $(valid_service $arg) = '0' ]]; then
print_error "Unknown service '$arg'!"
exit 1
fi
echo "${CYAN}==>${RESET} Setting default service to $arg"
set_default_service $arg
enable_service $arg
;;
'startup')
if [[ $(valid_gpu $arg) = '0' ]] || [[ $arg = 'auto' ]]; then
print_error "Invalid GPU value '$arg'!"
exit 1
fi
optimus_status=$(service_status optimus)
if [[ $optimus_status = 'inactive' ]]; then
stop_service bumblebee
start_service optimus
fi
echo "${CYAN}==>${RESET} Setting default startup to $arg"
sudo_run "true" # Do nothing but prompt message
optimus-manager --set-startup $arg &> /dev/null
if [[ $optimus_status = 'inactive' ]]; then
stop_service optimus
start_service bumblebee
fi
echo "${CYAN}==>${RESET} Default startup for ${BOLD}${WHITE}optimus${RESET} is ${BOLD}${WHITE}$(get_optimus_startup)${RESET}"
;;
'inspector')
if [[ $(service_enabled optimus) = 'enabled' ]] &&
[[ $(service_enabled bumblebee) = 'enabled' ]]; then
enable_service $(get_default_service)
exit 0
fi
if [[ $(service_enabled optimus) = 'disabled' ]] &&
[[ $(service_enabled bumblebee) = 'disabled' ]]; then
enable_service $(get_default_service)
exit 0
fi
;;
'help')
print_usage
;;
*)
print_error "Unknown command '$cmd'!"
print_usage
exit 1
;;
esac
# ================================================================================