Debian – Why Are There Empty Directories Under /sys/fs/cgroup After Unmounting cgroup v1?

cgroupsdebianmountsysfstmpfs

I just unmounted cgroup version 1, leaving just a single cgroup2 mount on my system.

$ mount | grep -i cgroup
tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,size=4096k,nr_inodes=1024,mode=755)
cgroup2 on /sys/fs/cgroup/unified type cgroup2 (rw,nosuid,nodev,noexec,relatime,nsdelegate)

I was under the impression everything in /sys/fs/cgroup that was not /sys/fs/cgroup/unified is an artifact of cgroup 1. How come these remain though after unmounting cgroup version 1?

$ ls -lh
drwxr-xr-x  2 root root 40 Dec 25 18:57 blkio
lrwxrwxrwx  1 root root 11 Dec 25 18:57 cpu -> cpu,cpuacct
lrwxrwxrwx  1 root root 11 Dec 25 18:57 cpuacct -> cpu,cpuacct
drwxr-xr-x  2 root root 40 Dec 25 18:57 cpu,cpuacct
drwxr-xr-x  2 root root 40 Dec 25 18:57 cpuset
drwxr-xr-x  2 root root 40 Dec 25 18:57 devices
drwxr-xr-x  2 root root 40 Dec 25 18:57 freezer
drwxr-xr-x  2 root root 40 Dec 25 18:57 memory
lrwxrwxrwx  1 root root 16 Dec 25 18:57 net_cls -> net_cls,net_prio
drwxr-xr-x  2 root root 40 Dec 25 18:57 net_cls,net_prio
lrwxrwxrwx  1 root root 16 Dec 25 18:57 net_prio -> net_cls,net_prio
drwxr-xr-x  2 root root 40 Dec 25 18:57 perf_event
drwxr-xr-x  2 root root 40 Dec 25 18:57 pids
drwxr-xr-x  2 root root 40 Dec 25 18:57 rdma
drwxr-xr-x  2 root root 40 Dec 25 18:57 systemd
dr-xr-xr-x 13 root root  0 Dec 26 21:37 unified

Are these remaining temp dirs that are not kernel interfaces?

$ find . | grep -v unified
./freezer
./cpuset
./cpu
./cpuacct
./cpu,cpuacct
./rdma
./perf_event
./blkio
./memory
./devices
./net_prio
./net_cls
./net_cls,net_prio
./pids
./systemd

How does these empty directories work with cgroups v1?

Best Answer

It's actually very simple. All cgroup mounts must be done on top of a directory. Before you had,

cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,name=systemd)
cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,pids)
cgroup on /sys/fs/cgroup/net_cls,net_prio type cgroup (rw,nosuid,nodev,noexec,relatime,net_cls,net_prio)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)
cgroup on /sys/fs/cgroup/rdma type cgroup (rw,nosuid,nodev,noexec,relatime,rdma)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpu,cpuacct)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)

When you unmounted all these, you left the underlying directories there. Alas, these can be removed by remounting /sys/fs/cgroup as rw and simply deleting them.

sudo mount -o remount,rw /sys/fs/cgroup
# Delete the symlinks
sudo find /sys/fs/cgroup -maxdepth 1 -type l -exec rm {} \;
# Delete the empty directories
sudo find /sys/fs/cgroup/ -links 2 -type d -not -path '/sys/fs/cgroup/unified/*' -exec rmdir -v {} \;
sudo mount -o remount,ro /sys/fs/cgroup

After which you should just see your beautiful and clean cgroup2 remaining,

$ ls /sys/fs/cgroup
unified
Related Question