Add support for CgroupsPath field

Fixes #396

Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com>
This commit is contained in:
Kenfe-Mickael Laventure 2016-01-20 18:04:59 -08:00
parent dceeb0d0df
commit 256f3a8ebc
4 changed files with 46 additions and 28 deletions

View File

@ -14,7 +14,6 @@ import (
"github.com/opencontainers/runc/libcontainer/cgroups" "github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/configs" "github.com/opencontainers/runc/libcontainer/configs"
libcontainerUtils "github.com/opencontainers/runc/libcontainer/utils"
) )
var ( var (
@ -95,11 +94,10 @@ func getCgroupRoot() (string, error) {
} }
type cgroupData struct { type cgroupData struct {
root string root string
parent string innerPath string
name string config *configs.Cgroup
config *configs.Cgroup pid int
pid int
} }
func (m *Manager) Apply(pid int) (err error) { func (m *Manager) Apply(pid int) (err error) {
@ -274,15 +272,20 @@ func getCgroupData(c *configs.Cgroup, pid int) (*cgroupData, error) {
return nil, err return nil, err
} }
// Clean the parent slice path. if (c.Name != "" || c.Parent != "") && c.Path != "" {
c.Parent = libcontainerUtils.CleanPath(c.Parent) return nil, fmt.Errorf("cgroup: either Path or Name and Parent should be used")
}
innerPath := c.Path
if innerPath == "" {
innerPath = filepath.Join(c.Parent, c.Name)
}
return &cgroupData{ return &cgroupData{
root: root, root: root,
parent: c.Parent, innerPath: c.Path,
name: c.Name, config: c,
config: c, pid: pid,
pid: pid,
}, nil }, nil
} }
@ -310,11 +313,10 @@ func (raw *cgroupData) path(subsystem string) (string, error) {
return "", err return "", err
} }
cgPath := filepath.Join(raw.parent, raw.name)
// If the cgroup name/path is absolute do not look relative to the cgroup of the init process. // If the cgroup name/path is absolute do not look relative to the cgroup of the init process.
if filepath.IsAbs(cgPath) { if filepath.IsAbs(raw.innerPath) {
// Sometimes subsystems can be mounted togethger as 'cpu,cpuacct'. // Sometimes subsystems can be mounted togethger as 'cpu,cpuacct'.
return filepath.Join(raw.root, filepath.Base(mnt), cgPath), nil return filepath.Join(raw.root, filepath.Base(mnt), raw.innerPath), nil
} }
parentPath, err := raw.parentPath(subsystem, mnt, root) parentPath, err := raw.parentPath(subsystem, mnt, root)
@ -322,7 +324,7 @@ func (raw *cgroupData) path(subsystem string) (string, error) {
return "", err return "", err
} }
return filepath.Join(parentPath, cgPath), nil return filepath.Join(parentPath, raw.innerPath), nil
} }
func (raw *cgroupData) join(subsystem string) (string, error) { func (raw *cgroupData) join(subsystem string) (string, error) {

View File

@ -11,15 +11,22 @@ const (
) )
type Cgroup struct { type Cgroup struct {
Name string `json:"name"` // Deprecated, use Path instead
Name string `json:"name,omitempty"`
// name of parent cgroup or slice // name of parent of cgroup or slice
Parent string `json:"parent"` // Deprecated, use Path instead
Parent string `json:"parent,omitempty"`
// Path 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.
Path string `json:"path"`
// ScopePrefix decribes prefix for the scope name // ScopePrefix decribes prefix for the scope name
ScopePrefix string `json:"scope_prefix"` ScopePrefix string `json:"scope_prefix"`
// Paths represent the cgroups paths to join // Paths represent the absolute cgroups paths to join.
// This takes precedence over Path.
Paths map[string]string Paths map[string]string
// Resources contains various cgroups settings to apply // Resources contains various cgroups settings to apply

View File

@ -46,8 +46,7 @@ func newTemplateConfig(rootfs string) *configs.Config {
{Type: configs.NEWNET}, {Type: configs.NEWNET},
}), }),
Cgroups: &configs.Cgroup{ Cgroups: &configs.Cgroup{
Name: "test", Path: "integration/test",
Parent: "integration",
Resources: &configs.Resources{ Resources: &configs.Resources{
MemorySwappiness: -1, MemorySwappiness: -1,
AllowAllDevices: false, AllowAllDevices: false,

20
spec.go
View File

@ -18,6 +18,7 @@ import (
"github.com/opencontainers/runc/libcontainer/cgroups" "github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/configs" "github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runc/libcontainer/seccomp" "github.com/opencontainers/runc/libcontainer/seccomp"
libcontainerUtils "github.com/opencontainers/runc/libcontainer/utils"
"github.com/opencontainers/specs" "github.com/opencontainers/specs"
) )
@ -326,13 +327,22 @@ func createLibcontainerMount(cwd string, m specs.Mount) *configs.Mount {
} }
func createCgroupConfig(name string, spec *specs.LinuxSpec) (*configs.Cgroup, error) { func createCgroupConfig(name string, spec *specs.LinuxSpec) (*configs.Cgroup, error) {
myCgroupPath, err := cgroups.GetThisCgroupDir("devices") var (
if err != nil { err error
return nil, err myCgroupPath string
)
if spec.Linux.CgroupsPath != nil {
myCgroupPath = libcontainerUtils.CleanPath(*spec.Linux.CgroupsPath)
} else {
myCgroupPath, err = cgroups.GetThisCgroupDir("devices")
if err != nil {
return nil, err
}
} }
c := &configs.Cgroup{ c := &configs.Cgroup{
Name: name, Path: filepath.Join(myCgroupPath, name),
Parent: myCgroupPath,
Resources: &configs.Resources{}, Resources: &configs.Resources{},
} }
c.Resources.AllowedDevices = allowedDevices c.Resources.AllowedDevices = allowedDevices