Rewrite + make suspend work on NVIDIA

This commit is contained in:
r4 2021-05-31 13:23:07 +02:00
parent a4e512b547
commit a7cbe56f39

View File

@ -1,64 +1,88 @@
#! /usr/bin/env sh #!/usr/bin/env sh
[ "$USER" = "root" ] && echo "Please DO NOT run this script as root" && exit 1 [ "$USER" = "root" ] && echo "Please DO NOT run this script as root" && exit 1
checkinst() {
which $1 >/dev/null 2>/dev/null && return 0 || return 1
}
usage() { usage() {
echo "Usage:" echo "Usage:"
echo " opti-sw.sh (i)ntel | (n)vidia" echo " opti-sw.sh (i)ntel | (n)vidia"
} }
checkinst() { if ! checkinst "sudo"; then
which $1 >/dev/null 2>/dev/null && return 0 || return 1 if checkinst "doas"; then
} alias sudo="doas"
else
alias sudo="su -c"
fi
fi
case "$1" in
i|intel) OPT=intel;;
n|nvidia) OPT=nvidia;;
*) usage; exit 1;;
esac
SUSPEND_SCRIPT=/lib64/elogind/system-sleep/opti-sw-nvidia.sh
rc_msg() { rc_msg() {
TERM_WIDTH=$(stty size | cut -d" " -f2) TERM_WIDTH=$(stty size | cut -d" " -f2)
printf "%${TERM_WIDTH}s" | tr " " "_" printf "%${TERM_WIDTH}s" | tr " " "_"
printf "\n\n" 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 "If you haven't done so already, please put the following line at 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" echo "sh $HOME/.local/lib/nvidia-switch-rc.sh"
printf "%${TERM_WIDTH}s" | tr " " "_" printf "%${TERM_WIDTH}s" | tr " " "_"
} }
su_run() { # Make suspend work for NVIDIA
sh -c "$SU_PROG sh -c '$1'" install_nvidia_suspend_script() {
[ ! -d /lib64/elogind/system-sleep/ ] &&
echo "Unable to install NVIDIA suspend script: /lib64/elogind/system-sleep/ not found. This might be because you aren't using elogind."
sudo sh -c "echo '#!/usr/bin/env sh' > $SUSPEND_SCRIPT"
sudo sh -c "echo '# Managed by opti-sw.sh' >> $SUSPEND_SCRIPT"
sudo sh -c "echo 'lsmod | grep \"^nvidia\" || exit 0' >> $SUSPEND_SCRIPT"
sudo sh -c "echo 'WHEN=\"\$1\"' >> $SUSPEND_SCRIPT"
sudo sh -c "echo 'WHAT=\"\$2\"' >> $SUSPEND_SCRIPT"
sudo sh -c "echo 'if [ pre = \"\$WHEN\" ]; then' >> $SUSPEND_SCRIPT"
sudo sh -c "echo ' if [ suspend = \"\$WHAT\" ]; then' >> $SUSPEND_SCRIPT"
sudo sh -c "echo ' /usr/bin/nvidia-sleep.sh suspend' >> $SUSPEND_SCRIPT"
sudo sh -c "echo ' else' >> $SUSPEND_SCRIPT"
sudo sh -c "echo ' /usr/bin/nvidia-sleep.sh hibernate' >> $SUSPEND_SCRIPT"
sudo sh -c "echo ' fi' >> $SUSPEND_SCRIPT"
sudo sh -c "echo 'elif [ post = \"\$WHEN\" ]; then' >> $SUSPEND_SCRIPT"
sudo sh -c "echo ' sleep 1' >> $SUSPEND_SCRIPT"
sudo sh -c "echo ' /usr/bin/nvidia-sleep.sh resume &' >> $SUSPEND_SCRIPT"
sudo sh -c "echo 'fi' >> $SUSPEND_SCRIPT"
sudo chmod +x $SUSPEND_SCRIPT
} }
[ ! -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 case $OPT in
intel)
sudo modprobe -r nvidia
[ -e /etc/X11/xorg.conf ] &&
sudo 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
sudo sed -i '/^nvidia$/d' /etc/modules-load.d/video.conf
echo "Switched to Intel GPU"
;;
nvidia)
rc_msg
sudo nvidia-xconfig --prime > /dev/null 2> /dev/null
sudo modprobe nvidia
echo -e "xrandr --setprovideroutputsource modesetting NVIDIA-0\nxrandr --auto" > ~/.local/lib/nvidia-switch-rc.sh
sudo mkdir -p /etc/modules-load.d
grep '^nvidia$' /etc/modules-load.d/video.conf > /dev/null || sudo su -c 'echo nvidia >> /etc/modules-load.d/video.conf'
if checkinst "sudo"; then # Make suspend work
SU_PROG="sudo" [ ! -e "$SUSPEND_SCRIPT" ] && install_nvidia_suspend_script && echo "Installed $SUSPEND_SCRIPT to make suspend work on NVIDIA"
elif checkinst "doas"; then echo "Switched to NVIDIA GPU. You will probably need to restart."
SU_PROG="doas" ;;
else esac
SU_PROG="su -c"
fi
case $1 in [ ! -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
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
su_run "sed -i '/^nvidia$/d' /etc/modules-load.d/video.conf"
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"
grep '^nvidia$' /etc/modules-load.d/video.conf > /dev/null || 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