4

Is there a way to get the cgroup root path(s) for a podman container?

It seems podman inspect does not reveal anything about cgroups.

One "hacky" way I've found is to do something like

# Run the container, writing its container ID to /tmp/cid
podman run --rm -it --cidfile=/tmp/cid busybox

# Search /sys/fs/cgroup for the CID
cid=$(cat /tmp/cid)
find /sys/fs/cgroup | grep "/libpod-$cid/" | grep '/cpu,cpuacct/' | grep '/cpuacct.usage'
# prints something like: /sys/fs/cgroup/cpu,cpuacct/libpod_parent/libpod-abc123.../cpuacct.usage

But I am not happy with this solution, because:

  • It requires an inefficient search (find)
  • I'm not sure whether the search path of /sys/fs/cgroup is guaranteed to be correct
  • If cgroup v2 is in use, the file I am looking for might have a different basename (for example, it might be memory.current instead of memory.usage_in_bytes for cgroup v2)

Is there a more direct way using podman or perhaps one of the Go SDKs to get this information?

2 Answers 2

1

All you need to know is the container ID. There's no need to use find or grep; you can construct the explicit path for what you want just by insert the container id in the right place.

With cgroupsv2, the base path is /sys/fs/cgroup/machine.slice/libpod-<container_id>.scope. Under that directory, you'll find:

cgroup...
container...
cpu...
cpuset...
hugetlb...
io...
memory...
pids...

With cgroupvs1, the path is /sys/fs/cgroup/<cgroup>/machine.slice/libpod-<container_id>.scope. So if you want the cpu,cpuacct cgroup, you ask for:

$ ls /sys/fs/cgroup/cpu,cpuacct/machine.slice/libpod-756bf46cd9ff1f509f65d063b69e42a5a560b9d29608ec48ec7466880b26d65e.scope
cgroup.clone_children
cgroup.procs
cpu.cfs_period_us
cpu.cfs_quota_us
cpu.rt_period_us
cpu.rt_runtime_us
cpu.shares
cpu.stat
cpuacct.stat
cpuacct.usage
cpuacct.usage_all
cpuacct.usage_percpu
cpuacct.usage_percpu_sys
cpuacct.usage_percpu_user
cpuacct.usage_sys
cpuacct.usage_user
notify_on_release
tasks
2
  • if it is rootless the folder is not correct
    – larrycai
    Commented Sep 3, 2022 at 20:35
  • 1
    That's true, but a simple find /sys/fs/cgroup |grep <pod id> will help you find it. With cgroups v2, that will be /sys/fs/cgroup/user.slice/user-<UID>.slice/user@<UID>.service/user.slice/libpod-<PODID>
    – larsks
    Commented Sep 3, 2022 at 21:34
1
+50

With recent podman builds, the following should give you the cgroup path: podman inspect --format '{{.State.CgroupPath}}' <container-id>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .