LXC Audio / Sound

Hi there,

I’m trying to set up an LXC container for Gui-Apps. I followed the blog of stgraber (https://stgraber.org/2014/02/09/lxc-1-0-gui-in-containers/) and the programs start.
But the sound is not working.
My host is running Void Linux with kernel 5.7.11_1

I created a debian sid container with lxc-create, installed nvidia drivers. It is an unprivileged container.
Then i tried to play some sound via
aplay test.wav
and that worked. Because many programs rely on pulseaudio, I then installed pulseaudio. There is no sound when pulseaudio is installed, and it only shows the “Dummy Output” as available devices.

I hope you can help me getting the sound work inside the container.

Container Config:

# Template used to create this container: /usr/share/lxc/templates/lxc-download
# Parameters passed to the template:
# Template script checksum (SHA-1): 9893b2e0dba7be0d74cf38537bebe0af939c269c
# For additional config options, please look at lxc.container.conf(5)
# Uncomment the following line to support nesting containers:
#lxc.include = /usr/share/lxc/config/nesting.conf
# (Be aware this has security implications)

#bind-mounts for nvidia proprietary driver

# Distribution configuration
lxc.include = /usr/share/lxc/config/common.conf
lxc.include = /usr/share/lxc/config/userns.conf
lxc.arch = linux64

# Container specific configuration
#lxc.mount.entry = tmpfs tmp tmpfs defaults
#lxc.mount.entry = /dev/nvidia-uvm dev/nvidia-uvm none bind,optional,create=file

lxc.mount.entry = /dev/dri dev/dri none bind,optional,create=dir
lxc.mount.entry = /dev/snd dev/snd none bind,optional,create=dir
lxc.mount.entry = /tmp/.X11-unix tmp/.X11-unix none bind,optional,create=dir

lxc.mount.entry = /dev/nvidia0 dev/nvidia0 none bind,optional,create=file
lxc.mount.entry = /dev/nvidiactl dev/nvidiactl none bind,optional,create=file
lxc.mount.entry = /dev/nvidia-modeset dev/nvidia-modeset none bind,optional,create=file

#audio test
#lxc.mount.entry = /home/k/.config/pulse home/k/.config/pulse none bind,optional,create=dir
#lxc.mount.entry = /run/user/1000/pulse/native tmp/pulse/native none rw,bind,create=file 0 0
#lxc.environment = PULSE_SERVER=unix:/tmp/pulse/native
#lxc.cgroup.devices.allow = c 116:* rwm


lxc.idmap = u 0 100000 1000
lxc.idmap = g 0 100000 1000
lxc.idmap = u 1000 1000 1
lxc.idmap = g 1000 1000 1
lxc.idmap = u 1001 101001 64535
lxc.idmap = g 1001 101001 64535


lxc.hook.pre-start=/home/k/.config/lxc/preStartHook.sh
lxc.rootfs.path = dir:/home/k/.local/share/lxc/sid/rootfs
lxc.uts.name = sid

# Network configuration
lxc.net.0.type = veth
lxc.net.0.link = virbr0
lxc.net.0.flags = up

preStartHook.sh:
#!/bin/sh

  mountpoint -q "/sys/fs/cgroup/systemd" \
   || (sudo mkdir -p /sys/fs/cgroup/systemd && sudo mount -t cgroup cgroup -o none,name=systemd /sys/fs/cgroup/systemd)

  for d in /sys/fs/cgroup/*; do
    f=$(basename $d)
    if [ "$f" = "unified" ]; then
        continue
    elif [ "$f" = "cpuset" ]; then
        echo 1 | sudo tee -a $d/cgroup.clone_children;
    elif [ "$f" = "memory" ]; then
        echo 1 | sudo tee -a $d/memory.use_hierarchy;
    fi
    sudo mkdir -p $d/$USER
    sudo chown -R $USER $d/$USER
    echo $PPID > $d/$USER/tasks
  done

PULSE_PATH=$LXC_ROOTFS_PATH/home/k/.pulse_socket

if [ ! -e "$PULSE_PATH" ] || [ -z "$(lsof -n $PULSE_PATH 2>&1)" ]; then
    pactl load-module module-native-protocol-unix auth-anonymous=1 socket=$PULSE_PATH
fi

aplay -L

default
    Playback/recording through the PulseAudio sound server
surround21
    2.1 Surround output to Front and Subwoofer speakers
surround40
    4.0 Surround output to Front and Rear speakers
surround41
    4.1 Surround output to Front, Rear and Subwoofer speakers
surround50
    5.0 Surround output to Front, Center and Rear speakers
surround51
    5.1 Surround output to Front, Center, Rear and Subwoofer speakers
surround71
    7.1 Surround output to Front, Center, Side, Rear and Woofer speakers
null
    Discard all samples (playback) or generate zero samples (capture)
lavrate
    Rate Converter Plugin Using Libav/FFmpeg Library
samplerate
    Rate Converter Plugin Using Samplerate Library
speexrate
    Rate Converter Plugin Using Speex Resampler
jack
    JACK Audio Connection Kit
oss
    Open Sound System
pulse
    PulseAudio Sound Server
upmix
    Plugin for channel upmix (4,6,8)
vdownmix
    Plugin for channel downmix (stereo) with a simple spacialization
hdmi:CARD=NVidia,DEV=0
    HDA NVidia, HDMI 0
    HDMI Audio Output
hdmi:CARD=NVidia,DEV=1
    HDA NVidia, HDMI 1
    HDMI Audio Output
hdmi:CARD=NVidia,DEV=2
    HDA NVidia, HDMI 2
    HDMI Audio Output
hdmi:CARD=NVidia,DEV=3
    HDA NVidia, HDMI 3
    HDMI Audio Output
hdmi:CARD=NVidia,DEV=4
    HDA NVidia, HDMI 4
    HDMI Audio Output
hdmi:CARD=NVidia,DEV=5
    HDA NVidia, HDMI 5
    HDMI Audio Output
hdmi:CARD=NVidia,DEV=6
    HDA NVidia, HDMI 6
    HDMI Audio Output
dmix:CARD=NVidia,DEV=3
    HDA NVidia, HDMI 0
    Direct sample mixing device
dmix:CARD=NVidia,DEV=7
    HDA NVidia, HDMI 1
    Direct sample mixing device
dmix:CARD=NVidia,DEV=8
    HDA NVidia, HDMI 2
    Direct sample mixing device
dmix:CARD=NVidia,DEV=9
    HDA NVidia, HDMI 3
    Direct sample mixing device
dmix:CARD=NVidia,DEV=10
    HDA NVidia, HDMI 4
    Direct sample mixing device
dmix:CARD=NVidia,DEV=11
    HDA NVidia, HDMI 5
    Direct sample mixing device
dmix:CARD=NVidia,DEV=12
    HDA NVidia, HDMI 6
    Direct sample mixing device
dsnoop:CARD=NVidia,DEV=3
    HDA NVidia, HDMI 0
    Direct sample snooping device
dsnoop:CARD=NVidia,DEV=7
    HDA NVidia, HDMI 1
    Direct sample snooping device
dsnoop:CARD=NVidia,DEV=8
    HDA NVidia, HDMI 2
    Direct sample snooping device
dsnoop:CARD=NVidia,DEV=9
    HDA NVidia, HDMI 3
    Direct sample snooping device
dsnoop:CARD=NVidia,DEV=10
    HDA NVidia, HDMI 4
    Direct sample snooping device
dsnoop:CARD=NVidia,DEV=11
    HDA NVidia, HDMI 5
    Direct sample snooping device
dsnoop:CARD=NVidia,DEV=12
    HDA NVidia, HDMI 6
    Direct sample snooping device
hw:CARD=NVidia,DEV=3
    HDA NVidia, HDMI 0
    Direct hardware device without any conversions
hw:CARD=NVidia,DEV=7
    HDA NVidia, HDMI 1
    Direct hardware device without any conversions
hw:CARD=NVidia,DEV=8
    HDA NVidia, HDMI 2
    Direct hardware device without any conversions
hw:CARD=NVidia,DEV=9
    HDA NVidia, HDMI 3
    Direct hardware device without any conversions
hw:CARD=NVidia,DEV=10
    HDA NVidia, HDMI 4
    Direct hardware device without any conversions
hw:CARD=NVidia,DEV=11
    HDA NVidia, HDMI 5
    Direct hardware device without any conversions
hw:CARD=NVidia,DEV=12
    HDA NVidia, HDMI 6
    Direct hardware device without any conversions
plughw:CARD=NVidia,DEV=3
    HDA NVidia, HDMI 0
    Hardware device with all software conversions
plughw:CARD=NVidia,DEV=7
    HDA NVidia, HDMI 1
    Hardware device with all software conversions
plughw:CARD=NVidia,DEV=8
    HDA NVidia, HDMI 2
    Hardware device with all software conversions
plughw:CARD=NVidia,DEV=9
    HDA NVidia, HDMI 3
    Hardware device with all software conversions
plughw:CARD=NVidia,DEV=10
    HDA NVidia, HDMI 4
    Hardware device with all software conversions
plughw:CARD=NVidia,DEV=11
    HDA NVidia, HDMI 5
    Hardware device with all software conversions
plughw:CARD=NVidia,DEV=12
    HDA NVidia, HDMI 6
    Hardware device with all software conversions
usbstream:CARD=NVidia
    HDA NVidia
    USB Stream Output
sysdefault:CARD=Generic
    HD-Audio Generic, ALC892 Analog
    Default Audio Device
front:CARD=Generic,DEV=0
    HD-Audio Generic, ALC892 Analog
    Front speakers
surround21:CARD=Generic,DEV=0
    HD-Audio Generic, ALC892 Analog
    2.1 Surround output to Front and Subwoofer speakers
surround40:CARD=Generic,DEV=0
    HD-Audio Generic, ALC892 Analog
    4.0 Surround output to Front and Rear speakers
surround41:CARD=Generic,DEV=0
    HD-Audio Generic, ALC892 Analog
    4.1 Surround output to Front, Rear and Subwoofer speakers
surround50:CARD=Generic,DEV=0
    HD-Audio Generic, ALC892 Analog
    5.0 Surround output to Front, Center and Rear speakers
surround51:CARD=Generic,DEV=0
    HD-Audio Generic, ALC892 Analog
    5.1 Surround output to Front, Center, Rear and Subwoofer speakers
surround71:CARD=Generic,DEV=0
    HD-Audio Generic, ALC892 Analog
    7.1 Surround output to Front, Center, Side, Rear and Woofer speakers
iec958:CARD=Generic,DEV=0
    HD-Audio Generic, ALC892 Digital
    IEC958 (S/PDIF) Digital Audio Output
dmix:CARD=Generic,DEV=0
    HD-Audio Generic, ALC892 Analog
    Direct sample mixing device
dmix:CARD=Generic,DEV=1
    HD-Audio Generic, ALC892 Digital
    Direct sample mixing device
dsnoop:CARD=Generic,DEV=0
    HD-Audio Generic, ALC892 Analog
    Direct sample snooping device
dsnoop:CARD=Generic,DEV=1
    HD-Audio Generic, ALC892 Digital
    Direct sample snooping device
hw:CARD=Generic,DEV=0
    HD-Audio Generic, ALC892 Analog
    Direct hardware device without any conversions
hw:CARD=Generic,DEV=1
    HD-Audio Generic, ALC892 Digital
    Direct hardware device without any conversions
plughw:CARD=Generic,DEV=0
    HD-Audio Generic, ALC892 Analog
    Hardware device with all software conversions
plughw:CARD=Generic,DEV=1
    HD-Audio Generic, ALC892 Digital
    Hardware device with all software conversions
usbstream:CARD=Generic
    HD-Audio Generic
    USB Stream Output
sysdefault:CARD=U192k
    UMC202HD 192k, USB Audio
    Default Audio Device
front:CARD=U192k,DEV=0
    UMC202HD 192k, USB Audio
    Front speakers
surround21:CARD=U192k,DEV=0
    UMC202HD 192k, USB Audio
    2.1 Surround output to Front and Subwoofer speakers
surround40:CARD=U192k,DEV=0
    UMC202HD 192k, USB Audio
    4.0 Surround output to Front and Rear speakers
surround41:CARD=U192k,DEV=0
    UMC202HD 192k, USB Audio
    4.1 Surround output to Front, Rear and Subwoofer speakers
surround50:CARD=U192k,DEV=0
    UMC202HD 192k, USB Audio
    5.0 Surround output to Front, Center and Rear speakers
surround51:CARD=U192k,DEV=0
    UMC202HD 192k, USB Audio
    5.1 Surround output to Front, Center, Rear and Subwoofer speakers
surround71:CARD=U192k,DEV=0
    UMC202HD 192k, USB Audio
    7.1 Surround output to Front, Center, Side, Rear and Woofer speakers
iec958:CARD=U192k,DEV=0
    UMC202HD 192k, USB Audio
    IEC958 (S/PDIF) Digital Audio Output
dmix:CARD=U192k,DEV=0
    UMC202HD 192k, USB Audio
    Direct sample mixing device
dsnoop:CARD=U192k,DEV=0
    UMC202HD 192k, USB Audio
    Direct sample snooping device
hw:CARD=U192k,DEV=0
    UMC202HD 192k, USB Audio
    Direct hardware device without any conversions
plughw:CARD=U192k,DEV=0
    UMC202HD 192k, USB Audio
    Hardware device with all software conversions
usbstream:CARD=U192k
    UMC202HD 192k
    USB Stream Output

pacmd list-cards
0 card(s) available.

Please can you show output of ls -la /dev/snd in container?

ls -la /dev/snd in container:

total 0
drwxr-xr-x  4 nobody nogroup     480 Aug  3 13:40 .
drwxr-xr-x  7 root   root        600 Aug  3 13:52 ..
drwxr-xr-x  2 nobody nogroup      60 Aug  3 13:40 by-id
drwxr-xr-x  2 nobody nogroup     100 Aug  3 13:40 by-path
crw-rw----+ 1 nobody nogroup 116, 10 Aug  3 13:40 controlC0
crw-rw----+ 1 nobody nogroup 116, 16 Aug  3 13:40 controlC1
crw-rw----+ 1 nobody nogroup 116, 19 Aug  3 13:40 controlC2
crw-rw----+ 1 nobody nogroup 116,  9 Aug  3 13:40 hwC0D0
crw-rw----+ 1 nobody nogroup 116, 15 Aug  3 13:40 hwC1D0
crw-rw----+ 1 nobody nogroup 116,  6 Aug  3 13:40 pcmC0D10p
crw-rw----+ 1 nobody nogroup 116,  7 Aug  3 13:40 pcmC0D11p
crw-rw----+ 1 nobody nogroup 116,  8 Aug  3 13:40 pcmC0D12p
crw-rw----+ 1 nobody nogroup 116,  2 Aug  3 13:40 pcmC0D3p
crw-rw----+ 1 nobody nogroup 116,  3 Aug  3 13:40 pcmC0D7p
crw-rw----+ 1 nobody nogroup 116,  4 Aug  3 13:40 pcmC0D8p
crw-rw----+ 1 nobody nogroup 116,  5 Aug  3 13:40 pcmC0D9p
crw-rw----+ 1 nobody nogroup 116, 12 Aug  3 13:40 pcmC1D0c
crw-rw----+ 1 nobody nogroup 116, 11 Aug  3 13:50 pcmC1D0p
crw-rw----+ 1 nobody nogroup 116, 13 Aug  3 13:40 pcmC1D1p
crw-rw----+ 1 nobody nogroup 116, 14 Aug  3 13:40 pcmC1D2c
crw-rw----+ 1 nobody nogroup 116, 18 Aug  3 13:40 pcmC2D0c
crw-rw----+ 1 nobody nogroup 116, 17 Aug  3 13:40 pcmC2D0p
crw-rw----+ 1 nobody nogroup 116,  1 Aug  3 13:40 seq
crw-rw----+ 1 nobody nogroup 116, 33 Aug  3 13:40 timer

It might be a permissions issue as the sound devices haven’t been UID shifted into the container’s UID range.

What I have done in the past with mpd running as non-privileged user is to recreate the sound devices I wanted on the host in /var/lib/lxc/<container>/snd/ and then chown them to the UID of your container’s root, then use those devices as pass through into your container.

That way they will appear then as the correct UID in your container.

1 Like

I currently have the same problem and the only alternative solution that is told everywhere is to uid shift uid 1000 into the container, but that would give access of the entire uid 1000 namespace to the container right?

I have tried chowning with a post-mount hook, with a setuid binary with execve(), but inside the container the file is still nobody:nogroup and chowning inside the container is denied.

What do you mean with “recreating the device”?
Do you not mean mounting,
but using “mknod” or “cp -a” and chowning that?

Edit:
Can confirm that works for getting root:root inside the container,
without uid shifting etc (I guess it actually is a uid shift, by chowning)