LXD/GPU Acceleration (NVIDIA)

From Funtoo
Jump to navigation Jump to search

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 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.

Prerequisites

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.

You will also need to run this command as your regular user to ensure that the container can access your X server. Additionally, you'll need to run this every time you restart your computer (you can put it in your ~/.xprofile file for convenience.)

 xauth +local:

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
config:
  environment.DISPLAY: :1
devices:
  X11-unix:
    path: /tmp/.X11-unix
    source: /tmp/.X11-unix
    type: disk
  mygpu:
    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 nvidia-drivers mesa-progs
nvidia-test # glxgears

You should be greeted with the glxgears application running in an X window, running using GPU acceleration.