LXD container ALSA audio support

host: Ubuntu 18.04 LXD/LXC 3.0.3

I am trying to use ALSA in my ubuntu 18.04 container. ALSA on host works. I have seen this post but it doesn’t work for me.

My configuration:

lxc config set mycontainer raw.lxc "lxc.cgroup.devices.allow = c 116:* rwm"
lxc config set mycontainer raw.lxc "lxc.mount.entry = /dev/snd dev/snd none bind,optional,create=dir 0 0"

aplay: aplay -l aplay: device_list:270: no soundcards found…

ALSA on the host works perfectly well.

I need it for a home automation text-to-speech functionality.

What else do I need to do to get it working?

Thank you in advance.

Hi!
Does your host have Pulseaudio?

If the software can use Pulseaudio instead, then you can share the Pulseaudio’s Unix socket. It’s a single thing to do. See more on this at https://blog.simos.info/how-to-easily-run-graphics-accelerated-gui-apps-in-lxd-containers-on-your-ubuntu-desktop/

@simos,

Thank you for this info. I would prefer not to install pulseaudio on my host/server, it would be much simpler to use ALSA if it is possible of course.

I guess, I have managed to get it working.
Found this post

lxc config device add <container> pcmC0D2c unix-char path=/dev/snd/pcmC0D2c
I have added all devices listed in /dev/snd:

controlC0 hwC0D0 hwC0D2 pcmC0D0c pcmC0D0p pcmC0D10p pcmC0D1p pcmC0D2c pcmC0D3p pcmC0D7p pcmC0D8p pcmC0D9p seq timer

It is important to add seq and timer, as well.

2 Likes

It seems that /dev/snd/pcmXXXX are random. When reboot the host, all /dev/snd/pcmXXXX changes. So I think mount /run/user/<uid>/pulse/native to container is easier.

Have you managed to find “permanent” flexible solution for it, if pcmXXX are indeed random as mentioned by @abcfy2?

I do not think that they are random. Check this article. I have restarted my server numerous times and those pcm’s have never been changed.

1 Like

@lk7777, have you had to manualy change group for container’s /dev/snd/* devices? Mine were owned by root and not audio so I couldn’t get aplay to work till I reworked my audio profile to include gid.
Also, it appeared that lxc config set mycontainer raw.lxc is not required, at lest speaker-test works without it.

So for anyone interested (I’m using RaspberryPi 4, so it probably should work for others)

config:
  user.user-data: |
    #cloud-config
    packages:
      - alsa-utils
description: ALSA LXD profile
devices:
  controlC0:
	gid: "29"
    path: /dev/snd/controlC0
    type: unix-char
  controlC1:
	gid: "29"
    path: /dev/snd/controlC1
    type: unix-char
  pcmC0D0p:
	gid: "29"
    path: /dev/snd/pcmC0D0p
    type: unix-char
  pcmC1D0p:
    gid: "29"
    path: /dev/snd/pcmC1D0p
    type: unix-char
  seq:
	gid: "29"
    path: /dev/snd/seq
    type: unix-char
  timer:
	gid: "29"
    path: /dev/snd/timer
    type: unix-char
name: audio-alsa
used_by:

On my host /dev/snd root:audio and I do use mounting:
lxc config set mycontainer raw.lxc "lxc.mount.entry =

One thing that I added to the configuration is mode: "0666"

So it looks like this:

  seq:
    mode: "0666"
    path: /dev/snd/seq
    type: unix-char

:slight_smile: "mode: “0666” - that explains.
It works, but, probably, not inline with recommendations, as host’s policy is “0660” for those. Ok, tnx!

I know it is dirty but I trust my container, there is nothing to worry about, it is just a /snd :wink:

true