info_unix.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // +build !windows
  2. package daemon // import "github.com/docker/docker/daemon"
  3. import (
  4. "context"
  5. "fmt"
  6. "os/exec"
  7. "path/filepath"
  8. "strings"
  9. "github.com/docker/docker/api/types"
  10. containertypes "github.com/docker/docker/api/types/container"
  11. "github.com/docker/docker/dockerversion"
  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. v.MemoryLimit = sysInfo.MemoryLimit
  24. v.SwapLimit = sysInfo.SwapLimit
  25. v.KernelMemory = sysInfo.KernelMemory
  26. v.KernelMemoryTCP = sysInfo.KernelMemoryTCP
  27. v.OomKillDisable = sysInfo.OomKillDisable
  28. v.CPUCfsPeriod = sysInfo.CPUCfs
  29. v.CPUCfsQuota = sysInfo.CPUCfs
  30. v.CPUShares = sysInfo.CPUShares
  31. v.CPUSet = sysInfo.Cpuset
  32. v.PidsLimit = sysInfo.PidsLimit
  33. v.Runtimes = daemon.configStore.GetAllRuntimes()
  34. v.DefaultRuntime = daemon.configStore.GetDefaultRuntimeName()
  35. v.InitBinary = daemon.configStore.GetInitPath()
  36. defaultRuntimeBinary := daemon.configStore.GetRuntime(v.DefaultRuntime).Path
  37. if rv, err := exec.Command(defaultRuntimeBinary, "--version").Output(); err == nil {
  38. if _, _, commit, err := parseRuntimeVersion(string(rv)); err != nil {
  39. logrus.Warnf("failed to parse %s version: %v", defaultRuntimeBinary, err)
  40. v.RuncCommit.ID = "N/A"
  41. } else {
  42. v.RuncCommit.ID = commit
  43. }
  44. } else {
  45. logrus.Warnf("failed to retrieve %s version: %v", defaultRuntimeBinary, err)
  46. v.RuncCommit.ID = "N/A"
  47. }
  48. // runc is now shipped as a separate package. Set "expected" to same value
  49. // as "ID" to prevent clients from reporting a version-mismatch
  50. v.RuncCommit.Expected = v.RuncCommit.ID
  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. v.ContainerdCommit.ID = "N/A"
  56. }
  57. // containerd is now shipped as a separate package. Set "expected" to same
  58. // value as "ID" to prevent clients from reporting a version-mismatch
  59. v.ContainerdCommit.Expected = v.ContainerdCommit.ID
  60. // TODO is there still a need to check the expected version for tini?
  61. // if not, we can change this, and just set "Expected" to v.InitCommit.ID
  62. v.InitCommit.Expected = dockerversion.InitCommitID
  63. defaultInitBinary := daemon.configStore.GetInitPath()
  64. if rv, err := exec.Command(defaultInitBinary, "--version").Output(); err == nil {
  65. if _, commit, err := parseInitVersion(string(rv)); err != nil {
  66. logrus.Warnf("failed to parse %s version: %s", defaultInitBinary, err)
  67. v.InitCommit.ID = "N/A"
  68. } else {
  69. v.InitCommit.ID = commit
  70. if len(dockerversion.InitCommitID) > len(commit) {
  71. v.InitCommit.Expected = dockerversion.InitCommitID[0:len(commit)]
  72. }
  73. }
  74. } else {
  75. logrus.Warnf("failed to retrieve %s version: %s", defaultInitBinary, err)
  76. v.InitCommit.ID = "N/A"
  77. }
  78. if v.CgroupDriver == cgroupNoneDriver {
  79. if v.CgroupVersion == "2" {
  80. v.Warnings = append(v.Warnings, "WARNING: Running in rootless-mode without cgroup. To enable cgroup in rootless-mode, you need to set exec-opt \"native.cgroupdriver=systemd\".")
  81. } else {
  82. v.Warnings = append(v.Warnings, "WARNING: Running in rootless-mode without cgroup. To enable cgroup in rootless-mode, you need to boot the system in cgroup v2 mode and set exec-opt \"native.cgroupdriver=systemd\".")
  83. }
  84. } else {
  85. if !v.MemoryLimit {
  86. v.Warnings = append(v.Warnings, "WARNING: No memory limit support")
  87. }
  88. if !v.SwapLimit {
  89. v.Warnings = append(v.Warnings, "WARNING: No swap limit support")
  90. }
  91. if !v.KernelMemory {
  92. v.Warnings = append(v.Warnings, "WARNING: No kernel memory limit support")
  93. }
  94. if !v.KernelMemoryTCP {
  95. v.Warnings = append(v.Warnings, "WARNING: No kernel memory TCP limit support")
  96. }
  97. if !v.OomKillDisable {
  98. v.Warnings = append(v.Warnings, "WARNING: No oom kill disable support")
  99. }
  100. if !v.CPUCfsQuota {
  101. v.Warnings = append(v.Warnings, "WARNING: No cpu cfs quota support")
  102. }
  103. if !v.CPUCfsPeriod {
  104. v.Warnings = append(v.Warnings, "WARNING: No cpu cfs period support")
  105. }
  106. if !v.CPUShares {
  107. v.Warnings = append(v.Warnings, "WARNING: No cpu shares support")
  108. }
  109. if !v.CPUSet {
  110. v.Warnings = append(v.Warnings, "WARNING: No cpuset support")
  111. }
  112. if v.CgroupVersion == "2" {
  113. v.Warnings = append(v.Warnings, "WARNING: Support for cgroup v2 is experimental")
  114. }
  115. }
  116. if !v.IPv4Forwarding {
  117. v.Warnings = append(v.Warnings, "WARNING: IPv4 forwarding is disabled")
  118. }
  119. if !v.BridgeNfIptables {
  120. v.Warnings = append(v.Warnings, "WARNING: bridge-nf-call-iptables is disabled")
  121. }
  122. if !v.BridgeNfIP6tables {
  123. v.Warnings = append(v.Warnings, "WARNING: bridge-nf-call-ip6tables is disabled")
  124. }
  125. }
  126. func (daemon *Daemon) fillPlatformVersion(v *types.Version) {
  127. if rv, err := daemon.containerd.Version(context.Background()); err == nil {
  128. v.Components = append(v.Components, types.ComponentVersion{
  129. Name: "containerd",
  130. Version: rv.Version,
  131. Details: map[string]string{
  132. "GitCommit": rv.Revision,
  133. },
  134. })
  135. }
  136. defaultRuntime := daemon.configStore.GetDefaultRuntimeName()
  137. defaultRuntimeBinary := daemon.configStore.GetRuntime(defaultRuntime).Path
  138. if rv, err := exec.Command(defaultRuntimeBinary, "--version").Output(); err == nil {
  139. if _, ver, commit, err := parseRuntimeVersion(string(rv)); err != nil {
  140. logrus.Warnf("failed to parse %s version: %v", defaultRuntimeBinary, err)
  141. } else {
  142. v.Components = append(v.Components, types.ComponentVersion{
  143. Name: defaultRuntime,
  144. Version: ver,
  145. Details: map[string]string{
  146. "GitCommit": commit,
  147. },
  148. })
  149. }
  150. } else {
  151. logrus.Warnf("failed to retrieve %s version: %v", defaultRuntimeBinary, err)
  152. }
  153. defaultInitBinary := daemon.configStore.GetInitPath()
  154. if rv, err := exec.Command(defaultInitBinary, "--version").Output(); err == nil {
  155. if ver, commit, err := parseInitVersion(string(rv)); err != nil {
  156. logrus.Warnf("failed to parse %s version: %s", defaultInitBinary, err)
  157. } else {
  158. v.Components = append(v.Components, types.ComponentVersion{
  159. Name: filepath.Base(defaultInitBinary),
  160. Version: ver,
  161. Details: map[string]string{
  162. "GitCommit": commit,
  163. },
  164. })
  165. }
  166. } else {
  167. logrus.Warnf("failed to retrieve %s version: %s", defaultInitBinary, err)
  168. }
  169. }
  170. func fillDriverWarnings(v *types.Info) {
  171. for _, pair := range v.DriverStatus {
  172. if pair[0] == "Data loop file" {
  173. msg := fmt.Sprintf("WARNING: %s: usage of loopback devices is "+
  174. "strongly discouraged for production use.\n "+
  175. "Use `--storage-opt dm.thinpooldev` to specify a custom block storage device.", v.Driver)
  176. v.Warnings = append(v.Warnings, msg)
  177. continue
  178. }
  179. if pair[0] == "Supports d_type" && pair[1] == "false" {
  180. backingFs := getBackingFs(v)
  181. msg := fmt.Sprintf("WARNING: %s: the backing %s filesystem is formatted without d_type support, which leads to incorrect behavior.\n", v.Driver, backingFs)
  182. if backingFs == "xfs" {
  183. msg += " Reformat the filesystem with ftype=1 to enable d_type support.\n"
  184. }
  185. msg += " Running without d_type support will not be supported in future releases."
  186. v.Warnings = append(v.Warnings, msg)
  187. continue
  188. }
  189. }
  190. }
  191. func getBackingFs(v *types.Info) string {
  192. for _, pair := range v.DriverStatus {
  193. if pair[0] == "Backing Filesystem" {
  194. return pair[1]
  195. }
  196. }
  197. return ""
  198. }
  199. // parseInitVersion parses a Tini version string, and extracts the "version"
  200. // and "git commit" from the output.
  201. //
  202. // Output example from `docker-init --version`:
  203. //
  204. // tini version 0.18.0 - git.fec3683
  205. func parseInitVersion(v string) (version string, commit string, err error) {
  206. parts := strings.Split(v, " - ")
  207. if len(parts) >= 2 {
  208. gitParts := strings.Split(strings.TrimSpace(parts[1]), ".")
  209. if len(gitParts) == 2 && gitParts[0] == "git" {
  210. commit = gitParts[1]
  211. }
  212. }
  213. parts[0] = strings.TrimSpace(parts[0])
  214. if strings.HasPrefix(parts[0], "tini version ") {
  215. version = strings.TrimPrefix(parts[0], "tini version ")
  216. }
  217. if version == "" && commit == "" {
  218. err = errors.Errorf("unknown output format: %s", v)
  219. }
  220. return version, commit, err
  221. }
  222. // parseRuntimeVersion parses the output of `[runtime] --version` and extracts the
  223. // "name", "version" and "git commit" from the output.
  224. //
  225. // Output example from `runc --version`:
  226. //
  227. // runc version 1.0.0-rc5+dev
  228. // commit: 69663f0bd4b60df09991c08812a60108003fa340
  229. // spec: 1.0.0
  230. func parseRuntimeVersion(v string) (runtime string, version string, commit string, err error) {
  231. lines := strings.Split(strings.TrimSpace(v), "\n")
  232. for _, line := range lines {
  233. if strings.Contains(line, "version") {
  234. s := strings.Split(line, "version")
  235. runtime = strings.TrimSpace(s[0])
  236. version = strings.TrimSpace(s[len(s)-1])
  237. continue
  238. }
  239. if strings.HasPrefix(line, "commit:") {
  240. commit = strings.TrimSpace(strings.TrimPrefix(line, "commit:"))
  241. continue
  242. }
  243. }
  244. if version == "" && commit == "" {
  245. err = errors.Errorf("unknown output format: %s", v)
  246. }
  247. return runtime, version, commit, err
  248. }
  249. func (daemon *Daemon) cgroupNamespacesEnabled(sysInfo *sysinfo.SysInfo) bool {
  250. return sysInfo.CgroupNamespaces && containertypes.CgroupnsMode(daemon.configStore.CgroupNamespaceMode).IsPrivate()
  251. }
  252. // Rootless returns true if daemon is running in rootless mode
  253. func (daemon *Daemon) Rootless() bool {
  254. return daemon.configStore.Rootless
  255. }