github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/pkg/sysinfo/cgroup2_linux.go (about) 1 package sysinfo // import "github.com/Prakhar-Agarwal-byte/moby/pkg/sysinfo" 2 3 import ( 4 "context" 5 "os" 6 "path" 7 "strings" 8 9 "github.com/containerd/cgroups/v3" 10 cgroupsV2 "github.com/containerd/cgroups/v3/cgroup2" 11 "github.com/containerd/containerd/pkg/userns" 12 "github.com/containerd/log" 13 ) 14 15 func newV2(options ...Opt) *SysInfo { 16 sysInfo := &SysInfo{ 17 CgroupUnified: true, 18 cg2GroupPath: "/", 19 } 20 for _, o := range options { 21 o(sysInfo) 22 } 23 24 ops := []infoCollector{ 25 applyNetworkingInfo, 26 applyAppArmorInfo, 27 applySeccompInfo, 28 applyCgroupNsInfo, 29 } 30 31 m, err := cgroupsV2.Load(sysInfo.cg2GroupPath) 32 if err != nil { 33 log.G(context.TODO()).Warn(err) 34 } else { 35 sysInfo.cg2Controllers = make(map[string]struct{}) 36 controllers, err := m.Controllers() 37 if err != nil { 38 log.G(context.TODO()).Warn(err) 39 } 40 for _, c := range controllers { 41 sysInfo.cg2Controllers[c] = struct{}{} 42 } 43 ops = append(ops, 44 applyMemoryCgroupInfoV2, 45 applyCPUCgroupInfoV2, 46 applyIOCgroupInfoV2, 47 applyCPUSetCgroupInfoV2, 48 applyPIDSCgroupInfoV2, 49 applyDevicesCgroupInfoV2, 50 ) 51 } 52 53 for _, o := range ops { 54 o(sysInfo) 55 } 56 return sysInfo 57 } 58 59 func getSwapLimitV2() bool { 60 _, g, err := cgroups.ParseCgroupFileUnified("/proc/self/cgroup") 61 if err != nil { 62 return false 63 } 64 65 if g == "" { 66 return false 67 } 68 69 cGroupPath := path.Join("/sys/fs/cgroup", g, "memory.swap.max") 70 if _, err = os.Stat(cGroupPath); os.IsNotExist(err) { 71 return false 72 } 73 return true 74 } 75 76 func applyMemoryCgroupInfoV2(info *SysInfo) { 77 if _, ok := info.cg2Controllers["memory"]; !ok { 78 info.Warnings = append(info.Warnings, "Unable to find memory controller") 79 return 80 } 81 82 info.MemoryLimit = true 83 info.SwapLimit = getSwapLimitV2() 84 info.MemoryReservation = true 85 info.OomKillDisable = false 86 info.MemorySwappiness = false 87 info.KernelMemory = false 88 info.KernelMemoryTCP = false 89 } 90 91 func applyCPUCgroupInfoV2(info *SysInfo) { 92 if _, ok := info.cg2Controllers["cpu"]; !ok { 93 info.Warnings = append(info.Warnings, "Unable to find cpu controller") 94 return 95 } 96 info.CPUShares = true 97 info.CPUCfs = true 98 info.CPURealtime = false 99 } 100 101 func applyIOCgroupInfoV2(info *SysInfo) { 102 if _, ok := info.cg2Controllers["io"]; !ok { 103 info.Warnings = append(info.Warnings, "Unable to find io controller") 104 return 105 } 106 107 info.BlkioWeight = true 108 info.BlkioWeightDevice = true 109 info.BlkioReadBpsDevice = true 110 info.BlkioWriteBpsDevice = true 111 info.BlkioReadIOpsDevice = true 112 info.BlkioWriteIOpsDevice = true 113 } 114 115 func applyCPUSetCgroupInfoV2(info *SysInfo) { 116 if _, ok := info.cg2Controllers["cpuset"]; !ok { 117 info.Warnings = append(info.Warnings, "Unable to find cpuset controller") 118 return 119 } 120 info.Cpuset = true 121 122 cpus, err := os.ReadFile(path.Join("/sys/fs/cgroup", info.cg2GroupPath, "cpuset.cpus.effective")) 123 if err != nil { 124 return 125 } 126 info.Cpus = strings.TrimSpace(string(cpus)) 127 128 mems, err := os.ReadFile(path.Join("/sys/fs/cgroup", info.cg2GroupPath, "cpuset.mems.effective")) 129 if err != nil { 130 return 131 } 132 info.Mems = strings.TrimSpace(string(mems)) 133 } 134 135 func applyPIDSCgroupInfoV2(info *SysInfo) { 136 if _, ok := info.cg2Controllers["pids"]; !ok { 137 info.Warnings = append(info.Warnings, "Unable to find pids controller") 138 return 139 } 140 info.PidsLimit = true 141 } 142 143 func applyDevicesCgroupInfoV2(info *SysInfo) { 144 info.CgroupDevicesEnabled = !userns.RunningInUserNS() 145 }