Skip to content

Commit 15ca5f2

Browse files
mheonrh-atomic-bot
authored andcommitted
Add validation for CGroup parents. Pass CGroups path into runc
Signed-off-by: Matthew Heon <[email protected]> Closes: #507 Approved by: baude
1 parent 6756af3 commit 15ca5f2

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

libpod/container.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@ const (
3939
ContainerStatePaused ContainerStatus = iota
4040
)
4141

42-
// DefaultCgroupParent is the default prefix to a cgroup path in libpod
43-
var DefaultCgroupParent = "/libpod_parent"
42+
// CgroupfsDefaultCgroupParent is the cgroup parent for CGroupFS in libpod
43+
const CgroupfsDefaultCgroupParent = "/libpod_parent"
44+
45+
// SystemdDefaultCgroupParent is the cgroup parent for the systemd cgroup
46+
// manager in libpod
47+
const SystemdDefaultCgroupParent = "system.slice"
4448

4549
// LinuxNS represents a Linux namespace
4650
type LinuxNS int
@@ -851,7 +855,8 @@ func (c *Container) NamespacePath(ns LinuxNS) (string, error) {
851855

852856
// CGroupPath returns a cgroups "path" for a given container.
853857
func (c *Container) CGroupPath() cgroups.Path {
854-
return cgroups.StaticPath(filepath.Join(c.config.CgroupParent, fmt.Sprintf("libpod-conmon-%s/%s", c.ID(), c.ID())))
858+
// TODO add support for systemd cgroup paths
859+
return cgroups.StaticPath(filepath.Join(c.config.CgroupParent, fmt.Sprintf("libpod-conmon-%s", c.ID())))
855860
}
856861

857862
// RootFsSize returns the root FS size of the container

libpod/container_internal.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ func newContainer(rspec *spec.Spec, lockDir string) (*Container, error) {
160160
ctr.config.CreatedTime = time.Now()
161161

162162
ctr.config.ShmSize = DefaultShmSize
163-
ctr.config.CgroupParent = DefaultCgroupParent
164163

165164
ctr.state.BindMounts = make(map[string]string)
166165

@@ -1129,6 +1128,13 @@ func (c *Container) generateSpec(ctx context.Context) (*spec.Spec, error) {
11291128
g.AddProcessEnv("container", "libpod")
11301129
}
11311130

1131+
cgroupPath, err := c.CGroupPath()("")
1132+
if err != nil {
1133+
return nil, errors.Wrapf(err, "error retrieving CGroup path for container %s", c.ID())
1134+
}
1135+
logrus.Debugf("Setting CGroup path for container %s to %s", c.ID(), cgroupPath)
1136+
g.SetLinuxCgroupsPath(cgroupPath)
1137+
11321138
return g.Spec(), nil
11331139
}
11341140

libpod/runtime_ctr.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package libpod
33
import (
44
"context"
55
"os"
6+
"path"
67
"path/filepath"
78
"strings"
89
"time"
@@ -60,6 +61,24 @@ func (r *Runtime) NewContainer(ctx context.Context, rSpec *spec.Spec, options ..
6061
ctr.config.Name = name
6162
}
6263

64+
// Check CGroup parent sanity, and set it if it was not set
65+
switch r.config.CgroupManager {
66+
case CgroupfsCgroupsManager:
67+
if ctr.config.CgroupParent == "" {
68+
ctr.config.CgroupParent = CgroupfsDefaultCgroupParent
69+
} else if strings.HasSuffix(path.Base(ctr.config.CgroupParent), ".slice") {
70+
return nil, errors.Wrapf(ErrInvalidArg, "systemd slice received as cgroup parent when using cgroupfs")
71+
}
72+
case SystemdCgroupsManager:
73+
if ctr.config.CgroupParent == "" {
74+
ctr.config.CgroupParent = SystemdDefaultCgroupParent
75+
} else if len(ctr.config.CgroupParent) < 6 || !strings.HasSuffix(path.Base(ctr.config.CgroupParent), ".slice") {
76+
return nil, errors.Wrapf(ErrInvalidArg, "did not receive systemd slice as cgroup parent when using systemd to manage cgroups")
77+
}
78+
default:
79+
return nil, errors.Wrapf(ErrInvalidArg, "unsupported CGroup manager: %s - cannot validate cgroup parent", r.config.CgroupManager)
80+
}
81+
6382
// Set up storage for the container
6483
if err := ctr.setupStorage(ctx); err != nil {
6584
return nil, err

0 commit comments

Comments
 (0)