summaryrefslogtreecommitdiff
path: root/libpod/util.go
diff options
context:
space:
mode:
authorMatthew Heon <matthew.heon@gmail.com>2018-08-13 15:39:11 -0400
committerAtomic Bot <atomic-devel@projectatomic.io>2018-08-15 18:07:04 +0000
commit0998808a752a8a7ae43f2630023b22c1b3de47a4 (patch)
tree7307d450f56898f5779c324b8c75efd2b40060f0 /libpod/util.go
parentad7d46957156a2ab9ae5bae6b4e337d941a3b971 (diff)
downloadpodman-0998808a752a8a7ae43f2630023b22c1b3de47a4.tar.gz
podman-0998808a752a8a7ae43f2630023b22c1b3de47a4.tar.bz2
podman-0998808a752a8a7ae43f2630023b22c1b3de47a4.zip
Create pod CGroups when using the systemd cgroup driver
Signed-off-by: Matthew Heon <matthew.heon@gmail.com> Closes: #1266 Approved by: baude
Diffstat (limited to 'libpod/util.go')
-rw-r--r--libpod/util.go58
1 files changed, 58 insertions, 0 deletions
diff --git a/libpod/util.go b/libpod/util.go
index 13235059f..5b16b5755 100644
--- a/libpod/util.go
+++ b/libpod/util.go
@@ -9,10 +9,12 @@ import (
"strings"
"time"
+ "github.com/containerd/cgroups"
"github.com/containers/image/signature"
"github.com/containers/image/types"
spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
+ "github.com/sirupsen/logrus"
)
// Runtime API constants
@@ -123,6 +125,62 @@ func WaitForFile(path string, timeout time.Duration) error {
}
}
+// systemdSliceFromPath makes a new systemd slice under the given parent with
+// the given name.
+// The parent must be a slice. The name must NOT include ".slice"
+func systemdSliceFromPath(parent, name string) (string, error) {
+ cgroupPath, err := assembleSystemdCgroupName(parent, name)
+ if err != nil {
+ return "", err
+ }
+
+ logrus.Debugf("Created cgroup path %s for parent %s and name %s", cgroupPath, parent, name)
+
+ if err := makeSystemdCgroup(cgroupPath); err != nil {
+ return "", errors.Wrapf(err, "error creating cgroup %s", cgroupPath)
+ }
+
+ logrus.Debugf("Created cgroup %s", cgroupPath)
+
+ return cgroupPath, nil
+}
+
+// makeSystemdCgroup creates a systemd CGroup at the given location.
+func makeSystemdCgroup(path string) error {
+ controller, err := cgroups.NewSystemd(SystemdDefaultCgroupParent)
+ if err != nil {
+ return err
+ }
+
+ return controller.Create(path, &spec.LinuxResources{})
+}
+
+// deleteSystemdCgroup deletes the systemd cgroup at the given location
+func deleteSystemdCgroup(path string) error {
+ controller, err := cgroups.NewSystemd(SystemdDefaultCgroupParent)
+ if err != nil {
+ return err
+ }
+
+ return controller.Delete(path)
+}
+
+// assembleSystemdCgroupName creates a systemd cgroup path given a base and
+// a new component to add.
+// The base MUST be systemd slice (end in .slice)
+func assembleSystemdCgroupName(baseSlice, newSlice string) (string, error) {
+ const sliceSuffix = ".slice"
+
+ if !strings.HasSuffix(baseSlice, sliceSuffix) {
+ return "", errors.Wrapf(ErrInvalidArg, "cannot assemble cgroup path with base %q - must end in .slice", baseSlice)
+ }
+
+ noSlice := strings.TrimSuffix(baseSlice, sliceSuffix)
+ final := fmt.Sprintf("%s/%s-%s%s", baseSlice, noSlice, newSlice, sliceSuffix)
+
+ return final, nil
+}
+
type byDestination []spec.Mount
func (m byDestination) Len() int {