sysinfo.go 4.9 KB

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