info_unix.go 12 KB

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