info_unix.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. //go:build !windows
  2. package daemon // import "github.com/docker/docker/daemon"
  3. import (
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "os"
  8. "os/exec"
  9. "path/filepath"
  10. "strings"
  11. v2runcoptions "github.com/containerd/containerd/runtime/v2/runc/options"
  12. "github.com/containerd/log"
  13. "github.com/docker/docker/api/types"
  14. containertypes "github.com/docker/docker/api/types/container"
  15. "github.com/docker/docker/api/types/system"
  16. "github.com/docker/docker/daemon/config"
  17. "github.com/docker/docker/errdefs"
  18. "github.com/docker/docker/pkg/rootless"
  19. "github.com/docker/docker/pkg/sysinfo"
  20. "github.com/pkg/errors"
  21. rkclient "github.com/rootless-containers/rootlesskit/v2/pkg/api/client"
  22. )
  23. // fillPlatformInfo fills the platform related info.
  24. func (daemon *Daemon) fillPlatformInfo(ctx context.Context, v *system.Info, sysInfo *sysinfo.SysInfo, cfg *configStore) error {
  25. v.CgroupDriver = cgroupDriver(&cfg.Config)
  26. v.CgroupVersion = "1"
  27. if sysInfo.CgroupUnified {
  28. v.CgroupVersion = "2"
  29. }
  30. if v.CgroupDriver != cgroupNoneDriver {
  31. v.MemoryLimit = sysInfo.MemoryLimit
  32. v.SwapLimit = sysInfo.SwapLimit
  33. v.KernelMemory = sysInfo.KernelMemory
  34. v.KernelMemoryTCP = sysInfo.KernelMemoryTCP
  35. v.OomKillDisable = sysInfo.OomKillDisable
  36. v.CPUCfsPeriod = sysInfo.CPUCfs
  37. v.CPUCfsQuota = sysInfo.CPUCfs
  38. v.CPUShares = sysInfo.CPUShares
  39. v.CPUSet = sysInfo.Cpuset
  40. v.PidsLimit = sysInfo.PidsLimit
  41. }
  42. v.Runtimes = make(map[string]system.RuntimeWithStatus)
  43. for n, p := range stockRuntimes() {
  44. v.Runtimes[n] = system.RuntimeWithStatus{
  45. Runtime: system.Runtime{
  46. Path: p,
  47. },
  48. Status: daemon.runtimeStatus(ctx, cfg, n),
  49. }
  50. }
  51. for n, r := range cfg.Config.Runtimes {
  52. v.Runtimes[n] = system.RuntimeWithStatus{
  53. Runtime: system.Runtime{
  54. Path: r.Path,
  55. Args: append([]string(nil), r.Args...),
  56. },
  57. Status: daemon.runtimeStatus(ctx, cfg, n),
  58. }
  59. }
  60. v.DefaultRuntime = cfg.Runtimes.Default
  61. v.RuncCommit.ID = "N/A"
  62. v.ContainerdCommit.ID = "N/A"
  63. v.InitCommit.ID = "N/A"
  64. if err := populateRuncCommit(&v.RuncCommit, cfg); err != nil {
  65. log.G(ctx).WithError(err).Warn("Failed to retrieve default runtime version")
  66. }
  67. if err := daemon.populateContainerdCommit(ctx, &v.ContainerdCommit); err != nil {
  68. return err
  69. }
  70. if err := daemon.populateInitCommit(ctx, v, cfg); err != nil {
  71. return 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. if v.CgroupDriver == cgroupNoneDriver {
  76. if v.CgroupVersion == "2" {
  77. v.Warnings = append(v.Warnings, "WARNING: Running in rootless-mode without cgroups. Systemd is required to enable cgroups in rootless-mode.")
  78. } else {
  79. 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.")
  80. }
  81. } else {
  82. if !v.MemoryLimit {
  83. v.Warnings = append(v.Warnings, "WARNING: No memory limit support")
  84. }
  85. if !v.SwapLimit {
  86. v.Warnings = append(v.Warnings, "WARNING: No swap limit support")
  87. }
  88. if !v.KernelMemoryTCP && v.CgroupVersion == "1" {
  89. // kernel memory is not available for cgroup v2.
  90. // Warning is not printed on cgroup v2, because there is no action user can take.
  91. v.Warnings = append(v.Warnings, "WARNING: No kernel memory TCP limit support")
  92. }
  93. if !v.OomKillDisable && v.CgroupVersion == "1" {
  94. // oom kill disable is not available for cgroup v2.
  95. // Warning is not printed on cgroup v2, because there is no action user can take.
  96. v.Warnings = append(v.Warnings, "WARNING: No oom kill disable support")
  97. }
  98. if !v.CPUCfsQuota {
  99. v.Warnings = append(v.Warnings, "WARNING: No cpu cfs quota support")
  100. }
  101. if !v.CPUCfsPeriod {
  102. v.Warnings = append(v.Warnings, "WARNING: No cpu cfs period support")
  103. }
  104. if !v.CPUShares {
  105. v.Warnings = append(v.Warnings, "WARNING: No cpu shares support")
  106. }
  107. if !v.CPUSet {
  108. v.Warnings = append(v.Warnings, "WARNING: No cpuset support")
  109. }
  110. // TODO add fields for these options in types.Info
  111. if !sysInfo.BlkioWeight && v.CgroupVersion == "2" {
  112. // blkio weight is not available on cgroup v1 since kernel 5.0.
  113. // Warning is not printed on cgroup v1, because there is no action user can take.
  114. // On cgroup v2, blkio weight is implemented using io.weight
  115. v.Warnings = append(v.Warnings, "WARNING: No io.weight support")
  116. }
  117. if !sysInfo.BlkioWeightDevice && v.CgroupVersion == "2" {
  118. v.Warnings = append(v.Warnings, "WARNING: No io.weight (per device) support")
  119. }
  120. if !sysInfo.BlkioReadBpsDevice {
  121. if v.CgroupVersion == "2" {
  122. v.Warnings = append(v.Warnings, "WARNING: No io.max (rbps) support")
  123. } else {
  124. v.Warnings = append(v.Warnings, "WARNING: No blkio throttle.read_bps_device support")
  125. }
  126. }
  127. if !sysInfo.BlkioWriteBpsDevice {
  128. if v.CgroupVersion == "2" {
  129. v.Warnings = append(v.Warnings, "WARNING: No io.max (wbps) support")
  130. } else {
  131. v.Warnings = append(v.Warnings, "WARNING: No blkio throttle.write_bps_device support")
  132. }
  133. }
  134. if !sysInfo.BlkioReadIOpsDevice {
  135. if v.CgroupVersion == "2" {
  136. v.Warnings = append(v.Warnings, "WARNING: No io.max (riops) support")
  137. } else {
  138. v.Warnings = append(v.Warnings, "WARNING: No blkio throttle.read_iops_device support")
  139. }
  140. }
  141. if !sysInfo.BlkioWriteIOpsDevice {
  142. if v.CgroupVersion == "2" {
  143. v.Warnings = append(v.Warnings, "WARNING: No io.max (wiops) support")
  144. } else {
  145. v.Warnings = append(v.Warnings, "WARNING: No blkio throttle.write_iops_device support")
  146. }
  147. }
  148. }
  149. if !v.IPv4Forwarding {
  150. v.Warnings = append(v.Warnings, "WARNING: IPv4 forwarding is disabled")
  151. }
  152. if !v.BridgeNfIptables {
  153. v.Warnings = append(v.Warnings, "WARNING: bridge-nf-call-iptables is disabled")
  154. }
  155. if !v.BridgeNfIP6tables {
  156. v.Warnings = append(v.Warnings, "WARNING: bridge-nf-call-ip6tables is disabled")
  157. }
  158. return nil
  159. }
  160. func (daemon *Daemon) fillPlatformVersion(ctx context.Context, v *types.Version, cfg *configStore) error {
  161. if err := daemon.populateContainerdVersion(ctx, v); err != nil {
  162. return err
  163. }
  164. if err := populateRuncVersion(cfg, v); err != nil {
  165. log.G(ctx).WithError(err).Warn("Failed to retrieve default runtime version")
  166. }
  167. if err := populateInitVersion(ctx, cfg, v); err != nil {
  168. return err
  169. }
  170. if err := daemon.fillRootlessVersion(ctx, v); err != nil {
  171. if errdefs.IsContext(err) {
  172. return err
  173. }
  174. log.G(ctx).WithError(err).Warn("Failed to fill rootless version")
  175. }
  176. return nil
  177. }
  178. func populateRuncCommit(v *system.Commit, cfg *configStore) error {
  179. _, _, commit, err := parseDefaultRuntimeVersion(&cfg.Runtimes)
  180. if err != nil {
  181. return err
  182. }
  183. v.ID = commit
  184. v.Expected = commit
  185. return nil
  186. }
  187. func (daemon *Daemon) populateInitCommit(ctx context.Context, v *system.Info, cfg *configStore) error {
  188. v.InitBinary = cfg.GetInitPath()
  189. initBinary, err := cfg.LookupInitPath()
  190. if err != nil {
  191. log.G(ctx).WithError(err).Warnf("Failed to find docker-init")
  192. return nil
  193. }
  194. rv, err := exec.CommandContext(ctx, initBinary, "--version").Output()
  195. if err != nil {
  196. if errdefs.IsContext(err) {
  197. return err
  198. }
  199. log.G(ctx).WithError(err).Warnf("Failed to retrieve %s version", initBinary)
  200. return nil
  201. }
  202. _, commit, err := parseInitVersion(string(rv))
  203. if err != nil {
  204. log.G(ctx).WithError(err).Warnf("failed to parse %s version", initBinary)
  205. return nil
  206. }
  207. v.InitCommit.ID = commit
  208. v.InitCommit.Expected = v.InitCommit.ID
  209. return nil
  210. }
  211. func (daemon *Daemon) fillRootlessVersion(ctx context.Context, v *types.Version) error {
  212. if !rootless.RunningWithRootlessKit() {
  213. return nil
  214. }
  215. rlc, err := getRootlessKitClient()
  216. if err != nil {
  217. return errors.Wrap(err, "failed to create RootlessKit client")
  218. }
  219. rlInfo, err := rlc.Info(ctx)
  220. if err != nil {
  221. return errors.Wrap(err, "failed to retrieve RootlessKit version")
  222. }
  223. v.Components = append(v.Components, types.ComponentVersion{
  224. Name: "rootlesskit",
  225. Version: rlInfo.Version,
  226. Details: map[string]string{
  227. "ApiVersion": rlInfo.APIVersion,
  228. "StateDir": rlInfo.StateDir,
  229. "NetworkDriver": rlInfo.NetworkDriver.Driver,
  230. "PortDriver": rlInfo.PortDriver.Driver,
  231. },
  232. })
  233. switch rlInfo.NetworkDriver.Driver {
  234. case "slirp4netns":
  235. err = func() error {
  236. rv, err := exec.CommandContext(ctx, "slirp4netns", "--version").Output()
  237. if err != nil {
  238. if errdefs.IsContext(err) {
  239. return err
  240. }
  241. log.G(ctx).WithError(err).Warn("Failed to retrieve slirp4netns version")
  242. return nil
  243. }
  244. _, ver, commit, err := parseRuntimeVersion(string(rv))
  245. if err != nil {
  246. log.G(ctx).WithError(err).Warn("Failed to parse slirp4netns version")
  247. return nil
  248. }
  249. v.Components = append(v.Components, types.ComponentVersion{
  250. Name: "slirp4netns",
  251. Version: ver,
  252. Details: map[string]string{
  253. "GitCommit": commit,
  254. },
  255. })
  256. return nil
  257. }()
  258. if err != nil {
  259. return err
  260. }
  261. case "vpnkit":
  262. err = func() error {
  263. out, err := exec.CommandContext(ctx, "vpnkit", "--version").Output()
  264. if err != nil {
  265. if errdefs.IsContext(err) {
  266. return err
  267. }
  268. log.G(ctx).WithError(err).Warn("Failed to retrieve vpnkit version")
  269. return nil
  270. }
  271. v.Components = append(v.Components, types.ComponentVersion{
  272. Name: "vpnkit",
  273. Version: strings.TrimSpace(strings.TrimSpace(string(out))),
  274. })
  275. return nil
  276. }()
  277. if err != nil {
  278. return err
  279. }
  280. }
  281. return nil
  282. }
  283. // getRootlessKitClient returns RootlessKit client
  284. func getRootlessKitClient() (rkclient.Client, error) {
  285. stateDir := os.Getenv("ROOTLESSKIT_STATE_DIR")
  286. if stateDir == "" {
  287. return nil, errors.New("environment variable `ROOTLESSKIT_STATE_DIR` is not set")
  288. }
  289. apiSock := filepath.Join(stateDir, "api.sock")
  290. return rkclient.New(apiSock)
  291. }
  292. func fillDriverWarnings(v *system.Info) {
  293. for _, pair := range v.DriverStatus {
  294. if pair[0] == "Extended file attributes" && pair[1] == "best-effort" {
  295. msg := fmt.Sprintf("WARNING: %s: extended file attributes from container images "+
  296. "will be silently discarded if the backing filesystem does not support them.\n"+
  297. " CONTAINERS MAY MALFUNCTION IF EXTENDED ATTRIBUTES ARE MISSING.\n"+
  298. " This is an UNSUPPORTABLE configuration for which no bug reports will be accepted.\n", v.Driver)
  299. v.Warnings = append(v.Warnings, msg)
  300. continue
  301. }
  302. }
  303. }
  304. // parseInitVersion parses a Tini version string, and extracts the "version"
  305. // and "git commit" from the output.
  306. //
  307. // Output example from `docker-init --version`:
  308. //
  309. // tini version 0.18.0 - git.fec3683
  310. func parseInitVersion(v string) (version string, commit string, err error) {
  311. parts := strings.Split(v, " - ")
  312. if len(parts) >= 2 {
  313. gitParts := strings.Split(strings.TrimSpace(parts[1]), ".")
  314. if len(gitParts) == 2 && gitParts[0] == "git" {
  315. commit = gitParts[1]
  316. }
  317. }
  318. parts[0] = strings.TrimSpace(parts[0])
  319. if strings.HasPrefix(parts[0], "tini version ") {
  320. version = strings.TrimPrefix(parts[0], "tini version ")
  321. }
  322. if version == "" && commit == "" {
  323. err = errors.Errorf("unknown output format: %s", v)
  324. }
  325. return version, commit, err
  326. }
  327. // parseRuntimeVersion parses the output of `[runtime] --version` and extracts the
  328. // "name", "version" and "git commit" from the output.
  329. //
  330. // Output example from `runc --version`:
  331. //
  332. // runc version 1.0.0-rc5+dev
  333. // commit: 69663f0bd4b60df09991c08812a60108003fa340
  334. // spec: 1.0.0
  335. func parseRuntimeVersion(v string) (runtime, version, commit string, err error) {
  336. lines := strings.Split(strings.TrimSpace(v), "\n")
  337. for _, line := range lines {
  338. if strings.Contains(line, "version") {
  339. s := strings.Split(line, "version")
  340. runtime = strings.TrimSpace(s[0])
  341. version = strings.TrimSpace(s[len(s)-1])
  342. continue
  343. }
  344. if strings.HasPrefix(line, "commit:") {
  345. commit = strings.TrimSpace(strings.TrimPrefix(line, "commit:"))
  346. continue
  347. }
  348. }
  349. if version == "" && commit == "" {
  350. err = errors.Errorf("unknown output format: %s", v)
  351. }
  352. return runtime, version, commit, err
  353. }
  354. func parseDefaultRuntimeVersion(rts *runtimes) (runtime, version, commit string, err error) {
  355. shim, opts, err := rts.Get(rts.Default)
  356. if err != nil {
  357. return "", "", "", err
  358. }
  359. shimopts, ok := opts.(*v2runcoptions.Options)
  360. if !ok {
  361. return "", "", "", fmt.Errorf("%s: retrieving version not supported", shim)
  362. }
  363. rt := shimopts.BinaryName
  364. if rt == "" {
  365. rt = defaultRuntimeName
  366. }
  367. rv, err := exec.Command(rt, "--version").Output()
  368. if err != nil {
  369. return "", "", "", fmt.Errorf("failed to retrieve %s version: %w", rt, err)
  370. }
  371. runtime, version, commit, err = parseRuntimeVersion(string(rv))
  372. if err != nil {
  373. return "", "", "", fmt.Errorf("failed to parse %s version: %w", rt, err)
  374. }
  375. return runtime, version, commit, err
  376. }
  377. func cgroupNamespacesEnabled(sysInfo *sysinfo.SysInfo, cfg *config.Config) bool {
  378. return sysInfo.CgroupNamespaces && containertypes.CgroupnsMode(cfg.CgroupNamespaceMode).IsPrivate()
  379. }
  380. // Rootless returns true if daemon is running in rootless mode
  381. func Rootless(cfg *config.Config) bool {
  382. return cfg.Rootless
  383. }
  384. func noNewPrivileges(cfg *config.Config) bool {
  385. return cfg.NoNewPrivileges
  386. }
  387. func (daemon *Daemon) populateContainerdCommit(ctx context.Context, v *system.Commit) error {
  388. rv, err := daemon.containerd.Version(ctx)
  389. if err != nil {
  390. if errdefs.IsContext(err) {
  391. return err
  392. }
  393. log.G(ctx).WithError(err).Warnf("Failed to retrieve containerd version")
  394. return nil
  395. }
  396. v.ID = rv.Revision
  397. v.Expected = rv.Revision
  398. return nil
  399. }
  400. func (daemon *Daemon) populateContainerdVersion(ctx context.Context, v *types.Version) error {
  401. rv, err := daemon.containerd.Version(ctx)
  402. if err != nil {
  403. if errdefs.IsContext(err) {
  404. return err
  405. }
  406. log.G(ctx).WithError(err).Warn("Failed to retrieve containerd version")
  407. return nil
  408. }
  409. v.Components = append(v.Components, types.ComponentVersion{
  410. Name: "containerd",
  411. Version: rv.Version,
  412. Details: map[string]string{
  413. "GitCommit": rv.Revision,
  414. },
  415. })
  416. return nil
  417. }
  418. func populateRuncVersion(cfg *configStore, v *types.Version) error {
  419. _, ver, commit, err := parseDefaultRuntimeVersion(&cfg.Runtimes)
  420. if err != nil {
  421. return err
  422. }
  423. v.Components = append(v.Components, types.ComponentVersion{
  424. Name: cfg.Runtimes.Default,
  425. Version: ver,
  426. Details: map[string]string{
  427. "GitCommit": commit,
  428. },
  429. })
  430. return nil
  431. }
  432. func populateInitVersion(ctx context.Context, cfg *configStore, v *types.Version) error {
  433. initBinary, err := cfg.LookupInitPath()
  434. if err != nil {
  435. log.G(ctx).WithError(err).Warn("Failed to find docker-init")
  436. return nil
  437. }
  438. rv, err := exec.CommandContext(ctx, initBinary, "--version").Output()
  439. if err != nil {
  440. if errdefs.IsContext(err) {
  441. return err
  442. }
  443. log.G(ctx).WithError(err).Warnf("Failed to retrieve %s version", initBinary)
  444. return nil
  445. }
  446. ver, commit, err := parseInitVersion(string(rv))
  447. if err != nil {
  448. log.G(ctx).WithError(err).Warnf("failed to parse %s version", initBinary)
  449. return nil
  450. }
  451. v.Components = append(v.Components, types.ComponentVersion{
  452. Name: filepath.Base(initBinary),
  453. Version: ver,
  454. Details: map[string]string{
  455. "GitCommit": commit,
  456. },
  457. })
  458. return nil
  459. }
  460. // ociRuntimeFeaturesKey is the "well-known" key used for including the
  461. // OCI runtime spec "features" struct.
  462. //
  463. // see https://github.com/opencontainers/runtime-spec/blob/main/features.md
  464. const ociRuntimeFeaturesKey = "org.opencontainers.runtime-spec.features"
  465. func (daemon *Daemon) runtimeStatus(ctx context.Context, cfg *configStore, runtimeName string) map[string]string {
  466. m := make(map[string]string)
  467. if runtimeName == "" {
  468. runtimeName = cfg.Runtimes.Default
  469. }
  470. if features := cfg.Runtimes.Features(runtimeName); features != nil {
  471. if j, err := json.Marshal(features); err == nil {
  472. m[ociRuntimeFeaturesKey] = string(j)
  473. } else {
  474. log.G(ctx).WithFields(log.Fields{"error": err, "runtime": runtimeName}).Warn("Failed to call json.Marshal for the OCI features struct of runtime")
  475. }
  476. }
  477. return m
  478. }