info_unix.go 12 KB

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