info_unix.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. }
  158. func (daemon *Daemon) fillPlatformVersion(v *types.Version) {
  159. if rv, err := daemon.containerd.Version(context.Background()); err == nil {
  160. v.Components = append(v.Components, types.ComponentVersion{
  161. Name: "containerd",
  162. Version: rv.Version,
  163. Details: map[string]string{
  164. "GitCommit": rv.Revision,
  165. },
  166. })
  167. }
  168. defaultRuntime := daemon.configStore.GetDefaultRuntimeName()
  169. if rt := daemon.configStore.GetRuntime(defaultRuntime); rt != nil {
  170. if rv, err := exec.Command(rt.Path, "--version").Output(); err == nil {
  171. if _, ver, commit, err := parseRuntimeVersion(string(rv)); err != nil {
  172. logrus.Warnf("failed to parse %s version: %v", rt.Path, err)
  173. } else {
  174. v.Components = append(v.Components, types.ComponentVersion{
  175. Name: defaultRuntime,
  176. Version: ver,
  177. Details: map[string]string{
  178. "GitCommit": commit,
  179. },
  180. })
  181. }
  182. } else {
  183. logrus.Warnf("failed to retrieve %s version: %v", rt.Path, err)
  184. }
  185. }
  186. if initBinary, err := daemon.configStore.LookupInitPath(); err != nil {
  187. logrus.Warnf("failed to find docker-init: %s", err)
  188. } else if rv, err := exec.Command(initBinary, "--version").Output(); err == nil {
  189. if ver, commit, err := parseInitVersion(string(rv)); err != nil {
  190. logrus.Warnf("failed to parse %s version: %s", initBinary, err)
  191. } else {
  192. v.Components = append(v.Components, types.ComponentVersion{
  193. Name: filepath.Base(initBinary),
  194. Version: ver,
  195. Details: map[string]string{
  196. "GitCommit": commit,
  197. },
  198. })
  199. }
  200. } else {
  201. logrus.Warnf("failed to retrieve %s version: %s", initBinary, err)
  202. }
  203. daemon.fillRootlessVersion(v)
  204. }
  205. func (daemon *Daemon) fillRootlessVersion(v *types.Version) {
  206. if !rootless.RunningWithRootlessKit() {
  207. return
  208. }
  209. rlc, err := rootless.GetRootlessKitClient()
  210. if err != nil {
  211. logrus.Warnf("failed to create RootlessKit client: %v", err)
  212. return
  213. }
  214. rlInfo, err := rlc.Info(context.TODO())
  215. if err != nil {
  216. logrus.Warnf("failed to retrieve RootlessKit version: %v", err)
  217. return
  218. }
  219. v.Components = append(v.Components, types.ComponentVersion{
  220. Name: "rootlesskit",
  221. Version: rlInfo.Version,
  222. Details: map[string]string{
  223. "ApiVersion": rlInfo.APIVersion,
  224. "StateDir": rlInfo.StateDir,
  225. "NetworkDriver": rlInfo.NetworkDriver.Driver,
  226. "PortDriver": rlInfo.PortDriver.Driver,
  227. },
  228. })
  229. switch rlInfo.NetworkDriver.Driver {
  230. case "slirp4netns":
  231. if rv, err := exec.Command("slirp4netns", "--version").Output(); err == nil {
  232. if _, ver, commit, err := parseRuntimeVersion(string(rv)); err != nil {
  233. logrus.Warnf("failed to parse slirp4netns version: %v", err)
  234. } else {
  235. v.Components = append(v.Components, types.ComponentVersion{
  236. Name: "slirp4netns",
  237. Version: ver,
  238. Details: map[string]string{
  239. "GitCommit": commit,
  240. },
  241. })
  242. }
  243. } else {
  244. logrus.Warnf("failed to retrieve slirp4netns version: %v", err)
  245. }
  246. case "vpnkit":
  247. if rv, err := exec.Command("vpnkit", "--version").Output(); err == nil {
  248. v.Components = append(v.Components, types.ComponentVersion{
  249. Name: "vpnkit",
  250. Version: strings.TrimSpace(string(rv)),
  251. })
  252. } else {
  253. logrus.Warnf("failed to retrieve vpnkit version: %v", err)
  254. }
  255. }
  256. }
  257. func fillDriverWarnings(v *types.Info) {
  258. for _, pair := range v.DriverStatus {
  259. if pair[0] == "Extended file attributes" && pair[1] == "best-effort" {
  260. msg := fmt.Sprintf("WARNING: %s: extended file attributes from container images "+
  261. "will be silently discarded if the backing filesystem does not support them.\n"+
  262. " CONTAINERS MAY MALFUNCTION IF EXTENDED ATTRIBUTES ARE MISSING.\n"+
  263. " This is an UNSUPPORTABLE configuration for which no bug reports will be accepted.\n", v.Driver)
  264. v.Warnings = append(v.Warnings, msg)
  265. continue
  266. }
  267. }
  268. }
  269. // parseInitVersion parses a Tini version string, and extracts the "version"
  270. // and "git commit" from the output.
  271. //
  272. // Output example from `docker-init --version`:
  273. //
  274. // tini version 0.18.0 - git.fec3683
  275. func parseInitVersion(v string) (version string, commit string, err error) {
  276. parts := strings.Split(v, " - ")
  277. if len(parts) >= 2 {
  278. gitParts := strings.Split(strings.TrimSpace(parts[1]), ".")
  279. if len(gitParts) == 2 && gitParts[0] == "git" {
  280. commit = gitParts[1]
  281. }
  282. }
  283. parts[0] = strings.TrimSpace(parts[0])
  284. if strings.HasPrefix(parts[0], "tini version ") {
  285. version = strings.TrimPrefix(parts[0], "tini version ")
  286. }
  287. if version == "" && commit == "" {
  288. err = errors.Errorf("unknown output format: %s", v)
  289. }
  290. return version, commit, err
  291. }
  292. // parseRuntimeVersion parses the output of `[runtime] --version` and extracts the
  293. // "name", "version" and "git commit" from the output.
  294. //
  295. // Output example from `runc --version`:
  296. //
  297. // runc version 1.0.0-rc5+dev
  298. // commit: 69663f0bd4b60df09991c08812a60108003fa340
  299. // spec: 1.0.0
  300. func parseRuntimeVersion(v string) (runtime string, version string, commit string, err error) {
  301. lines := strings.Split(strings.TrimSpace(v), "\n")
  302. for _, line := range lines {
  303. if strings.Contains(line, "version") {
  304. s := strings.Split(line, "version")
  305. runtime = strings.TrimSpace(s[0])
  306. version = strings.TrimSpace(s[len(s)-1])
  307. continue
  308. }
  309. if strings.HasPrefix(line, "commit:") {
  310. commit = strings.TrimSpace(strings.TrimPrefix(line, "commit:"))
  311. continue
  312. }
  313. }
  314. if version == "" && commit == "" {
  315. err = errors.Errorf("unknown output format: %s", v)
  316. }
  317. return runtime, version, commit, err
  318. }
  319. func (daemon *Daemon) cgroupNamespacesEnabled(sysInfo *sysinfo.SysInfo) bool {
  320. return sysInfo.CgroupNamespaces && containertypes.CgroupnsMode(daemon.configStore.CgroupNamespaceMode).IsPrivate()
  321. }
  322. // Rootless returns true if daemon is running in rootless mode
  323. func (daemon *Daemon) Rootless() bool {
  324. return daemon.configStore.Rootless
  325. }
  326. func (daemon *Daemon) noNewPrivileges() bool {
  327. return daemon.configStore.NoNewPrivileges
  328. }