LXD/GPU Acceleration (NVIDIA)

From Funtoo
Revision as of 00:18, October 22, 2019 by Drobbins (talk | contribs) (Created page with "These steps will show you how to set up NVIDIA acceleration inside an LXD container. === Background === Setting up a container with GPU acceleration on NVIDIA systems is ex...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

These steps will show you how to set up NVIDIA acceleration inside an LXD container.


Setting up a container with GPU acceleration on NVIDIA systems is extremely easy, thanks to the libnvidia-container ebuild. This ebuild contains an abstraction layer and support tools which allow NVIDIA acceleration to be available inside containers, without having to install any NVIDIA-related tooling inside the container itself. LXD is set up to detect this framework when available and take advantage of it, providing easy access to CUDA as well as OpenGL on your host's NVIDIA graphics card.


It is assumed that you are:

  • Running Funtoo Linux 1.4 or later
  • Have the proprietary NVIDIA drivers installed and running on your host system
  • Have successfully followed the instructions on the main LXD page to start up a console-only testcontainer.

LXD Setup on Host

After setting up LXD following the instructions on the main LXD page, you will next want to emerge libnvidia-container:

root # emerge libnvidia-container

While this ebuild doesn't really require an initscript, there is a special command you need to run prior to starting LXD to get GPU acceleration to work. To make this easy and automatic, I've created an initscript just to perform this step for you. Add nvidia-container to your default runlevel:

root # rc-update add nvidia-container default
root # rc

When you run rc, lxd should restart, with nvidia-container starting after to configure everything for video acceleration. See /etc/init.d/nvidia-container for more information if you're curious about what it does.

What next? That's it! You're ready to start using GPU acceleration inside your container.

LXD Setup for Container

LXD setup for the container is also extremely easy. First, let's create a test container called nvidia-test:

root # lxc launch funtoo nvidia-test

Now, we'll edit its configuration to enable access to the goodies:

root # lxc config edit nvidia-test

Make sure the following bits are added to the YAML:

   LXD config
  environment.DISPLAY: :1
    path: /tmp/.X11-unix
    source: /tmp/.X11-unix
    type: disk
    type: gpu

In my environment, my DISPLAY environment variable is set to :1, thus I also set this variable inside the container. If yours is, for example :0, set it to that value instead. This, along with the X11-unix mapping, will allow X applications to run. Without these two things, you will still have access to the GPU inside your container, but can only use non-GUI CUDA apps.

The mygpu part automatically configures access to the NVIDIA graphics card on your system. Thanks to libnvidia-container, you don't need to have any drivers installed at all.

Now, you can do a bare-bones test of GPU acceleration using glxgears:

root # lxc exec nvidia-test -- su --login
nvidia-test # ego sync
nvidia-test # ego profile flavor desktop
nvidia-test # emerge --jobs xorg-server mesa-progs
nvidia-test # glxgears

You should be greeted with the glxgears application running in an X window, running using GPU acceleration. What's amazing about all this is that you don't even have any official NVIDIA drivers installed on your container (you can verify this by looking in /var/db/pkg/x11-drivers -- see? I told you!)