在 Linux 系统中,cgroupfs
(即 cgroup 文件系统)通常由 systemd 或其他初始化系统自动挂载
1、检查已挂载的 cgroup 文件系统
[root@kylin01 ~]# mount | grep cgroup
tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpu,cpuacct)
cgroup on /sys/fs/cgroup/pids type cgroup (rw,nosuid,nodev,noexec,relatime,pids)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)
cgroup on /sys/fs/cgroup/rdma type cgroup (rw,nosuid,nodev,noexec,relatime,rdma)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb)
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/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)
####################
root@seanserver:~# mount | grep cgroup
cgroup2 on /sys/fs/cgroup type cgroup2 (rw,nosuid,nodev,noexec,relatime,nsdelegate,memory_recursiveprot)
或者命令
[root@kylin01 ~]# cat /proc/mounts | grep cgroup
tmpfs /sys/fs/cgroup tmpfs ro,nosuid,nodev,noexec,mode=755 0 0
cgroup /sys/fs/cgroup/systemd cgroup rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd 0 0
cgroup /sys/fs/cgroup/cpu,cpuacct cgroup rw,nosuid,nodev,noexec,relatime,cpu,cpuacct 0 0
cgroup /sys/fs/cgroup/pids cgroup rw,nosuid,nodev,noexec,relatime,pids 0 0
cgroup /sys/fs/cgroup/devices cgroup rw,nosuid,nodev,noexec,relatime,devices 0 0
cgroup /sys/fs/cgroup/blkio cgroup rw,nosuid,nodev,noexec,relatime,blkio 0 0
cgroup /sys/fs/cgroup/rdma cgroup rw,nosuid,nodev,noexec,relatime,rdma 0 0
cgroup /sys/fs/cgroup/cpuset cgroup rw,nosuid,nodev,noexec,relatime,cpuset 0 0
cgroup /sys/fs/cgroup/memory cgroup rw,nosuid,nodev,noexec,relatime,memory 0 0
cgroup /sys/fs/cgroup/hugetlb cgroup rw,nosuid,nodev,noexec,relatime,hugetlb 0 0
cgroup /sys/fs/cgroup/net_cls,net_prio cgroup rw,nosuid,nodev,noexec,relatime,net_cls,net_prio 0 0
cgroup /sys/fs/cgroup/perf_event cgroup rw,nosuid,nodev,noexec,relatime,perf_event 0 0
cgroup /sys/fs/cgroup/freezer cgroup rw,nosuid,nodev,noexec,relatime,freezer 0 0
#########################
root@seanserver:~# cat /proc/mounts | grep cgroup
cgroup2 /sys/fs/cgroup cgroup2 rw,nosuid,nodev,noexec,relatime,nsdelegate,memory_recursiveprot 0 0
上面展示了两个cgroup的版本。
对于cgroup V1 (传统版本)每个控制器(如 cpu
、memory
)会单独挂载。
对于 cgroup v2(现代版本,统一层级) 只有一个统一的挂载点
2、检查挂载点目录结构
[root@kylin01 ~]# ls -l /sys/fs/cgroup
总用量 0
dr-xr-xr-x 6 root root 0 5月 20 16:05 blkio
lrwxrwxrwx 1 root root 11 5月 20 16:05 cpu -> cpu,cpuacct
lrwxrwxrwx 1 root root 11 5月 20 16:05 cpuacct -> cpu,cpuacct
dr-xr-xr-x 6 root root 0 5月 20 16:05 cpu,cpuacct
dr-xr-xr-x 3 root root 0 5月 20 16:05 cpuset
dr-xr-xr-x 6 root root 0 5月 20 16:05 devices
dr-xr-xr-x 3 root root 0 5月 20 16:05 freezer
dr-xr-xr-x 3 root root 0 5月 20 16:05 hugetlb
dr-xr-xr-x 6 root root 0 5月 20 16:05 memory
lrwxrwxrwx 1 root root 16 5月 20 16:05 net_cls -> net_cls,net_prio
dr-xr-xr-x 3 root root 0 5月 20 16:05 net_cls,net_prio
lrwxrwxrwx 1 root root 16 5月 20 16:05 net_prio -> net_cls,net_prio
dr-xr-xr-x 3 root root 0 5月 20 16:05 perf_event
dr-xr-xr-x 6 root root 0 5月 20 16:05 pids
dr-xr-xr-x 3 root root 0 5月 20 16:05 rdma
dr-xr-xr-x 6 root root 0 5月 20 16:05 systemd
#################################
root@seanserver:~# ls -l /sys/fs/cgroup
total 0
drwxr-xr-x 2 root root 0 May 27 09:17 aegis
drwxr-xr-x 2 root root 0 May 30 17:22 aegisMonitor
drwxr-xr-x 2 root root 0 May 27 16:59 aegisRtap0
-r--r--r-- 1 root root 0 May 27 16:59 cgroup.controllers
-rw-r--r-- 1 root root 0 May 31 05:24 cgroup.max.depth
-rw-r--r-- 1 root root 0 Jun 4 18:07 cgroup.max.descendants
-rw-r--r-- 1 root root 0 May 31 05:24 cgroup.pressure
-rw-r--r-- 1 root root 0 May 3 23:10 cgroup.procs
-r--r--r-- 1 root root 0 Jun 4 18:07 cgroup.stat
-rw-r--r-- 1 root root 0 Apr 27 00:32 cgroup.subtree_control
-rw-r--r-- 1 root root 0 Jun 4 18:07 cgroup.threads
-rw-r--r-- 1 root root 0 Jun 4 18:07 cpu.pressure
-r--r--r-- 1 root root 0 Jun 4 18:07 cpuset.cpus.effective
-r--r--r-- 1 root root 0 May 31 05:24 cpuset.mems.effective
-r--r--r-- 1 root root 0 Jun 4 18:07 cpu.stat
drwxr-xr-x 2 root root 0 Oct 26 2024 dev-hugepages.mount
drwxr-xr-x 2 root root 0 Oct 26 2024 dev-mqueue.mount
drwxr-xr-x 2 root root 0 Oct 26 2024 init.scope
-rw-r--r-- 1 root root 0 Jun 4 18:07 io.cost.model
-rw-r--r-- 1 root root 0 Jun 4 18:07 io.cost.qos
-rw-r--r-- 1 root root 0 Jun 4 18:07 io.pressure
-r--r--r-- 1 root root 0 May 31 05:24 io.stat
-r--r--r-- 1 root root 0 Jun 4 18:07 memory.numa_stat
-rw-r--r-- 1 root root 0 Jun 4 18:07 memory.pressure
--w------- 1 root root 0 May 31 05:24 memory.reclaim
-r--r--r-- 1 root root 0 Jun 4 18:07 memory.stat
-r--r--r-- 1 root root 0 Jun 4 18:07 misc.capacity
drwxr-xr-x 2 root root 0 Oct 26 2024 proc-sys-fs-binfmt_misc.mount
drwxr-xr-x 2 root root 0 Oct 26 2024 sys-fs-fuse-connections.mount
drwxr-xr-x 2 root root 0 Oct 26 2024 sys-kernel-config.mount
drwxr-xr-x 2 root root 0 Oct 26 2024 sys-kernel-debug.mount
drwxr-xr-x 2 root root 0 Oct 26 2024 sys-kernel-tracing.mount
drwxr-xr-x 30 root root 0 Jun 4 18:00 system.slice
drwxr-xr-x 3 root root 0 Jun 4 16:48 user.slice
3、查看当前使用cgroup版本
[root@kylin01 ~]# stat -fc %T /sys/fs/cgroup/
tmpfs
####################
root@seanserver:~# stat -fc %T /sys/fs/cgroup/
cgroup2fs
4. 检查 systemd 的 cgroup 配置
如果使用 systemd,检查其默认配置:
systemd-analyze cgroup
或查看 systemd 使用的层级:
systemctl status | grep -i cgroup
5. 手动挂载(如需修复)
如果发现未正确挂载,可以手动挂载:
挂载 cgroup v1:
mount -t cgroup -o none,name=systemd cgroup /sys/fs/cgroup/systemd
挂载 cgroup v2:
mount -t cgroup2 none /sys/fs/cgroup
6. 常见问题排查
问题1:
/sys/fs/cgroup
为空或未挂载。
解决:确保内核支持 cgroup 并检查启动参数(如cgroup_enable=memory swapaccount=1
)。问题2:Docker/Kubernetes 无法识别 cgroup。
解决:确认是否需切换版本(如 Docker 需配置--exec-opt native.cgroupdriver=systemd
)。问题3:系统使用混合模式(v1 + v2)。
解决:通过内核参数明确指定版本(如systemd.unified_cgroup_hierarchy=0
强制用 v1)