vgaswitcheroo

vgaswitcheroo on thinkpad T500 running xubuntu 13.04:

Based on this great howTo, but permissions-problem solved with sudo. Original version of script and icons also from there.

Improvements:

– changing permissions on /sys-file not needed

– works for multiuser (the ‘chown user /sys/kernel/debug/vgaswitcheroo/switch’-approach from the original script works only for one user)
– using xubuntu-specific logout (but more specialisation still needed for other DEs / distros)

script:

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

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=$(sudo cat /sys/kernel/debug/vgaswitcheroo/switch | grep $pci_integrated | grep -o -P ':.:...:')
discrete=$(sudo 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="ON"
elif [ "$integrated" = ": :Pwr:" ]
then
integrated_condition="ON"
elif [ "$integrated" = ": :Off:" ]
then
integrated_condition="Off"
fi

if [ "$discrete" = ":+:Pwr:" ]
then
discrete_condition="ON"
elif [ "$discrete" = ": :Pwr:" ]
then
discrete_condition="ON"
elif [ "$discrete" = ": :Off:" ]
then
discrete_condition="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 | sudo tee /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 | sudo tee /sys/kernel/debug/vgaswitcheroo/switch
if [ "$DESKTOP_SESSION" = "openbox" ]
then
killall -u "$USER"
elif [ "$DESKTOP_SESSION" = "gnome" ]
then
gnome-session-save --logout
elif [ "$DESKTOP_SESSION" = "xubuntu" ]
then
xfce4-session-logout -l
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 | sudo tee /sys/kernel/debug/vgaswitcheroo/switch
echo DIGD | sudo tee /sys/kernel/debug/vgaswitcheroo/switch
if [ "$DESKTOP_SESSION" = "openbox" ]
then
killall -u "$USER"
elif [ "$DESKTOP_SESSION" = "gnome" ]
then
gnome-session-save --logout
elif [ "$DESKTOP_SESSION" = "xubuntu" ]
then
xfce4-session-logout -l
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 | sudo tee /sys/kernel/debug/vgaswitcheroo/switch
if [ "$DESKTOP_SESSION" = "openbox" ]
then
killall -u "$USER"
elif [ "$DESKTOP_SESSION" = "gnome" ]
then
gnome-session-save --logout
elif [ "$DESKTOP_SESSION" = "xubuntu" ]
then
xfce4-session-logout -l
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 | sudo tee /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 | sudo tee /sys/kernel/debug/vgaswitcheroo/switch
echo DDIS | sudo tee /sys/kernel/debug/vgaswitcheroo/switch
if [ "$DESKTOP_SESSION" = "openbox" ]
then
killall -u "$USER"
elif [ "$DESKTOP_SESSION" = "gnome" ]
then
gnome-session-save --logout
elif [ "$DESKTOP_SESSION" = "xubuntu" ]
then
xfce4-session-logout -l
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

make it executable, store it in /usr/sbin as root

if you use another desktop environment, you might need to change the logout-commands and the if-statements accordingly

add to /etc/sudoers file (edit with sudoedit or visudo!):

# alias for all switcheroo-related commands
Cmnd_Alias VGASWITCH = /bin/cat /sys/kernel/debug/vgaswitcheroo/switch, /usr/bin/tee /sys/kernel/debug/vgaswitcheroo/switch
# execute VGASWITCH-commands as root without password
%switcheroo ALL=(root)NOPASSWD: VGASWITCH
# Alias fuer normale Benutzer
# %user	ALL = NOPASSWD: VGASWITCH

create group ‘switcheroo’ (or name as you like or use some existing group) and add every user who should be able to switch graphics cards

ATTENTION! Switching graphics-cards with this script logs you out and kills all running programs immediately without saving or asking!!! If you want to have the logout-dialog to appear for confirmation, remove the -l option from theĀ xfce4-session-logout-commands. (only xubuntu)

After reboot, both graphics-cards are turned on, which might lead to overheating and needs a lot of power. To prevent this, add this line to /etc/rc.local before exit 0:

/sys/kernel/debug/vgaswitcheroo/switch

fire script from panel-starter or desktop-starter

tested only on T500 running xubuntu 13.04

one remaining problem is, that the xfce4-session-logout -l kills all running programs which leads to a warning on next login

thanks to and see also:

http://asusm51ta-with-linux.blogspot.de/2010/05/07052010-fedora-13-beta-kernel-2.html

https://help.ubuntu.com/community/HybridGraphics

http://www.thinkwiki.org/wiki/Switchable_Graphics