info_unix.go 13 KB

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