info_unix.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 cgroups. Systemd is required to enable cgroups in rootless-mode.")
  81. } else {
  82. 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.")
  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.KernelMemoryTCP {
  92. v.Warnings = append(v.Warnings, "WARNING: No kernel memory TCP limit support")
  93. }
  94. if !v.OomKillDisable {
  95. v.Warnings = append(v.Warnings, "WARNING: No oom kill disable support")
  96. }
  97. if !v.CPUCfsQuota {
  98. v.Warnings = append(v.Warnings, "WARNING: No cpu cfs quota support")
  99. }
  100. if !v.CPUCfsPeriod {
  101. v.Warnings = append(v.Warnings, "WARNING: No cpu cfs period support")
  102. }
  103. if !v.CPUShares {
  104. v.Warnings = append(v.Warnings, "WARNING: No cpu shares support")
  105. }
  106. if !v.CPUSet {
  107. v.Warnings = append(v.Warnings, "WARNING: No cpuset support")
  108. }
  109. if v.CgroupVersion == "2" {
  110. v.Warnings = append(v.Warnings, "WARNING: Support for cgroup v2 is experimental")
  111. }
  112. // TODO add fields for these options in types.Info
  113. if !sysInfo.BlkioWeight {
  114. v.Warnings = append(v.Warnings, "WARNING: No blkio weight support")
  115. }
  116. if !sysInfo.BlkioWeightDevice {
  117. v.Warnings = append(v.Warnings, "WARNING: No blkio weight_device support")
  118. }
  119. if !sysInfo.BlkioReadBpsDevice {
  120. v.Warnings = append(v.Warnings, "WARNING: No blkio throttle.read_bps_device support")
  121. }
  122. if !sysInfo.BlkioWriteBpsDevice {
  123. v.Warnings = append(v.Warnings, "WARNING: No blkio throttle.write_bps_device support")
  124. }
  125. if !sysInfo.BlkioReadIOpsDevice {
  126. v.Warnings = append(v.Warnings, "WARNING: No blkio throttle.read_iops_device support")
  127. }
  128. if !sysInfo.BlkioWriteIOpsDevice {
  129. v.Warnings = append(v.Warnings, "WARNING: No blkio throttle.write_iops_device support")
  130. }
  131. }
  132. if !v.IPv4Forwarding {
  133. v.Warnings = append(v.Warnings, "WARNING: IPv4 forwarding is disabled")
  134. }
  135. if !v.BridgeNfIptables {
  136. v.Warnings = append(v.Warnings, "WARNING: bridge-nf-call-iptables is disabled")
  137. }
  138. if !v.BridgeNfIP6tables {
  139. v.Warnings = append(v.Warnings, "WARNING: bridge-nf-call-ip6tables is disabled")
  140. }
  141. }
  142. func (daemon *Daemon) fillPlatformVersion(v *types.Version) {
  143. if rv, err := daemon.containerd.Version(context.Background()); err == nil {
  144. v.Components = append(v.Components, types.ComponentVersion{
  145. Name: "containerd",
  146. Version: rv.Version,
  147. Details: map[string]string{
  148. "GitCommit": rv.Revision,
  149. },
  150. })
  151. }
  152. defaultRuntime := daemon.configStore.GetDefaultRuntimeName()
  153. defaultRuntimeBinary := daemon.configStore.GetRuntime(defaultRuntime).Path
  154. if rv, err := exec.Command(defaultRuntimeBinary, "--version").Output(); err == nil {
  155. if _, ver, commit, err := parseRuntimeVersion(string(rv)); err != nil {
  156. logrus.Warnf("failed to parse %s version: %v", defaultRuntimeBinary, err)
  157. } else {
  158. v.Components = append(v.Components, types.ComponentVersion{
  159. Name: defaultRuntime,
  160. Version: ver,
  161. Details: map[string]string{
  162. "GitCommit": commit,
  163. },
  164. })
  165. }
  166. } else {
  167. logrus.Warnf("failed to retrieve %s version: %v", defaultRuntimeBinary, err)
  168. }
  169. defaultInitBinary := daemon.configStore.GetInitPath()
  170. if rv, err := exec.Command(defaultInitBinary, "--version").Output(); err == nil {
  171. if ver, commit, err := parseInitVersion(string(rv)); err != nil {
  172. logrus.Warnf("failed to parse %s version: %s", defaultInitBinary, err)
  173. } else {
  174. v.Components = append(v.Components, types.ComponentVersion{
  175. Name: filepath.Base(defaultInitBinary),
  176. Version: ver,
  177. Details: map[string]string{
  178. "GitCommit": commit,
  179. },
  180. })
  181. }
  182. } else {
  183. logrus.Warnf("failed to retrieve %s version: %s", defaultInitBinary, err)
  184. }
  185. }
  186. func fillDriverWarnings(v *types.Info) {
  187. for _, pair := range v.DriverStatus {
  188. if pair[0] == "Data loop file" {
  189. msg := fmt.Sprintf("WARNING: %s: usage of loopback devices is "+
  190. "strongly discouraged for production use.\n "+
  191. "Use `--storage-opt dm.thinpooldev` to specify a custom block storage device.", v.Driver)
  192. v.Warnings = append(v.Warnings, msg)
  193. continue
  194. }
  195. if pair[0] == "Supports d_type" && pair[1] == "false" {
  196. backingFs := getBackingFs(v)
  197. msg := fmt.Sprintf("WARNING: %s: the backing %s filesystem is formatted without d_type support, which leads to incorrect behavior.\n", v.Driver, backingFs)
  198. if backingFs == "xfs" {
  199. msg += " Reformat the filesystem with ftype=1 to enable d_type support.\n"
  200. }
  201. msg += " Running without d_type support will not be supported in future releases."
  202. v.Warnings = append(v.Warnings, msg)
  203. continue
  204. }
  205. }
  206. }
  207. func getBackingFs(v *types.Info) string {
  208. for _, pair := range v.DriverStatus {
  209. if pair[0] == "Backing Filesystem" {
  210. return pair[1]
  211. }
  212. }
  213. return ""
  214. }
  215. // parseInitVersion parses a Tini version string, and extracts the "version"
  216. // and "git commit" from the output.
  217. //
  218. // Output example from `docker-init --version`:
  219. //
  220. // tini version 0.18.0 - git.fec3683
  221. func parseInitVersion(v string) (version string, commit string, err error) {
  222. parts := strings.Split(v, " - ")
  223. if len(parts) >= 2 {
  224. gitParts := strings.Split(strings.TrimSpace(parts[1]), ".")
  225. if len(gitParts) == 2 && gitParts[0] == "git" {
  226. commit = gitParts[1]
  227. }
  228. }
  229. parts[0] = strings.TrimSpace(parts[0])
  230. if strings.HasPrefix(parts[0], "tini version ") {
  231. version = strings.TrimPrefix(parts[0], "tini version ")
  232. }
  233. if version == "" && commit == "" {
  234. err = errors.Errorf("unknown output format: %s", v)
  235. }
  236. return version, commit, err
  237. }
  238. // parseRuntimeVersion parses the output of `[runtime] --version` and extracts the
  239. // "name", "version" and "git commit" from the output.
  240. //
  241. // Output example from `runc --version`:
  242. //
  243. // runc version 1.0.0-rc5+dev
  244. // commit: 69663f0bd4b60df09991c08812a60108003fa340
  245. // spec: 1.0.0
  246. func parseRuntimeVersion(v string) (runtime string, version string, commit string, err error) {
  247. lines := strings.Split(strings.TrimSpace(v), "\n")
  248. for _, line := range lines {
  249. if strings.Contains(line, "version") {
  250. s := strings.Split(line, "version")
  251. runtime = strings.TrimSpace(s[0])
  252. version = strings.TrimSpace(s[len(s)-1])
  253. continue
  254. }
  255. if strings.HasPrefix(line, "commit:") {
  256. commit = strings.TrimSpace(strings.TrimPrefix(line, "commit:"))
  257. continue
  258. }
  259. }
  260. if version == "" && commit == "" {
  261. err = errors.Errorf("unknown output format: %s", v)
  262. }
  263. return runtime, version, commit, err
  264. }
  265. func (daemon *Daemon) cgroupNamespacesEnabled(sysInfo *sysinfo.SysInfo) bool {
  266. return sysInfo.CgroupNamespaces && containertypes.CgroupnsMode(daemon.configStore.CgroupNamespaceMode).IsPrivate()
  267. }
  268. // Rootless returns true if daemon is running in rootless mode
  269. func (daemon *Daemon) Rootless() bool {
  270. return daemon.configStore.Rootless
  271. }