info_unix.go 11 KB

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