Migrating to libglvnd in Gentoo Linux on a laptop with NVIDIA Optimus

In a 2015 post I described how I configured my Gentoo Linux installation to switch between the closed-source NVIDIA driver and the open-source Intel driver on a Clevo W230SS laptop that has NVIDIA Optimus hardware (NVIDIA GeForce GTX 860M GPU plus Intel HD 4600 IGP). I did not want to use Bumblebee, preferring to use only the NVIDIA driver or only the Intel driver, switching between them by running a Bash script then logging out of KDE Plasma and back in again. Basically, the scheme a) swapped the xorg.conf file depending on which driver I wanted to use, and b) used the eselect opengl command to select the applicable OpenGL library. The latest versions of the files in my scheme are listed below if you’re interested (I use LightDM instead of KDM these days, as KDM is no more), otherwise just skip to the section after, titled ‘Migrating to libglvnd’.

Previous scheme using eselect opengl

~/Desktop/Select_NVIDIA_GPU.desktop

[Desktop Entry]
Comment[en_GB]=Run a script to configure your installation to use the NVIDIA GeForce GTX 860M GPU when you restart X Windows
Comment=Run a script to configure your installation to use the NVIDIA GeForce GTX 860M GPU when you restart X Windows
Exec=konsole -e sh /home/fitzcarraldo/nvidia.sh
GenericName[en_GB]=Configure your installation to use the NVIDIA GeForce GTX 860M GPU
GenericName=Configure your installation to use the NVIDIA GeForce GTX 860M GPU
Icon=/home/fitzcarraldo/Pictures/Icons/nvidia_icon.png
MimeType=
Name[en_GB]=NVIDIA GPU
Name=NVIDIA GPU
Path=
StartupNotify=true
Terminal=false
TerminalOptions=\s--noclose
Type=Application
X-DBUS-ServiceName=
X-DBUS-StartupType=none
X-KDE-SubstituteUID=false
X-KDE-Username=fitzcarraldo

~/nvidia.sh

#!/bin/bash
echo
echo "Your installation is currently configured to use the following graphics processor:"
echo
GPU=`eselect opengl list | grep \* | awk '{ print $2 }'`
if [ "$GPU" = "nvidia" ]; then
  echo "NVIDIA GeForce GTX 860M"
  echo
  echo "You do not need to do anything. Please close this window."
elif [ "$GPU" = "xorg-x11" ]; then
  echo "Intel HD 4600 Integrated Graphics Processor"
  echo
  echo "This script will configure your installation to use the NVIDIA GeForce GTX 860M GPU all the time."
  echo
  echo "Enter your own password."
  echo
  sudo eselect opengl set nvidia
# See separate configuration of LightDM for NVIDIA GPU and Intel HD Graphics.
  sudo cp /etc/X11/xorg.conf.nvidia /etc/X11/xorg.conf
  echo
  echo "Now you should logout to restart X Windows."
fi
echo
echo -n "Press ENTER to end: "
read ACKNOWLEDGE

/etc/X11/xorg.conf.nvidia

Section "ServerLayout"
    Identifier     "Layout0"
    Screen      1  "nvidia" 0 0
    Inactive       "intel"
EndSection

Section "Monitor"
    Identifier     "Monitor0"
    Option         "DPMS"
    Option         "DPI" "96 x 96"
EndSection

Section "Device"
    Identifier     "nvidia"
    Driver         "nvidia"
    BusID          "PCI:1:0:0"
EndSection

Section "Device"
    Identifier     "intel"
    Driver         "modesetting"
    BusID          "PCI:0:2:0"
EndSection

Section "Screen"
    Identifier     "nvidia"
    Device         "nvidia"
    Monitor        "Monitor0"
    DefaultDepth    24
    Option         "UseDisplayDevice" "none"
    SubSection     "Display"
        Depth       24
        Virtual     1920 1080
    EndSubSection
EndSection

Section "Screen"
    Identifier     "intel"
    Device         "intel"
    Monitor        "Monitor0"
EndSection

~/Desktop/Select_Intel_HD_Graphics.desktop

[Desktop Entry]
Comment[en_GB]=Run a script to configure your installation to use Intel Integrated Graphics when you restart X Windows
Comment=Run a script to configure your installation to use Intel Integrated Graphics when you restart X Windows
Exec=konsole -e sh /home/fitzcarraldo/intel.sh
GenericName[en_GB]=Configure your installation to use Intel HD Graphics
GenericName=Configure your installation to use Intel HD Graphics
Icon=/home/fitzcarraldo/Pictures/Icons/intel-hd-icon.png
MimeType=
Name[en_GB]=Intel HD Graphics
Name=Intel HD Graphics
Path=
StartupNotify=true
Terminal=false
TerminalOptions=\s--noclose
Type=Application
X-DBUS-ServiceName=
X-DBUS-StartupType=none
X-KDE-SubstituteUID=false
X-KDE-Username=fitzcarraldo

~/intel.sh

#!/bin/bash
echo
echo "Your installation is currently configured to use the following graphics processor:"
echo
GPU=`eselect opengl list | grep \* | awk '{ print $2 }'`
if [ "$GPU" = "xorg-x11" ]; then
  echo "Intel HD 4600 Integrated Graphics Processor"
  echo
  echo "You do not need to do anything. Please close this window."
elif [ "$GPU" = "nvidia" ]; then
  echo "NVIDIA GeForce GTX 860M"
  echo
  echo "This script will configure your installation to use the"
  echo "Intel HD 4600 Integrated Graphics Controller all the time."
  echo
  echo "Enter your own password."
  echo
  sudo eselect opengl set xorg-x11
# See separate configuration of LightDM for Intel HD Graphics and NVIDIA GPU.
  sudo cp /etc/X11/xorg.conf.intel /etc/X11/xorg.conf
  echo
  echo "Now you should logout to restart X Windows."
fi
echo
echo -n "Press ENTER to end: "
read ACKNOWLEDGE

/etc/X11/xorg.conf.intel

Section "Device" 
   Identifier  "Intel Graphics" 
   Driver      "intel" 
   Option      "AccelMethod" "sna" 
   Option      "TearFree" "true" 
EndSection

/etc/X11/xorg.conf.d/20-opengl.conf

Section "Files"
        ModulePath "/usr/lib/xorg/modules"
        ModulePath "/usr/lib64/xorg/modules"
EndSection

/etc/X11/Sessions/plasma (used by LightDM)

#!/bin/bash
#
# Make sure the following is in /etc/lightdm/lightdm.conf
# display-setup-script=/etc/X11/Sessions/plasma
#
GPU=`eselect opengl list | grep \* | awk '{ print $2 }'`
if [ "$GPU" = "nvidia" ]; then
    xrandr --setprovideroutputsource modesetting NVIDIA-0
    xrandr --auto
fi

Migrating to libglvnd

Well, the above scheme worked fine… until the recent decision by the Gentoo Linux developers to drop the app-eselect/eselect-opengl ebuild and switch the x11-base/xorg-server, media-libs/mesa and x11-drivers/nvidia-drivers to using the libglvnd library:

This package is masked and could be removed soon!
The mask comment indicates that this package is scheduled for removal from our package repository.
Please review the mask information below for more details.

Replaced by media-libs/libglvnd. Masked for removal in 30 days. Bug #728286

Affected packages	app-eselect/eselect-opengl

Author/Date		Matt Turner  (2020-08-11 00:00:00 +0000 UTC)

Without the eselect opengl command, my scripts were scuppered. So I decided to bite the bullet and switch to using libglvnd. It turned out not to be difficult, and I took the following steps to migrate:

  1. Deleted the file /etc/X11/xorg.conf
  2. Created the file /etc/X11/xorg.conf.d/01-nvidia-offload.conf containing the following:
    Section "ServerLayout"
        Identifier "layout"
        Option "AllowNVIDIAGPUScreens"
    EndSection
    
  3. Performed the usual ‘emerge -uvDN @world‘ to update and upgrade the relevant packages, which automatically unmerged app-eselect/eselect-opengl

If I had run into trouble with the installed app-eselect/eselect-opengl blocking the upgrade, I could have worked around that by doing the following:

root # emerge -C eselect-opengl
root # emerge -1v nvidia-drivers mesa xorg-server xorg-drivers

I removed all references to the libglvnd USE flag from /etc/portage/make.conf, and the only place libglvnd is declared explicitly now is in the file /etc/portage/package.use/world because I have a multilib installation:

root # grep libglvnd /etc/portage/package.*/*
/etc/portage/package.use/world:>=media-libs/libglvnd-1.3.1 abi_x86_32

The status of the applicable packages in my installation is now as follows:

root # eix -I nvidia-drivers
[I] x11-drivers/nvidia-drivers
     Available versions:  [M]340.108-r1(0/340)^mtd ~390.132-r4(0/390)^mtd 390.138-r1(0/390)^mtd 435.21-r6(0/435)^mtd 440.100-r2(0/440)^mtd 450.57-r1(0/450)^mtd {+X compat (+)driver gtk3 +kms +libglvnd multilib static-libs +tools uvm wayland ABI_MIPS="n32 n64 o32" ABI_RISCV="lp64 lp64d" ABI_S390="32 64" ABI_X86="32 64 x32" KERNEL="FreeBSD linux"}
     Installed versions:  450.57-r1(0/450)^mtd(22:04:56 14/08/20)(X driver kms libglvnd multilib tools wayland -compat -gtk3 -static-libs -uvm ABI_MIPS="-n32 -n64 -o32" ABI_RISCV="-lp64 -lp64d" ABI_S390="-32 -64" ABI_X86="32 64 -x32" KERNEL="linux -FreeBSD")
     Homepage:            https://www.nvidia.com/Download/Find.aspx
     Description:         NVIDIA Accelerated Graphics Driver

root # eix -I mesa
[I] media-libs/mesa
     Available versions:  20.0.8^t ~20.1.4^t ~20.1.5^t ~20.2.0_rc1^t ~20.2.0_rc2^t **9999*l^t {+X +classic d3d9 debug +dri3 +egl +gallium +gbm gles1 +gles2 +libglvnd +llvm lm-sensors opencl osmesa selinux test unwind vaapi valgrind vdpau vulkan vulkan-overlay wayland xa xvmc zink +zstd ABI_MIPS="n32 n64 o32" ABI_RISCV="lp64 lp64d" ABI_S390="32 64" ABI_X86="32 64 x32" KERNEL="linux" VIDEO_CARDS="freedreno i915 i965 intel iris lima nouveau panfrost r100 r200 r300 r600 radeon radeonsi v3d vc4 virgl vivante vmware"}
     Installed versions:  20.0.8^t(22:03:42 14/08/20)(X classic dri3 egl gallium gbm gles2 libglvnd llvm wayland zstd -d3d9 -debug -gles1 -lm-sensors -opencl -osmesa -selinux -test -unwind -vaapi -valgrind -vdpau -vulkan -vulkan-overlay -xa -xvmc ABI_MIPS="-n32 -n64 -o32" ABI_RISCV="-lp64 -lp64d" ABI_S390="-32 -64" ABI_X86="32 64 -x32" KERNEL="linux" VIDEO_CARDS="i965 intel -freedreno -i915 -iris -lima -nouveau -panfrost -r100 -r200 -r300 -r600 -radeon -radeonsi -vc4 -virgl -vivante -vmware")
     Homepage:            https://www.mesa3d.org/ https://mesa.freedesktop.org/
     Description:         OpenGL-like graphic library for Linux

[I] x11-apps/mesa-progs
     Available versions:  8.4.0 **9999*l {egl gles2}
     Installed versions:  8.4.0(13:53:51 02/05/19)(-egl -gles2)
     Homepage:            https://www.mesa3d.org/ https://mesa.freedesktop.org/ https://gitlab.freedesktop.org/mesa/demos
     Description:         Mesa's OpenGL utility and demo programs (glxgears and glxinfo)

Found 2 matches
root # eix -I xorg-server
[I] x11-base/xorg-server
     Available versions:  1.20.8-r1(0/1.20.8) **9999(0/9999)*l {debug dmx doc +elogind ipv6 kdrive +libglvnd libressl minimal selinux static-libs suid systemd +udev unwind wayland xcsecurity xephyr xnest xorg xvfb}
     Installed versions:  1.20.8-r1(0/1.20.8)(22:07:21 14/08/20)(elogind ipv6 libglvnd udev wayland xorg -debug -dmx -doc -kdrive -libressl -minimal -selinux -static-libs -suid -systemd -unwind -xcsecurity -xephyr -xnest -xvfb)
     Homepage:            https://www.x.org/wiki/ https://gitlab.freedesktop.org/xorg/xserver/xorg-server
     Description:         X.Org X servers

root # eix -I xorg-drivers
[I] x11-base/xorg-drivers
     Available versions:  1.20-r2 **9999*l {INPUT_DEVICES="elographics evdev joystick libinput synaptics vmmouse void wacom" VIDEO_CARDS="amdgpu ast dummy fbdev freedreno geode glint i915 i965 intel mga nouveau nv nvidia omap qxl r128 radeon radeonsi siliconmotion tegra vc4 vesa via virtualbox vmware"}
     Installed versions:  1.20-r2(22:05:41 14/08/20)(INPUT_DEVICES="evdev synaptics -elographics -joystick -libinput -vmmouse -void -wacom" VIDEO_CARDS="i965 intel nvidia -amdgpu -ast -dummy -fbdev -freedreno -geode -glint -i915 -mga -nouveau -nv -omap -qxl -r128 -radeon -radeonsi -siliconmotion -tegra -vc4 -vesa -via -virtualbox -vmware")
     Homepage:            https://wiki.gentoo.org/wiki/No_homepage
     Description:         Meta package containing deps on all xorg drivers

I can now delete the line display-setup-script=/etc/X11/Sessions/plasma in /etc/lightdm/lightdm.conf, and delete the script /etc/X11/Sessions/plasma, as the script no longer works and the xrandr commands in it are no longer necessary in any case. The files and scripts Select_NVIDIA_GPU.desktop, nvidia.sh, xorg.conf.nvidia, Select_Intel_HD_Graphics.desktop, intel.sh and xorg.conf.intel are also redundant now and can be deleted.

After rebooting, the LightDM login screen appears as usual and I can login to the Desktop Environment. I can connect an external monitor to the laptop via either VGA cable or HDMI cable and both methods work, and I can switch between the laptop monitor and the external monitor using KDE Plasma’s ‘System Settings’ > ‘Display Configuration’, so everything appears to be working correctly.

The command xrandr --listproviders (add ‘--verbose‘ to provide more information) lists both the NVIDIA and Intel video devices, so I assume everything is working correctly:

user $ xrandr --listproviders
Providers: number : 2
Provider 0: id: 0x47 cap: 0xb, Source Output, Sink Output, Sink Offload crtcs: 4 outputs: 4 associated providers: 0 name:Intel
Provider 1: id: 0x203 cap: 0x0 crtcs: 0 outputs: 0 associated providers: 0 name:NVIDIA-G0

It appears that the default is to use the Intel IGP:

user $ glxinfo | grep -E 'OpenGL (vendor|renderer)'
OpenGL vendor string: Intel Open Source Technology Center
OpenGL renderer string: Mesa DRI Intel(R) HD Graphics 4600 (HSW GT2)
user $ __NV_PRIME_RENDER_OFFLOAD_PROVIDER=Intel __GLX_VENDOR_LIBRARY_NAME=mesa glxinfo  | grep -E 'OpenGL (vendor|renderer)'
OpenGL vendor string: Intel Open Source Technology Center
OpenGL renderer string: Mesa DRI Intel(R) HD Graphics 4600 (HSW GT2)

unless I use environment variables explicitly to specify that the NVIDIA GPU be used for a specific application:

user $ __NV_PRIME_RENDER_OFFLOAD_PROVIDER=NVIDIA-G0 __GLX_VENDOR_LIBRARY_NAME=nvidia glxinfo  | grep -E 'OpenGL (vendor|renderer)'
OpenGL vendor string: NVIDIA Corporation
OpenGL renderer string: GeForce GTX 860M/PCIe/SSE2

Performance seems reasonable:

user $ __NV_PRIME_RENDER_OFFLOAD_PROVIDER=NVIDIA-G0 __GLX_VENDOR_LIBRARY_NAME=nvidia __GL_SYNC_TO_VBLANK=0 glxgears
27197 frames in 5.0 seconds = 5439.292 FPS
27332 frames in 5.0 seconds = 5466.274 FPS
27857 frames in 5.0 seconds = 5571.184 FPS
27553 frames in 5.0 seconds = 5510.447 FPS
27128 frames in 5.0 seconds = 5425.556 FPS
^C

To run a program such as LibreCAD using the NVIDIA GPU I can do the following:

user $ __NV_PRIME_RENDER_OFFLOAD_PROVIDER=NVIDIA-G0 __GLX_VENDOR_LIBRARY_NAME=nvidia librecad

I need to play around more to understand how to use nvidia-drivers and libglvnd with the NVIDIA Optimus hardware in this laptop, but at least I have managed to migrate from app-eselect/eselect-opengl to media-libs/libglvnd before the former is dropped from the Portage tree in the near future.

About Fitzcarraldo
A Linux user with an interest in all things technical.

2 Responses to Migrating to libglvnd in Gentoo Linux on a laptop with NVIDIA Optimus

  1. Lucarioxx12 says:

    thanks for everything, I spent a week seeing why the installation of the Nvidia drivers did not work for me on my laptop with Gentoo, and the wiki does not help, because it is outdated and still has the eselect / opengl package as valid, thanks for publishing this post , you solved the problem I had (I don’t speak English, so I may have faults)

  2. Brandon Enright says:

    Thank you for writing this! As of my most recent system update modesetting/nvidia was causing a segfault in X.

    I switched to using a mostly blank xorg.conf and using
    __NV_PRIME_RENDER_OFFLOAD_PROVIDER=NVIDIA-G0 __GLX_VENDOR_LIBRARY_NAME=nvidia

    To allow use of the nvidia GPU and things are working great.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.