info_unix.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. if rt := daemon.configStore.GetRuntime(v.DefaultRuntime); rt != nil {
  43. if rv, err := exec.Command(rt.Path, "--version").Output(); err == nil {
  44. if _, _, commit, err := parseRuntimeVersion(string(rv)); err != nil {
  45. logrus.Warnf("failed to parse %s version: %v", rt.Path, err)
  46. } else {
  47. v.RuncCommit.ID = commit
  48. }
  49. } else {
  50. logrus.Warnf("failed to retrieve %s version: %v", rt.Path, err)
  51. }
  52. }
  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. }
  58. defaultInitBinary := daemon.configStore.GetInitPath()
  59. if rv, err := exec.Command(defaultInitBinary, "--version").Output(); err == nil {
  60. if _, commit, err := parseInitVersion(string(rv)); err != nil {
  61. logrus.Warnf("failed to parse %s version: %s", defaultInitBinary, err)
  62. } else {
  63. v.InitCommit.ID = commit
  64. }
  65. } else {
  66. logrus.Warnf("failed to retrieve %s version: %s", defaultInitBinary, 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. defaultInitBinary := daemon.configStore.GetInitPath()
  186. if rv, err := exec.Command(defaultInitBinary, "--version").Output(); err == nil {
  187. if ver, commit, err := parseInitVersion(string(rv)); err != nil {
  188. logrus.Warnf("failed to parse %s version: %s", defaultInitBinary, err)
  189. } else {
  190. v.Components = append(v.Components, types.ComponentVersion{
  191. Name: filepath.Base(defaultInitBinary),
  192. Version: ver,
  193. Details: map[string]string{
  194. "GitCommit": commit,
  195. },
  196. })
  197. }
  198. } else {
  199. logrus.Warnf("failed to retrieve %s version: %s", defaultInitBinary, err)
  200. }
  201. daemon.fillRootlessVersion(v)
  202. }
  203. func (daemon *Daemon) fillRootlessVersion(v *types.Version) {
  204. if !rootless.RunningWithRootlessKit() {
  205. return
  206. }
  207. rlc, err := rootless.GetRootlessKitClient()
  208. if err != nil {
  209. logrus.Warnf("failed to create RootlessKit client: %v", err)
  210. return
  211. }
  212. rlInfo, err := rlc.Info(context.TODO())
  213. if err != nil {
  214. logrus.Warnf("failed to retrieve RootlessKit version: %v", err)
  215. return
  216. }
  217. v.Components = append(v.Components, types.ComponentVersion{
  218. Name: "rootlesskit",
  219. Version: rlInfo.Version,
  220. Details: map[string]string{
  221. "ApiVersion": rlInfo.APIVersion,
  222. "StateDir": rlInfo.StateDir,
  223. "NetworkDriver": rlInfo.NetworkDriver.Driver,
  224. "PortDriver": rlInfo.PortDriver.Driver,
  225. },
  226. })
  227. switch rlInfo.NetworkDriver.Driver {
  228. case "slirp4netns":
  229. if rv, err := exec.Command("slirp4netns", "--version").Output(); err == nil {
  230. if _, ver, commit, err := parseRuntimeVersion(string(rv)); err != nil {
  231. logrus.Warnf("failed to parse slirp4netns version: %v", err)
  232. } else {
  233. v.Components = append(v.Components, types.ComponentVersion{
  234. Name: "slirp4netns",
  235. Version: ver,
  236. Details: map[string]string{
  237. "GitCommit": commit,
  238. },
  239. })
  240. }
  241. } else {
  242. logrus.Warnf("failed to retrieve slirp4netns version: %v", err)
  243. }
  244. case "vpnkit":
  245. if rv, err := exec.Command("vpnkit", "--version").Output(); err == nil {
  246. v.Components = append(v.Components, types.ComponentVersion{
  247. Name: "vpnkit",
  248. Version: strings.TrimSpace(string(rv)),
  249. })
  250. } else {
  251. logrus.Warnf("failed to retrieve vpnkit version: %v", err)
  252. }
  253. }
  254. }
  255. func fillDriverWarnings(v *types.Info) {
  256. for _, pair := range v.DriverStatus {
  257. if pair[0] == "Data loop file" {
  258. msg := fmt.Sprintf("WARNING: %s: usage of loopback devices is "+
  259. "strongly discouraged for production use.\n "+
  260. "Use `--storage-opt dm.thinpooldev` to specify a custom block storage device.", v.Driver)
  261. v.Warnings = append(v.Warnings, msg)
  262. continue
  263. }
  264. if pair[0] == "Supports d_type" && pair[1] == "false" {
  265. backingFs := getBackingFs(v)
  266. msg := fmt.Sprintf("WARNING: %s: the backing %s filesystem is formatted without d_type support, which leads to incorrect behavior.\n", v.Driver, backingFs)
  267. if backingFs == "xfs" {
  268. msg += " Reformat the filesystem with ftype=1 to enable d_type support.\n"
  269. }
  270. msg += " Running without d_type support will not be supported in future releases."
  271. v.Warnings = append(v.Warnings, msg)
  272. continue
  273. }
  274. }
  275. }
  276. func getBackingFs(v *types.Info) string {
  277. for _, pair := range v.DriverStatus {
  278. if pair[0] == "Backing Filesystem" {
  279. return pair[1]
  280. }
  281. }
  282. return ""
  283. }
  284. // parseInitVersion parses a Tini version string, and extracts the "version"
  285. // and "git commit" from the output.
  286. //
  287. // Output example from `docker-init --version`:
  288. //
  289. // tini version 0.18.0 - git.fec3683
  290. func parseInitVersion(v string) (version string, commit string, err error) {
  291. parts := strings.Split(v, " - ")
  292. if len(parts) >= 2 {
  293. gitParts := strings.Split(strings.TrimSpace(parts[1]), ".")
  294. if len(gitParts) == 2 && gitParts[0] == "git" {
  295. commit = gitParts[1]
  296. }
  297. }
  298. parts[0] = strings.TrimSpace(parts[0])
  299. if strings.HasPrefix(parts[0], "tini version ") {
  300. version = strings.TrimPrefix(parts[0], "tini version ")
  301. }
  302. if version == "" && commit == "" {
  303. err = errors.Errorf("unknown output format: %s", v)
  304. }
  305. return version, commit, err
  306. }
  307. // parseRuntimeVersion parses the output of `[runtime] --version` and extracts the
  308. // "name", "version" and "git commit" from the output.
  309. //
  310. // Output example from `runc --version`:
  311. //
  312. // runc version 1.0.0-rc5+dev
  313. // commit: 69663f0bd4b60df09991c08812a60108003fa340
  314. // spec: 1.0.0
  315. func parseRuntimeVersion(v string) (runtime string, version string, commit string, err error) {
  316. lines := strings.Split(strings.TrimSpace(v), "\n")
  317. for _, line := range lines {
  318. if strings.Contains(line, "version") {
  319. s := strings.Split(line, "version")
  320. runtime = strings.TrimSpace(s[0])
  321. version = strings.TrimSpace(s[len(s)-1])
  322. continue
  323. }
  324. if strings.HasPrefix(line, "commit:") {
  325. commit = strings.TrimSpace(strings.TrimPrefix(line, "commit:"))
  326. continue
  327. }
  328. }
  329. if version == "" && commit == "" {
  330. err = errors.Errorf("unknown output format: %s", v)
  331. }
  332. return runtime, version, commit, err
  333. }
  334. func (daemon *Daemon) cgroupNamespacesEnabled(sysInfo *sysinfo.SysInfo) bool {
  335. return sysInfo.CgroupNamespaces && containertypes.CgroupnsMode(daemon.configStore.CgroupNamespaceMode).IsPrivate()
  336. }
  337. // Rootless returns true if daemon is running in rootless mode
  338. func (daemon *Daemon) Rootless() bool {
  339. return daemon.configStore.Rootless
  340. }