LXD/GPU Acceleration (NVIDIA)
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
LXD Setup on Host
After setting up LXD following the instructions on the main LXD page, you will next want to emerge
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
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 (we're assuming you're using a regular user account for your X session) 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.)
user $ 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
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:
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.
mygpu part automatically configures access to the NVIDIA graphics card on your system.
LXD Setup Inside Container
Once the LXD container is configured properly, we can now enter the container to complete configuration:
root # lxc exec nvidia-test -- su --login nvidia-test #
You will need to emerge
xorg-server to provide a basic environment for accelerated NVIDIA drivers to function. The following setup is suggested:
nvidia-test # epro flavor desktop nvidia-test # emerge -auDN @world --jobs nvidia-test # emerge --jobs xorg-server nvidia-drivers
Now, you can perform a quick investigation of your GL environment. Try running
glxinfo inside your container, as root. You should see output that looks like this:
nvidia-test # glxinfo name of display: :1 display: :1 screen: 0 direct rendering: Yes server glx vendor string: NVIDIA Corporation server glx version string: 1.4
You should also be able to run
glxgears and have it open an X window on your host's X display :1, using full GPU acceleration:
nvidia-test # glxgears
You should be greeted with the glxgears application running in an X window.
We've successfully set up an unprivileged Funtoo Linux container within LXD that has the ability to run GPU-accelerated apps on the host's X session. You should be able to replicate this setup with any other Linux distributions, provided that you are able to install the NVIDIA drivers inside the container. Remember to run
xhost +local: after every reboot under the user account that runs the X session, or to add this to your
~/.xprofile file so it is run every time your X session starts.