github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/pkg/sysinfo/sysinfo.go (about) 1 // Package sysinfo stores information about which features a kernel supports. 2 package sysinfo // import "github.com/Prakhar-Agarwal-byte/moby/pkg/sysinfo" 3 4 import "github.com/Prakhar-Agarwal-byte/moby/pkg/parsers" 5 6 // Opt for New(). 7 type Opt func(info *SysInfo) 8 9 // SysInfo stores information about which features a kernel supports. 10 // TODO Windows: Factor out platform specific capabilities. 11 type SysInfo struct { 12 // Whether the kernel supports AppArmor or not 13 AppArmor bool 14 // Whether the kernel supports Seccomp or not 15 Seccomp bool 16 17 cgroupMemInfo 18 cgroupCPUInfo 19 cgroupBlkioInfo 20 cgroupCpusetInfo 21 cgroupPids 22 23 // Whether the kernel supports cgroup namespaces or not 24 CgroupNamespaces bool 25 26 // Whether IPv4 forwarding is supported or not, if this was disabled, networking will not work 27 IPv4ForwardingDisabled bool 28 29 // Whether bridge-nf-call-iptables is supported or not 30 BridgeNFCallIPTablesDisabled bool 31 32 // Whether bridge-nf-call-ip6tables is supported or not 33 BridgeNFCallIP6TablesDisabled bool 34 35 // Whether the cgroup has the mountpoint of "devices" or not 36 CgroupDevicesEnabled bool 37 38 // Whether the cgroup is in unified mode (v2). 39 CgroupUnified bool 40 41 // Warnings contains a slice of warnings that occurred while collecting 42 // system information. These warnings are intended to be informational 43 // messages for the user, and can either be logged or returned to the 44 // client; they are not intended to be parsed / used for other purposes, 45 // and do not have a fixed format. 46 Warnings []string 47 48 // cgMounts is the list of cgroup v1 mount paths, indexed by subsystem, to 49 // inspect availability of subsystems. 50 cgMounts map[string]string 51 52 // cg2GroupPath is the cgroup v2 group path to inspect availability of the controllers. 53 cg2GroupPath string 54 55 // cg2Controllers is an index of available cgroup v2 controllers. 56 cg2Controllers map[string]struct{} 57 } 58 59 type cgroupMemInfo struct { 60 // Whether memory limit is supported or not 61 MemoryLimit bool 62 63 // Whether swap limit is supported or not 64 SwapLimit bool 65 66 // Whether soft limit is supported or not 67 MemoryReservation bool 68 69 // Whether OOM killer disable is supported or not 70 OomKillDisable bool 71 72 // Whether memory swappiness is supported or not 73 MemorySwappiness bool 74 75 // Whether kernel memory limit is supported or not. This option is used to 76 // detect support for kernel-memory limits on API < v1.42. Kernel memory 77 // limit (`kmem.limit_in_bytes`) is not supported on cgroups v2, and has been 78 // removed in kernel 5.4. 79 KernelMemory bool 80 81 // Whether kernel memory TCP limit is supported or not. Kernel memory TCP 82 // limit (`memory.kmem.tcp.limit_in_bytes`) is not supported on cgroups v2. 83 KernelMemoryTCP bool 84 } 85 86 type cgroupCPUInfo struct { 87 // Whether CPU shares is supported or not 88 CPUShares bool 89 90 // Whether CPU CFS (Completely Fair Scheduler) is supported 91 CPUCfs bool 92 93 // Whether CPU real-time scheduler is supported 94 CPURealtime bool 95 } 96 97 type cgroupBlkioInfo struct { 98 // Whether Block IO weight is supported or not 99 BlkioWeight bool 100 101 // Whether Block IO weight_device is supported or not 102 BlkioWeightDevice bool 103 104 // Whether Block IO read limit in bytes per second is supported or not 105 BlkioReadBpsDevice bool 106 107 // Whether Block IO write limit in bytes per second is supported or not 108 BlkioWriteBpsDevice bool 109 110 // Whether Block IO read limit in IO per second is supported or not 111 BlkioReadIOpsDevice bool 112 113 // Whether Block IO write limit in IO per second is supported or not 114 BlkioWriteIOpsDevice bool 115 } 116 117 type cgroupCpusetInfo struct { 118 // Whether Cpuset is supported or not 119 Cpuset bool 120 121 // Available Cpuset's cpus 122 Cpus string 123 124 // Available Cpuset's memory nodes 125 Mems string 126 } 127 128 type cgroupPids struct { 129 // Whether Pids Limit is supported or not 130 PidsLimit bool 131 } 132 133 // IsCpusetCpusAvailable returns `true` if the provided string set is contained 134 // in cgroup's cpuset.cpus set, `false` otherwise. 135 // If error is not nil a parsing error occurred. 136 func (c cgroupCpusetInfo) IsCpusetCpusAvailable(provided string) (bool, error) { 137 return isCpusetListAvailable(provided, c.Cpus) 138 } 139 140 // IsCpusetMemsAvailable returns `true` if the provided string set is contained 141 // in cgroup's cpuset.mems set, `false` otherwise. 142 // If error is not nil a parsing error occurred. 143 func (c cgroupCpusetInfo) IsCpusetMemsAvailable(provided string) (bool, error) { 144 return isCpusetListAvailable(provided, c.Mems) 145 } 146 147 func isCpusetListAvailable(provided, available string) (bool, error) { 148 parsedAvailable, err := parsers.ParseUintList(available) 149 if err != nil { 150 return false, err 151 } 152 // 8192 is the normal maximum number of CPUs in Linux, so accept numbers up to this 153 // or more if we actually have more CPUs. 154 maxCPUs := 8192 155 for m := range parsedAvailable { 156 if m > maxCPUs { 157 maxCPUs = m 158 } 159 } 160 parsedProvided, err := parsers.ParseUintListMaximum(provided, maxCPUs) 161 if err != nil { 162 return false, err 163 } 164 for k := range parsedProvided { 165 if !parsedAvailable[k] { 166 return false, nil 167 } 168 } 169 return true, nil 170 }