Difference between revisions of "LXD/GPU Acceleration (NVIDIA)"

From Funtoo
< LXD
Jump to: navigation, search
(you actually need nvidia-drivers (d'oh!))
(LXD Setup for Container)
 
(7 intermediate revisions by the same user not shown)
Line 28: Line 28:
  
 
When you run {{c|rc}}, {{c|lxd}} should restart, with {{c|nvidia-container}} starting after to configure everything for video acceleration. See {{f|/etc/init.d/nvidia-container}} for more information if you're curious about what it does.
 
When you run {{c|rc}}, {{c|lxd}} should restart, with {{c|nvidia-container}} starting after to configure everything for video acceleration. See {{f|/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 {{c|~/.xprofile}} file for convenience.)'''
 +
 +
{{console|body=
 +
$ ##i##xauth +local:
 +
}}
  
 
What next? That's it! You're ready to start using GPU acceleration inside your container.
 
What next? That's it! You're ready to start using GPU acceleration inside your container.
Line 61: Line 67:
 
In my environment, my {{c|DISPLAY}} environment variable is set to {{c|:1}}, thus I also set this variable inside the container. If yours is, for example {{c|:0}}, set it to that value instead. This, along with the {{c|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.
 
In my environment, my {{c|DISPLAY}} environment variable is set to {{c|:1}}, thus I also set this variable inside the container. If yours is, for example {{c|:0}}, set it to that value instead. This, along with the {{c|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 {{c|mygpu}} part automatically configures access to the NVIDIA graphics card on your system. Thanks to {{c|libnvidia-container}}, you don't need to have any drivers installed at all.
+
The {{c|mygpu}} part automatically configures access to the NVIDIA graphics card on your system.
 +
 
 +
After the config changes, reboot your container for good measure:
 +
 
 +
{{console|body=
 +
# ##i##lxc restart nvidia-test
 +
}}
 +
 
 +
=== LXD Setup Inside Container ===
  
Now, you can do a bare-bones test of GPU acceleration using glxgears:
+
Once the LXD container is configured properly, we can now enter the container to complete configuration:
  
 
{{console|body=
 
{{console|body=
 
###i## lxc exec nvidia-test -- su --login
 
###i## lxc exec nvidia-test -- su --login
%nvidia-test% ##i##ego sync
+
%nvidia-test%
%nvidia-test% ##i##ego profile flavor desktop
+
}}
%nvidia-test% ##i##emerge --jobs xorg-server nvidia-drivers mesa-progs
+
 
 +
You will need to emerge {{c|nvidia-drivers}} and {{c|xorg-server}} to provide a basic environment for accelerated NVIDIA drivers to function. The following setup is suggested:
 +
 
 +
{{console|body=
 +
%nvidia-test% ##i## epro flavor desktop
 +
%nvidia-test% ##i## emerge -auDN @world --jobs
 +
%nvidia-test% ##i## emerge --jobs xorg-server nvidia-drivers
 +
}}
 +
 
 +
Now, you can perform a quick investigation of your GL environment. Try running {{c|glxinfo}} inside your container, as root. You should see output that looks like this:
 +
 
 +
{{console|body=
 +
%nvidia-test% ##i##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 {{c|glxgears}} and have it open an X window on your host's X display :1, using full GPU acceleration:
 +
 
 +
{{console|body=
 
%nvidia-test% ##i##glxgears
 
%nvidia-test% ##i##glxgears
 
}}
 
}}
  
You should be greeted with the glxgears application running in an X window, running using GPU acceleration.
+
You should be greeted with the glxgears application running in an X window.
 +
 
 +
=== Conclusion ===
 +
 
 +
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 {{c|xhost +local:}} after every reboot under the user account that runs the X session, or to add this to your {{c|~/.xprofile}} file so it is run every time your X session starts.
 +
 
 +
[[Category:NVIDIA]]
 +
[[Category:Official Documentation]]
 +
[[Category:LXD]]

Latest revision as of 01:04, October 22, 2019

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

After the config changes, reboot your container for good measure:

root # lxc restart nvidia-test

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 nvidia-drivers and 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.

Conclusion

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.