From de2d81cbc4c6f25f8ea841fb22bf38262c80ac4d Mon Sep 17 00:00:00 2001 From: r4 Date: Thu, 1 Apr 2021 19:18:34 +0000 Subject: [PATCH] Upload files to '' --- opti-sw.sh | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 opti-sw.sh diff --git a/opti-sw.sh b/opti-sw.sh new file mode 100644 index 0000000..53d8611 --- /dev/null +++ b/opti-sw.sh @@ -0,0 +1,62 @@ +#! /usr/bin/env sh + +[ "$USER" = "root" ] && echo "Please DO NOT run this script as root" && exit 1 + +usage() { + echo "Usage:" + echo " nvidia-switch.sh (i)ntel | (n)vidia" +} + +checkinst() { + which $1 >/dev/null 2>/dev/null && return 0 || return 1 +} + +rc_msg() { + TERM_WIDTH=$(stty size | cut -d" " -f2) + + printf "%${TERM_WIDTH}s" | tr " " "_" + printf "\n\n" + echo "If you haven't done so already, please put the following line as the top of your display manager or XOrg init script (for example ~/.xinitrc for startx or /usr/share/sddm/scripts/Xsetup for SDDM):" + echo "sh $HOME/.local/lib/nvidia-switch-rc.sh" + printf "%${TERM_WIDTH}s" | tr " " "_" +} + +su_run() { + sh -c "$SU_PROG sh -c '$1'" +} + +[ ! -e ~/.local/lib/nvidia-switch-rc.sh ] && mkdir -p ~/.local/lib/ && touch ~/.local/lib/nvidia-switch-rc.sh && chmod +x ~/.local/lib/nvidia-switch-rc.sh + +if checkinst "sudo"; then + SU_PROG="sudo" +elif checkinst "doas"; then + SU_PROG="doas" +else + SU_PROG="su -c" +fi + +case $1 in + i|intel) + su_run "modprobe -r nvidia" + [ -e /etc/X11/xorg.conf ] && + su_run "mv -f /etc/X11/xorg.conf /etc/X11/xorg.conf.nvidia" && + echo "Backed up existing XOrg config file /etc/X11/xorg.conf to /etc/X11/xorg.conf.nvidia" + echo " " > ~/.local/lib/nvidia-switch-rc.sh + + echo "Switched to Intel GPU" + ;; + n|nvidia) + rc_msg + # It will complain about the XOrg config file missing, so I am supressing all output, including errors + su_run "nvidia-xconfig --prime" >/dev/null 2>/dev/null + su_run "modprobe nvidia" + echo -e "xrandr --setprovideroutputsource modesetting NVIDIA-0\nxrandr --auto" > ~/.local/lib/nvidia-switch-rc.sh + + su_run "mkdir -p /etc/modules-load.d" + su_run "echo nvidia > /etc/modules-load.d/video.conf" + + echo "Switched to NVIDIA GPU. You will probably need to restart." + ;; + *) usage; exit 1 + ;; +esac