Skip to content

Support cgroup v2 Unified Mode #1

Description

@Pijukatel

The system should be able to correctly report the maximum available memory when restricted through cgroups.

Scenario examples:

1 Example invocation without a container:
systemd-run --user --scope --quiet --no-ask-password -p MemoryMax=50M -p MemorySwapMax=0 python3 sensor.py

Should report 50M

2 Example invocation with container:
sudo docker run --rm --memory=50m --memory-swap=50m -v "$(pwd)/sensor.py:/sensor.py:ro" python:3.12-slim python3 /sensor.py

Should report 50M

3 Restricted container, but even more restricted script:

  sudo docker run --rm \
    --memory=50m --memory-swap=50m \
    --cgroupns=private --privileged \
    -v "$(pwd)/sensor.py:/sensor.py:ro" \
    python:3.12-slim \
    bash -c '
      mount -o remount,rw /sys/fs/cgroup 2>/dev/null || true
      mkdir -p /sys/fs/cgroup/init /sys/fs/cgroup/inner
      echo $$ > /sys/fs/cgroup/init/cgroup.procs          # vacate the namespace-root cgroup
      echo +memory > /sys/fs/cgroup/cgroup.subtree_control # now delegation is allowed
      echo 30M > /sys/fs/cgroup/inner/memory.max
      echo 0   > /sys/fs/cgroup/inner/memory.swap.max
      echo $$ > /sys/fs/cgroup/inner/cgroup.procs          # move shell (→python) into the tight cgroup
      exec python3 /sensor.py
    '

Should report 30M

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions