info_unix.go 13 KB

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