2020-03-10 08:09:25 -04:00
|
|
|
package sysinfo // import "github.com/docker/docker/pkg/sysinfo"
|
|
|
|
|
|
|
|
import (
|
2021-06-03 16:54:10 -04:00
|
|
|
"os"
|
2020-03-10 08:09:25 -04:00
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
2022-03-07 18:07:23 -05:00
|
|
|
"github.com/containerd/cgroups"
|
2020-03-10 08:09:25 -04:00
|
|
|
cgroupsV2 "github.com/containerd/cgroups/v2"
|
2021-06-18 05:01:24 -04:00
|
|
|
"github.com/containerd/containerd/pkg/userns"
|
2020-03-10 08:09:25 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2021-07-14 10:45:02 -04:00
|
|
|
func newV2(options ...Opt) *SysInfo {
|
2020-03-10 08:09:25 -04:00
|
|
|
sysInfo := &SysInfo{
|
|
|
|
CgroupUnified: true,
|
2021-07-14 09:05:12 -04:00
|
|
|
cg2GroupPath: "/",
|
2020-03-10 08:09:25 -04:00
|
|
|
}
|
2021-07-14 06:36:38 -04:00
|
|
|
for _, o := range options {
|
2021-07-14 09:05:12 -04:00
|
|
|
o(sysInfo)
|
2021-07-14 06:36:38 -04:00
|
|
|
}
|
2021-07-14 09:44:38 -04:00
|
|
|
|
|
|
|
ops := []infoCollector{
|
|
|
|
applyNetworkingInfo,
|
|
|
|
applyAppArmorInfo,
|
|
|
|
applySeccompInfo,
|
|
|
|
applyCgroupNsInfo,
|
|
|
|
}
|
|
|
|
|
2021-07-14 09:05:12 -04:00
|
|
|
m, err := cgroupsV2.LoadManager("/sys/fs/cgroup", sysInfo.cg2GroupPath)
|
2020-03-10 08:09:25 -04:00
|
|
|
if err != nil {
|
|
|
|
logrus.Warn(err)
|
|
|
|
} else {
|
2021-07-14 09:44:38 -04:00
|
|
|
sysInfo.cg2Controllers = make(map[string]struct{})
|
2020-03-10 08:09:25 -04:00
|
|
|
controllers, err := m.Controllers()
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warn(err)
|
|
|
|
}
|
|
|
|
for _, c := range controllers {
|
2021-07-14 09:44:38 -04:00
|
|
|
sysInfo.cg2Controllers[c] = struct{}{}
|
2020-03-10 08:09:25 -04:00
|
|
|
}
|
2021-07-14 09:44:38 -04:00
|
|
|
ops = append(ops,
|
2020-03-10 08:09:25 -04:00
|
|
|
applyMemoryCgroupInfoV2,
|
|
|
|
applyCPUCgroupInfoV2,
|
|
|
|
applyIOCgroupInfoV2,
|
|
|
|
applyCPUSetCgroupInfoV2,
|
|
|
|
applyPIDSCgroupInfoV2,
|
|
|
|
applyDevicesCgroupInfoV2,
|
2021-07-14 09:44:38 -04:00
|
|
|
)
|
2020-03-10 08:09:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range ops {
|
2021-07-14 10:24:03 -04:00
|
|
|
o(sysInfo)
|
2020-03-10 08:09:25 -04:00
|
|
|
}
|
|
|
|
return sysInfo
|
|
|
|
}
|
|
|
|
|
2021-06-03 16:54:10 -04:00
|
|
|
func getSwapLimitV2() bool {
|
2022-05-26 21:51:54 -04:00
|
|
|
_, g, err := cgroups.ParseCgroupFileUnified("/proc/self/cgroup")
|
2021-06-03 16:54:10 -04:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if g == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
cGroupPath := path.Join("/sys/fs/cgroup", g, "memory.swap.max")
|
|
|
|
if _, err = os.Stat(cGroupPath); os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-07-14 10:24:03 -04:00
|
|
|
func applyMemoryCgroupInfoV2(info *SysInfo) {
|
2021-07-14 09:44:38 -04:00
|
|
|
if _, ok := info.cg2Controllers["memory"]; !ok {
|
2021-07-14 10:24:03 -04:00
|
|
|
info.Warnings = append(info.Warnings, "Unable to find memory controller")
|
|
|
|
return
|
2020-03-10 08:09:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
info.MemoryLimit = true
|
2021-06-03 16:54:10 -04:00
|
|
|
info.SwapLimit = getSwapLimitV2()
|
2020-03-10 08:09:25 -04:00
|
|
|
info.MemoryReservation = true
|
|
|
|
info.OomKillDisable = false
|
|
|
|
info.MemorySwappiness = false
|
|
|
|
info.KernelMemory = false
|
|
|
|
info.KernelMemoryTCP = false
|
|
|
|
}
|
|
|
|
|
2021-07-14 10:24:03 -04:00
|
|
|
func applyCPUCgroupInfoV2(info *SysInfo) {
|
2021-07-14 09:44:38 -04:00
|
|
|
if _, ok := info.cg2Controllers["cpu"]; !ok {
|
2021-07-14 10:24:03 -04:00
|
|
|
info.Warnings = append(info.Warnings, "Unable to find cpu controller")
|
|
|
|
return
|
2020-03-10 08:09:25 -04:00
|
|
|
}
|
|
|
|
info.CPUShares = true
|
2020-05-22 17:18:06 -04:00
|
|
|
info.CPUCfs = true
|
|
|
|
info.CPURealtime = false
|
2020-03-10 08:09:25 -04:00
|
|
|
}
|
|
|
|
|
2021-07-14 10:24:03 -04:00
|
|
|
func applyIOCgroupInfoV2(info *SysInfo) {
|
2021-07-14 09:44:38 -04:00
|
|
|
if _, ok := info.cg2Controllers["io"]; !ok {
|
2021-07-14 10:24:03 -04:00
|
|
|
info.Warnings = append(info.Warnings, "Unable to find io controller")
|
|
|
|
return
|
2020-03-10 08:09:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
info.BlkioWeight = true
|
|
|
|
info.BlkioWeightDevice = true
|
|
|
|
info.BlkioReadBpsDevice = true
|
|
|
|
info.BlkioWriteBpsDevice = true
|
|
|
|
info.BlkioReadIOpsDevice = true
|
|
|
|
info.BlkioWriteIOpsDevice = true
|
|
|
|
}
|
|
|
|
|
2021-07-14 10:24:03 -04:00
|
|
|
func applyCPUSetCgroupInfoV2(info *SysInfo) {
|
2021-07-14 09:44:38 -04:00
|
|
|
if _, ok := info.cg2Controllers["cpuset"]; !ok {
|
2021-07-14 10:24:03 -04:00
|
|
|
info.Warnings = append(info.Warnings, "Unable to find cpuset controller")
|
|
|
|
return
|
2020-03-10 08:09:25 -04:00
|
|
|
}
|
|
|
|
info.Cpuset = true
|
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
cpus, err := os.ReadFile(path.Join("/sys/fs/cgroup", info.cg2GroupPath, "cpuset.cpus.effective"))
|
2020-03-10 08:09:25 -04:00
|
|
|
if err != nil {
|
2021-07-14 10:24:03 -04:00
|
|
|
return
|
2020-03-10 08:09:25 -04:00
|
|
|
}
|
|
|
|
info.Cpus = strings.TrimSpace(string(cpus))
|
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
mems, err := os.ReadFile(path.Join("/sys/fs/cgroup", info.cg2GroupPath, "cpuset.mems.effective"))
|
2020-03-10 08:09:25 -04:00
|
|
|
if err != nil {
|
2021-07-14 10:24:03 -04:00
|
|
|
return
|
2020-03-10 08:09:25 -04:00
|
|
|
}
|
|
|
|
info.Mems = strings.TrimSpace(string(mems))
|
|
|
|
}
|
|
|
|
|
2021-07-14 10:24:03 -04:00
|
|
|
func applyPIDSCgroupInfoV2(info *SysInfo) {
|
2021-07-14 09:44:38 -04:00
|
|
|
if _, ok := info.cg2Controllers["pids"]; !ok {
|
2021-07-14 10:24:03 -04:00
|
|
|
info.Warnings = append(info.Warnings, "Unable to find pids controller")
|
|
|
|
return
|
2020-03-10 08:09:25 -04:00
|
|
|
}
|
|
|
|
info.PidsLimit = true
|
|
|
|
}
|
|
|
|
|
2021-07-14 10:24:03 -04:00
|
|
|
func applyDevicesCgroupInfoV2(info *SysInfo) {
|
2021-06-18 05:01:24 -04:00
|
|
|
info.CgroupDevicesEnabled = !userns.RunningInUserNS()
|
2020-03-10 08:09:25 -04:00
|
|
|
}
|