info_unix.go 12 KB

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