benjarobin

switch-mesa-nvidia

Feb 20th, 2016
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.72 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # The MIT License (MIT)
  4. #
  5. # Copyright (c) 2015 Benjamin ROBIN
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining a copy
  8. # of this software and associated documentation files (the "Software"), to deal
  9. # in the Software without restriction, including without limitation the rights
  10. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. # copies of the Software, and to permit persons to whom the Software is
  12. # furnished to do so, subject to the following conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be included in all
  15. # copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. # SOFTWARE.
  24.  
  25. set -e
  26.  
  27. # $1 : The system lib directory
  28. # $2 : The vendor lib directory
  29. # $3 : The basename of the file
  30. #
  31. # Print first the target and then the links
  32. function find_links_lib {
  33.     local dir_sys="$1"
  34.     local dir_vendor="$2"
  35.     local lib_name="$3"
  36.     local src_file=""
  37.     local src_files=($(find "${dir_vendor}" -name "${lib_name}*" \( -type f -o -type l \)))
  38.     local dst_files=($(readlink -q -e "${src_files[@]}" | sort -u))
  39.  
  40.     if [ ${#dst_files[@]} -ne 1 ] ; then
  41.         echo "Unable to find unique canonical file name for: ${dir_vendor}/${lib_name}*" 1>&2
  42.         exit 2
  43.     fi
  44.  
  45.     echo "${dst_files[0]}"
  46.  
  47.     for src_file in "${src_files[@]}" ; do
  48.         echo "${dir_sys}/${src_file##*/}"
  49.     done
  50. }
  51.  
  52. function find_links_xorg_nvidia {
  53.     find_links_lib "/usr/lib/xorg/modules/extensions" "/usr/lib/nvidia/xorg/modules/extensions" "libglx.so"
  54.     echo "/usr/lib/xorg/modules/extensions/libglx.so.1"
  55. }
  56.  
  57. function find_links_xorg_mesa {
  58.     echo "/usr/lib/xorg/modules/extensions/libglx.xorg"
  59.     echo "/usr/lib/xorg/modules/extensions/libglx.so"
  60. }
  61.  
  62. # Check links computed by find_links*
  63. function check_links {
  64.  
  65.     local target="${1}"
  66.     shift
  67.     local link_names=("${@}")
  68.     local sys_file=""
  69.  
  70.     for sys_file in "${link_names[@]}" ; do
  71.         if [ "$(readlink -q -e "$sys_file")" != "$target" ] ; then
  72.             return 1
  73.         fi
  74.     done
  75. }
  76.  
  77. # Create links computed by find_links*
  78. function create_links {
  79.  
  80.     local target="${1}"
  81.     shift
  82.     local link_names=("${@}")
  83.     local sys_file=""
  84.  
  85.     for sys_file in "${link_names[@]}" ; do
  86.         rm -f "$sys_file"
  87.         ln -s "$target" "$sys_file"
  88.     done
  89. }
  90.  
  91. # $1 : The system lib directory
  92. function cleanup_links_lib {
  93.     local dir_sys="$1"
  94.     rm -f "$dir_sys/libEGL.so"*
  95.     rm -f "$dir_sys/libGLESv1_CM.so"*
  96.     rm -f "$dir_sys/libGLESv2.so"*
  97.     rm -f "$dir_sys/libGL.so"*
  98. }
  99.  
  100. function cleanup_links_xorg {
  101.     rm -f /usr/lib/xorg/modules/extensions/libglx.so*
  102. }
  103.  
  104. # $1 : The system directory name : lib ou lib32
  105. # $2 : The vendor directory name : mesa or nvidia
  106. function check_and_create_links_lib {
  107.     local dir_sys="/usr/$1"
  108.     local vendor="$2"
  109.     local dir_vendor="$dir_sys/$vendor"
  110.  
  111.     local path_EGL=($(find_links_lib "$dir_sys" "$dir_vendor" libEGL.so))
  112.     local path_GLESv1=($(find_links_lib "$dir_sys" "$dir_vendor" libGLESv1_CM.so))
  113.     local path_GLESv2=($(find_links_lib "$dir_sys" "$dir_vendor" libGLESv2.so))
  114.     local path_GL=($(find_links_lib "$dir_sys" "$dir_vendor" libGL.so))
  115.  
  116.     if  check_links "${path_EGL[@]}"    && \
  117.         check_links "${path_GLESv1[@]}" && \
  118.         check_links "${path_GLESv2[@]}" && \
  119.         check_links "${path_GL[@]}"  ; then
  120.         echo "System $dir_sys is already using $vendor files"
  121.     else
  122.         cleanup_links_lib "$dir_sys"
  123.         create_links "${path_EGL[@]}"
  124.         create_links "${path_GLESv1[@]}"
  125.         create_links "${path_GLESv2[@]}"
  126.         create_links "${path_GL[@]}"
  127.     fi
  128. }
  129.  
  130. # $1 : The vendor directory name : mesa or nvidia
  131. function check_and_create_links_xorg {
  132.     local vendor="$1"
  133.     local path_Xorg=($("find_links_xorg_$vendor"))
  134.  
  135.     if check_links "${path_Xorg[@]}" ; then
  136.         echo "Xorg is already using $vendor files"
  137.     else
  138.         cleanup_links_xorg
  139.         create_links "${path_Xorg[@]}"
  140.     fi
  141. }
  142.  
  143. # $1 : The vendor directory name : mesa or nvidia
  144. function check_and_create_links {
  145.     local vendor="$1"
  146.     check_and_create_links_lib lib "$vendor"
  147.     check_and_create_links_xorg "$vendor"
  148.     [ -f "/usr/lib32/$vendor/libGL.so" ] && \
  149.         check_and_create_links_lib lib32 "$vendor"
  150. }
  151.  
  152. function detect_card {
  153.     lspci | grep -Eq "VGA.*NVIDIA" && echo nvidia || echo mesa
  154. }
  155.  
  156. function usage() {
  157. cat << EOF
  158. usage: $0 [options]
  159.  
  160. This utility reconfigures the system to use mesa or nvidia lib
  161.  
  162. OPTIONS:
  163.    -h, --help      Show this message
  164.    -c, cleanup     Remove any symbolique links inside system lib
  165.    -m, mesa        Configure to use mesa lib
  166.    -n, nvidia      Configure to use nvidia lib
  167.    -a, auto        Try to guess which card is enabled
  168. EOF
  169. }
  170.  
  171. ###################################
  172. ### Argument parsing            ###
  173. ###################################
  174.  
  175. case "$1" in
  176.     -c|--cleanup|cleanup)
  177.         cleanup_links_xorg
  178.         cleanup_links_lib /usr/lib
  179.         cleanup_links_lib /usr/lib32
  180.     ;;
  181.     -m|--mesa|mesa)
  182.         check_and_create_links mesa
  183.     ;;
  184.     -n|--nvidia|nvidia)
  185.         check_and_create_links nvidia
  186.     ;;
  187.     -a|--auto|auto)
  188.         check_and_create_links "$(detect_card)"
  189.     ;;
  190.     *)
  191.         usage
  192.     ;;
  193. esac
Advertisement
Add Comment
Please, Sign In to add comment