From 79004aaa6bd3990a24aa41b136d5cc907888d256 Mon Sep 17 00:00:00 2001 From: Henrique Lechner Date: Tue, 20 Dec 2016 17:02:21 -0200 Subject: [PATCH] modified: README.md modified: nvidia-xrun new file: nvidia-xrun-core --- README.md | 3 +- nvidia-xrun | 78 ++++++++++++++++++++---------------------------- nvidia-xrun-core | 48 +++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 47 deletions(-) create mode 100644 nvidia-xrun-core diff --git a/README.md b/README.md index 1b09a78..04567c9 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,6 @@ These utility scripts aim to make the life easier for nvidia cards users. It started with a revelation that bumblebee in current state offers very poor performance. This solution offers a bit more complicated procedure but offers a full GPU utilization(in terms of linux drivers) ## Usage: - 1. switch to free tty - 1. login 1. run `nvidia-xrun [app]` 1. enjoy @@ -13,6 +11,7 @@ Currently sudo is required as the script needs to wake up GPU, modprobe the nvid ## Structure * **nvidia-xrun** - uses following dir structure: * **/usr/bin/nvidia-xrun** - the executable script +* **/usr/bin/nvidia-xrun-core** - the script core * **/etc/X11/nvidia-xorg.conf** - the main X confing file * **/etc/X11/xinit/nvidia-xinitrc** - xinitrc config file. Contains the setting of provider output source * **/etc/X11/xinit/nvidia-xinitrc.d** - custom xinitrc scripts directory diff --git a/nvidia-xrun b/nvidia-xrun index 7be3dfa..72b3ea3 100644 --- a/nvidia-xrun +++ b/nvidia-xrun @@ -1,4 +1,5 @@ #!/bin/bash + DRY_RUN=0 function printHelp { echo "Utility to run games and applications in separate X on discrete Nvidia graphic card" @@ -17,72 +18,57 @@ function execute { fi } - -if [[ $EUID -eq 0 ]]; then - echo "This script must not be run as root" >&2 - exit 1 -fi - if [ "$1" == "-d" ] then DRY_RUN=1 shift 1 fi -# calculate current VT -LVT=`fgconsole` - -# calculate first usable display -XNUM="-1" -SOCK="something" -while [ ! -z "$SOCK" ] -do - XNUM=$(( $XNUM + 1 )) - SOCK=$(ls -A -1 /tmp/.X11-unix | grep "X$XNUM" ) -done +## check if script is running through sudo/su or directly from root user +if [[ ${SUDO_USER:-$USER} == "root" ]]; then + if [[ "$SUDO_USER" == "" ]]; then + echo "Do not run it direclty from root user!"; + echo "Run it from a normal user and use sudo or su instead."; + fi + exit 1; +fi -NEWDISP=":$XNUM" +## check if script is running through X +if [[ $DISPLAY != "" && `tty` == "/dev/pts/0" ]]; then + execute "sudo -i openvt -s nvidia-xrun $*" + if [[ $DRY_RUN == "1" ]]; then + exit 0; + fi +fi -if [ ! -z "$*" ] # generate exec line if arguments are given -then - # test if executable exists - if [ ! -x "$(which $1 2> /dev/null)" ] - then - echo "$1: No such executable!" - exit 1 - fi - # generate exec line - EXECL="$(which $1)" - shift 1 - EXECL="$EXECL $*" - EXECPH="" -else # prepare to start new X sessions if no arguments passed - EXECL="" - EXECPH="New X session" +## check if does have necessary permission +if [[ $EUID != "0" ]]; then + execute "sudo -i nvidia-xrun $*" + exit 0; fi -EXECL="/etc/X11/xinit/nvidia-xinitrc $EXECL" +## check current vt in use +[[ $(< '/sys/class/tty/tty0/active') =~ tty([0-9]{1,}) ]] +LVT=${BASH_REMATCH[1]} + -COMMAND="xinit $EXECL -- $NEWDISP vt$LVT -nolisten tcp -br -config nvidia-xorg.conf -configdir nvidia-xorg.conf.d" -# --------- TURNING ON GPU ----------- echo 'Waking up nvidia GPU' execute "sudo tee /proc/acpi/bbswitch <<&2 + exit 1 +fi + +# calculate current VT +[[ $(< '/sys/class/tty/tty0/active') =~ tty([0-9]{1,}) ]] +LVT=${BASH_REMATCH[1]} + +# calculate first usable display +XNUM="-1" +SOCK="something" +while [ ! -z "$SOCK" ] +do + XNUM=$(( $XNUM + 1 )) + SOCK=$(ls -A -1 /tmp/.X11-unix | grep "X$XNUM" ) +done + +if [ ! -z "$*" ] # generate exec line if arguments are given +then + # test if executable exists + if [ ! -x "$(which $1 2> /dev/null)" ] + then + echo "$1: No such executable!" + exit 1 + fi + # generate exec line + EXECL="$(which $1)" + shift 1 + EXECL="$EXECL $*" + EXECPH="" +else # prepare to start new X sessions if no arguments passed + EXECL="" + EXECPH="New X session" +fi + +NEWDISP=":$XNUM" + +EXECL="/etc/X11/xinit/nvidia-xinitrc $EXECL" + +COMMAND="xinit $EXECL -- $NEWDISP vt$LVT -nolisten tcp -br -config nvidia-xorg.conf -configdir nvidia-xorg.conf.d" + + +exec $COMMAND