info_unix.go 10 KB

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