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.

578 lines
15 KiB

  1. #!/bin/env bash
  2. # Compile latest Nvidia drivers on a Debian-based Linux
  3. # Copyright (C) 2018 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), 2018"
  31. ########################################################
  32. BUILD_MAINDIR=${PWD}/debian_nvidia
  33. ########################################################
  34. _pkgname="nvidia"
  35. arch="x86_64"
  36. pkgver=396.54
  37. files=(
  38. "http://archive.ubuntu.com/ubuntu/pool/restricted/n/nvidia-graphics-drivers-390/nvidia-graphics-drivers-390_390.48-0ubuntu3.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}/{debian,NVIDIA-Linux,NVIDIA-Linux-amd64,NVIDIA-Linux-x86_64-${pkgver},LICENSE.txt,unpack-stamp},compiled_deb,compiled_other}
  142. exit 0
  143. }
  144. # Allow interruption of the script at any time (Ctrl + C)
  145. trap "Nvidia_intCleanup" INT
  146. ###########################################################
  147. # Remove old build files
  148. rm -rf ${WORKDIR}/{${pkgdir}/{debian,NVIDIA-Linux,NVIDIA-Linux-amd64,NVIDIA-Linux-x86_64-${pkgver},LICENSE.txt,unpack-stamp},compiled_deb,compiled_other}
  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 -rv ${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 "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. # Check and install package related dependencies if they are missing
  231. function pkgdependencies() {
  232. # Generate a list of missing dependencies
  233. local a=0
  234. for p in ${@}; do
  235. if [[ $(echo $(dpkg -s ${p} &>/dev/null)$?) -ne 0 ]]; then
  236. validlist[$a]=${p}
  237. let a++
  238. fi
  239. done
  240. if [[ -n ${list[*]} ]]; then
  241. echo -e "Some build time dependencies are missing. In order to continue, you must install them."
  242. if [[ ${UID} -ne 0 ]] && [[ $(echo $(sudo -vn &>/dev/null)$?) -ne 0 ]]; then
  243. echo -e "For that, sudo password is required. Please provide it now.\n"
  244. sudo -v
  245. if [[ $? -ne 0 ]]; then
  246. echo -e "Error: couldn't continue due to lacking permissions. Aborting\n"
  247. exit 1
  248. fi
  249. fi
  250. fi
  251. # Install missing dependencies, be informative
  252. local b=0
  253. for pkgdep in ${validlist[@]}; do
  254. echo -e "$(( $b + 1 ))/$(( ${#validlist[*]} )) - Installing ${_pkgname} dependency ${pkgdep}"
  255. sudo apt install -y ${pkgdep} &> /dev/null
  256. if [[ $? -eq 0 ]]; then
  257. let b++
  258. else
  259. echo -e "\nError occured while installing ${pkgdep}. Aborting.\n"
  260. exit 1
  261. fi
  262. done
  263. if [[ -n ${validlist[*]} ]]; then
  264. # Add empty newline
  265. echo ""
  266. fi
  267. }
  268. ########################################################
  269. function download_files() {
  270. mkdir -p ${WORKDIR}/${pkgdir}
  271. i=0
  272. for f in ${files[@]}; do
  273. if [[ ! -f ${WORKDIR}/${pkgdir}/${filebases[$i]} ]]; then
  274. echo -e "\nDownloading ${filebases[$i]}"
  275. wget ${f} -o /dev/null --show-progress -O "${WORKDIR}/${pkgdir}/${filebases[$i]}"
  276. if [[ $? -ne 0 ]];then
  277. echo -e "Error: couldn't retrieve file ${filebases[$i]}. Aborting\n"
  278. exit 1
  279. fi
  280. fi
  281. let i++
  282. done
  283. }
  284. ########################################################
  285. function prepare_deb_sources() {
  286. # Extract debian control files
  287. cd ${WORKDIR}/${pkgdir}/
  288. tar xf ${filebases[0]}
  289. if [[ $? -eq 0 ]]; then
  290. function fix_library_versions() {
  291. for oldlib in ${!library_fixes[@]}; do
  292. # sed-friendly name
  293. local oldlib_sed=$(printf '%s' ${oldlib} | sed 's/\./\\\./g')
  294. for lib in ${library_fixes[$oldlib]}; do
  295. # sed-friendly name
  296. local lib_sed=$(printf '%s' ${lib} | sed 's/\./\\\./g')
  297. # Files which have old library files mentioned
  298. local i=0
  299. for oldlib_file in $(grep -rl "${oldlib}" debian/ | tr '\n' ' '); do
  300. local oldlib_files[$i]=${oldlib_file}
  301. let i++
  302. done
  303. for targetfile in ${oldlib_files[@]}; do
  304. sed -i "s/${oldlib_sed}/${lib_sed}/g" ${targetfile}
  305. done
  306. done
  307. done
  308. }
  309. function rename_deb_files() {
  310. # Remove this suffix
  311. sed -i 's/\-no\-compat32//' debian/rules.defs
  312. # Tell that Nvidia .run file is at our build root, not in amd64 subfolder
  313. sed -i 's|sh \$\*\/\${NVIDIA_FILENAME_\$\*}|sh \${NVIDIA_FILENAME_\$\*}|' debian/rules
  314. ############
  315. # TODO Individual fix for strange version number present in debian control files
  316. # Remove when not needed!
  317. sed -i "s/384/${pkgver_major}/g" debian/control
  318. sed -i "s/384/${pkgver_major}/g" debian/templates/control.in
  319. ############
  320. local IFS=$'\n'
  321. for n in $(ls debian/ -w 1); do
  322. # IMPORTANT! KEEP THIS IF STATEMENT ORDER BELOW!!
  323. # Do this for every file in debian subfolder regardless of their name
  324. if [[ -f debian/${n} ]]; then
  325. # Keep this order. It is important!
  326. sed -i "s/${oldver_sed}/${pkgver_sed}/g" debian/${n}
  327. sed -i "s/${oldver_major}/${pkgver_major}/g" debian/${n}
  328. fi
  329. if [[ $(printf '%s' ${n} | grep ${oldver_major}) ]]; then
  330. local n_new=$(printf '%s' ${n} | sed "s/${oldver_major}/${pkgver_major}/")
  331. mv debian/${n} debian/${n_new}
  332. if [[ $? -ne 0 ]]; then
  333. echo -e "Error: couldn't rename file debian/${n}. Aborting\n"
  334. exit 1
  335. fi
  336. fi
  337. done
  338. unset IFS
  339. }
  340. fix_library_versions
  341. rename_deb_files
  342. else
  343. echo -e "Error: couldn't extract Nvidia Debian archive. Aborting\n"
  344. exit 1
  345. fi
  346. }
  347. ########################################################
  348. function compile_nvidia() {
  349. cd ${WORKDIR}/${pkgdir}/
  350. DEB_BUILD_OPTIONS="strip noddebs" dpkg-buildpackage -rfakeroot -b -us -uc
  351. if [[ $? -eq 0 ]]; then
  352. mkdir -p ${WORKDIR}/compiled_deb
  353. for p in ${nvidia_install_packages[*]}; do
  354. mv ${WORKDIR}/${p}*.deb ${WORKDIR}/compiled_deb/
  355. done
  356. if [[ $? -eq 0 ]]; then
  357. 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"
  358. else
  359. echo -e "Error: couldn't move deb packages into '${WORKDIR}/compiled_deb/'. Aborting\n"
  360. exit 1
  361. fi
  362. mkdir -p ${WORKDIR}/compiled_other
  363. for a in ${WORKDIR}/*; do
  364. if [[ -f ${a} ]]; then
  365. mv ${a} ${WORKDIR}/compiled_other/
  366. fi
  367. done
  368. if [[ $? -eq 0 ]]; then
  369. echo -e "Other files moved into '${WORKDIR}/compiled_other/' \n"
  370. else
  371. echo -e "Error: couldn't move other files into '${WORKDIR}/compiled_other/'. Aborting\n"
  372. exit 1
  373. fi
  374. else
  375. echo -e "Error: couldn't compile Nvidia package bundle. Aborting\n"
  376. exit 1
  377. fi
  378. }
  379. ########################################################
  380. function install_nvidia() {
  381. for syspkg in ${nvidia_required_packages[@]}; do
  382. if [[ $(echo $(dpkg -s ${syspkg} &>/dev/null)$?) -ne 0 ]]; then
  383. echo -e "Installing missing dependency ${syspkg}\n"
  384. sudo apt install -y ${syspkg}
  385. if [[ $? -ne 0 ]]; then
  386. echo -e "Error: couldn't install dependency ${syspkg}. Aborting\n"
  387. exit 1
  388. fi
  389. fi
  390. done
  391. cd ${WORKDIR}/compiled_deb
  392. for pkg in ${nvidia_install_packages[@]}; do
  393. local oldpkg=$(printf '%s' ${pkg} | sed 's/\-[0-9]*$//')
  394. local oldpkg_check=$(dpkg --get-selections | grep ${oldpkg} | awk '{print $1}')
  395. if [[ $(echo ${oldpkg_check} | wc -w) -eq 1 ]]; then
  396. if [[ ! ${oldpkg_check} =~ ^*${pkgver_major}$ ]]; then
  397. echo -e "Removing old ${oldpkg}\n"
  398. sudo apt purge --remove -y "${oldpkg}*"
  399. if [[ $? -ne 0 ]]; then
  400. echo -e "Error: couldn't uninstall ${oldpkg}. Aborting\n"
  401. exit 1
  402. fi
  403. fi
  404. fi
  405. echo -e "Installing ${pkg}\n"
  406. sudo dpkg -i ${pkg}*.deb
  407. if [[ $? -ne 0 ]]; then
  408. echo -e "Warning: couldn't install ${pkg}\n"
  409. fi
  410. done
  411. }
  412. function install_vulkan() {
  413. # Vulkan loader
  414. if [[ $? -eq 0 ]]; then
  415. local syspkg=libvulkan1
  416. if [[ $(echo $(dpkg -s ${syspkg} &>/dev/null)$?) -ne 0 ]]; then
  417. sudo apt update && sudo apt install -y ${syspkg}
  418. fi
  419. fi
  420. }
  421. ########################################################
  422. download_files && \
  423. pkgdependencies ${nvidia_builddeps[*]}
  424. prepare_deb_sources && \
  425. compile_nvidia
  426. if [[ $? -eq 0 ]] && [[ -v AUTOINSTALL ]]; then
  427. install_nvidia && \
  428. install_vulkan
  429. fi
  430. # If any buildtime deps were installed, inform the user
  431. if [[ -n ${list[*]} ]]; then
  432. echo -e "The following buildtime dependencies were installed, and they may not be required anymore:\n\n\
  433. $(for h in ${list[*]}; do echo ${h}; done)\n"
  434. fi