Skip to content

Commit

Permalink
libcontainer: support creating cgroups v2 path
Browse files Browse the repository at this point in the history
when creating a cgroups v2 path, propagate down all the enabled
controllers.

Signed-off-by: Giuseppe Scrivano <[email protected]>
  • Loading branch information
giuseppe committed Aug 22, 2019
1 parent 75872b8 commit 97b293c
Showing 1 changed file with 62 additions and 2 deletions.
64 changes: 62 additions & 2 deletions libcontainer/cgroups/systemd/apply_systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,13 +346,64 @@ func (m *Manager) GetPaths() map[string]string {
return paths
}

func createCgroupsv2Path(path string) (Err error) {
content, err := ioutil.ReadFile("/sys/fs/cgroup/cgroup.controllers")
if err != nil {
return err
}
if !filepath.HasPrefix(path, "/sys/fs/cgroup") {
return fmt.Errorf("invalid cgroup path %s", path)
}

res := ""
for i, c := range strings.Split(strings.TrimSpace(string(content)), " ") {
if i == 0 {
res = fmt.Sprintf("+%s", c)
} else {
res = res + fmt.Sprintf(" +%s", c)
}
}
resByte := []byte(res)

current := "/sys/fs"
elements := strings.Split(path, "/")
for i, e := range elements[3:] {
current = filepath.Join(current, e)
if i > 0 {
if err := os.Mkdir(current, 0755); err != nil {
if !os.IsExist(err) {
return err
}
} else {
// If the directory was created, be sure it is not left around on errors.
defer func() {
if Err != nil {
os.Remove(current)
}
}()
}
}
if err := ioutil.WriteFile(filepath.Join(current, "cgroup.subtree_control"), resByte, 0755); err != nil {
return err
}
}
return nil
}

func join(c *configs.Cgroup, subsystem string, pid int) (string, error) {
path, err := getSubsystemPath(c, subsystem)
if err != nil {
return "", err
}
if err := os.MkdirAll(path, 0755); err != nil {
return "", err

if cgroups.IsCgroup2UnifiedMode() {
if err := createCgroupsv2Path(path); err != nil {
return "", err
}
} else {
if err := os.MkdirAll(path, 0755); err != nil {
return "", err
}
}
if err := cgroups.WriteCgroupProc(path, pid); err != nil {
return "", err
Expand All @@ -361,6 +412,15 @@ func join(c *configs.Cgroup, subsystem string, pid int) (string, error) {
}

func joinCgroups(c *configs.Cgroup, pid int) error {
if cgroups.IsCgroup2UnifiedMode() {
path, err := getSubsystemPath(c, "memory")
if err != nil {
return err
}
if err := createCgroupsv2Path(path); err != nil {
return err
}
}
for _, sys := range subsystems {
name := sys.Name()
switch name {
Expand Down

0 comments on commit 97b293c

Please sign in to comment.