Skip to content

Instantly share code, notes, and snippets.

@ghostface
Created November 26, 2018 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghostface/dce713475fddffa707b08f80b05df363 to your computer and use it in GitHub Desktop.
Save ghostface/dce713475fddffa707b08f80b05df363 to your computer and use it in GitHub Desktop.
#!/bin/bash
CMD_TO_RUN=$@
DRY_RUN=0
function printHelp {
echo "Utility to run games and applications in separate X on discrete Nvidia graphic card"
echo "Usage: "
echo "nvidia-xrun [<options>] [<app>]"
echo "Options: "
echo " -d Dry run - prints the final command but does not execute it"
}
function execute {
if [ ${DRY_RUN} -eq 1 ]
then
echo ">>Dry run. Command: $*"
else
eval $*
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
NEWDISP=":$XNUM"
if [ ! -z "$*" ] # generate exec line if arguments are given
then
# test if executable exists in path
if [ -x "$(which $1 2> /dev/null)" ]
then
# generate exec line
EXECL="$(which $1)"
# test if executable exists on disk
elif [ -e "$(realpath "$1")" ]
then
# generate exec line
EXECL="$(realpath "$1")"
else
echo "$1: No such executable!"
exit 1
fi
shift 1
EXECL="$EXECL $*"
else # prepare to start new X sessions if no arguments passed
EXECL=""
fi
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"
# --------- TURNING ON GPU -----------
echo 'Turning the PCIe controller on to allow card rescan'
execute "sudo tee /sys/bus/pci/devices/0000:00:01.0/power/control <<<on"
echo 'Waiting 1 second'
execute "sleep 1"
echo 'Rescanning PCI devices'
execute "sudo tee /sys/bus/pci/rescan <<<1"
# ---------- LOADING MODULES ----------
echo 'Loading nvidia module'
execute "sudo modprobe nvidia"
echo 'Loading nvidia_uvm module'
execute "sudo modprobe nvidia_uvm"
echo 'Loading nvidia_modeset module'
execute "sudo modprobe nvidia_modeset"
echo 'Loading nvidia_drm module'
execute "sudo modprobe nvidia_drm modeset=1"
# ---------- EXECUTING COMMAND --------
#execute ${COMMAND}
echo "optirun $CMD_TO_RUN"
execute "optirun $CMD_TO_RUN"
execute "sleep 2"
# ---------- UNLOADING MODULES --------
#No need for this if you have AlwaysUnloadKernelDriver=True in patched bumblebee.conf
#echo 'Unloading nvidia_drm module'
#execute "sudo rmmod nvidia_drm"
#echo 'Unloading nvidia_modeset module'
#execute "sudo rmmod nvidia_modeset"
#echo 'Unloading nvidia_uvm module'
#execute "sudo rmmod nvidia_uvm"
#echo 'Unloading nvidia module'
#execute "sudo rmmod nvidia"
# --------- TURNING OFF GPU ----------
if [ -f /sys/bus/pci/devices/0000:01:00.0/remove ]; then
echo 'Removing Nvidia bus from the kernel'
execute "sudo tee /sys/bus/pci/devices/0000:01:00.0/remove <<<1"
fi
echo 'Enabling powersave for the PCIe controller'
execute "sudo tee /sys/bus/pci/devices/0000:00:01.0/power/control <<<auto"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment