sysinfo.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package sysinfo // import "github.com/docker/docker/pkg/sysinfo"
  2. import "github.com/docker/docker/pkg/parsers"
  3. // SysInfo stores information about which features a kernel supports.
  4. // TODO Windows: Factor out platform specific capabilities.
  5. type SysInfo struct {
  6. // Whether the kernel supports AppArmor or not
  7. AppArmor bool
  8. // Whether the kernel supports Seccomp or not
  9. Seccomp bool
  10. cgroupMemInfo
  11. cgroupCPUInfo
  12. cgroupBlkioInfo
  13. cgroupCpusetInfo
  14. cgroupPids
  15. // Whether the kernel supports cgroup namespaces or not
  16. CgroupNamespaces bool
  17. // Whether IPv4 forwarding is supported or not, if this was disabled, networking will not work
  18. IPv4ForwardingDisabled bool
  19. // Whether bridge-nf-call-iptables is supported or not
  20. BridgeNFCallIPTablesDisabled bool
  21. // Whether bridge-nf-call-ip6tables is supported or not
  22. BridgeNFCallIP6TablesDisabled bool
  23. // Whether the cgroup has the mountpoint of "devices" or not
  24. CgroupDevicesEnabled bool
  25. // Whether the cgroup is in unified mode (v2).
  26. CgroupUnified bool
  27. }
  28. type cgroupMemInfo struct {
  29. // Whether memory limit is supported or not
  30. MemoryLimit bool
  31. // Whether swap limit is supported or not
  32. SwapLimit bool
  33. // Whether soft limit is supported or not
  34. MemoryReservation bool
  35. // Whether OOM killer disable is supported or not
  36. OomKillDisable bool
  37. // Whether memory swappiness is supported or not
  38. MemorySwappiness bool
  39. // Whether kernel memory limit is supported or not
  40. KernelMemory bool
  41. // Whether kernel memory TCP limit is supported or not
  42. KernelMemoryTCP bool
  43. }
  44. type cgroupCPUInfo struct {
  45. // Whether CPU shares is supported or not
  46. CPUShares bool
  47. // Whether CPU CFS (Completely Fair Scheduler) is supported
  48. CPUCfs bool
  49. // Whether CPU real-time scheduler is supported
  50. CPURealtime bool
  51. }
  52. type cgroupBlkioInfo struct {
  53. // Whether Block IO weight is supported or not
  54. BlkioWeight bool
  55. // Whether Block IO weight_device is supported or not
  56. BlkioWeightDevice bool
  57. // Whether Block IO read limit in bytes per second is supported or not
  58. BlkioReadBpsDevice bool
  59. // Whether Block IO write limit in bytes per second is supported or not
  60. BlkioWriteBpsDevice bool
  61. // Whether Block IO read limit in IO per second is supported or not
  62. BlkioReadIOpsDevice bool
  63. // Whether Block IO write limit in IO per second is supported or not
  64. BlkioWriteIOpsDevice bool
  65. }
  66. type cgroupCpusetInfo struct {
  67. // Whether Cpuset is supported or not
  68. Cpuset bool
  69. // Available Cpuset's cpus
  70. Cpus string
  71. // Available Cpuset's memory nodes
  72. Mems string
  73. }
  74. type cgroupPids struct {
  75. // Whether Pids Limit is supported or not
  76. PidsLimit bool
  77. }
  78. // IsCpusetCpusAvailable returns `true` if the provided string set is contained
  79. // in cgroup's cpuset.cpus set, `false` otherwise.
  80. // If error is not nil a parsing error occurred.
  81. func (c cgroupCpusetInfo) IsCpusetCpusAvailable(provided string) (bool, error) {
  82. return isCpusetListAvailable(provided, c.Cpus)
  83. }
  84. // IsCpusetMemsAvailable returns `true` if the provided string set is contained
  85. // in cgroup's cpuset.mems set, `false` otherwise.
  86. // If error is not nil a parsing error occurred.
  87. func (c cgroupCpusetInfo) IsCpusetMemsAvailable(provided string) (bool, error) {
  88. return isCpusetListAvailable(provided, c.Mems)
  89. }
  90. func isCpusetListAvailable(provided, available string) (bool, error) {
  91. parsedAvailable, err := parsers.ParseUintList(available)
  92. if err != nil {
  93. return false, err
  94. }
  95. // 8192 is the normal maximum number of CPUs in Linux, so accept numbers up to this
  96. // or more if we actually have more CPUs.
  97. max := 8192
  98. for m := range parsedAvailable {
  99. if m > max {
  100. max = m
  101. }
  102. }
  103. parsedProvided, err := parsers.ParseUintListMaximum(provided, max)
  104. if err != nil {
  105. return false, err
  106. }
  107. for k := range parsedProvided {
  108. if !parsedAvailable[k] {
  109. return false, nil
  110. }
  111. }
  112. return true, nil
  113. }