Easy switch between bumblebee and nvidia-prime

Generally I use bumblebee - it’s better solution for me than logging out every time when I want to use nvidia card. But sometimes I need to use nvidia prime because of some issues in bumblebee.

If you want to use nvidia-prime and you still want to have bumblebee installed on your system, look at this script. You probably need to modify some lines because I wrote it only for my own needs. But it shows steps which must be done to make it work.

It’s written for Ubuntu. If you have Debian you need to change glx alternatives. I don’t know what about other systems.

File xorg.conf-nvidia is default xorg.conf for nvidia optimus systems. You can get it from there:
http://us.download.nvidia.com/XFree86/Linux-x86/319.12/README/randr14.html

#!/bin/bash

if [ $# = 0 ]; then
	echo "Use the one of following commands:"
	echo
	echo "1) sudo nohup $0 nvidia"
	echo "2) sudo nohup $0 intel"
	echo
	
	read command
	
	if [ "$command" = "1" ]; then
		sudo nohup "$0" nvidia
	fi
	
	if [ "$command" = "2" ]; then
		sudo nohup "$0" intel
	fi
	
	exit
fi


if [ `whoami` != "root" ]; then 
	echo "You must run it as root";
	exit
fi


if [ "$1" = "nvidia" ]; then
	gnome-session-quit
	stop gdm

	echo "ON" > /proc/acpi/bbswitch
	sleep 2
	modprobe nvidia-331

	cp /etc/X11/xorg.conf-nvidia /etc/X11/xorg.conf
	echo 'xrandr --setprovideroutputsource modesetting NVIDIA-0 && xrandr --auto' > /home/dave/.xsessionrc
	
	echo 'start on runlevel [2345]' > /etc/init/nvidia-moja.conf
	echo 'script' >> /etc/init/nvidia-moja.conf
	echo '    echo "ON" > /proc/acpi/bbswitch' >> /etc/init/nvidia-moja.conf
	echo '    modprobe nvidia-331' >> /etc/init/nvidia-moja.conf
	echo 'end script' >> /etc/init/nvidia-moja.conf
	
	update-alternatives --set x86_64-linux-gnu_gl_conf /usr/lib/nvidia-331/ld.so.conf
	update-alternatives --set i386-linux-gnu_gl_conf /usr/lib/nvidia-331/alt_ld.so.conf

	ldconfig

	start gdm
	restart gdm
fi


if [ "$1" = "intel" ]; then
	gnome-session-quit
	stop gdm

	rm /etc/X11/xorg.conf
	echo > /home/dave/.xsessionrc
	rm /etc/init/nvidia-moja.conf

	update-alternatives --set x86_64-linux-gnu_gl_conf /usr/lib/x86_64-linux-gnu/mesa/ld.so.conf
	update-alternatives --set i386-linux-gnu_gl_conf /usr/lib/i386-linux-gnu/mesa/ld.so.conf

	echo "OFF" > /proc/acpi/bbswitch	
	sleep 2
	rmmod nvidia
	
	ldconfig

	start gdm
	restart gdm
fi

I know you posted this a little while ago but it came up during my research to switch easily between bumblebee and prime.

As I was modifying your script I thought “might as well make is as agnostic as possible”, put in some automation and what not and uploaded the results.

If you wanna email me your email and I’ll give you proper credit on the script (or if you put yours on github, ill take mine down and make it a fork of yours or something).

So again, thanks for this.

Mine might need some work to make it work on a gnome environment, since i run xfce4 I cant really test.

I’m glad that someone is interested in it :)

Currently I’m using slightly improved script. Basically the changes are:

  • use graphical interface if gxmessage is available (the same can be done using zenity)
  • automatically find nvidia module name
  • overwrite optirun command (this may be unsafe)

Though still your user home directory is not detected. Not sure if it’s possible because it must be run as root…

#!/bin/bash

if [ $# = 0 ]; then
	if [ ! -z "$DISPLAY" ] && [ -x /usr/bin/gxmessage ]; then
		gxmessage -title "nvidia-switch" -center -buttons "Nvidia":2,"Intel":3 "Set nvidia or intel graphics card.";
	
		case $? in
		  "2") gksudo nohup $0 nvidia ;;
		  "3") gksudo nohup $0 intel ;;
		esac
	else
		echo "Use the one of following commands:"
		echo
		echo "1) sudo nohup $0 nvidia"
		echo "2) sudo nohup $0 intel"
		echo
		
		read command
		
		if [ "$command" = "1" ]; then
			sudo nohup "$0" nvidia
		fi
		
		if [ "$command" = "2" ]; then
			sudo nohup "$0" intel
		fi	
	fi

	exit
fi

if [ `whoami` != "root" ]; then 
	echo "You must run it as root";
	exit
fi

if [ "$1" = "nvidia" ]; then
	NVIDIA_MODULE="`ls /usr/lib | grep -o nvidia\-[1-9][1-9][1-9]\-updates | head -n1`"
	if [ -z "$NVIDIA_MODULE" ]; then
		NVIDIA_MODULE="`ls /usr/lib | grep -o nvidia\-[1-9][1-9][1-9] | head -n1`"
	fi
	if [ -z "$NVIDIA_MODULE" ]; then
		echo "Error! Cannot find nvidia module name. You should modify this script to make it work."
		exit
	fi

	gnome-session-quit
	#stop gdm
	stop lightdm

	echo "ON" > /proc/acpi/bbswitch
	sleep 2
	modprobe $NVIDIA_MODULE

	cp /etc/X11/xorg.conf-nvidia-moja /etc/X11/xorg.conf
	echo 'xrandr --setprovideroutputsource modesetting NVIDIA-0 && xrandr --auto' > /home/dave/.xsessionrc
	
	echo 'start on runlevel [2345]' > /etc/init/nvidia-moja.conf
	echo 'script' >> /etc/init/nvidia-moja.conf
	echo '    echo "ON" > /proc/acpi/bbswitch' >> /etc/init/nvidia-moja.conf
	echo "    exec modprobe $NVIDIA_MODULE" >> /etc/init/nvidia-moja.conf
	echo 'end script' >> /etc/init/nvidia-moja.conf
	echo >> /etc/init/nvidia-moja.conf

	if [ "`cat /usr/bin/optirun`" != '$@' ]; then 
		cp /usr/bin/optirun /usr/bin/optirun-org 
		echo '$@' > /usr/bin/optirun 
	fi
	
	update-alternatives --set x86_64-linux-gnu_gl_conf /usr/lib/$NVIDIA_MODULE/ld.so.conf
	update-alternatives --set i386-linux-gnu_gl_conf /usr/lib/$NVIDIA_MODULE/alt_ld.so.conf

	ldconfig

	start lightdm
	#start gdm
	#restart gdm
fi

if [ "$1" = "intel" ]; then
	gnome-session-quit
	#stop gdm
	stop lightdm

	rm /etc/X11/xorg.conf
	echo > /home/dave/.xsessionrc
	rm /etc/init/nvidia-moja.conf

	if [ "`cat /usr/bin/optirun`" = '$@' ]; then 
		mv /usr/bin/optirun-org /usr/bin/optirun
	fi

	update-alternatives --set x86_64-linux-gnu_gl_conf /usr/lib/x86_64-linux-gnu/mesa/ld.so.conf
	update-alternatives --set i386-linux-gnu_gl_conf /usr/lib/i386-linux-gnu/mesa/ld.so.conf

	echo "OFF" > /proc/acpi/bbswitch	
	sleep 2
	rmmod nvidia
	
	ldconfig

	start lightdm
	#start gdm
	#restart gdm
fi

Ah awesome, yeah I added in auto module name stuff but use dpkg -l to find it (so it does rule out people who’ve installed manually, hhmmm) , I found the best way for home dir is “logname”;

maquis196@d-deridex ~ % logname
maquis196
maquis196@d-deridex ~ % sudo logname
maquis196
maquis196@d-deridex ~ % whoami
maquis196
maquis196@d-deridex ~ % sudo whoami
root
maquis196@d-deridex ~ %

Been damn useful so far for playing metro redux, so again. Thanks :)

@Maquis196

Problem is that you can execute is as root using “su” command. In this case $HOME variable is overwritten by path to root home dir. You can assume that only one user is logged in or read which one user has auto-logging enabled in lightdm config files (which AFAIK is needed for nvidia-prime). Or you can just assume that using “sudo” instead of “su” is necessary :)