podman icon indicating copy to clipboard operation
podman copied to clipboard

Devices are not mounted inside rootless container and groups are not properly ported

Open dozenposture opened this issue 1 year ago • 12 comments

Issue Description

I can't mount devices inside a podman container when rootless. I tried the same quadlet file with rootful and I'm able to use mounted devices.

I'm using quadlet and this is the content of the file:

[Unit]
Description=Tdarr container
After=local-fs.target

[Container]
Image=ghcr.io/haveagitgat/tdarr:latest
ContainerName=tdarr
AutoUpdate=registry
Environment=PUID=1002
Environment=PGID=1002
Environment=UMASK=002
Environment=TZ=Europe/Amsterdam
Environment=serverIP=0.0.0.0
Environment=serverPort=8266
Environment=webUIPort=8265
Environment=internalNode=true
Environment=inContainer=true
Environment=ffmpegVersion=6
Environment=nodeName=MyInternalNode
PublishPort=8265:8265
PublishPort=8266:8266
Volume=${CONFIG_DIR}/tdarr/server:/app/server:Z
Volume=${CONFIG_DIR}/tdarr/configs:/app/configs:Z
Volume=${CONFIG_DIR}/tdarr/logs:/app/logs:Z
Volume=${CONFIG_DIR}/tdarr/transcode_cache:/temp:Z
Volume=${DATA_DIR}/test:/media:z
AddDevice=/dev/dri:/dev/dri
#Network=pqsw.network
User=0
UserNS=keep-id
GroupAdd=keep-groups
HealthStartPeriod=1m
HealthCmd=CMD-SHELL curl -f --insecure http://localhost:8265 || exit 1
HealthInterval=30s
HealthRetries=2
HealthOnFailure=kill

[Service]
Restart=on-failure

[Install]
WantedBy=multi-user.target default.target

User podman is member of groups render and video:

podman@host~> groups
podman video render

And this are the devices on the host:

podman@host:~> ls -l /dev/dri
total 0
drwxr-xr-x. 2 root root         80 Jan 11 11:26 by-path
crw-rw----. 1 root video  226,   1 Jan 11 11:26 card1
crw-rw----. 1 root render 226, 128 Jan 11 11:26 renderD128

Steps to reproduce the issue

Steps to reproduce the issue

  1. Add user to video and render group
  2. Lauch rootles container with GroupAdd=keep-groups directive
  3. Run podman inspect tdarr and see no devices are mounted

Describe the results you received

When executing podman inspect tdarr I don't see any device:

...
"CpusetMems": "",
"Devices": [],
"DiskQuota": 0,
...

Moreover, when inspecting the container, I don't see the groups video and render begin ported inside the container:

podman@host:~> podman exec -it tdarr bash
root@afbf943fadda:/# cd /dev/dri/
root@afbf943fadda:/dev/dri# ls -l
total 0
drwxr-xr-x. 2 nobody nogroup       80 Jan 11 11:26 by-path
crw-rw----. 1 nobody nogroup 226,   1 Jan 11 11:26 card1
crw-rw----. 1 nobody nogroup 226, 128 Jan 11 11:26 renderD128

This is how it looks on the host:

podman@host:~> cd /dev/dri
podman@host:/dev/dri> ls -l
total 0
drwxr-xr-x. 2 root root         80 Jan 11 11:26 by-path
crw-rw----. 1 root video  226,   1 Jan 11 11:26 card1
crw-rw----. 1 root render 226, 128 Jan 11 11:26 renderD128

Describe the results you expected

I would expect to have groups video and render inside the container. I would expect to see devices present when running podman inspect tdarr

When rootful, it looks like this:

"Devices": [
                    {
                         "PathOnHost": "/dev/dri/card1",
                         "PathInContainer": "/dev/dri/card1",
                         "CgroupPermissions": ""
                    },
                    {
                         "PathOnHost": "/dev/dri/renderD128",
                         "PathInContainer": "/dev/dri/renderD128",
                         "CgroupPermissions": ""
                    }
               ],

podman info output

@host:~> podman info
host:
  arch: amd64
  buildahVersion: 1.38.0
  cgroupControllers:
  - pids
  cgroupManager: systemd
  cgroupVersion: v2
  conmon:
    package: conmon-2.1.12-1.1.x86_64
    path: /usr/bin/conmon
    version: 'conmon version 2.1.12, commit: unknown'
  cpuUtilization:
    idlePercent: 72.94
    systemPercent: 14.22
    userPercent: 12.83
  cpus: 4
  databaseBackend: sqlite
  distribution:
    distribution: opensuse-tumbleweed
    version: "20250108"
  eventLogger: journald
  freeLocks: 2016
  hostname: host
  idMappings:
    gidmap:
    - container_id: 0
      host_id: 1002
      size: 1
    - container_id: 1
      host_id: 231072
      size: 65536
    uidmap:
    - container_id: 0
      host_id: 1002
      size: 1
    - container_id: 1
      host_id: 231072
      size: 65536
  kernel: 6.12.8-2-default
  linkmode: dynamic
  logDriver: journald
  memFree: 173830144
  memTotal: 7983931392
  networkBackend: netavark
  networkBackendInfo:
    backend: netavark
    dns:
      package: aardvark-dns-1.13.1-1.1.x86_64
      path: /usr/libexec/podman/aardvark-dns
      version: aardvark-dns 1.13.1
    package: netavark-1.13.1-1.1.x86_64
    path: /usr/libexec/podman/netavark
    version: netavark 1.13.1
  ociRuntime:
    name: crun
    package: crun-1.19-1.1.x86_64
    path: /usr/bin/crun
    version: |-
      crun version 1.19
      commit: db31c42ac46e20b5527f5339dcbf6f023fcd539c
      rundir: /run/user/1002/crun
      spec: 1.0.0
      +SYSTEMD +SELINUX +APPARMOR +CAP +SECCOMP +EBPF +CRIU +LIBKRUN +WASM:wasmedge +YAJL
  os: linux
  pasta:
    executable: /usr/bin/pasta
    package: passt-20241211.09478d5-1.1.x86_64
    version: |
      pasta 20241211.09478d5-1.1
      Copyright Red Hat
      GNU General Public License, version 2 or later
        <https://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
      This is free software: you are free to change and redistribute it.
      There is NO WARRANTY, to the extent permitted by law.
  remoteSocket:
    exists: true
    path: /run/user/1002/podman/podman.sock
  rootlessNetworkCmd: pasta
  security:
    apparmorEnabled: false
    capabilities: CAP_CHOWN,CAP_DAC_OVERRIDE,CAP_FOWNER,CAP_FSETID,CAP_KILL,CAP_NET_BIND_SERVICE,CAP_SETFCAP,CAP_SETGID,CAP_SETPCAP,CAP_SETUID,CAP_SYS_CHROOT
    rootless: true
    seccompEnabled: true
    seccompProfilePath: /usr/share/containers/seccomp.json
    selinuxEnabled: true
  serviceIsRemote: false
  slirp4netns:
    executable: ""
    package: ""
    version: ""
  swapFree: 5508030464
  swapTotal: 7983927296
  uptime: 34h 18m 11.00s (Approximately 1.42 days)
  variant: ""
plugins:
  authorization: null
  log:
  - k8s-file
  - none
  - passthrough
  - journald
  network:
  - bridge
  - macvlan
  - ipvlan
  volume:
  - local
registries:
  search:
  - registry.opensuse.org
  - registry.suse.com
  - docker.io
store:
  configFile: /home/podman/.config/containers/storage.conf
  containerStore:
    number: 28
    paused: 0
    running: 26
    stopped: 2
  graphDriverName: overlay
  graphOptions: {}
  graphRoot: /home/podman/.local/share/containers/storage
  graphRootAllocated: 255522574336
  graphRootUsed: 40699297792
  graphStatus:
    Backing Filesystem: btrfs
    Native Overlay Diff: "true"
    Supports d_type: "true"
    Supports shifting: "false"
    Supports volatile: "true"
    Using metacopy: "false"
  imageCopyTmpDir: /var/tmp
  imageStore:
    number: 26
  runRoot: /run/user/1002/containers
  transientStore: false
  volumePath: /home/podman/.local/share/containers/storage/volumes
version:
  APIVersion: 5.3.1
  Built: 1733485830
  BuiltTime: Fri Dec  6 12:50:30 2024
  GitCommit: ""
  GoVersion: go1.23.4
  Os: linux
  OsArch: linux/amd64
  Version: 5.3.1

Podman in a container

No

Privileged Or Rootless

Rootless

Upstream Latest Release

Yes

Additional environment details

Additional environment details

Additional information

Additional information like issue happens only occasionally or issue happens with a particular architecture or on a particular setting

dozenposture avatar Jan 12 '25 21:01 dozenposture

I would expect to have groups video and render inside the container. I would expect to see devices present when running podman inspect tdarr

In an unprivileged user namespace, host GIDs cannot be mapped, unless the user has access to them (i.e. added to /etc/subuid and /etc/subgid). Any files or processes with unmapped GIDs will appear as owned by nobody:nogroup within the namespace.

If your user had access to the devices on the host and the access was granted through groups membership, then it should still have it inside the user namespace with keep-groups.

Can the user access the devices?

giuseppe avatar Jan 13 '25 09:01 giuseppe

Same for me, previously it worked, but nowadays, since 1 or 2 weeks, my groups are not mapped even if I specify the keep-groups option. My user is member of 4 groups on the host, but has only one group in the container. Also the desired group (render) isn't in the /etc/subgid file. I have podman v4.9.3 on Ubuntu 24.04.

I'm curious why did it work earlier. Isn't there a way to use GPU devices in rootless containers?

UPDATE: I've solved it. In my configuration I also set the User and Group ids additionaly to the UserNS and AddGroup settings, when I removed the User and Group configs, the keep-groups setting worked and my user could access the gpu devices.

Breinich avatar Jan 25 '25 13:01 Breinich

@Breinich do you mind sharing your quadlet file? I'm getting crazy and I still can't get the iGPU to work with a rootless container.

dozenposture avatar Feb 10 '25 14:02 dozenposture

@dozenposture sure, here it is:

[Unit]
Description=Jellyfin media service
RequiresMountsFor=/mnt/data

[Container]
Image=docker.io/jellyfin/jellyfin:latest
AutoUpdate=registry
UserNS=keep-id
PodmanArgs=--group-add keep-groups
#GroupAdd=keep-groups
PublishPort=8096:8096/tcp
AddDevice=/dev/dri/:/dev/dri/
Volume=/.../config:/config
Volume=/.../cache:/cache
Volume=/mnt/data/media:/data/media
Timezone=Europe/Budapest

[Service]
# Inform systemd of additional exit status
SuccessExitStatus=0 143
Restart=always

[Install]
# Start by default on boot
WantedBy=default.target

Also note, that the user, who runs this container, is member of the video and render groups, this is essential.

And also note that the groups inside the container won't have the original name of render and video, instead something else, so you should not check if the device is available via only looking at the group names.

Breinich avatar Feb 10 '25 16:02 Breinich

Hi @Breinich do you know why you had to use podman args instead of the GroupAdd=keep-groups? This fixed it for me (I was trying to use GroupAdd=keep-groups) but it seems weird. I tried searching if this was added recently and that maybe my podman version is too old but no luck. I appreciate your help figuring out my issue (now I can remove global access to the renderer) but now it leaves me wondering why

EDIT: found my answer just manually searching release notes finally found it was added in 5.1 and I'm still on 4.9 https://github.com/containers/podman/releases/tag/v5.1.0

defron avatar May 23 '25 03:05 defron

Yes, that's the case.

Breinich avatar May 23 '25 23:05 Breinich

If you're on Ubuntu, a dist-upgrade could solve your issue with that, on 25.04 the 5.4 version of Podman is available.

Breinich avatar May 25 '25 13:05 Breinich

I have come across this myself and have not found a solution. I was seeing it in a Jellyfin container as above but I have put together a more minimal example using plain Podman, without the systemd integration, and avoiding Jellyfin for now.

I am running a BeeLink EQ14 (Intel N150) with:

  • Podman version: 5.6.0
  • OS: openSUSE MicroOS Tumbleweed
  • Kernel: 6.16.3-1-default
  • SELinux disabled with enforcing=0

Working on host

On the system I have the device showing up as:

$ ls -hl /dev/dri/
total 0
drwxr-xr-x. 2 root root         80 Sep  6 00:53 by-path
crw-rw----. 1 root video  226,   1 Sep  6 00:53 card1
crw-rw----. 1 root render 226, 128 Sep  6 00:53 renderD128

and with a user myuser in the render group (as a secondary):

$ id
uid=1001(myuser) gid=1001(mygroup) groups=1001(mygroup),485(video),488(render) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

I can use the vainfo tool to query the device:

$ vainfo --display drm
Trying display: drm
libva info: VA-API version 1.22.0
libva info: Trying to open /usr/lib64/dri/iHD_drv_video.so
libva info: Found init function __vaDriverInit_1_22
libva info: va_openDriver() returns 0
vainfo: VA-API version: 1.22 (libva 2.22.0)
vainfo: Driver version: Intel iHD driver for Intel(R) Gen Graphics - 25.2.6 ()
vainfo: Supported profile and entrypoints
      VAProfileNone                   : VAEntrypointVideoProc
      VAProfileNone                   : VAEntrypointStats
      ...

Failing on rootless Podman

I put together a simple Containerfile to test it (note the CMD is vainfo --display drm):

FROM alpine:3.22
RUN echo https://dl-cdn.alpinelinux.org/alpine/v3.22/community/ >> /etc/apk/repositories
RUN apk add libva-utils intel-media-driver
CMD ["vainfo", "--display", "drm"]

which I build and run:

$ podman build --tag test .
$ podman run --userns keep-id --group-add keep-groups --device /dev/dri/renderD128 test
Trying display: drm
error: failed to initialize display 'drm'

Alternatives (also not working)

I have tried a number of other combinations of arguments to podman:

  • podman run --userns keep-id --group-add keep-groups --device /dev/dri/renderD128 test
  • podman run --group-add keep-groups --device /dev/dri/renderD128 test
  • podman run --userns keep-id --device /dev/dri/renderD128 test
  • podman run --userns keep-id --group-add 488 --device /dev/dri/renderD128 test
  • podman run --userns keep-id --group-add keep-groups --device /dev/dri/ test
  • podman run --userns keep-id --group-add keep-groups --device /dev/dri/:/dev/dri/ test

but they all give identical results.

Details

For the record, my `subuid` and `subgid` are: ```console $ cat /etc/subuid matt:100000:65536 root:400000000:500000001 myuser:165536:65536 $ cat /etc/subgid matt:100000:65536 root:400000000:500000001 myuser:165536:65536 ```
and running `id` inside the container gives: ```console $ podman run --userns keep-id --group-add keep-groups --device /dev/dri/renderD128 test id uid=1001(myuser) gid=1001(mygroup) groups=1001(mygroup) $ podman run --userns keep-id --group-add 488 --device /dev/dri/renderD128 test id uid=1001(myuser) gid=1001(mygroup) groups=1001(mygroup),488 ```
and showing that SELinux is disabled: ```console $ sestatus SELinux status: enabled SELinuxfs mount: /sys/fs/selinux SELinux root directory: /etc/selinux Loaded policy name: targeted Current mode: permissive Mode from config file: enforcing Policy MLS status: enabled Policy deny_unknown status: allowed Memory protection checking: actual (secure) Max kernel policy version: 34 ```

Other tests

I can test a similar setup with volumes where I create a directory on the host system with similar permissions:

$ ls -hl /var/foo
total 4.0K
-rw-r--r--. 1 root render 5 Sep  6 13:57 bar
$ cat /var/foo/bar 
blah

and mounting this as a volume and cating it in the container works fine:

$ podman run --userns keep-id --group-add keep-groups --volume /var/foo:/foo test cat /foo/bar
blah

But trying to mount the device a a volume does not work:

$ podman run --userns keep-id --group-add keep-groups --volume /dev/dri/renderD128 test
Trying display: drm
error: failed to initialize display 'drm'

Hack solution

If I edit the ownership of /dev/dri/renderD128 to either be owned by the user myuser or by the group mygroup (the user's primary group) then the podman run ... command works.

$ ls -hl /dev/dri/renderD128 
crw-rw----. 1 root mygroup 226, 128 Sep  6 00:53 /dev/dri/renderD128
$ podman run --userns keep-id --group-add keep-groups --device /dev/dri/renderD128 test
Trying display: drm
libva info: VA-API version 1.22.0
...

It also works if I build the container and run it as root and avoid the myuser account entirely.

Summary

It seems that the secondary groups of the user are not being mapped into the container as I would expect. Or at least the processes inside the container are not gaining the privileges of them. However this only seems to be affecting devices and not normal volume mounts.

milliams avatar Sep 06 '25 13:09 milliams

It's been a few months, so sorry to shamelessly bump, but did anybody manage to solve the issue? Is there any non-hacky way to actually properly mount the devices (such as /dev/kfd) into a rootless-container, where file ownership is sometrhing else than nobody and nogroup?

Battlesheepu avatar Nov 27 '25 22:11 Battlesheepu

Yes, for me, my previously posted solution worked fine, however I don't know what could be wrong with @milliams 's setup

Breinich avatar Nov 28 '25 16:11 Breinich

I've now solved it for my use case. The problem was that Podman was using runc as the runtime rather than runc. It seems that the --group-add keep-groups flag is only supported on crun (this is documented but I either didn't notice it or didn't understand it since I'm new to Podman).

Now, with a containers.conf of:

[engine]
runtime="crun"

I can do podman run --userns keep-id --group-add keep-groups --device /dev/dri/renderD128 test (based on my above containers) and it works correctly:

libva info: VA-API version 1.22.0
Trying display: drm
libva info: Trying to open /usr/lib/dri/iHD_drv_video.so
libva info: Found init function __vaDriverInit_1_22
libva info: va_openDriver() returns 0
vainfo: VA-API version: 1.22 (libva 2.22.0)
vainfo: Driver version: Intel iHD driver for Intel(R) Gen Graphics - 25.2.6 ()
vainfo: Supported profile and entrypoints
...

but I note that the ls listing for the device in the container still shows nobody:

$ podman run --userns keep-id --group-add keep-groups --device /dev/dri/renderD128 test ls -hl /dev/dri
total 0      
crw-rw----    1 nobody   nobody    226, 128 Dec 19 12:51 renderD128

but I don't think that matters.

In summary: use crun as the Podman runtime.

milliams avatar Dec 19 '25 13:12 milliams

you need to chmod the directory you are trying to use like /dev/dri so that rootless podman can read it. I think 700 may work but I just make it sudo chmod -R 777 /dev/dri and be done with it. (note, security experts are crying right now)

upmcplanetracker avatar Jul 04 '26 19:07 upmcplanetracker