Skip to content

Instantly share code, notes, and snippets.

@khromalabs
Last active May 31, 2020 15:51
Show Gist options
  • Save khromalabs/0c5e637f3cd6b34faf2b95d215edb63d to your computer and use it in GitHub Desktop.
Save khromalabs/0c5e637f3cd6b34faf2b95d215edb63d to your computer and use it in GitHub Desktop.
Bash script to easily toggle in Nvidia Optimus based laptops between integrated and discrete graphics cards, compatible out of the box with Archlinux+LightDM systems
#!/bin/bash
# Nvidia official linux drivers and the bbswitch module are asumed to be installed, when using the integrated
# intel video card bbswitch will be used to turn the Nvidia card off for energy saving purposes. Reboot is needed to
# switch between modes.
# NOTE:
# /etc/lightdm.conf must contain this text, uncommented:
#
# [Seat:*]
# ... etc ...
# display-setup-script=/etc/lightdm/display-setup
[ "$UID" -eq 0 ] || exec sudo "$0" "$@"
ME=$(echo $(basename $0))
MODPROBE_CONF_FILE="/etc/modprobe.d/video.conf"
XORG_CONFIG_FILE="/usr/share/X11/xorg.conf.d/10-nvidia.conf"
LIGHTDM_CONF_FILE="/etc/lightdm/display-setup"
IFS='' read -r -d '' MODPROBE_NVIDIA <<"EOD"
blacklist bbswitch
EOD
IFS='' read -r -d '' MODPROBE_INTEL <<"EOD"
install nvidia /bin/true
install nvidia_drm /bin/true
install nvidia_modeset /bin/true
options bbswitch load_state=0 unload_state=1
EOD
IFS='' read -r -d '' LIGHTDM_NVIDIA_INFO <<"EOD"
#!/bin/bash
xrandr --setprovideroutputsource modesetting NVIDIA-0
xrandr --auto
xrandr --dpi 96
EOD
IFS='' read -r -d '' XORG_CONF_NVIDIA <<"EOD"
Section "OutputClass"
Identifier "nvidia"
MatchDriver "nvidia-drm"
Driver "nvidia"
Option "AllowEmptyInitialConfiguration"
Option "PrimaryGPU" "yes"
ModulePath "/usr/lib/nvidia/xorg"
ModulePath "/usr/lib/xorg/modules"
EndSection
EOD
set -eu
echo
if [ $# -lt 1 ]; then
if [ ! -x "$(which glxinfo)" ]; then
echo "Missing required tool glxinfo for autodetection, please install it or specify a setup (NVIDIA|INTEL)"
exit
fi
if [ -z "$(glxinfo|grep 'OpenGL vendor string: NVIDIA')" ]; then
echo "Autodetected INTEL OpenGL vendor in use"
WILLENABLE="NVIDIA"
else
echo "Autodetected NVIDIA OpenGL vendor in use"
WILLENABLE="INTEL"
fi
elif [ "$1" = "intel" ]; then
WILLENABLE="INTEL"
elif [ "$1" = "nvidia" ]; then
WILLENABLE="NVIDIA"
else
echo -e "Invalid parameter, use:\n$ME (optional: nvidia|intel)"
exit
fi
echo "Will switch to $WILLENABLE video setup, a reboot is needed after that"
read -p "Please press Enter to continue, C-c to abort"
DONE=0
echo -n "Processing"
if [ "$WILLENABLE" = "NVIDIA" ]; then
echo -n "."
echo "${MODPROBE_NVIDIA}" > $MODPROBE_CONF_FILE
echo -n "."
echo "${XORG_CONF_NVIDIA}" > $XORG_CONFIG_FILE
echo -n "."
echo "${LIGHTDM_NVIDIA_INFO}" > $LIGHTDM_CONF_FILE
chmod a+x $LIGHTDM_CONF_FILE
DONE=1
elif [ "$WILLENABLE" = "INTEL" ]; then
echo -n "."
echo "${MODPROBE_INTEL}" > $MODPROBE_CONF_FILE
echo -n "."
test -f $XORG_CONFIG_FILE && rm $XORG_CONFIG_FILE
echo -n "."
test -f $LIGHTDM_CONF_FILE && echo "" > $LIGHTDM_CONF_FILE
DONE=1
fi
if [ "$DONE" = "1" ]; then
echo -e "\nDone"
if [ -z "$(lsmod | egrep "(^nvidia|^bbswitch)")" ]; then
echo "Warning: Neither the nvidia or the bbswitch kernel modules are loaded, recommending a full system halt"
fi
read -p "Do you want to reboot now?(y/N) " yn
if [ "$yn" = "y" ]; then
reboot
fi
else
echo "Something went wrong"
fi
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment