Video driver under lxc container?

5,301

Please, take a look at this script to create a LXC container that runs steam with sound and video acceleration:

http://bazaar.launchpad.net/~ubuntu-lxc/lxc/steam-lxc/view/head:/steam-lxc

The magic comes here:

Outside LXC:

    # Add the bind mounts to the container's fstab
    self.container.set_config_item("lxc.mount.entry",
                                   "/tmp/.X11-unix tmp/.X11-unix "
                                   "none bind,ro")
    self.container.set_config_item("lxc.mount.entry",
                                   "/dev/dri dev/dri none bind,ro")
    self.container.set_config_item("lxc.mount.entry",
                                   "%s/pulse.socket home/%s/.pulse_socket "
                                   "none bind,ro" % (self.config_path,
                                                     self.user.pw_name))

We export X11 with a bind mount the /tmp/.X11-unix directory to allow container to use host X11. Do the same with /dev/dri directory and audio socket.

Inside LXC:

    # Get pulseaudio to listen on the socket
    with self.user_privileges():
        subprocess.call(['pactl', 'load-module',
                        'module-native-protocol-unix',
                        'socket=%s' % self.pulse_socket,
                        'auth-cookie-enabled=0'])

    # Start steam
    self.run_command(
        ["steam"], {'DISPLAY': os.environ['DISPLAY'],
                    'PULSE_SERVER': "/home/%s/.pulse_socket" %
                                    self.user.pw_name})

Uses pactl to use a unix socket to communicate with host pulse audio server and later export the socket and DISPLAY environment variable to allow steam to use local X11 server and socket to audio server.

Take a look at the script and enjoy it :)

With that environment variables inside LXC you could play (theoretically) almost all games.

Best regards!

Share:
5,301

Related videos on Youtube

lurscher
Author by

lurscher

Updated on September 18, 2022

Comments

  • lurscher
    lurscher over 1 year

    Since LXC (Linux Containers) is a kernel level super-chroot, I've been wondering what sort of video driver the containers have:

    My host is Ubuntu 12.04 64-bit machine with ATI gpu. Will the LXC container have access to the same driver? Or do they need to be installed on each container?

    • Admin
      Admin over 11 years
      Please share a bit more on what you're trying to accomplish. Are you trying to run GUI applications or even OpenGL-enabled applications in your LXC container?
  • gertvdijk
    gertvdijk over 11 years
    Your link is a page about the Libvirt LXC client/plugin, they call a 'driver'. I don't see any information regarding video drivers or alike.
  • lurscher
    lurscher over 11 years
    so, the question is: if i run LIBGL_DEBUG=verbose glxinfo | grep rendering inside the lxc container console, will i see "direct rendering: yes"?
  • gertvdijk
    gertvdijk over 11 years
    @lurscher I can't tell what you will see on your hardware. Usually, people are not running anything directly related to hardware in containers. Either way, I think this article might give you some insight.
  • lurscher
    lurscher over 11 years
    i mean, i can see direct rendering: yes on the host, but that doesn't translate directly to the container, because the system installation (including the fglrx driver) will not be shared with the containers, right? That article seems to point that at least some direct rendering is possible using the open source driver, which is a good fallback plan for me, but if i could rely on the propietary fglrx ati driver it would be golden
  • lurscher
    lurscher almost 9 years
    interesting. Have you tried to have at least 2 simultaneous containers using HW acceleration?
  • OscarGarcia
    OscarGarcia almost 9 years
    No, I'm currently using Docker (not plain LXC) to run Gazebo 5 containers, I didn't try to run two containers running accelerated applications at once. I'll try it this afternoon and I'll tell you if it works (I think that it'll work).
  • OscarGarcia
    OscarGarcia almost 9 years
    Sorry for the delay. Yes, it is possible to run multiple LXC containers and run simultaneously several accelerated applications. Take a look to this video that I recorded for you: youtu.be/RAUtZBaN1Oo
  • OscarGarcia
    OscarGarcia almost 9 years
    Here is the github repository with the code used in the video: github.com/ojgarciab/docker3d
  • lurscher
    lurscher almost 9 years
    so, create.sh and create2.sh create the two containers and start.sh and start2.shstart each separately. What does make.sh do and in what order should it be run?
  • OscarGarcia
    OscarGarcia almost 9 years
    make.sh builds the template "ub1404-dev" from template "ubuntu:14.04", creating user redstar with its home directory, installs mesa-utils, ect. Then create[2].sh create a template named "ub1404-dev[2]" from "ub1404-dev" template, bind mounting /dev/dri and /tmp/.X11-unix directories inside the container (and my own home directory). Before that we stop and destroy container if it was created and running previously. Then we could run start[2].sh to start running the container at background (but for now nothing executing on it).
  • OscarGarcia
    OscarGarcia almost 9 years
    At the very end we run commands inside the containers: we execute a bash shell. -- We could connect with ssh -X [email protected] (installing previously openssh-server) and running X applications as we do normally with remote servers, but without hardware acceleration.
  • OscarGarcia
    OscarGarcia almost 9 years
    Sorry: "Then create[2].sh create a LXC CONTAINER named "ub1404-dev" and "ub1404-dev2" from same "ub1404-dev" template". So we use the common template to create two different (and isolated) LXC containers.