info_unix.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. //go:build !windows
  2. // +build !windows
  3. package daemon // import "github.com/docker/docker/daemon"
  4. import (
  5. "context"
  6. "fmt"
  7. "os/exec"
  8. "path/filepath"
  9. "strings"
  10. "github.com/docker/docker/api/types"
  11. containertypes "github.com/docker/docker/api/types/container"
  12. "github.com/docker/docker/pkg/sysinfo"
  13. "github.com/pkg/errors"
  14. "github.com/sirupsen/logrus"
  15. )
  16. // fillPlatformInfo fills the platform related info.
  17. func (daemon *Daemon) fillPlatformInfo(v *types.Info, sysInfo *sysinfo.SysInfo) {
  18. v.CgroupDriver = daemon.getCgroupDriver()
  19. v.CgroupVersion = "1"
  20. if sysInfo.CgroupUnified {
  21. v.CgroupVersion = "2"
  22. }
  23. if v.CgroupDriver != cgroupNoneDriver {
  24. v.MemoryLimit = sysInfo.MemoryLimit
  25. v.SwapLimit = sysInfo.SwapLimit
  26. v.KernelMemory = sysInfo.KernelMemory
  27. v.KernelMemoryTCP = sysInfo.KernelMemoryTCP
  28. v.OomKillDisable = sysInfo.OomKillDisable
  29. v.CPUCfsPeriod = sysInfo.CPUCfs
  30. v.CPUCfsQuota = sysInfo.CPUCfs
  31. v.CPUShares = sysInfo.CPUShares
  32. v.CPUSet = sysInfo.Cpuset
  33. v.PidsLimit = sysInfo.PidsLimit
  34. }
  35. v.Runtimes = daemon.configStore.GetAllRuntimes()
  36. v.DefaultRuntime = daemon.configStore.GetDefaultRuntimeName()
  37. v.InitBinary = daemon.configStore.GetInitPath()
  38. v.RuncCommit.ID = "N/A"
  39. v.ContainerdCommit.ID = "N/A"
  40. v.InitCommit.ID = "N/A"
  41. defaultRuntimeBinary := daemon.configStore.GetRuntime(v.DefaultRuntime).Path
  42. if rv, err := exec.Command(defaultRuntimeBinary, "--version").Output(); err == nil {
  43. if _, _, commit, err := parseRuntimeVersion(string(rv)); err != nil {
  44. logrus.Warnf("failed to parse %s version: %v", defaultRuntimeBinary, err)
  45. } else {
  46. v.RuncCommit.ID = commit
  47. }
  48. } else {
  49. logrus.Warnf("failed to retrieve %s version: %v", defaultRuntimeBinary, err)
  50. }
  51. if rv, err := daemon.containerd.Version(context.Background()); err == nil {
  52. v.ContainerdCommit.ID = rv.Revision
  53. } else {
  54. logrus.Warnf("failed to retrieve containerd version: %v", err)
  55. }
  56. defaultInitBinary := daemon.configStore.GetInitPath()
  57. if rv, err := exec.Command(defaultInitBinary, "--version").Output(); err == nil {
  58. if _, commit, err := parseInitVersion(string(rv)); err != nil {
  59. logrus.Warnf("failed to parse %s version: %s", defaultInitBinary, err)
  60. } else {
  61. v.InitCommit.ID = commit
  62. }
  63. } else {
  64. logrus.Warnf("failed to retrieve %s version: %s", defaultInitBinary, err)
  65. }
  66. // Set expected and actual commits to the same value to prevent the client
  67. // showing that the version does not match the "expected" version/commit.
  68. v.RuncCommit.Expected = v.RuncCommit.ID
  69. v.ContainerdCommit.Expected = v.ContainerdCommit.ID
  70. v.InitCommit.Expected = v.InitCommit.ID
  71. if v.CgroupDriver == cgroupNoneDriver {
  72. if v.CgroupVersion == "2" {
  73. v.Warnings = append(v.Warnings, "WARNING: Running in rootless-mode without cgroups. Systemd is required to enable cgroups in rootless-mode.")
  74. } else {
  75. v.Warnings = append(v.Warnings, "WARNING: Running in rootless-mode without cgroups. To enable cgroups in rootless-mode, you need to boot the system in cgroup v2 mode.")
  76. }
  77. } else {
  78. if !v.MemoryLimit {
  79. v.Warnings = append(v.Warnings, "WARNING: No memory limit support")
  80. }
  81. if !v.SwapLimit {
  82. v.Warnings = append(v.Warnings, "WARNING: No swap limit support")
  83. }
  84. if !v.KernelMemoryTCP && v.CgroupVersion == "1" {
  85. // kernel memory is not available for cgroup v2.
  86. // Warning is not printed on cgroup v2, because there is no action user can take.
  87. v.Warnings = append(v.Warnings, "WARNING: No kernel memory TCP limit support")
  88. }
  89. if !v.OomKillDisable && v.CgroupVersion == "1" {
  90. // oom kill disable is not available for cgroup v2.
  91. // Warning is not printed on cgroup v2, because there is no action user can take.
  92. v.Warnings = append(v.Warnings, "WARNING: No oom kill disable support")
  93. }
  94. if !v.CPUCfsQuota {
  95. v.Warnings = append(v.Warnings, "WARNING: No cpu cfs quota support")
  96. }
  97. if !v.CPUCfsPeriod {
  98. v.Warnings = append(v.Warnings, "WARNING: No cpu cfs period support")
  99. }
  100. if !v.CPUShares {
  101. v.Warnings = append(v.Warnings, "WARNING: No cpu shares support")
  102. }
  103. if !v.CPUSet {
  104. v.Warnings = append(v.Warnings, "WARNING: No cpuset support")
  105. }
  106. // TODO add fields for these options in types.Info
  107. if !sysInfo.BlkioWeight && v.CgroupVersion == "2" {
  108. // blkio weight is not available on cgroup v1 since kernel 5.0.
  109. // Warning is not printed on cgroup v1, because there is no action user can take.
  110. // On cgroup v2, blkio weight is implemented using io.weight
  111. v.Warnings = append(v.Warnings, "WARNING: No io.weight support")
  112. }
  113. if !sysInfo.BlkioWeightDevice && v.CgroupVersion == "2" {
  114. v.Warnings = append(v.Warnings, "WARNING: No io.weight (per device) support")
  115. }
  116. if !sysInfo.BlkioReadBpsDevice {
  117. if v.CgroupVersion == "2" {
  118. v.Warnings = append(v.Warnings, "WARNING: No io.max (rbps) support")
  119. } else {
  120. v.Warnings = append(v.Warnings, "WARNING: No blkio throttle.read_bps_device support")
  121. }
  122. }
  123. if !sysInfo.BlkioWriteBpsDevice {
  124. if v.CgroupVersion == "2" {
  125. v.Warnings = append(v.Warnings, "WARNING: No io.max (wbps) support")
  126. } else {
  127. v.Warnings = append(v.Warnings, "WARNING: No blkio throttle.write_bps_device support")
  128. }
  129. }
  130. if !sysInfo.BlkioReadIOpsDevice {
  131. if v.CgroupVersion == "2" {
  132. v.Warnings = append(v.Warnings, "WARNING: No io.max (riops) support")
  133. } else {
  134. v.Warnings = append(v.Warnings, "WARNING: No blkio throttle.read_iops_device support")
  135. }
  136. }
  137. if !sysInfo.BlkioWriteIOpsDevice {
  138. if v.CgroupVersion == "2" {
  139. v.Warnings = append(v.Warnings, "WARNING: No io.max (wiops) support")
  140. } else {
  141. v.Warnings = append(v.Warnings, "WARNING: No blkio throttle.write_iops_device support")
  142. }
  143. }
  144. }
  145. if !v.IPv4Forwarding {
  146. v.Warnings = append(v.Warnings, "WARNING: IPv4 forwarding is disabled")
  147. }
  148. if !v.BridgeNfIptables {
  149. v.Warnings = append(v.Warnings, "WARNING: bridge-nf-call-iptables is disabled")
  150. }
  151. if !v.BridgeNfIP6tables {
  152. v.Warnings = append(v.Warnings, "WARNING: bridge-nf-call-ip6tables is disabled")
  153. }
  154. }
  155. func (daemon *Daemon) fillPlatformVersion(v *types.Version) {
  156. if rv, err := daemon.containerd.Version(context.Background()); err == nil {
  157. v.Components = append(v.Components, types.ComponentVersion{
  158. Name: "containerd",
  159. Version: rv.Version,
  160. Details: map[string]string{
  161. "GitCommit": rv.Revision,
  162. },
  163. })
  164. }
  165. defaultRuntime := daemon.configStore.GetDefaultRuntimeName()
  166. defaultRuntimeBinary := daemon.configStore.GetRuntime(defaultRuntime).Path
  167. if rv, err := exec.Command(defaultRuntimeBinary, "--version").Output(); err == nil {
  168. if _, ver, commit, err := parseRuntimeVersion(string(rv)); err != nil {
  169. logrus.Warnf("failed to parse %s version: %v", defaultRuntimeBinary, err)
  170. } else {
  171. v.Components = append(v.Components, types.ComponentVersion{
  172. Name: defaultRuntime,
  173. Version: ver,
  174. Details: map[string]string{
  175. "GitCommit": commit,
  176. },
  177. })
  178. }
  179. } else {
  180. logrus.Warnf("failed to retrieve %s version: %v", defaultRuntimeBinary, err)
  181. }
  182. defaultInitBinary := daemon.configStore.GetInitPath()
  183. if rv, err := exec.Command(defaultInitBinary, "--version").Output(); err == nil {
  184. if ver, commit, err := parseInitVersion(string(rv)); err != nil {
  185. logrus.Warnf("failed to parse %s version: %s", defaultInitBinary, err)
  186. } else {
  187. v.Components = append(v.Components, types.ComponentVersion{
  188. Name: filepath.Base(defaultInitBinary),
  189. Version: ver,
  190. Details: map[string]string{
  191. "GitCommit": commit,
  192. },
  193. })
  194. }
  195. } else {
  196. logrus.Warnf("failed to retrieve %s version: %s", defaultInitBinary, err)
  197. }
  198. }
  199. func fillDriverWarnings(v *types.Info) {
  200. for _, pair := range v.DriverStatus {
  201. if pair[0] == "Data loop file" {
  202. msg := fmt.Sprintf("WARNING: %s: usage of loopback devices is "+
  203. "strongly discouraged for production use.\n "+
  204. "Use `--storage-opt dm.thinpooldev` to specify a custom block storage device.", v.Driver)
  205. v.Warnings = append(v.Warnings, msg)
  206. continue
  207. }
  208. if pair[0] == "Supports d_type" && pair[1] == "false" {
  209. backingFs := getBackingFs(v)
  210. msg := fmt.Sprintf("WARNING: %s: the backing %s filesystem is formatted without d_type support, which leads to incorrect behavior.\n", v.Driver, backingFs)
  211. if backingFs == "xfs" {
  212. msg += " Reformat the filesystem with ftype=1 to enable d_type support.\n"
  213. }
  214. msg += " Running without d_type support will not be supported in future releases."
  215. v.Warnings = append(v.Warnings, msg)
  216. continue
  217. }
  218. }
  219. }
  220. func getBackingFs(v *types.Info) string {
  221. for _, pair := range v.DriverStatus {
  222. if pair[0] == "Backing Filesystem" {
  223. return pair[1]
  224. }
  225. }
  226. return ""
  227. }
  228. // parseInitVersion parses a Tini version string, and extracts the "version"
  229. // and "git commit" from the output.
  230. //
  231. // Output example from `docker-init --version`:
  232. //
  233. // tini version 0.18.0 - git.fec3683
  234. func parseInitVersion(v string) (version string, commit string, err error) {
  235. parts := strings.Split(v, " - ")
  236. if len(parts) >= 2 {
  237. gitParts := strings.Split(strings.TrimSpace(parts[1]), ".")
  238. if len(gitParts) == 2 && gitParts[0] == "git" {
  239. commit = gitParts[1]
  240. }
  241. }
  242. parts[0] = strings.TrimSpace(parts[0])
  243. if strings.HasPrefix(parts[0], "tini version ") {
  244. version = strings.TrimPrefix(parts[0], "tini version ")
  245. }
  246. if version == "" && commit == "" {
  247. err = errors.Errorf("unknown output format: %s", v)
  248. }
  249. return version, commit, err
  250. }
  251. // parseRuntimeVersion parses the output of `[runtime] --version` and extracts the
  252. // "name", "version" and "git commit" from the output.
  253. //
  254. // Output example from `runc --version`:
  255. //
  256. // runc version 1.0.0-rc5+dev
  257. // commit: 69663f0bd4b60df09991c08812a60108003fa340
  258. // spec: 1.0.0
  259. func parseRuntimeVersion(v string) (runtime string, version string, commit string, err error) {
  260. lines := strings.Split(strings.TrimSpace(v), "\n")
  261. for _, line := range lines {
  262. if strings.Contains(line, "version") {
  263. s := strings.Split(line, "version")
  264. runtime = strings.TrimSpace(s[0])
  265. version = strings.TrimSpace(s[len(s)-1])
  266. continue
  267. }
  268. if strings.HasPrefix(line, "commit:") {
  269. commit = strings.TrimSpace(strings.TrimPrefix(line, "commit:"))
  270. continue
  271. }
  272. }
  273. if version == "" && commit == "" {
  274. err = errors.Errorf("unknown output format: %s", v)
  275. }
  276. return runtime, version, commit, err
  277. }
  278. func (daemon *Daemon) cgroupNamespacesEnabled(sysInfo *sysinfo.SysInfo) bool {
  279. return sysInfo.CgroupNamespaces && containertypes.CgroupnsMode(daemon.configStore.CgroupNamespaceMode).IsPrivate()
  280. }
  281. // Rootless returns true if daemon is running in rootless mode
  282. func (daemon *Daemon) Rootless() bool {
  283. return daemon.configStore.Rootless
  284. }