Skip to content

Commit

Permalink
Replace Cgroup Parent and Name fields by CgroupsPath
Browse files Browse the repository at this point in the history
Fixes #396

Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com>
  • Loading branch information
mlaventure committed Jan 21, 2016
1 parent 46f2322 commit bb9396d
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 42 deletions.
23 changes: 9 additions & 14 deletions libcontainer/cgroups/fs/apply_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,10 @@ func getCgroupRoot() (string, error) {
}

type cgroupData struct {
root string
parent string
name string
config *configs.Cgroup
pid int
root string
cgroupspath string
config *configs.Cgroup
pid int
}

func (m *Manager) Apply(pid int) (err error) {
Expand Down Expand Up @@ -280,15 +279,11 @@ func getCgroupData(c *configs.Cgroup, pid int) (*cgroupData, error) {
return nil, err
}

// Clean the parent slice path.
c.Parent = libcontainerUtils.PathClean(c.Parent)

return &cgroupData{
root: root,
parent: c.Parent,
name: c.Name,
config: c,
pid: pid,
root: root,
cgroupspath: libcontainerUtils.PathClean(c.CgroupsPath),
config: c,
pid: pid,
}, nil
}

Expand Down Expand Up @@ -316,7 +311,7 @@ func (raw *cgroupData) path(subsystem string) (string, error) {
return "", err
}

cgPath := filepath.Join(raw.parent, raw.name)
cgPath := raw.cgroupspath
// If the cgroup name/path is absolute do not look relative to the cgroup of the init process.
if filepath.IsAbs(cgPath) {
// Sometimes subsystems can be mounted togethger as 'cpu,cpuacct'.
Expand Down
23 changes: 13 additions & 10 deletions libcontainer/cgroups/systemd/apply_systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups/fs"
"github.com/opencontainers/runc/libcontainer/configs"
libcontainerUtils "github.com/opencontainers/runc/libcontainer/utils"
)

type Manager struct {
Expand Down Expand Up @@ -162,10 +163,11 @@ func getIfaceForUnit(unitName string) string {

func (m *Manager) Apply(pid int) error {
var (
c = m.Cgroups
unitName = getUnitName(c)
slice = "system.slice"
properties []systemdDbus.Property
c = m.Cgroups
unitName = getUnitName(c)
parent, name = filepath.Split(c.CgroupsPath)
slice = "system.slice"
properties []systemdDbus.Property
)

if c.Paths != nil {
Expand All @@ -185,13 +187,13 @@ func (m *Manager) Apply(pid int) error {
return cgroups.EnterPid(m.Paths, pid)
}

if c.Parent != "" {
slice = c.Parent
if parent != "" {
slice = libcontainerUtils.PathClean(parent)
}

properties = append(properties,
systemdDbus.PropSlice(slice),
systemdDbus.PropDescription("docker container "+c.Name),
systemdDbus.PropDescription("docker container "+name),
newProp("PIDs", []uint32{uint32(pid)}),
)

Expand Down Expand Up @@ -399,8 +401,8 @@ func getSubsystemPath(c *configs.Cgroup, subsystem string) (string, error) {
}

slice := "system.slice"
if c.Parent != "" {
slice = c.Parent
if dir, _ := filepath.Split(c.CgroupsPath); dir != "" {
slice = libcontainerUtils.PathClean(dir)
}

return filepath.Join(mountpoint, initPath, slice, getUnitName(c)), nil
Expand Down Expand Up @@ -486,7 +488,8 @@ func (m *Manager) Set(container *configs.Config) error {
}

func getUnitName(c *configs.Cgroup) string {
return fmt.Sprintf("%s-%s.scope", c.ScopePrefix, c.Name)
_, name := filepath.Split(c.CgroupsPath)
return fmt.Sprintf("%s-%s.scope", c.ScopePrefix, name)
}

// Atm we can't use the systemd device support because of two missing things:
Expand Down
10 changes: 5 additions & 5 deletions libcontainer/configs/cgroup_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ const (
)

type Cgroup struct {
Name string `json:"name"`

// name of parent cgroup or slice
Parent string `json:"parent"`
// CgroupsPath specifies the path to cgroups that are created and/or joined by the container.
// The path is assumed to be relative to the host system cgroup mountpoint.
CgroupsPath string `json:"cgroupsPath"`

// ScopePrefix decribes prefix for the scope name
ScopePrefix string `json:"scope_prefix"`

// Paths represent the cgroups paths to join
// Paths represent the absolute cgroups paths to join.
// This takes precedence over CgroupsPath.
Paths map[string]string

// Resources contains various cgroups settings to apply
Expand Down
11 changes: 8 additions & 3 deletions libcontainer/integration/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,8 @@ func testFreeze(t *testing.T, systemd bool) {
f := factory
if systemd {
f = systemdFactory
_, name := filepath.Split(config.Cgroups.CgroupsPath)
config.Cgroups.CgroupsPath = filepath.Join("system.slice", name)
}

container, err := f.Create("test", config)
Expand Down Expand Up @@ -521,7 +523,8 @@ func testCpuShares(t *testing.T, systemd bool) {

config := newTemplateConfig(rootfs)
if systemd {
config.Cgroups.Parent = "system.slice"
_, name := filepath.Split(config.Cgroups.CgroupsPath)
config.Cgroups.CgroupsPath = filepath.Join("system.slice", name)
}
config.Cgroups.Resources.CpuShares = 1

Expand Down Expand Up @@ -553,7 +556,8 @@ func testPids(t *testing.T, systemd bool) {

config := newTemplateConfig(rootfs)
if systemd {
config.Cgroups.Parent = "system.slice"
_, name := filepath.Split(config.Cgroups.CgroupsPath)
config.Cgroups.CgroupsPath = filepath.Join("system.slice", name)
}
config.Cgroups.Resources.PidsLimit = -1

Expand Down Expand Up @@ -621,7 +625,8 @@ func testRunWithKernelMemory(t *testing.T, systemd bool) {

config := newTemplateConfig(rootfs)
if systemd {
config.Cgroups.Parent = "system.slice"
_, name := filepath.Split(config.Cgroups.CgroupsPath)
config.Cgroups.CgroupsPath = filepath.Join("system.slice", name)
}
config.Cgroups.Resources.KernelMemory = 52428800

Expand Down
3 changes: 1 addition & 2 deletions libcontainer/integration/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ func newTemplateConfig(rootfs string) *configs.Config {
{Type: configs.NEWNET},
}),
Cgroups: &configs.Cgroup{
Name: "test",
Parent: "integration",
CgroupsPath: "integration/test",
Resources: &configs.Resources{
MemorySwappiness: -1,
AllowAllDevices: false,
Expand Down
4 changes: 2 additions & 2 deletions libcontainer/integration/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ func copyBusybox(dest string) error {

func newContainer(config *configs.Config) (libcontainer.Container, error) {
f := factory

if config.Cgroups != nil && config.Cgroups.Parent == "system.slice" {
dir, _ := filepath.Split(config.Cgroups.CgroupsPath)
if config.Cgroups != nil && dir == "system.slice" {
f = systemdFactory
}

Expand Down
22 changes: 16 additions & 6 deletions spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runc/libcontainer/seccomp"
libcontainerUtils "github.com/opencontainers/runc/libcontainer/utils"
"github.com/opencontainers/specs"
)

Expand Down Expand Up @@ -449,14 +450,23 @@ func createLibcontainerMount(cwd, dest string, m specs.Mount) *configs.Mount {
}

func createCgroupConfig(name string, spec *specs.LinuxRuntimeSpec, devices []*configs.Device) (*configs.Cgroup, error) {
myCgroupPath, err := cgroups.GetThisCgroupDir("devices")
if err != nil {
return nil, err
var (
err error
myCgroupPath string
)

if spec.Linux.CgroupsPath != "" {
myCgroupPath = libcontainerUtils.PathClean(spec.Linux.CgroupsPath)
} else {
myCgroupPath, err = cgroups.GetThisCgroupDir("devices")
if err != nil {
return nil, err
}
}

c := &configs.Cgroup{
Name: name,
Parent: myCgroupPath,
Resources: &configs.Resources{},
CgroupsPath: filepath.Join(myCgroupPath, name),
Resources: &configs.Resources{},
}
c.Resources.AllowedDevices = append(devices, allowedDevices...)
r := spec.Linux.Resources
Expand Down

0 comments on commit bb9396d

Please sign in to comment.