nvidia.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/env bash
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. nvidia_run_file=""
  18. ## @description get nvidia version
  19. ## @audience public
  20. ## @stability stable
  21. function get_nvidia_version()
  22. {
  23. chmod +x ${DOWNLOAD_DIR}/nvidia/nvidia-detect
  24. local nvidia_detect_info=`${DOWNLOAD_DIR}/nvidia/nvidia-detect -v`
  25. echo $nvidia_detect_info | sed "s/^.*This device requires the current \([0-9.]*\).*/\1/"
  26. }
  27. ## @description download nvidia driver
  28. ## @audience public
  29. ## @stability stable
  30. function download_nvidia_driver()
  31. {
  32. # submarin http server
  33. if [[ -n "$DOWNLOAD_HTTP" ]]; then
  34. MY_NVIDIA_DETECT_URL="${DOWNLOAD_HTTP}/downloads/nvidia/nvidia-detect"
  35. else
  36. MY_NVIDIA_DETECT_URL=${NVIDIA_DETECT_URL}
  37. fi
  38. # download nvidia_detect
  39. if [[ -f "${DOWNLOAD_DIR}/nvidia/nvidia-detect" ]]; then
  40. echo "${DOWNLOAD_DIR}/nvidia/nvidia-detect is exist."
  41. else
  42. echo "download ${MY_NVIDIA_DETECT_URL} ..."
  43. wget -P ${DOWNLOAD_DIR}/nvidia/ ${MY_NVIDIA_DETECT_URL}
  44. fi
  45. echo "execution nvidia-detect to check the graphics card ..."
  46. local nvidiaVersion=`get_nvidia_version`
  47. echo -e "detect nvidia version is \033[31m${nvidiaVersion}\033[0m"
  48. # download NVIDIA driver
  49. if [[ "$nvidiaVersion" = "" ]]; then
  50. echo -e "\033[31mERROR: No graphics card device detected.\033[0m"
  51. return 1
  52. else
  53. nvidia_run_file="NVIDIA-Linux-x86_64-${nvidiaVersion}.run"
  54. # submarin http server
  55. if [[ -n "$DOWNLOAD_HTTP" ]]; then
  56. MY_NVIDIA_DRIVER_RUN_URL="${DOWNLOAD_HTTP}/downloads/nvidia/${nvidia_run_file}"
  57. else
  58. # http://us.download.nvidia.com/XFree86/Linux-x86_64/390.87/NVIDIA-Linux-x86_64-390.87.run
  59. MY_NVIDIA_DRIVER_RUN_URL="http://us.download.nvidia.com/XFree86/Linux-x86_64/${nvidiaVersion}/${nvidia_run_file}"
  60. fi
  61. if [[ -f ${DOWNLOAD_DIR}/nvidia/${nvidia_run_file} ]]; then
  62. echo "NVIDIA driver files already exist in the ${DOWNLOAD_DIR}/nvidia/${nvidia_run_file} directory."
  63. echo "===== Please make sure the ${DOWNLOAD_DIR}/nvidia/nvidia/${nvidia_run_file} file is complete and can be used normally. ====="
  64. else
  65. echo "Download the NVIDIA driver from the ${MY_NVIDIA_DRIVER_RUN_URL}"
  66. wget -P ${DOWNLOAD_DIR}/nvidia/ ${MY_NVIDIA_DRIVER_RUN_URL}
  67. fi
  68. fi
  69. }
  70. ## @description install nvidia
  71. ## @audience public
  72. ## @stability stable
  73. function install_nvidia()
  74. {
  75. download_nvidia_driver
  76. # Confirm that the system disables nouveau
  77. local disable_nouveau_info=`lsmod | grep nouveau`
  78. if [[ "$disable_nouveau_info" = "" ]]; then
  79. echo "===== Start installing the NVIDIA driver ====="
  80. echo -e "Some options during the installation
  81. Would you like to register the kernel module sources with DKMS?
  82. This will allow DKMS to automatically build a new module, if you install a different kernel later. \033[33m[Yes]\033[0m
  83. Install NVIDIA's 32-bit compatibility libraries \033[33m[Yes]\033[0m
  84. centos Install NVIDIA's 32-bit compatibility libraries \033[33m[Yes]\033[0m
  85. Would you like to run the nvidia-xconfig utility to automatically update your X configuration file... \033[33m[No]\033[0m"
  86. sleep 2
  87. sh ${DOWNLOAD_DIR}/nvidia/${nvidia_run_file}
  88. else
  89. echo -e "ERROR: Nouveau is not disabled"
  90. return 1
  91. fi
  92. echo -e "\033[32m===== execute nvidia-smi. You should be able to see the list of graphics cards =====\033[0m"
  93. sleep 1
  94. nvidia-smi
  95. }
  96. ## @description uninstall nvidia
  97. ## @audience public
  98. ## @stability stable
  99. function uninstall_nvidia()
  100. {
  101. if [ ! -f "/usr/bin/nvidia-uninstall" ]; then
  102. echo -e "\033[31mERROR: /usr/bin/nvidia-uninstall file is not exist!\033[0m"
  103. return 1
  104. fi
  105. echo -e "execute /usr/bin/nvidia-uninstall"
  106. /usr/bin/nvidia-uninstall
  107. }