From Fedora Project Wiki

http://asusm51ta-with-linux.blogspot.com/


[10/11/2010] Fedora 14 / Ubuntu 10.10 + vga_switcheroo + switch scripts

(0) Install Fedora 14 / Ubuntu 10.10, install needed packages & download icons

(0.1) Burn a CD or make a bootable USB with your fabourite distribution and install it on your laptop.

<img style="margin: 0pt 10px 10px 0pt; float: left; width: 48px; height: 48px;" src="http://lh3.ggpht.com/_Dw3SC8gD9Jk/S4fBc2QY_4I/AAAAAAAAAEE/PJ6LitzZGI0/s800/fedoraLogoMini.png" /><a href="http://fedoraproject.org/get-fedora">Fedora 14</a>




<img style="margin: 0pt 10px 10px 0pt; float: left; width: 48px; height: 48px;" src="http://lh3.ggpht.com/_Dw3SC8gD9Jk/TI5fOYoTtTI/AAAAAAAAAJY/Iu_RNfvP8Jk/s800/ubuntu-logo48.png" /><a href="http://www.ubuntu.com/desktop/get-ubuntu/download">Ubuntu 10.10</a>



(0.2) Install the following packages from the command line and download icons:

<img style="margin: 0pt 10px 10px 0pt; float: left; width: 12px; height: 12px;" src="http://lh4.ggpht.com/_Dw3SC8gD9Jk/TNbQWyYuRBI/AAAAAAAAAJ0/P6klqreBeC8/s800/fedoraLogoMiniMini.png" />

$ sudo yum update
$ sudo yum install wget gxmessage libnotify-bin
$ wget -P /home/"$USER"/.local/share/icons/ http://lh4.ggpht.com/_Dw3SC8gD9Jk/S-MGVcEfaiI/AAAAAAAAAIA/Pguy_uSeqSk/s800/hardware_down.png
$ wget -P /home/"$USER"/.local/share/icons/ http://lh5.ggpht.com/_Dw3SC8gD9Jk/S-MGVSO0JbI/AAAAAAAAAIE/_mdAnW7UiCQ/s800/hardware_up.png
$ wget -P /home/"$USER"/.local/share/icons/ http://lh4.ggpht.com/_Dw3SC8gD9Jk/TNbQXzK6hrI/AAAAAAAAAJ8/YlqK46MusuE/s800/hardware.png
$ wget -P /home/"$USER"/.local/share/icons/ http://lh5.ggpht.com/_Dw3SC8gD9Jk/S41DjuMt_MI/AAAAAAAAAF4/5pQm2-dX6Sg/s800/switch_before_shutdown.png</div>

<img style="margin: 0pt 10px 10px 0pt; float: left; width: 12px; height: 12px;" src="http://lh3.ggpht.com/_Dw3SC8gD9Jk/TNbQXv-yg9I/AAAAAAAAAJ4/rOBoFHV4hvg/s800/ubuntuLogoMiniMini.png" />

$ sudo apt-get update
$ sudo apt-get install wget gxmessage libnotify-bin
$ wget -P /home/"$USER"/.local/share/icons/ http://lh4.ggpht.com/_Dw3SC8gD9Jk/S-MGVcEfaiI/AAAAAAAAAIA/Pguy_uSeqSk/s800/hardware_down.png
$ wget -P /home/"$USER"/.local/share/icons/ http://lh5.ggpht.com/_Dw3SC8gD9Jk/S-MGVSO0JbI/AAAAAAAAAIE/_mdAnW7UiCQ/s800/hardware_up.png
$ wget -P /home/"$USER"/.local/share/icons/ http://lh4.ggpht.com/_Dw3SC8gD9Jk/TNbQXzK6hrI/AAAAAAAAAJ8/YlqK46MusuE/s800/hardware.png
$ wget -P /home/"$USER"/.local/share/icons/ http://lh5.ggpht.com/_Dw3SC8gD9Jk/S41DjuMt_MI/AAAAAAAAAF4/5pQm2-dX6Sg/s800/switch_before_shutdown.png</div>

(1) Mount "none" partition on fstab (just on Fedora, Ubuntu does it by default)

<img style="margin: 0pt 10px 10px 0pt; float: left; width: 12px; height: 12px;" src="http://lh4.ggpht.com/_Dw3SC8gD9Jk/TNbQWyYuRBI/AAAAAAAAAJ0/P6klqreBeC8/s800/fedoraLogoMiniMini.png" />
$ sudo gedit /etc/fstab
And add the following line:
none                    /sys/kernel/debug	debugfs	defaults	0 0

(2) Change switch file permissions every time computer initiates

<img style="margin: 0pt 10px 10px 0pt; float: left; width: 12px; height: 12px;" src="http://lh4.ggpht.com/_Dw3SC8gD9Jk/TNbQWyYuRBI/AAAAAAAAAJ0/P6klqreBeC8/s800/fedoraLogoMiniMini.png" />
$ sudo gedit /etc/rc.d/rc.local
And add the following lines:

chown "username" /sys/kernel/debug/vgaswitcheroo/switch	# change "username" with your user name
echo OFF > /sys/kernel/debug/vgaswitcheroo/switch

<img style="margin: 0pt 10px 10px 0pt; float: left; width: 12px; height: 12px;" src="http://lh3.ggpht.com/_Dw3SC8gD9Jk/TNbQXv-yg9I/AAAAAAAAAJ4/rOBoFHV4hvg/s800/ubuntuLogoMiniMini.png" />
$ sudo gedit /etc/rc.local
And add the following lines:

chown "username" /sys/kernel/debug/vgaswitcheroo/switch	# change "username" with your user name
echo OFF > /sys/kernel/debug/vgaswitcheroo/switch

(3) Reboot & copy and run script to switch between graphic cards

<img style="margin: 0pt 10px 10px 0pt; float: left; width: 48px; height: 48px;" src="http://lh4.ggpht.com/_Dw3SC8gD9Jk/S4nIxZBts2I/AAAAAAAAAEo/1wDkx45n4XU/s800/executable.png" />switch_between_cards.sh

#!/bin/bash
# "switch_between_cards.sh" script by RM, with useful changes from LoLL
# version 20101107

pci_integrated=$(lspci | grep VGA | sed -n '1p' | cut -f 1 -d " ")
pci_discrete=$(lspci | grep VGA | sed -n '2p' | cut -f 1 -d " ")

integrated=$(cat /sys/kernel/debug/vgaswitcheroo/switch | grep $pci_integrated | grep -o -P ':.:...:')
discrete=$(cat /sys/kernel/debug/vgaswitcheroo/switch | grep $pci_discrete | grep -o -P ':.:...:')

name_integrated=$(lspci | grep VGA | sed -n '1p' | sed -e "s/.* VGA compatible controller[ :]*//g" | sed -e "s/ Corporation//g" | sed -e "s/ Technologies Inc//g" | sed -e 's/\[[0-9]*\]: //g' | sed -e 's/\[[0-9:a-z]*\]//g' | sed -e 's/(rev [a-z0-9]*)//g' | sed -e "s/ Integrated Graphics Controller//g")

name_discrete=$(lspci | grep VGA | sed -n '2p' | sed -e "s/.* VGA compatible controller[ :]*//g" | sed -e "s/ Corporation//g" | sed -e "s/ Technologies Inc//g" | sed -e 's/\[[0-9]*\]: //g' | sed -e 's/\[[0-9:a-z]*\]//g' | sed -e 's/(rev [a-z0-9]*)//g' | sed -e "s/ Integrated Graphics Controller//g")

if [ "$integrated" = ":+:Pwr:" ]
then
	integrated_condition="(*) - Power ON"
elif [ "$integrated" = ": :Pwr:" ]
then
	integrated_condition="( ) - Power ON"
elif [ "$integrated" = ": :Off:" ]
then
	integrated_condition="( ) - Power OFF"
fi

if [ "$discrete" = ":+:Pwr:" ]
then
	discrete_condition="(*) - Power ON"
elif [ "$discrete" = ": :Pwr:" ]
then
	discrete_condition="( ) - Power ON"
elif [ "$discrete" = ": :Off:" ]
then
	discrete_condition="( ) - Power OFF"
fi

gxmessage -center \
          -buttons "_Cancel":1,"switch to _Integrated":101,"switch to _Discrete":102 \
          -wrap \
          -title "Choose Hybrid Graphic Card" \
"Choose Hybrid Graphic Card
=================
Integrated: $integrated_condition : $name_integrated
Discrete: $discrete_condition : $name_discrete"


whichCard=$?

case "$whichCard" in

1)
	echo "Exit"
;;
101)
	if [ "$integrated" == ":+:Pwr:" ] && [ "$discrete" == ": :Pwr:" ]
	then
		notify-send -t 5000 --icon="/home/$USER/.local/share/icons/hardware_down.png" "switching to $name_integrated"
		echo OFF > /sys/kernel/debug/vgaswitcheroo/switch
	elif [ "$integrated" == ": :Pwr:" ] && [ "$discrete" == ":+:Pwr:" ]
	then
		notify-send -t 5000 --icon="/home/$USER/.local/share/icons/hardware_down.png" "switching to $name_integrated"
		echo DIGD > /sys/kernel/debug/vgaswitcheroo/switch
		if [ "$DESKTOP_SESSION" = "openbox" ]
		then
			killall -u "$USER"
		elif [ "$DESKTOP_SESSION" = "gnome" ]
		then
			gnome-session-save --logout
		fi
	elif [ "$integrated" == ": :Off:" ] && [ "$discrete" == ":+:Pwr:" ]
	then
		notify-send -t 5000 --icon="/home/$USER/.local/share/icons/hardware_down.png" "switching to $name_integrated"
		echo ON > /sys/kernel/debug/vgaswitcheroo/switch
		echo DIGD > /sys/kernel/debug/vgaswitcheroo/switch
		if [ "$DESKTOP_SESSION" = "openbox" ]
		then
			killall -u "$USER"
		elif [ "$DESKTOP_SESSION" = "gnome" ]
		then
			gnome-session-save --logout
		fi
	elif [ "$integrated" == ":+:Pwr:" ] && [ "$discrete" == ": :Off:" ]
	then
		notify-send -t 5000 --icon="/home/$USER/.local/share/icons/hardware_down.png" "already switched to $name_integrated"		
	fi
;;
102)
	if [ "$integrated" == ":+:Pwr:" ] && [ "$discrete" == ": :Pwr:" ]
	then
		notify-send -t 5000 --icon="/home/$USER/.local/share/icons/hardware_up.png" "switching to $name_discrete"
		echo DDIS > /sys/kernel/debug/vgaswitcheroo/switch
		if [ "$DESKTOP_SESSION" = "openbox" ]
		then
			killall -u "$USER"
		elif [ "$DESKTOP_SESSION" = "gnome" ]
		then
			gnome-session-save --logout
		fi
	elif [ "$integrated" == ": :Pwr:" ] && [ "$discrete" == ":+:Pwr:" ]
	then
		notify-send -t 5000 --icon="/home/$USER/.local/share/icons/hardware_up.png" "switching to $name_discrete"
		echo OFF > /sys/kernel/debug/vgaswitcheroo/switch
	elif [ "$integrated" == ":+:Pwr:" ] && [ "$discrete" == ": :Off:" ]
	then
		notify-send -t 5000 --icon="/home/$USER/.local/share/icons/hardware_up.png" "switching to $name_discrete"		
		echo ON > /sys/kernel/debug/vgaswitcheroo/switch
		echo DDIS > /sys/kernel/debug/vgaswitcheroo/switch
		if [ "$DESKTOP_SESSION" = "openbox" ]
		then
			killall -u "$USER"
		elif [ "$DESKTOP_SESSION" = "gnome" ]
		then
			gnome-session-save --logout
		fi
	elif [ "$integrated" == ": :Off:" ] && [ "$discrete" == ":+:Pwr:" ]
	then
		notify-send -t 5000 --icon="/home/$USER/.local/share/icons/hardware_up.png" "already switched to $name_discrete"		
	fi
;;
esac

(4) OPTIONAL: Script to turn off laptop

This next script is used to turn off the computer. Because, in my case, if I turn off the computer without switching on both cards (specially when the integrated card is on and the discrete card is off), the fan runs at full speed when I turn it on again, stoping only after rebooting. Hence, to avoid this, this other launcher can be used to turn off the computer:

<img style="margin: 0pt 10px 10px 0pt; float: left; width: 48px; height: 48px;" src="http://lh4.ggpht.com/_Dw3SC8gD9Jk/S4nIxZBts2I/AAAAAAAAAEo/1wDkx45n4XU/s800/executable.png" />switch_before_shutdown.sh

#!/bin/bash

if [ "$DESKTOP_SESSION" = "openbox" ]
then
	gxmessage "Are you sure you want to shut down your computer?" -center -title "Take action" -font "Sans bold 10" -default "Cancel" -buttons "_Cancel":1,"L_ock screen":101,"_Log out":102,"_Reboot":103,"_Shut down":104

	case $? in

	1)
		echo "Exit";;
	101)
		xscreensaver-command -lock;;
	102)
		killall openbox;;
	103)
		echo ON > /sys/kernel/debug/vgaswitcheroo/switch;
		gnome-terminal -e 'sudo shutdown -r now';;
	104)
		echo ON > /sys/kernel/debug/vgaswitcheroo/switch
		gnome-terminal -e 'sudo shutdown -h now';;
	esac

elif [ "$DESKTOP_SESSION" = "gnome" ]
then

	gxmessage "Are you sure you want to shut down your computer?" -center -title "Take action" -font "Sans bold 10" -default "Cancel" -buttons "_Cancel":1,"L_ock screen":101,"_Log out":102,"_Reboot":103,"_Shut down":104

	case $? in

	1)
		echo "Exit";;
	101)
		gnome-screensaver-command -l;;
	102)
		gnome-session-save --logout;;
	103)
		echo ON > /sys/kernel/debug/vgaswitcheroo/switch;
		gnome-terminal -e 'sudo shutdown -r now';;
	104)
		echo ON > /sys/kernel/debug/vgaswitcheroo/switch
		gnome-terminal -e 'sudo shutdown -h now';;
	esac

fi

I tested this HowTo on my laptop with Fedora 14 (x64) and Ubuntu 10.10 (x32). Please write me if you find any errors or have any sugestion to modify and improve this HowTo.