Skip to content

Commit bc51452

Browse files
committed
libc/cgroups: mv cgroupv1 code to separate package
... and convert its users. It is now explicitly clear that this functionality is v1-specific and must not be used from any cgroup v2 code. Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
1 parent 14fede6 commit bc51452

20 files changed

+458
-437
lines changed

libcontainer/cgroups/cgroups_test.go

-20
This file was deleted.

libcontainer/cgroups/v1_utils.go libcontainer/cgroups/cgroupv1/utils.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cgroups
1+
package cgroupv1
22

33
import (
44
"bufio"
@@ -8,6 +8,8 @@ import (
88
"os"
99
"path/filepath"
1010
"strings"
11+
12+
"github.com/opencontainers/runc/libcontainer/cgroups"
1113
)
1214

1315
// Code in this source file are specific to cgroup v1,
@@ -18,6 +20,8 @@ const (
1820
)
1921

2022
var (
23+
IsCgroup2UnifiedMode = cgroups.IsCgroup2UnifiedMode
24+
2125
errUnified = errors.New("not implemented for cgroup v2 unified hierarchy")
2226
)
2327

libcontainer/cgroups/cgroupv1/utils_test.go

+407
Large diffs are not rendered by default.

libcontainer/cgroups/fs/apply_raw.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"sync"
1313

1414
"github.com/opencontainers/runc/libcontainer/cgroups"
15+
"github.com/opencontainers/runc/libcontainer/cgroups/cgroupv1"
1516
"github.com/opencontainers/runc/libcontainer/configs"
1617
libcontainerUtils "github.com/opencontainers/runc/libcontainer/utils"
1718
"github.com/pkg/errors"
@@ -190,7 +191,7 @@ func (m *manager) Apply(pid int) (err error) {
190191
for name, path := range c.Paths {
191192
_, err := d.path(name)
192193
if err != nil {
193-
if cgroups.IsNotFound(err) {
194+
if cgroupv1.IsNotFound(err) {
194195
continue
195196
}
196197
return err
@@ -205,7 +206,7 @@ func (m *manager) Apply(pid int) (err error) {
205206
if err != nil {
206207
// The non-presence of the devices subsystem is
207208
// considered fatal for security reasons.
208-
if cgroups.IsNotFound(err) && sys.Name() != "devices" {
209+
if cgroupv1.IsNotFound(err) && sys.Name() != "devices" {
209210
continue
210211
}
211212
return err
@@ -365,7 +366,7 @@ func getCgroupData(c *configs.Cgroup, pid int) (*cgroupData, error) {
365366
}
366367

367368
func (raw *cgroupData) path(subsystem string) (string, error) {
368-
mnt, err := cgroups.FindCgroupMountpoint(raw.root, subsystem)
369+
mnt, err := cgroupv1.FindCgroupMountpoint(raw.root, subsystem)
369370
// If we didn't mount the subsystem, there is no point we make the path.
370371
if err != nil {
371372
return "", err
@@ -380,7 +381,7 @@ func (raw *cgroupData) path(subsystem string) (string, error) {
380381
// Use GetOwnCgroupPath instead of GetInitCgroupPath, because the creating
381382
// process could in container and shared pid namespace with host, and
382383
// /proc/1/cgroup could point to whole other world of cgroups.
383-
parentPath, err := cgroups.GetOwnCgroupPath(subsystem)
384+
parentPath, err := cgroupv1.GetOwnCgroupPath(subsystem)
384385
if err != nil {
385386
return "", err
386387
}

libcontainer/cgroups/fs/blkio.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strings"
1212

1313
"github.com/opencontainers/runc/libcontainer/cgroups"
14+
"github.com/opencontainers/runc/libcontainer/cgroups/cgroupv1"
1415
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
1516
"github.com/opencontainers/runc/libcontainer/configs"
1617
)
@@ -24,7 +25,7 @@ func (s *BlkioGroup) Name() string {
2425

2526
func (s *BlkioGroup) Apply(d *cgroupData) error {
2627
_, err := d.join("blkio")
27-
if err != nil && !cgroups.IsNotFound(err) {
28+
if err != nil && !cgroupv1.IsNotFound(err) {
2829
return err
2930
}
3031
return nil

libcontainer/cgroups/fs/cpu.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strconv"
1010

1111
"github.com/opencontainers/runc/libcontainer/cgroups"
12+
"github.com/opencontainers/runc/libcontainer/cgroups/cgroupv1"
1213
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
1314
"github.com/opencontainers/runc/libcontainer/configs"
1415
)
@@ -24,7 +25,7 @@ func (s *CpuGroup) Apply(d *cgroupData) error {
2425
// We always want to join the cpu group, to allow fair cpu scheduling
2526
// on a container basis
2627
path, err := d.path("cpu")
27-
if err != nil && !cgroups.IsNotFound(err) {
28+
if err != nil && !cgroupv1.IsNotFound(err) {
2829
return err
2930
}
3031
return s.ApplyDir(path, d.config, d.pid)

libcontainer/cgroups/fs/cpuacct.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"strings"
1313

1414
"github.com/opencontainers/runc/libcontainer/cgroups"
15+
"github.com/opencontainers/runc/libcontainer/cgroups/cgroupv1"
1516
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
1617
"github.com/opencontainers/runc/libcontainer/configs"
1718
)
@@ -42,7 +43,7 @@ func (s *CpuacctGroup) Name() string {
4243

4344
func (s *CpuacctGroup) Apply(d *cgroupData) error {
4445
// we just want to join this group even though we don't set anything
45-
if _, err := d.join("cpuacct"); err != nil && !cgroups.IsNotFound(err) {
46+
if _, err := d.join("cpuacct"); err != nil && !cgroupv1.IsNotFound(err) {
4647
return err
4748
}
4849

libcontainer/cgroups/fs/cpuset.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/moby/sys/mountinfo"
1313
"github.com/opencontainers/runc/libcontainer/cgroups"
14+
"github.com/opencontainers/runc/libcontainer/cgroups/cgroupv1"
1415
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
1516
"github.com/opencontainers/runc/libcontainer/configs"
1617
libcontainerUtils "github.com/opencontainers/runc/libcontainer/utils"
@@ -25,7 +26,7 @@ func (s *CpusetGroup) Name() string {
2526

2627
func (s *CpusetGroup) Apply(d *cgroupData) error {
2728
dir, err := d.path("cpuset")
28-
if err != nil && !cgroups.IsNotFound(err) {
29+
if err != nil && !cgroupv1.IsNotFound(err) {
2930
return err
3031
}
3132
return s.ApplyDir(dir, d.config, d.pid)

libcontainer/cgroups/fs/freezer.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"time"
1111

1212
"github.com/opencontainers/runc/libcontainer/cgroups"
13+
"github.com/opencontainers/runc/libcontainer/cgroups/cgroupv1"
1314
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
1415
"github.com/opencontainers/runc/libcontainer/configs"
1516
"golang.org/x/sys/unix"
@@ -24,7 +25,7 @@ func (s *FreezerGroup) Name() string {
2425

2526
func (s *FreezerGroup) Apply(d *cgroupData) error {
2627
_, err := d.join("freezer")
27-
if err != nil && !cgroups.IsNotFound(err) {
28+
if err != nil && !cgroupv1.IsNotFound(err) {
2829
return err
2930
}
3031
return nil

libcontainer/cgroups/fs/hugetlb.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99

1010
"github.com/opencontainers/runc/libcontainer/cgroups"
11+
"github.com/opencontainers/runc/libcontainer/cgroups/cgroupv1"
1112
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
1213
"github.com/opencontainers/runc/libcontainer/configs"
1314
)
@@ -21,7 +22,7 @@ func (s *HugetlbGroup) Name() string {
2122

2223
func (s *HugetlbGroup) Apply(d *cgroupData) error {
2324
_, err := d.join("hugetlb")
24-
if err != nil && !cgroups.IsNotFound(err) {
25+
if err != nil && !cgroupv1.IsNotFound(err) {
2526
return err
2627
}
2728
return nil

libcontainer/cgroups/fs/memory.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"strings"
1414

1515
"github.com/opencontainers/runc/libcontainer/cgroups"
16+
"github.com/opencontainers/runc/libcontainer/cgroups/cgroupv1"
1617
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
1718
"github.com/opencontainers/runc/libcontainer/configs"
1819
)
@@ -39,7 +40,7 @@ func (s *MemoryGroup) Name() string {
3940

4041
func (s *MemoryGroup) Apply(d *cgroupData) (err error) {
4142
path, err := d.path("memory")
42-
if err != nil && !cgroups.IsNotFound(err) {
43+
if err != nil && !cgroupv1.IsNotFound(err) {
4344
return err
4445
} else if path == "" {
4546
return nil
@@ -67,7 +68,7 @@ func (s *MemoryGroup) Apply(d *cgroupData) (err error) {
6768
// We need to join memory cgroup after set memory limits, because
6869
// kmem.limit_in_bytes can only be set when the cgroup is empty.
6970
_, err = d.join("memory")
70-
if err != nil && !cgroups.IsNotFound(err) {
71+
if err != nil && !cgroupv1.IsNotFound(err) {
7172
return err
7273
}
7374
return nil

libcontainer/cgroups/fs/net_cls.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strconv"
77

88
"github.com/opencontainers/runc/libcontainer/cgroups"
9+
"github.com/opencontainers/runc/libcontainer/cgroups/cgroupv1"
910
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
1011
"github.com/opencontainers/runc/libcontainer/configs"
1112
)
@@ -19,7 +20,7 @@ func (s *NetClsGroup) Name() string {
1920

2021
func (s *NetClsGroup) Apply(d *cgroupData) error {
2122
_, err := d.join("net_cls")
22-
if err != nil && !cgroups.IsNotFound(err) {
23+
if err != nil && !cgroupv1.IsNotFound(err) {
2324
return err
2425
}
2526
return nil

libcontainer/cgroups/fs/net_prio.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package fs
44

55
import (
66
"github.com/opencontainers/runc/libcontainer/cgroups"
7+
"github.com/opencontainers/runc/libcontainer/cgroups/cgroupv1"
78
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
89
"github.com/opencontainers/runc/libcontainer/configs"
910
)
@@ -17,7 +18,7 @@ func (s *NetPrioGroup) Name() string {
1718

1819
func (s *NetPrioGroup) Apply(d *cgroupData) error {
1920
_, err := d.join("net_prio")
20-
if err != nil && !cgroups.IsNotFound(err) {
21+
if err != nil && !cgroupv1.IsNotFound(err) {
2122
return err
2223
}
2324
return nil

libcontainer/cgroups/fs/perf_event.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package fs
44

55
import (
66
"github.com/opencontainers/runc/libcontainer/cgroups"
7+
"github.com/opencontainers/runc/libcontainer/cgroups/cgroupv1"
78
"github.com/opencontainers/runc/libcontainer/configs"
89
)
910

@@ -16,7 +17,7 @@ func (s *PerfEventGroup) Name() string {
1617

1718
func (s *PerfEventGroup) Apply(d *cgroupData) error {
1819
// we just want to join this group even though we don't set anything
19-
if _, err := d.join("perf_event"); err != nil && !cgroups.IsNotFound(err) {
20+
if _, err := d.join("perf_event"); err != nil && !cgroupv1.IsNotFound(err) {
2021
return err
2122
}
2223
return nil

libcontainer/cgroups/fs/pids.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strconv"
99

1010
"github.com/opencontainers/runc/libcontainer/cgroups"
11+
"github.com/opencontainers/runc/libcontainer/cgroups/cgroupv1"
1112
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
1213
"github.com/opencontainers/runc/libcontainer/configs"
1314
)
@@ -21,7 +22,7 @@ func (s *PidsGroup) Name() string {
2122

2223
func (s *PidsGroup) Apply(d *cgroupData) error {
2324
_, err := d.join("pids")
24-
if err != nil && !cgroups.IsNotFound(err) {
25+
if err != nil && !cgroupv1.IsNotFound(err) {
2526
return err
2627
}
2728
return nil

libcontainer/cgroups/fscommon/fscommon_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ import (
1111
"time"
1212

1313
"github.com/opencontainers/runc/libcontainer/cgroups"
14+
"github.com/opencontainers/runc/libcontainer/cgroups/cgroupv1"
1415
)
1516

1617
func TestWriteCgroupFileHandlesInterrupt(t *testing.T) {
1718
if cgroups.IsCgroup2UnifiedMode() {
1819
t.Skip("cgroup v2 is not supported")
1920
}
2021

21-
memoryCgroupMount, err := cgroups.FindCgroupMountpoint("", "memory")
22+
memoryCgroupMount, err := cgroupv1.FindCgroupMountpoint("", "memory")
2223
if err != nil {
2324
t.Fatal(err)
2425
}

libcontainer/cgroups/systemd/v1.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
systemdDbus "github.com/coreos/go-systemd/v22/dbus"
1515
"github.com/opencontainers/runc/libcontainer/cgroups"
16+
"github.com/opencontainers/runc/libcontainer/cgroups/cgroupv1"
1617
"github.com/opencontainers/runc/libcontainer/cgroups/fs"
1718
"github.com/opencontainers/runc/libcontainer/configs"
1819
"github.com/sirupsen/logrus"
@@ -143,7 +144,7 @@ func (m *legacyManager) Apply(pid int) error {
143144
_, err := getSubsystemPath(m.cgroups, name)
144145
if err != nil {
145146
// Don't fail if a cgroup hierarchy was not found, just skip this subsystem
146-
if cgroups.IsNotFound(err) {
147+
if cgroupv1.IsNotFound(err) {
147148
continue
148149
}
149150
return err
@@ -214,7 +215,7 @@ func (m *legacyManager) Apply(pid int) error {
214215
subsystemPath, err := getSubsystemPath(m.cgroups, s.Name())
215216
if err != nil {
216217
// Don't fail if a cgroup hierarchy was not found, just skip this subsystem
217-
if cgroups.IsNotFound(err) {
218+
if cgroupv1.IsNotFound(err) {
218219
continue
219220
}
220221
return err
@@ -273,7 +274,7 @@ func joinCgroups(c *configs.Cgroup, pid int) error {
273274
// let systemd handle this
274275
case "cpuset":
275276
path, err := getSubsystemPath(c, name)
276-
if err != nil && !cgroups.IsNotFound(err) {
277+
if err != nil && !cgroupv1.IsNotFound(err) {
277278
return err
278279
}
279280
s := &fs.CpusetGroup{}
@@ -291,7 +292,7 @@ func joinCgroups(c *configs.Cgroup, pid int) error {
291292
}
292293
// For other subsystems, omit the `not found` error
293294
// because they are optional.
294-
if !cgroups.IsNotFound(err) {
295+
if !cgroupv1.IsNotFound(err) {
295296
return err
296297
}
297298
}
@@ -302,12 +303,12 @@ func joinCgroups(c *configs.Cgroup, pid int) error {
302303
}
303304

304305
func getSubsystemPath(c *configs.Cgroup, subsystem string) (string, error) {
305-
mountpoint, err := cgroups.FindCgroupMountpoint(c.Path, subsystem)
306+
mountpoint, err := cgroupv1.FindCgroupMountpoint(c.Path, subsystem)
306307
if err != nil {
307308
return "", err
308309
}
309310

310-
initPath, err := cgroups.GetInitCgroup(subsystem)
311+
initPath, err := cgroupv1.GetInitCgroup(subsystem)
311312
if err != nil {
312313
return "", err
313314
}
@@ -427,7 +428,7 @@ func (m *legacyManager) Set(container *configs.Config) error {
427428
for _, sys := range legacySubsystems {
428429
// Get the subsystem path, but don't error out for not found cgroups.
429430
path, err := getSubsystemPath(container.Cgroups, sys.Name())
430-
if err != nil && !cgroups.IsNotFound(err) {
431+
if err != nil && !cgroupv1.IsNotFound(err) {
431432
return err
432433
}
433434
if err := sys.Set(path, container.Cgroups); err != nil {
@@ -445,7 +446,7 @@ func (m *legacyManager) Set(container *configs.Config) error {
445446

446447
func setKernelMemory(c *configs.Cgroup) error {
447448
path, err := getSubsystemPath(c, "memory")
448-
if err != nil && !cgroups.IsNotFound(err) {
449+
if err != nil && !cgroupv1.IsNotFound(err) {
449450
return err
450451
}
451452

@@ -476,7 +477,7 @@ func (m *legacyManager) GetCgroups() (*configs.Cgroup, error) {
476477

477478
func (m *legacyManager) GetFreezerState() (configs.FreezerState, error) {
478479
path, err := getSubsystemPath(m.cgroups, "freezer")
479-
if err != nil && !cgroups.IsNotFound(err) {
480+
if err != nil && !cgroupv1.IsNotFound(err) {
480481
return configs.Undefined, err
481482
}
482483
freezer, err := legacySubsystems.Get("freezer")

0 commit comments

Comments
 (0)