Skip to content

Commit df83d36

Browse files
mheonrh-atomic-bot
authored andcommitted
Major fixes to systemd cgroup handling
Signed-off-by: Matthew Heon <[email protected]> Closes: containers#507 Approved by: baude
1 parent fee9ec1 commit df83d36

File tree

6 files changed

+51
-24
lines changed

6 files changed

+51
-24
lines changed

cmd/podman/run.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,13 @@ func runCmd(c *cli.Context) error {
123123
return err
124124
}
125125

126-
if logrus.GetLevel() == logrus.DebugLevel {
127-
logrus.Debugf("New container created %q", ctr.ID())
126+
logrus.Debugf("New container created %q", ctr.ID())
128127

129-
p, _ := ctr.CGroupPath()("")
130-
logrus.Debugf("container %q has CgroupParent %q", ctr.ID(), p)
128+
if logrus.GetLevel() == logrus.DebugLevel {
129+
cgroupPath, err := ctr.CGroupPath()
130+
if err == nil {
131+
logrus.Debugf("container %q has CgroupParent %q", ctr.ID(), cgroupPath)
132+
}
131133
}
132134

133135
createConfigJSON, err := json.Marshal(createConfig)

libpod/container.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"path/filepath"
77
"time"
88

9-
"github.com/containerd/cgroups"
109
"github.com/containernetworking/cni/pkg/types"
1110
cnitypes "github.com/containernetworking/cni/pkg/types/current"
1211
"github.com/containernetworking/plugins/pkg/ns"
@@ -854,9 +853,15 @@ func (c *Container) NamespacePath(ns LinuxNS) (string, error) {
854853
}
855854

856855
// CGroupPath returns a cgroups "path" for a given container.
857-
func (c *Container) CGroupPath() cgroups.Path {
858-
// TODO add support for systemd cgroup paths
859-
return cgroups.StaticPath(filepath.Join(c.config.CgroupParent, fmt.Sprintf("libpod-conmon-%s", c.ID())))
856+
func (c *Container) CGroupPath() (string, error) {
857+
switch c.runtime.config.CgroupManager {
858+
case CgroupfsCgroupsManager:
859+
return filepath.Join(c.config.CgroupParent, fmt.Sprintf("libpod-conmon-%s", c.ID())), nil
860+
case SystemdCgroupsManager:
861+
return filepath.Join(c.config.CgroupParent, createUnitName("libpod", c.ID())), nil
862+
default:
863+
return "", errors.Wrapf(ErrInvalidArg, "unsupported CGroup manager %s in use", c.runtime.config.CgroupManager)
864+
}
860865
}
861866

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

libpod/container_internal.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"io/ioutil"
99
"os"
10+
"path"
1011
"path/filepath"
1112
"regexp"
1213
"strings"
@@ -1128,12 +1129,19 @@ func (c *Container) generateSpec(ctx context.Context) (*spec.Spec, error) {
11281129
g.AddProcessEnv("container", "libpod")
11291130
}
11301131

1131-
cgroupPath, err := c.CGroupPath()("")
1132-
if err != nil {
1133-
return nil, errors.Wrapf(err, "error retrieving CGroup path for container %s", c.ID())
1132+
if c.runtime.config.CgroupManager == SystemdCgroupsManager {
1133+
// When runc is set to use Systemd as a cgroup manager, it
1134+
// expects cgroups to be passed as follows:
1135+
// slice:prefix:name
1136+
g.SetLinuxCgroupsPath(path.Base(c.config.CgroupParent) + ":" + "libpod" + ":" + c.ID())
1137+
} else {
1138+
cgroupPath, err := c.CGroupPath()
1139+
if err != nil {
1140+
return nil, err
1141+
}
1142+
logrus.Debugf("Setting CGroup path for container %s to %s", c.ID(), cgroupPath)
1143+
g.SetLinuxCgroupsPath(cgroupPath)
11341144
}
1135-
logrus.Debugf("Setting CGroup path for container %s to %s", c.ID(), cgroupPath)
1136-
g.SetLinuxCgroupsPath(cgroupPath)
11371145

11381146
return g.Spec(), nil
11391147
}

libpod/container_top.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ func (c *Container) GetContainerPids() ([]string, error) {
2727
// Gets the pids for a container without locking. should only be called from a func where
2828
// locking has already been established.
2929
func (c *Container) getContainerPids() ([]string, error) {
30-
cgroupPath, err := c.CGroupPath()("")
30+
cgroupPath, err := c.CGroupPath()
3131
if err != nil {
32-
return nil, errors.Wrapf(err, "error getting cgroup path for container %s", c.ID())
32+
return nil, err
3333
}
3434

3535
taskFile := filepath.Join("/sys/fs/cgroup/pids", cgroupPath, "tasks")

libpod/oci.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -321,19 +321,26 @@ func (r *OCIRuntime) createOCIContainer(ctr *Container, cgroupParent string) (er
321321

322322
// Move conmon to specified cgroup
323323
if r.cgroupManager == SystemdCgroupsManager {
324-
logrus.Infof("Running conmon under slice %s and unitName %s", cgroupParent, createUnitName("libpod-conmon", ctr.ID()))
325-
if err = utils.RunUnderSystemdScope(cmd.Process.Pid, cgroupParent, createUnitName("libpod-conmon", ctr.ID())); err != nil {
324+
unitName := createUnitName("libpod", ctr.ID())
325+
326+
logrus.Infof("Running conmon under slice %s and unitName %s", cgroupParent, unitName)
327+
if err = utils.RunUnderSystemdScope(cmd.Process.Pid, cgroupParent, unitName); err != nil {
326328
logrus.Warnf("Failed to add conmon to systemd sandbox cgroup: %v", err)
327329
}
328330
} else {
329-
control, err := cgroups.New(cgroups.V1, cgroups.StaticPath(filepath.Join(cgroupParent, "/libpod-conmon-"+ctr.ID())), &spec.LinuxResources{})
331+
cgroupPath, err := ctr.CGroupPath()
330332
if err != nil {
331-
logrus.Warnf("Failed to add conmon to cgroupfs sandbox cgroup: %v", err)
333+
logrus.Errorf("Failed to generate CGroup path for conmon: %v", err)
332334
} else {
333-
// we need to remove this defer and delete the cgroup once conmon exits
334-
// maybe need a conmon monitor?
335-
if err := control.Add(cgroups.Process{Pid: cmd.Process.Pid}); err != nil {
335+
control, err := cgroups.New(cgroups.V1, cgroups.StaticPath(cgroupPath), &spec.LinuxResources{})
336+
if err != nil {
336337
logrus.Warnf("Failed to add conmon to cgroupfs sandbox cgroup: %v", err)
338+
} else {
339+
// we need to remove this defer and delete the cgroup once conmon exits
340+
// maybe need a conmon monitor?
341+
if err := control.Add(cgroups.Process{Pid: cmd.Process.Pid}); err != nil {
342+
logrus.Warnf("Failed to add conmon to cgroupfs sandbox cgroup: %v", err)
343+
}
337344
}
338345
}
339346
}

libpod/stats.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,14 @@ func (c *Container) GetContainerStats(previousStats *ContainerStats) (*Container
4343
return stats, nil
4444
}
4545

46-
cgroup, err := cgroups.Load(cgroups.V1, c.CGroupPath())
46+
cgroupPath, err := c.CGroupPath()
4747
if err != nil {
48-
return stats, errors.Wrapf(err, "unable to load cgroup at %+v", c.CGroupPath())
48+
return nil, err
49+
}
50+
51+
cgroup, err := cgroups.Load(cgroups.V1, cgroups.StaticPath(cgroupPath))
52+
if err != nil {
53+
return stats, errors.Wrapf(err, "unable to load cgroup at %s", cgroupPath)
4954
}
5055

5156
cgroupStats, err := cgroup.Stat()

0 commit comments

Comments
 (0)