info_unix.go 13 KB

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