MCPcopy Create free account
hub / github.com/containerd/nerdctl / generateCgroupOpts

Function generateCgroupOpts

pkg/cmd/container/run_cgroup_linux.go:45–228  ·  view source on GitHub ↗
(id string, options types.ContainerCreateOptions, internalLabels *internalLabels)

Source from the content-addressed store, hash-verified

43}
44
45func generateCgroupOpts(id string, options types.ContainerCreateOptions, internalLabels *internalLabels) ([]oci.SpecOpts, error) {
46 if options.KernelMemory != "" {
47 log.L.Warnf("The --kernel-memory flag is no longer supported. This flag is a noop.")
48 }
49
50 if options.Memory == "" && options.OomKillDisable {
51 log.L.Warn("Disabling the OOM killer on containers without setting a '-m/--memory' limit may be dangerous.")
52 }
53
54 if options.GOptions.CgroupManager == "none" {
55 if !rootlessutil.IsRootless() {
56 return nil, errors.New(`cgroup-manager "none" is only supported for rootless`)
57 }
58
59 if options.CPUs > 0.0 || options.Memory != "" || options.MemorySwap != "" || options.PidsLimit > 0 {
60 log.L.Warn(`cgroup manager is set to "none", discarding resource limit requests. ` +
61 "(Hint: enable cgroup v2 with systemd: https://rootlesscontaine.rs/getting-started/common/cgroup2/)")
62 }
63 if options.CgroupParent != "" {
64 log.L.Warnf(`cgroup manager is set to "none", ignoring cgroup parent %q`+
65 "(Hint: enable cgroup v2 with systemd: https://rootlesscontaine.rs/getting-started/common/cgroup2/)", options.CgroupParent)
66 }
67 return []oci.SpecOpts{oci.WithCgroup("")}, nil
68 }
69
70 var opts []oci.SpecOpts // nolint: prealloc
71 path, err := generateCgroupPath(id, options.GOptions.CgroupManager, options.CgroupParent)
72 if err != nil {
73 return nil, err
74 }
75 if path != "" {
76 opts = append(opts, oci.WithCgroup(path))
77 }
78
79 // cpus: from https://github.com/containerd/containerd/blob/v1.4.3/cmd/ctr/commands/run/run_unix.go#L187-L193
80 if options.CPUs > 0.0 {
81 var (
82 period = uint64(100000)
83 quota = int64(options.CPUs * 100000.0)
84 )
85 opts = append(opts, oci.WithCPUCFS(quota, period))
86 }
87
88 if options.CPUShares != 0 {
89 opts = append(opts, oci.WithCPUShares(options.CPUShares))
90 }
91
92 if options.CPUSetCPUs != "" {
93 opts = append(opts, oci.WithCPUs(options.CPUSetCPUs))
94 }
95 if options.CPUQuota != -1 || options.CPUPeriod != 0 {
96 if options.CPUs > 0.0 {
97 return nil, errors.New("cpus and quota/period should be used separately")
98 }
99 opts = append(opts, oci.WithCPUCFS(options.CPUQuota, options.CPUPeriod))
100 }
101 if options.CPUSetMems != "" {
102 opts = append(opts, oci.WithCPUsMems(options.CPUSetMems))

Callers 1

setPlatformOptionsFunction · 0.85

Calls 8

IsRootlessFunction · 0.92
CPURealtimeFunction · 0.92
CgroupsVersionFunction · 0.92
generateCgroupPathFunction · 0.85
withUnifiedFunction · 0.85
BlkioOCIOptsFunction · 0.85
ParseDeviceFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…