Automate installation of DXVK, D9VK + Wine/Wine Staging & update GPU drivers + PlayonLinux wineprefixes (Debian/Ubuntu/Mint/Arch Linux/Manjaro)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

582 lines
15 KiB

  1. #!/bin/env bash
  2. # Compile latest Nvidia drivers on a Debian-based Linux
  3. # Copyright (C) 2019 Pekka Helenius
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. ##############################################################################
  18. # Check if we're using bash or sh to run the script. If bash, OK.
  19. # If another one, ask user to run the script with bash.
  20. BASH_CHECK=$(ps | grep `echo $$` | awk '{ print $4 }')
  21. if [ $BASH_CHECK != "bash" ]; then
  22. echo "
  23. Please run this script using bash (/usr/bin/bash).
  24. "
  25. exit 1
  26. fi
  27. ########################################################
  28. # Just a title & author for this script, used in initialization
  29. SCRIPT_TITLE="\e[1mNvidia drivers package builder & installer\e[0m"
  30. SCRIPT_AUTHOR="Pekka Helenius (~Fincer), 2019"
  31. ########################################################
  32. BUILD_MAINDIR=${PWD}/debian_nvidia
  33. ########################################################
  34. _pkgname="nvidia"
  35. arch="x86_64"
  36. pkgver=430.14
  37. files=(
  38. "http://archive.ubuntu.com/ubuntu/pool/restricted/n/nvidia-graphics-drivers-418/nvidia-graphics-drivers-418_418.56-0ubuntu1.debian.tar.xz"
  39. "http://us.download.nvidia.com/XFree86/Linux-${arch}/${pkgver}/NVIDIA-Linux-${arch}-${pkgver}.run"
  40. )
  41. ###################
  42. pkgver_major=$(printf '%s' ${pkgver} | grep -oE "^[0-9]+[^.]")
  43. pkgdir="nvidia-graphics-drivers-${pkgver_major}_${pkgver}"
  44. typeset -A library_fixes
  45. ###################
  46. # From time to time, bundled library version numbers change
  47. # in Nvidia driver packages. Update library version if needed
  48. #
  49. # Left side: old library version
  50. # Right side: new library version
  51. #
  52. library_fixes=(
  53. [libnvidia-egl-wayland.so.1.0.2]='libnvidia-egl-wayland.so.1.0.3'
  54. )
  55. ###################
  56. # These are defined build dependencies in debian/control file
  57. nvidia_builddeps=(
  58. 'dpkg-dev'
  59. 'xz-utils'
  60. 'dkms'
  61. 'libwayland-client0'
  62. 'libwayland-server0'
  63. 'libxext6'
  64. 'quilt'
  65. 'po-debconf'
  66. 'execstack'
  67. 'dh-modaliases'
  68. 'xserver-xorg-dev'
  69. 'libglvnd-dev'
  70. )
  71. ###################
  72. # These packages are required by the compiled Nvidia packages
  73. nvidia_required_packages=(
  74. # Required by libnvidia-gl
  75. 'libwayland-client0'
  76. 'libwayland-server0'
  77. # Required by libnvidia, libnvidia-decode & libnvidia-fbc1
  78. 'libx11-6'
  79. # Required by libnvidia, libnvidia-decode, libnvidia-fbc1 & libnvidia-ifr1
  80. 'libxext6'
  81. # Required by libnvidia-fbc1 & libnvidia-ifr1
  82. 'libgl1'
  83. # Required by xserver-xorg-video-nvidia
  84. 'xserver-xorg-core'
  85. 'xorg-video-abi-23'
  86. # Required by nvidia-compute-utils
  87. 'adduser'
  88. )
  89. ###################
  90. # Nvidia packages. THIS ORDER IS MANDATORY, DO NOT CHANGE!
  91. nvidia_install_packages=(
  92. # Similar than 'nvidia-dkms' package on Arch Linux
  93. "nvidia-kernel-source-${pkgver_major}"
  94. # Nvidia DKMS
  95. "nvidia-kernel-common-${pkgver_major}"
  96. "nvidia-dkms-${pkgver_major}"
  97. # Similar than 'nvidia-utils' package on Arch Linux
  98. "libnvidia-common-${pkgver_major}"
  99. "libnvidia-gl-${pkgver_major}"
  100. "libnvidia-cfg1-${pkgver_major}"
  101. "xserver-xorg-video-nvidia-${pkgver_major}"
  102. "libnvidia-compute-${pkgver_major}"
  103. "libnvidia-decode-${pkgver_major}"
  104. "libnvidia-encode-${pkgver_major}"
  105. "libnvidia-fbc1-${pkgver_major}"
  106. "libnvidia-ifr1-${pkgver_major}"
  107. "nvidia-compute-utils-${pkgver_major}"
  108. "nvidia-utils-${pkgver_major}"
  109. )
  110. ########################################################
  111. pkgver_sed=$(printf '%s' ${pkgver} | sed 's/\./\\\./')
  112. i=0
  113. for f in ${files[@]}; do
  114. file_basename=$(printf '%s' ${f} | awk -F / '{print $NF}')
  115. filebases[$i]=${file_basename}
  116. let i++
  117. done
  118. oldver=$(printf '%s' ${filebases[0]} | grep -oE "[0-9]{3}\.[0-9]{2}" | head -1)
  119. oldver_major=$(printf '%s' ${filebases[0]} | grep -oE "[0-9]{3}" | head -1)
  120. oldver_sed=$(printf '%s' ${oldver} | sed 's/\./\\\./')
  121. ########################################################
  122. # http://wiki.bash-hackers.org/snipplets/print_horizontal_line#a_line_across_the_entire_width_of_the_terminal
  123. function INFO_SEP() { printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' - ; }
  124. ###########################################################
  125. echo -e "\n${SCRIPT_TITLE}\n"
  126. ########################################################
  127. echo -e "Selected driver version:\t${pkgver}\n"
  128. INFO_SEP
  129. ########################################################
  130. mkdir -p ${BUILD_MAINDIR}
  131. if [[ $? -eq 0 ]]; then
  132. cd ${BUILD_MAINDIR}
  133. WORKDIR=${PWD}
  134. else
  135. echo -e "Error: couldn't create Nvidia build directory. Aborting\n"
  136. exit 1
  137. fi
  138. ########################################################
  139. # If the script is interrupted (Ctrl+C/SIGINT), do the following
  140. function Nvidia_intCleanup() {
  141. rm -rf ${WORKDIR}/{${pkgdir}
  142. }
  143. # Allow interruption of the script at any time (Ctrl + C)
  144. trap "Nvidia_intCleanup && exit 0" INT
  145. # Error event
  146. #trap "Nvidia_intCleanup && exit 1" ERR
  147. # Remove old build files
  148. Nvidia_intCleanup
  149. ###########################################################
  150. COMMANDS=(
  151. apt
  152. dpkg
  153. grep
  154. sudo
  155. wc
  156. wget
  157. )
  158. function checkCommands() {
  159. if [[ $(which --help 2>/dev/null) ]] && [[ $(echo --help 2>/dev/null) ]]; then
  160. local a=0
  161. for command in ${@}; do
  162. if [[ ! $(which $command 2>/dev/null) ]]; then
  163. local COMMANDS_NOTFOUND[$a]=${command}
  164. let a++
  165. fi
  166. done
  167. if [[ -n $COMMANDS_NOTFOUND ]]; then
  168. echo -e "\nError! The following commands could not be found: ${COMMANDS_NOTFOUND[*]}\nAborting\n"
  169. exit 1
  170. fi
  171. else
  172. exit 1
  173. fi
  174. }
  175. checkCommands "${COMMANDS[*]}"
  176. ########################################################
  177. # General function for question responses
  178. function questionresponse() {
  179. local response=${1}
  180. read -r -p "" response
  181. if [[ $(echo $response | sed 's/ //g') =~ ^([yY][eE][sS]|[yY])$ ]]; then
  182. echo ""
  183. return 0
  184. else
  185. return 1
  186. fi
  187. }
  188. ########################################################
  189. # Premilinary check for already compiled packages
  190. if [[ $(ls ${WORKDIR}/*.deb 2>/dev/null | wc -l) -ne 0 ]]; then
  191. echo -e "\nWarning: previously compiled deb archives found on the main build directory '${WORKDIR}'.\nDelete them and continue? [Y/n]"
  192. questionresponse
  193. fi
  194. if [[ $? -ne 0 ]]; then
  195. echo -e "Cancelling.\n"
  196. exit 1
  197. else
  198. rm -rfv ${WORKDIR}/*.{deb,buildinfo,changes} 2>/dev/null
  199. fi
  200. ########################################################
  201. # Premilinary check to see whether Nvidia card is present
  202. if [[ ! $(lspci | grep -oiE "vga.*nvidia") ]]; then
  203. echo -e "\nWarning: Nvidia card could not be detected on your system. Continue anyway? [Y/n]"
  204. questionresponse
  205. fi
  206. if [[ $? -ne 0 ]]; then
  207. echo -e "Cancelling.\n"
  208. exit 1
  209. fi
  210. ########################################################
  211. # Auto-install question
  212. echo -e "\nAuto-install Nvidia drivers after compilation? [Y/n]"
  213. questionresponse
  214. if [[ $? -eq 0 ]]; then
  215. if [[ ${UID} -ne 0 ]]; then
  216. if [[ $(echo $(sudo -vn &>/dev/null)$?) -ne 0 ]]; then
  217. echo -e "NVIDIA driver installation requires root permissions. Please provide your sudo password now.\n"
  218. sudo -v
  219. fi
  220. if [[ $? -eq 0 ]]; then
  221. AUTOINSTALL=
  222. else
  223. exit 1
  224. fi
  225. else
  226. AUTOINSTALL=
  227. fi
  228. fi
  229. ########################################################
  230. function pkg_installcheck() {
  231. RETURNVALUE=$(echo $(dpkg -s "${1}" &>/dev/null)$?)
  232. return $RETURNVALUE
  233. }
  234. ########################################################
  235. # Check and install package related dependencies if they are missing
  236. function pkgdependencies() {
  237. # Generate a list of missing dependencies
  238. local a=0
  239. for p in ${@}; do
  240. if [[ $(pkg_installcheck ${p}) -ne 0 ]]; then
  241. validlist[$a]=${p}
  242. let a++
  243. fi
  244. done
  245. if [[ -n ${validlist[*]} ]]; then
  246. echo -e "The following build time dependencies are missing. In order to continue, you must install them:\n$(for i in ${validlist[@]}; do echo ${i}; done)\n"
  247. if [[ ${UID} -ne 0 ]] && [[ $(echo $(sudo -vn &>/dev/null)$?) -ne 0 ]]; then
  248. echo -e "For that, sudo password is required. Please provide it now.\n"
  249. sudo -v
  250. if [[ $? -ne 0 ]]; then
  251. echo -e "Error: couldn't continue due to lacking permissions. Aborting\n"
  252. exit 1
  253. fi
  254. fi
  255. fi
  256. # Install missing dependencies, be informative
  257. local b=0
  258. for pkgdep in ${validlist[@]}; do
  259. echo -e "$(( $b + 1 ))/$(( ${#validlist[*]} )) - Installing ${_pkgname} dependency ${pkgdep}"
  260. sudo apt install -y ${pkgdep} &> /dev/null
  261. if [[ $? -eq 0 ]]; then
  262. let b++
  263. else
  264. echo -e "\nError occured while installing ${pkgdep}. Aborting.\n"
  265. exit 1
  266. fi
  267. done
  268. }
  269. ########################################################
  270. function download_files() {
  271. mkdir -p ${WORKDIR}/${pkgdir}
  272. i=0
  273. for f in ${files[@]}; do
  274. if [[ ! -f ${WORKDIR}/${pkgdir}/${filebases[$i]} ]]; then
  275. echo ${f}
  276. echo -e "\nDownloading ${filebases[$i]}"
  277. wget ${f} -o /dev/null --show-progress -O "${WORKDIR}/${pkgdir}/${filebases[$i]}"
  278. if [[ $? -ne 0 ]];then
  279. echo -e "Error: couldn't retrieve file ${filebases[$i]}. Aborting\n"
  280. exit 1
  281. fi
  282. fi
  283. let i++
  284. done
  285. }
  286. ########################################################
  287. function prepare_deb_sources() {
  288. # Extract debian control files
  289. cd ${WORKDIR}/${pkgdir}/
  290. tar xf ${filebases[0]}
  291. if [[ $? -eq 0 ]]; then
  292. function fix_library_versions() {
  293. for oldlib in ${!library_fixes[@]}; do
  294. # sed-friendly name
  295. local oldlib_sed=$(printf '%s' ${oldlib} | sed 's/\./\\\./g')
  296. for lib in ${library_fixes[$oldlib]}; do
  297. # sed-friendly name
  298. local lib_sed=$(printf '%s' ${lib} | sed 's/\./\\\./g')
  299. # Files which have old library files mentioned
  300. local i=0
  301. for oldlib_file in $(grep -rl "${oldlib}" debian/ | tr '\n' ' '); do
  302. local oldlib_files[$i]=${oldlib_file}
  303. let i++
  304. done
  305. for targetfile in ${oldlib_files[@]}; do
  306. sed -i "s/${oldlib_sed}/${lib_sed}/g" ${targetfile}
  307. done
  308. done
  309. done
  310. }
  311. function rename_deb_files() {
  312. # Remove this suffix
  313. sed -i 's/\-no\-compat32//' debian/rules.defs
  314. # Tell that Nvidia .run file is at our build root, not in amd64 subfolder
  315. sed -i 's|sh \$\*\/\${NVIDIA_FILENAME_\$\*}|sh \${NVIDIA_FILENAME_\$\*}|' debian/rules
  316. ############
  317. # TODO Individual fix for strange version number present in debian control files
  318. # Remove when not needed!
  319. sed -i "s/384/${pkgver_major}/g" debian/control
  320. sed -i "s/384/${pkgver_major}/g" debian/templates/control.in
  321. ############
  322. local IFS=$'\n'
  323. for n in $(ls debian/ -w 1); do
  324. # IMPORTANT! KEEP THIS IF STATEMENT ORDER BELOW!!
  325. # Do this for every file in debian subfolder regardless of their name
  326. if [[ -f debian/${n} ]]; then
  327. # Keep this order. It is important!
  328. sed -i "s/${oldver_sed}/${pkgver_sed}/g" debian/${n}
  329. sed -i "s/${oldver_major}/${pkgver_major}/g" debian/${n}
  330. fi
  331. if [[ $(printf '%s' ${n} | grep ${oldver_major}) ]]; then
  332. local n_new=$(printf '%s' ${n} | sed "s/${oldver_major}/${pkgver_major}/")
  333. mv debian/${n} debian/${n_new}
  334. if [[ $? -ne 0 ]]; then
  335. echo -e "Error: couldn't rename file debian/${n}. Aborting\n"
  336. exit 1
  337. fi
  338. fi
  339. done
  340. unset IFS
  341. }
  342. fix_library_versions
  343. rename_deb_files
  344. else
  345. echo -e "Error: couldn't extract Nvidia Debian archive. Aborting\n"
  346. exit 1
  347. fi
  348. }
  349. ########################################################
  350. function compile_nvidia() {
  351. cd ${WORKDIR}/${pkgdir}/
  352. DEB_BUILD_OPTIONS="strip noddebs" dpkg-buildpackage -rfakeroot -b -us -uc
  353. if [[ $? -eq 0 ]]; then
  354. mkdir -p ${WORKDIR}/compiled_deb
  355. for p in ${nvidia_install_packages[*]}; do
  356. mv ${WORKDIR}/${p}*.deb ${WORKDIR}/compiled_deb/
  357. done
  358. if [[ $? -eq 0 ]]; then
  359. echo -e "Compiled deb packages moved into '${WORKDIR}/compiled_deb/'. Install these to enable Nvidia support.\nAdditionally, you may need Vulkan loader package 'libvulkan1', too.\n"
  360. else
  361. echo -e "Error: couldn't move deb packages into '${WORKDIR}/compiled_deb/'. Aborting\n"
  362. exit 1
  363. fi
  364. mkdir -p ${WORKDIR}/compiled_other
  365. for a in ${WORKDIR}/*; do
  366. if [[ -f ${a} ]]; then
  367. mv ${a} ${WORKDIR}/compiled_other/
  368. fi
  369. done
  370. if [[ $? -eq 0 ]]; then
  371. echo -e "Other files moved into '${WORKDIR}/compiled_other/' \n"
  372. else
  373. echo -e "Error: couldn't move other files into '${WORKDIR}/compiled_other/'. Aborting\n"
  374. exit 1
  375. fi
  376. else
  377. echo -e "Error: couldn't compile Nvidia package bundle. Aborting\n"
  378. exit 1
  379. fi
  380. }
  381. ########################################################
  382. function install_nvidia() {
  383. for syspkg in ${nvidia_required_packages[@]}; do
  384. if [[ $(echo $(dpkg -s ${syspkg} &>/dev/null)$?) -ne 0 ]]; then
  385. echo -e "Installing missing dependency ${syspkg}\n"
  386. sudo apt install -y ${syspkg}
  387. if [[ $? -ne 0 ]]; then
  388. echo -e "Error: couldn't install dependency ${syspkg}. Aborting\n"
  389. exit 1
  390. fi
  391. fi
  392. done
  393. cd ${WORKDIR}/compiled_deb
  394. for pkg in ${nvidia_install_packages[@]}; do
  395. local oldpkg=$(printf '%s' ${pkg} | sed 's/\-[0-9]*$//')
  396. local oldpkg_check=$(dpkg --get-selections | grep ${oldpkg} | awk '{print $1}')
  397. if [[ $(echo ${oldpkg_check} | wc -w) -eq 1 ]]; then
  398. if [[ ! ${oldpkg_check} =~ ^*${pkgver_major}$ ]]; then
  399. echo -e "Removing old ${oldpkg}\n"
  400. sudo apt purge --remove -y "${oldpkg}*"
  401. if [[ $? -ne 0 ]]; then
  402. echo -e "Error: couldn't uninstall ${oldpkg}. Aborting\n"
  403. exit 1
  404. fi
  405. fi
  406. fi
  407. echo -e "Installing ${pkg}\n"
  408. sudo dpkg -i ${pkg}*.deb
  409. if [[ $? -ne 0 ]]; then
  410. echo -e "Warning: couldn't install ${pkg}\n"
  411. fi
  412. done
  413. }
  414. function install_vulkan() {
  415. # Vulkan loader
  416. if [[ $? -eq 0 ]]; then
  417. local syspkg=libvulkan1
  418. if [[ $(echo $(dpkg -s ${syspkg} &>/dev/null)$?) -ne 0 ]]; then
  419. sudo apt update && sudo apt install -y ${syspkg}
  420. fi
  421. fi
  422. }
  423. ########################################################
  424. pkgdependencies ${nvidia_builddeps[*]} && \
  425. download_files && \
  426. prepare_deb_sources && \
  427. compile_nvidia
  428. if [[ $? -eq 0 ]] && [[ -v AUTOINSTALL ]]; then
  429. install_nvidia && \
  430. install_vulkan
  431. fi
  432. # If any buildtime deps were installed, inform the user
  433. if [[ -n ${validlist[*]} ]]; then
  434. echo -e "The following buildtime dependencies were installed, and they may not be required anymore:\n\n\
  435. $(for h in ${validlist[*]}; do echo ${h}; done)\n"
  436. fi