12

Debian Stretch上安装、使用Nvidia显卡驱动

 3 years ago
source link: https://note.qidong.name/2017/12/debian-nvidia-driver/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Debian Stretch上安装、使用Nvidia显卡驱动

2017-12-04 13:59:52 +08  字数:3994  标签: Linux

近期,孤的Debian办公电脑——ThinkPad T430,经常死机。 直接原因,系统日志显示,CPU温度过高;操作原因,看视频、视频通话。

连续跑满CPU数小时(编译Android)都不会出现CPU温度过高,看视频却出问题。 因此,除了硬件老化以外,孤认为这是显卡驱动问题,所以折腾了一下Nvidia显卡驱动的安装。

综述

先把过程中的坑大概描述一下。

  1. 按照《Debian官方教程》安装显卡驱动,误删gnome核心组件,包括网卡驱动!
  2. 恢复后,再次安装,无法启动进入图形界面!
  3. 删除Nvidia驱动后,又遭遇login loop,眼睁睁看着登录界面却进不去!
  4. 安装Bumblebee却无法启动X-window!
  5. 终于进入图形界面,却发现仍然是在使用Intel的集成显卡!

孤折腾了两天,终于修成正果。 看贴发现,有些人反复重装数十次,最终黯然放弃。 为了避免后来人爬不出坑,孤决定多花点时间,记录下安装Nvidia显卡驱动的详细过程和必知原理。

必知原理

硬件情况是,在有一个Intel集成显卡的情况下,还有一个Nvidia的独立显卡。 软件上,Debian安装完成时,会自动给二者配上兼容驱动。 如果没有在BIOS中设置显卡为switchable,那么就只使用前者。

Nvidia和Linux之间恩怨颇深,因此额外诞生了一个Nouveau项目,是为第三方开源Nvidia显卡驱动。 Nouveau源于法文,意思是『新的』。 在Debian上,这也是默认安装的驱动。 它与官方驱动比,有一些功能更新滞后与性能的问题。

俺们需要做的事,和在Windows上类似,给独立显卡安装Nvidia显卡驱动,并且用起来。 一种方案是关闭集成显卡,只用独立显卡,这通常比较耗电。 另一种方案是大部分程序使用集成显卡,少部分关键程序,比如浏览器、视频播放器、游戏等,使用独立显卡。 这种方案,需要一个显卡切换程序。

官方的解决方案叫Optimus。 这是一种软件实现集成显卡与独立显卡自动切换的技术。 Nouveau的同类解决方案叫PRIME。 与官方方案相比,性能不如,也不省电。

Optimus Prime,就是是《变形金刚》系列中的主角擎天柱的英文名,Bumblebee则是配角大黄蜂Bumblebee项目试图模拟Optimus技术的行为:当需要的时候,使用独立显卡进行渲染,不使用的时候则关闭。

故障排查与恢复

先把丑话说在前面:搞显卡驱动是很可能挂的!

如果GUI界面无法启动,还可以使用TTY。 Ctrl+Alt+F2启动TTY2,依次到TTY6。 在Stretch,TTY1就是GUI界面,所以可以用Ctrl+Alt+F1切回去。 (在Jessie及以下,用Ctrl+Alt+F7切回GUI界面。)

在TTY的login shell中,可以通过查看系统日志,进一步确认问题,并解决(通常就是卸载)。

sudo vim /var/log/messages

检查显卡信息

安装前,当然要先检查显卡是否支持显卡驱动。 当然,除了最新的或超旧的,应该都没问题。

以下两种方法,任选其一。

lspci

$ lspci -nn | egrep -i "3d|display|vga"
00:02.0 VGA compatible controller [0300]: Intel Corporation 3rd Gen Core processor Graphics Controller [8086:0166] (rev 09)
00:16.3 Serial controller [0700]: Intel Corporation 7 Series/C210 Series Chipset Family KT Controller [8086:1e3d] (rev 04)
01:00.0 VGA compatible controller [0300]: NVIDIA Corporation GF108M [NVS 5400M] [10de:0def] (rev a1)

冒号右边的0def就是Device PCI ID。 在《Appendix A. Supported NVIDIA GPU Products》页面,可以搜索到这个ID,说明是支持的。

NVIDIA NVS GPUs

NVIDIA GPU product Device PCI ID* VDPAU features

NVS 5400M 0DEF C

NVS 5200M 0DFC C

nvidia-detect

安装nvidia-detect,自动检测。

sudo apt install nvidia-detect

这个包,一般的源都有,直接安装即可。 安装完成后,直接执行,可以查看结果。

$ nvidia-detect
Detected NVIDIA GPUs:
01:00.0 VGA compatible controller [0300]: NVIDIA Corporation GF108M [NVS 5400M] [10de:0def] (rev a1)

Checking card:  NVIDIA Corporation GF108M [NVS 5400M] (rev a1)
Your card is supported by all driver versions.
It is recommended to install the
    nvidia-driver
package.

安装

安装需注意两点:

  1. /etc/apt/sources.list要增加non-free的部分。
  2. 光安装是没用的,还需要配置。

修改sources.list

在常规的main中,下载不到Nvidia的驱动,因为它并不包括在DSFG(Debian Free Software Guidelines)中。

以163的源为例,把:

deb http://mirrors.163.com/debian/ stretch main #contrib non-free
deb-src http://mirrors.163.com/debian/ stretch main #contrib non-free
deb http://security.debian.org/ stable/updates main
deb http://mirrors.163.com/debian/ stretch main contrib non-free
deb-src http://mirrors.163.com/debian/ stretch main contrib non-free
deb http://security.debian.org/ stable/updates main

其实,就是把contribnon-free解除注释,正式启用。 这一步,也可以在【Software Updates】的图形界面操作,全部打钩。

改完以后,记得更新。

sudo apt update

安装nvidia-driver

安装驱动前,先要确保对应版本的内核源码与头文件已安装。 因为在安装过程中,Nvidia驱动需要编译为一个内核模块。 这也是为什么,很多人升级了发行版版本,比如从Jessie到Stretch,显卡驱动就会出问题。 准确的说,凡是更新Linux内核,都需要重装Nvidia驱动。

sudo apt install linux-headers-$(uname -r|sed 's/[^-]*-[^-]*-//')
sudo apt install nvidia-driver

顺利的话,就是这么简单。 但如果出现安装错误,就要谨慎对待。

unmet dependencies

$ sudo apt install nvidia-driver
Reading package lists... Done
Building dependency tree
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 nvidia-driver : Depends: nvidia-driver-libs (= 375.82-1~deb9u1) but it is not going to be installed
                 Depends: nvidia-driver-bin (= 375.82-1~deb9u1) but it is not going to be installed
                 Depends: xserver-xorg-video-nvidia (= 375.82-1~deb9u1) but it is not going to be installed
                 Depends: nvidia-vdpau-driver (= 375.82-1~deb9u1) but it is not going to be installed
                 Depends: nvidia-alternative (= 375.82-1~deb9u1)
                 Depends: nvidia-kernel-dkms (= 375.82-1~deb9u1) or
                          nvidia-kernel-375.82
                 Recommends: nvidia-settings (>= 375) but it is not going to be installed
                 Recommends: nvidia-persistenced
E: Unable to correct problems, you have held broken packages.

遇到这类问题,切记慎重。宁可不安装,也不要胡来。 unment dependencies问题,直接原因是某个依赖无法安装。 细分下来,分三类:

  1. 找不到依赖包。
  2. 找到的依赖包,需要的版本与已知版本无法匹配。
  3. 需要的某依赖包已经安装,但版本不匹配。

第三类情况最危险,强行硬来,会卸载已安装的软件包。 孤就是这样卸载了自己的网卡驱动,使用USB做了一个本地源,才装回来,真是一把辛酸泪。

孤遇到的这个问题,是按照《Debian官方教程》的提示,把自己的源改成了它的『for example』。

deb http://httpredir.debian.org/debian/ wheezy-backports main contrib non-free

这就是一种作死的表现。 切记源不能乱换,轻则有网速问题,重则有依赖问题。 孤原先是stretch的源,换成wheezy-backports当然会有依赖问题。

出现firmware警告

W: Possible missing firmware /lib/firmware/nvidia/gm206/gr/sw_method_init.bin for module nouveau

W: Possible missing firmware /lib/firmware/nvidia/gm206/gr/sw_bundle_init.bin for module nouveau

W: Possible missing firmware /lib/firmware/nvidia/gm206/gr/sw_nonctx.bin for module nouveau

W: Possible missing firmware /lib/firmware/nvidia/gm206/gr/sw_ctx.bin for module nouveau

W: Possible missing firmware /lib/firmware/nvidia/gm206/gr/gpccs_sig.bin for module nouveau

这类警告,是firmware缺失所致。 参考《#828907 - plymouth external firmware error - Debian Bug report logs》,可以通过安装解决。

sudo apt install firmware-misc-nonfree

安装Bumblebee

经过以上安装,显卡驱动仍然是未使用状态。 要使用有两种方法:一是默认启用,二是按需切换。 这里推荐按需切换的方法。 (因为默认启用的方案,孤没配置成功过。)

sudo apt install bumblebee-nvidia

Bumblebee是一个实现按需切换的程序,但在安装后还需配置。 它的配置文件在/etc/bumblebee/目录下,至少需要修改两个文件。 在/etc/bumblebee/xorg.conf.nvidia/etc/bumblebee/xorg.conf.nouveau中,各有一行注释的BusID配置。

#   BusID "PCI:01:00:0"

它们需要被启用(去掉井号),并且填上合适的PCI。 通过lspci,可以找到对应的PCI编号。

$ lspci | grep -w VGA
00:02.0 VGA compatible controller: Intel Corporation 3rd Gen Core processor Graphics Controller (rev 09)
01:00.0 VGA compatible controller: NVIDIA Corporation GF108M [NVS 5400M] (rev a1)

按照上面的显示,/etc/bumblebee/xorg.conf.nvidia中需要填01:00.0,而/etc/bumblebee/xorg.conf.nouveau中需要提案00:02.0

至此,重启即可完成安装。

默认启用的方案(勿用)

注意:这与Bumblebee方案并不兼容。 使用默认启用的方案,就不要安装Bumblebee。 如果使用Bumblebee,就不要再使用此方案。

sudo apt install nvidia-xconfig

安装nvidia-xconfig并执行。

nvidia-xconfig

这时会多出一个/etc/X11/xorg.conf文件,指定默认的显卡。 配置完成后重启生效。

如果发生进入不了图形界面的问题,可修改、甚至删除该文件。

孤在此方案中折腾良久,最终因搞不清如何修正配置问题而放弃。戒慎!

配置显卡切换

重启后,默认显卡仍然是集成显卡。 要想使用独立显卡,可以使用optirun。 比如,用独立显卡来使用Firefox。 (遗憾的是,Chrome或Chromium都不支持。)

optirun firefox

但是,并不是所有程序的入口,都是命令行。 真正需要独立显卡的GUI程序,大多都是从Gnome的Activities启动。 配置这些程序的启动,有两种方法。

一是直接修改.desktop文件。 这些文件都在~/.local/share/applications/目录下。 这种做法的缺点是,太不直观。

比如,把/home/yanqd1/.local/share/applications/nvidia-settings.desktopExec=那行修改为:

Exec=optirun nvidia-settings -c :8

二是使用一个编辑程序去编辑,比如menulibre。

sudo apt install menulibre

执行menulibre,可得图形界面。 选择需要使用独立显卡的程序,修改【Command】栏,在原有启动命令前加上optirun。 本质上,这也是修改.desktop文件,两种方法是等价的。

查看显卡使用情况

显卡驱动的安装、配置虽然貌似无误,但仍然需要直接确定其使用情况,才能确保无误。 大概有两种方法,通过nvidia-settings或nvidia-smi。 前者是GUI程序,后者是CLI程序。

nvidia-settings

sudo apt install nvidia-settings

直接执行nvidia-settings时,GUI界面会弹出各种问题。 这是因为默认显卡是Intel集成显卡。 需要执行以下命令,才能看到真正有效的设置界面。

optirun nvidia-settings -c :8

当然,可以像前面介绍的那样,配置其.desktop文件,从Activities启动它。

nvidia-smi

sudo apt install nvidia-smi

这个包就是NVIDIA System Management Interface,可以查看显卡的信息。

使用以下命令,可以每秒自动更新显示。 (参考《Linux 命令行监视显卡使用情况》。)

watch -n 1 nvidia-smi

由于不支持显示进程,所以只能从显存使用情况来判断。 每用optirun启动一个GUI程序,显存使用量都会提升。

总结

还好孤不是想在Linux下玩游戏,否则以上配置可能还差了一步更麻烦的性能优化。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK