Answer a question

I am learning about cgroup v2. The shortest way to get access to cgroup v2 from Windows is to spawn WSL2 instance hosting Ubuntu 22.04. Unfortunately, there is an issue. Removing v1 controller doesn't automatically adds that controller to v2 hierarchy.

By default, WSL2 has both cgroup v1 and cgroup v2 enabled. And no controllers available to v2. Pretty much everything is taken by cgroup v1:

A@B$ mount -l | grep cgroup
tmpfs on /sys/fs/cgroup type tmpfs (rw,nosuid,nodev,noexec,relatime,mode=755)
cgroup2 on /sys/fs/cgroup/unified type cgroup2 (rw,nosuid,nodev,noexec,relatime,nsdelegate)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)
cgroup on /sys/fs/cgroup/cpu type cgroup (rw,nosuid,nodev,noexec,relatime,cpu)
cgroup on /sys/fs/cgroup/cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpuacct)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)
cgroup on /sys/fs/cgroup/net_cls type cgroup (rw,nosuid,nodev,noexec,relatime,net_cls)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)
cgroup on /sys/fs/cgroup/net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,net_prio)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb)
cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,pids)
cgroup on /sys/fs/cgroup/rdma type cgroup (rw,nosuid,nodev,noexec,relatime,rdma)

I tried removing v1 controllers with $ umount /sys/fs/cgroup/*. This indeed removes cgroup v1 controllers from $ mount | grep cgroup list. But then nothing is being added to v2 (/sys/fs/cgroup/unified).

If I understand the cgroup v2 official documentation correctly, controller may be moved to cgroup v2 only when no more processes are handled by the controller.

How do I enable controllers like "cpu" and "memory" to cgroup v2?

Answers

Since Linux 5.0, the cgroup_no_v1=<what_controllers_to_disable_list> kernel boot option can be used to disable cgroup v1 hierarchies.

For WLS by following Microsoft documentation we need to create %UserProfile%\.wslconfig file with the following content:

[wsl2]
kernelCommandLine = cgroup_no_v1=all

Peace. Make sure you reboot your WSL instance with > wsl.exe --shutdown command. See controllers you got in <cgroup_fs_mount_point>/cgroup.controllers file.

Extra tweaks:

  1. By default, WSL will likely mount cgroup2 hierarchy at /sys/fs/cgroup/unified where it used to be. Some software might not like it (docker in particular). Move it to the conventional place with mount --move command:
$ mount --move /sys/fs/cgroup/unified /sys/fs/cgroup
  1. You can achieve similar effect using /etc/fstab file:
cgroup2 /sys/fs/cgroup cgroup2 rw,nosuid,nodev,noexec,relatime,nsdelegate 0 0

The downside of /etc/fstab is that the original cgroup mount point at /sys/fs/cgroup/unified keeps "dangling" in your $ mount | grep cgroup2 and /proc/self/mountinfo. The upside is that docker service starts without issues in this case. So if you can tolerate dangling mounts, the /etc/fstab method is preferred.

Logo

PostgreSQL社区为您提供最前沿的新闻资讯和知识内容

更多推荐