How to enable cgroup v2 in WSL2?
Asked Answered
L

1

24

The easiest way to get access to cgroup v2 capable system having only a Windows machine is to spawn WSL2 instance hosting Ubuntu 22.04. Unfortunately, there is an issue. Removal of v1 controllers doesn't result in that controllers added in v2 hierarchy.

By default, WSL2 has both cgroup v1 and cgroup v2 hierarchies enabled, with all controllers sitting in v1:

$ 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 seems 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, a controller may be moved to cgroup v2 only when no more processes are handled by that controller.

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

Loganiaceous answered 18/7, 2022 at 11:27 Comment(0)
L
40

Since Linux v5.0, kernel boot option cgroup_no_v1=<list_of_controllers_to_disable> can be used to disable cgroup v1 hierarchies. As result your machine should start as cgroup v2 only.

For WSL, Microsoft documentation says we have to create %UserProfile%\.wslconfig file with the following content:

[wsl2]
kernelCommandLine = cgroup_no_v1=all

That's it.
Make sure you reboot your WSL with > wsl.exe --shutdown command.
You can see enabled controllers you got in <cgroup_fs_mount_point>/cgroup.controllers file.

Extra tweaks:

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

The receipt #2 had one side-effect where the original cgroup mount point at /sys/fs/cgroup/unified kept "dangling". You could see that by running $ mount | grep cgroup2 or looking /proc/self/mountinfo file. The upside of #2 was that docker service was starting without any issues in this case. Hence, if you can tolerate dangling mounts, the /etc/fstab method is preferred.

Loganiaceous answered 16/8, 2022 at 15:5 Comment(7)
Excellent - I thought it was a good question when you posted last month (so upvoted then), and it's great to see you've been able to find and post a detailed solution (so upvoted the answer, too). Surprised no one else was interested in this, but I guess not many people follow the windows-subsystem-for-linux tag here as often as I do.Messene
@Messene Thank you. I hope my Q&A will help someone. BTW, the source of the answer I took from this thread. The people there are knowledgeable and very helpful.Loganiaceous
To me all this achieves is a Catastrophic failure Error code: Wsl/Service/CreateInstance/CreateVm/ConfigureNetworking/0x8000ffff printed 3 times.Warthog
Nevermind, had to update WSL with wsl --update.Warthog
After i did wsl.exe --shutdown, and reopen my ubuntu 22.04.2, the cgroup version goes back. Any way to fix this.Monticule
The boot cgroup configuration is defined by %UserProfile%\.wslconfig and /etc/fstab files. All the tweaks you do at runtime with mount are not persistent and will be reset to the defaults defined by %UserProfile%\.wslconfig and /etc/fstab files respectively.Loganiaceous
Quick note to say that things seem to have moved on and on my WSL2 2.0.14 applying the .wslconfig change I found that the cgroup fs was already mounted at /sys/fs/cgroup so the mount/fstab shenanigans were not required.Cloistral

© 2022 - 2024 — McMap. All rights reserved.