How to forward GPU and sound to the host?

Published: Tue 25 February 2020
By evpo

In OS.

tags: LXD

Further in the article we will use demo as the user in the host and ubuntu in the container.

Video

Add the below lines to the LXD container config to allow the container's X11 to connect to the host:

devices:
  Xauthority:
    path: /home/ubuntu/.Xauthority
    source: /home/demo/.Xauthority
    type: disk

Forward the X11 socket to the container:

devices:
  X0:
    path: /tmp/.X11-unix/X0
    source: /tmp/.X11-unix/X0
    type: disk

Forward the gpu device:

device:
  mygpu:
    gid: "1000"
    type: gpu
    uid: "1000"

Audio

We will forward ALSA audio system. Go to /dev/snd and list all devices in the directory. Forward all of them as unix-char. Below is an example of one of the devices:

device:
  hwC0D0:
    gid: "29"
    path: /dev/snd/hwC0D0
    type: unix-char

Note the gid parameter. 29 is id of the audio group. By default the forwarding of unix-char gives permissions that are different from the source system: root:root

To automate the steps above you can use the script below:

for snd in $(find /dev/snd -type c -maxdepth 1); do f=${snd##*\/}; lxc profile device add snd ${f} unix-char path=${snd} gid=29 ; done

Forward /dev/snd/by-path as disk:

device:
  sndbypath:
    path: /dev/snd/by-path
    source: /dev/snd/by-path
    type: disk

links

social