info_unix.go 15 KB

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