diff --git a/libcontainer/cgroups/systemd/unified_hierarchy.go b/libcontainer/cgroups/systemd/unified_hierarchy.go index d05ecacb..308a7917 100644 --- a/libcontainer/cgroups/systemd/unified_hierarchy.go +++ b/libcontainer/cgroups/systemd/unified_hierarchy.go @@ -4,7 +4,6 @@ package systemd import ( "bytes" - "fmt" "io/ioutil" "math" "os" @@ -38,14 +37,6 @@ func (m *UnifiedManager) Apply(pid int) error { if c.Paths != nil { paths := make(map[string]string) for name, path := range c.Paths { - _, err := getSubsystemPath(m.Cgroups, name) - if err != nil { - // Don't fail if a cgroup hierarchy was not found, just skip this subsystem - if cgroups.IsNotFound(err) { - continue - } - return err - } paths[name] = path } m.Paths = paths @@ -139,12 +130,11 @@ func (m *UnifiedManager) Apply(pid int) error { return err } - if err := joinCgroupsV2(c, pid); err != nil { + path, err := getv2Path(m.Cgroups) + if err != nil { return err } - - path, err := getSubsystemPath(m.Cgroups, "") - if err != nil { + if err := createCgroupsv2Path(path); err != nil { return err } m.Paths = map[string]string{ @@ -197,14 +187,26 @@ func (m *UnifiedManager) GetUnifiedPath() (string, error) { } return unifiedPath, nil } + +func getv2Path(c *configs.Cgroup) (string, error) { + slice := "system.slice" + if c.Parent != "" { + slice = c.Parent + } + + slice, err := ExpandSlice(slice) + if err != nil { + return "", err + } + + return filepath.Join(fs2.UnifiedMountpoint, slice, getUnitName(c)), nil +} + 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) - } ctrs := bytes.Fields(content) res := append([]byte("+"), bytes.Join(ctrs, []byte(" +"))...) @@ -236,14 +238,6 @@ func createCgroupsv2Path(path string) (Err error) { return nil } -func joinCgroupsV2(c *configs.Cgroup, pid int) error { - path, err := getSubsystemPath(c, "memory") - if err != nil { - return err - } - return createCgroupsv2Path(path) -} - func (m *UnifiedManager) fsManager() (cgroups.Manager, error) { path, err := m.GetUnifiedPath() if err != nil { diff --git a/libcontainer/cgroups/utils.go b/libcontainer/cgroups/utils.go index 7a465d52..7808c093 100644 --- a/libcontainer/cgroups/utils.go +++ b/libcontainer/cgroups/utils.go @@ -4,6 +4,7 @@ package cgroups import ( "bufio" + "errors" "fmt" "io" "io/ioutil" @@ -28,6 +29,8 @@ const ( var ( isUnifiedOnce sync.Once isUnified bool + + errUnified = errors.New("not implemented for cgroup v2 unified hierarchy") ) // HugePageSizeUnitList is a list of the units used by the linux kernel when @@ -307,6 +310,9 @@ func GetAllSubsystems() ([]string, error) { // GetOwnCgroup returns the relative path to the cgroup docker is running in. func GetOwnCgroup(subsystem string) (string, error) { + if IsCgroup2UnifiedMode() { + return "", errUnified + } cgroups, err := ParseCgroupFile("/proc/self/cgroup") if err != nil { return "", err @@ -325,6 +331,9 @@ func GetOwnCgroupPath(subsystem string) (string, error) { } func GetInitCgroup(subsystem string) (string, error) { + if IsCgroup2UnifiedMode() { + return "", errUnified + } cgroups, err := ParseCgroupFile("/proc/1/cgroup") if err != nil { return "", err