2014-05-15 06:21:44 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package systemd
|
|
|
|
|
|
|
|
import (
|
2015-10-17 02:32:19 +08:00
|
|
|
"errors"
|
2014-05-22 04:48:06 +08:00
|
|
|
"fmt"
|
2014-05-15 06:21:44 +08:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2014-06-04 08:25:07 +08:00
|
|
|
"time"
|
2014-05-15 06:21:44 +08:00
|
|
|
|
2015-07-25 07:35:48 +08:00
|
|
|
systemdDbus "github.com/coreos/go-systemd/dbus"
|
|
|
|
systemdUtil "github.com/coreos/go-systemd/util"
|
2015-06-24 06:55:02 +08:00
|
|
|
"github.com/godbus/dbus"
|
2015-06-22 10:29:59 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups/fs"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
2014-05-15 06:21:44 +08:00
|
|
|
)
|
|
|
|
|
2015-01-13 05:54:00 +08:00
|
|
|
type Manager struct {
|
2015-05-26 02:29:09 +08:00
|
|
|
mu sync.Mutex
|
2015-02-01 11:56:27 +08:00
|
|
|
Cgroups *configs.Cgroup
|
2015-01-14 23:47:26 +08:00
|
|
|
Paths map[string]string
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
type subsystem interface {
|
2015-10-17 02:32:19 +08:00
|
|
|
// Name returns the name of the subsystem.
|
|
|
|
Name() string
|
2015-02-25 17:20:01 +08:00
|
|
|
// Returns the stats, as 'stats', corresponding to the cgroup under 'path'.
|
|
|
|
GetStats(path string, stats *cgroups.Stats) error
|
|
|
|
// Set the cgroup represented by cgroup.
|
|
|
|
Set(path string, cgroup *configs.Cgroup) error
|
2014-06-20 21:13:56 +08:00
|
|
|
}
|
|
|
|
|
2015-10-17 02:32:19 +08:00
|
|
|
var errSubsystemDoesNotExist = errors.New("cgroup: subsystem does not exist")
|
|
|
|
|
|
|
|
type subsystemSet []subsystem
|
|
|
|
|
|
|
|
func (s subsystemSet) Get(name string) (subsystem, error) {
|
|
|
|
for _, ss := range s {
|
|
|
|
if ss.Name() == name {
|
|
|
|
return ss, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, errSubsystemDoesNotExist
|
|
|
|
}
|
|
|
|
|
|
|
|
var subsystems = subsystemSet{
|
|
|
|
&fs.CpusetGroup{},
|
|
|
|
&fs.DevicesGroup{},
|
|
|
|
&fs.MemoryGroup{},
|
|
|
|
&fs.CpuGroup{},
|
|
|
|
&fs.CpuacctGroup{},
|
2015-12-14 21:33:56 +08:00
|
|
|
&fs.PidsGroup{},
|
2015-10-17 02:32:19 +08:00
|
|
|
&fs.BlkioGroup{},
|
|
|
|
&fs.HugetlbGroup{},
|
|
|
|
&fs.PerfEventGroup{},
|
|
|
|
&fs.FreezerGroup{},
|
|
|
|
&fs.NetPrioGroup{},
|
|
|
|
&fs.NetClsGroup{},
|
|
|
|
&fs.NameGroup{GroupName: "name=systemd"},
|
2015-03-03 06:36:09 +08:00
|
|
|
}
|
|
|
|
|
2015-03-25 08:41:17 +08:00
|
|
|
const (
|
|
|
|
testScopeWait = 4
|
2016-09-28 04:01:03 +08:00
|
|
|
testSliceWait = 4
|
2015-03-25 08:41:17 +08:00
|
|
|
)
|
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
var (
|
2015-02-04 09:43:21 +08:00
|
|
|
connLock sync.Mutex
|
2015-07-25 07:35:48 +08:00
|
|
|
theConn *systemdDbus.Conn
|
2015-02-04 09:43:21 +08:00
|
|
|
hasStartTransientUnit bool
|
2016-09-28 04:01:03 +08:00
|
|
|
hasStartTransientSliceUnit bool
|
2015-02-04 09:43:21 +08:00
|
|
|
hasTransientDefaultDependencies bool
|
2016-06-01 19:26:12 +08:00
|
|
|
hasDelegate bool
|
2014-05-15 06:21:44 +08:00
|
|
|
)
|
|
|
|
|
2015-07-25 07:35:48 +08:00
|
|
|
func newProp(name string, units interface{}) systemdDbus.Property {
|
|
|
|
return systemdDbus.Property{
|
2014-11-06 00:56:47 +08:00
|
|
|
Name: name,
|
|
|
|
Value: dbus.MakeVariant(units),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
func UseSystemd() bool {
|
2015-07-25 07:35:48 +08:00
|
|
|
if !systemdUtil.IsRunningSystemd() {
|
2014-05-15 06:21:44 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
connLock.Lock()
|
|
|
|
defer connLock.Unlock()
|
|
|
|
|
|
|
|
if theConn == nil {
|
|
|
|
var err error
|
2015-07-25 07:35:48 +08:00
|
|
|
theConn, err = systemdDbus.New()
|
2014-05-15 06:21:44 +08:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assume we have StartTransientUnit
|
|
|
|
hasStartTransientUnit = true
|
|
|
|
|
|
|
|
// But if we get UnknownMethod error we don't
|
2015-07-24 21:54:59 +08:00
|
|
|
if _, err := theConn.StartTransientUnit("test.scope", "invalid", nil, nil); err != nil {
|
2014-05-15 06:21:44 +08:00
|
|
|
if dbusError, ok := err.(dbus.Error); ok {
|
|
|
|
if dbusError.Name == "org.freedesktop.DBus.Error.UnknownMethod" {
|
|
|
|
hasStartTransientUnit = false
|
2015-02-04 09:43:21 +08:00
|
|
|
return hasStartTransientUnit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 08:41:17 +08:00
|
|
|
// Ensure the scope name we use doesn't exist. Use the Pid to
|
|
|
|
// avoid collisions between multiple libcontainer users on a
|
|
|
|
// single host.
|
|
|
|
scope := fmt.Sprintf("libcontainer-%d-systemd-test-default-dependencies.scope", os.Getpid())
|
|
|
|
testScopeExists := true
|
|
|
|
for i := 0; i <= testScopeWait; i++ {
|
2015-07-24 21:54:59 +08:00
|
|
|
if _, err := theConn.StopUnit(scope, "replace", nil); err != nil {
|
2015-03-25 08:41:17 +08:00
|
|
|
if dbusError, ok := err.(dbus.Error); ok {
|
|
|
|
if strings.Contains(dbusError.Name, "org.freedesktop.systemd1.NoSuchUnit") {
|
|
|
|
testScopeExists = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bail out if we can't kill this scope without testing for DefaultDependencies
|
|
|
|
if testScopeExists {
|
|
|
|
return hasStartTransientUnit
|
|
|
|
}
|
|
|
|
|
2015-02-04 09:43:21 +08:00
|
|
|
// Assume StartTransientUnit on a scope allows DefaultDependencies
|
|
|
|
hasTransientDefaultDependencies = true
|
|
|
|
ddf := newProp("DefaultDependencies", false)
|
2015-07-25 07:35:48 +08:00
|
|
|
if _, err := theConn.StartTransientUnit(scope, "replace", []systemdDbus.Property{ddf}, nil); err != nil {
|
2015-02-04 09:43:21 +08:00
|
|
|
if dbusError, ok := err.(dbus.Error); ok {
|
2015-03-20 20:01:55 +08:00
|
|
|
if strings.Contains(dbusError.Name, "org.freedesktop.DBus.Error.PropertyReadOnly") {
|
2015-02-04 09:43:21 +08:00
|
|
|
hasTransientDefaultDependencies = false
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-25 08:41:17 +08:00
|
|
|
|
|
|
|
// Not critical because of the stop unit logic above.
|
2015-07-24 21:54:59 +08:00
|
|
|
theConn.StopUnit(scope, "replace", nil)
|
2016-06-01 19:26:12 +08:00
|
|
|
|
|
|
|
// Assume StartTransientUnit on a scope allows Delegate
|
|
|
|
hasDelegate = true
|
|
|
|
dl := newProp("Delegate", true)
|
|
|
|
if _, err := theConn.StartTransientUnit(scope, "replace", []systemdDbus.Property{dl}, nil); err != nil {
|
|
|
|
if dbusError, ok := err.(dbus.Error); ok {
|
|
|
|
if strings.Contains(dbusError.Name, "org.freedesktop.DBus.Error.PropertyReadOnly") {
|
|
|
|
hasDelegate = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-28 04:01:03 +08:00
|
|
|
// Assume we have the ability to start a transient unit as a slice
|
|
|
|
// This was broken until systemd v229, but has been back-ported on RHEL environments >= 219
|
|
|
|
// For details, see: https://bugzilla.redhat.com/show_bug.cgi?id=1370299
|
|
|
|
hasStartTransientSliceUnit = true
|
|
|
|
|
|
|
|
// To ensure simple clean-up, we create a slice off the root with no hierarchy
|
|
|
|
slice := fmt.Sprintf("libcontainer_%d_systemd_test_default.slice", os.Getpid())
|
|
|
|
if _, err := theConn.StartTransientUnit(slice, "replace", nil, nil); err != nil {
|
|
|
|
if _, ok := err.(dbus.Error); ok {
|
|
|
|
hasStartTransientSliceUnit = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i <= testSliceWait; i++ {
|
|
|
|
if _, err := theConn.StopUnit(slice, "replace", nil); err != nil {
|
|
|
|
if dbusError, ok := err.(dbus.Error); ok {
|
|
|
|
if strings.Contains(dbusError.Name, "org.freedesktop.systemd1.NoSuchUnit") {
|
|
|
|
hasStartTransientSliceUnit = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
}
|
|
|
|
|
2016-06-01 19:26:12 +08:00
|
|
|
// Not critical because of the stop unit logic above.
|
|
|
|
theConn.StopUnit(scope, "replace", nil)
|
2016-09-28 04:01:03 +08:00
|
|
|
theConn.StopUnit(slice, "replace", nil)
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
return hasStartTransientUnit
|
|
|
|
}
|
|
|
|
|
2015-01-13 05:54:00 +08:00
|
|
|
func (m *Manager) Apply(pid int) error {
|
2014-05-15 06:21:44 +08:00
|
|
|
var (
|
2015-01-13 05:54:00 +08:00
|
|
|
c = m.Cgroups
|
2014-05-22 04:48:06 +08:00
|
|
|
unitName = getUnitName(c)
|
2014-05-15 06:21:44 +08:00
|
|
|
slice = "system.slice"
|
2015-07-25 07:35:48 +08:00
|
|
|
properties []systemdDbus.Property
|
2014-05-15 06:21:44 +08:00
|
|
|
)
|
|
|
|
|
2016-01-12 05:12:51 +08:00
|
|
|
if c.Paths != nil {
|
|
|
|
paths := make(map[string]string)
|
|
|
|
for name, path := range c.Paths {
|
|
|
|
_, err := getSubsystemPath(m.Cgroups, name)
|
|
|
|
if err != nil {
|
|
|
|
// Don't fail if a cgroup hierarchy was not found, just skip this subsystem
|
|
|
|
if cgroups.IsNotFound(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
paths[name] = path
|
|
|
|
}
|
|
|
|
m.Paths = paths
|
|
|
|
return cgroups.EnterPid(m.Paths, pid)
|
|
|
|
}
|
|
|
|
|
2015-12-03 12:57:02 +08:00
|
|
|
if c.Parent != "" {
|
|
|
|
slice = c.Parent
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2016-09-28 04:01:03 +08:00
|
|
|
properties = append(properties, systemdDbus.PropDescription("libcontainer container "+c.Name))
|
|
|
|
|
|
|
|
// if we create a slice, the parent is defined via a Wants=
|
|
|
|
if strings.HasSuffix(unitName, ".slice") {
|
|
|
|
// This was broken until systemd v229, but has been back-ported on RHEL environments >= 219
|
|
|
|
if !hasStartTransientSliceUnit {
|
|
|
|
return fmt.Errorf("systemd version does not support ability to start a slice as transient unit")
|
|
|
|
}
|
|
|
|
properties = append(properties, systemdDbus.PropWants(slice))
|
|
|
|
} else {
|
|
|
|
// otherwise, we use Slice=
|
|
|
|
properties = append(properties, systemdDbus.PropSlice(slice))
|
|
|
|
}
|
|
|
|
|
|
|
|
// only add pid if its valid, -1 is used w/ general slice creation.
|
|
|
|
if pid != -1 {
|
|
|
|
properties = append(properties, newProp("PIDs", []uint32{uint32(pid)}))
|
|
|
|
}
|
2014-05-15 06:21:44 +08:00
|
|
|
|
2016-06-01 19:26:12 +08:00
|
|
|
if hasDelegate {
|
|
|
|
// This is only supported on systemd versions 218 and above.
|
|
|
|
properties = append(properties, newProp("Delegate", true))
|
|
|
|
}
|
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
// Always enable accounting, this gets us the same behaviour as the fs implementation,
|
|
|
|
// plus the kernel has some problems with joining the memory cgroup at a later time.
|
|
|
|
properties = append(properties,
|
2014-11-06 00:56:47 +08:00
|
|
|
newProp("MemoryAccounting", true),
|
|
|
|
newProp("CPUAccounting", true),
|
|
|
|
newProp("BlockIOAccounting", true))
|
2014-05-15 06:21:44 +08:00
|
|
|
|
2015-02-04 09:43:21 +08:00
|
|
|
if hasTransientDefaultDependencies {
|
|
|
|
properties = append(properties,
|
|
|
|
newProp("DefaultDependencies", false))
|
|
|
|
}
|
|
|
|
|
2015-12-15 08:26:29 +08:00
|
|
|
if c.Resources.Memory != 0 {
|
2014-05-15 06:21:44 +08:00
|
|
|
properties = append(properties,
|
2017-03-20 18:51:39 +08:00
|
|
|
newProp("MemoryLimit", c.Resources.Memory))
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2015-12-15 08:26:29 +08:00
|
|
|
if c.Resources.CpuShares != 0 {
|
2014-05-15 06:21:44 +08:00
|
|
|
properties = append(properties,
|
2017-03-20 18:51:39 +08:00
|
|
|
newProp("CPUShares", c.Resources.CpuShares))
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2017-02-24 17:44:02 +08:00
|
|
|
// cpu.cfs_quota_us and cpu.cfs_period_us are controlled by systemd.
|
|
|
|
if c.Resources.CpuQuota != 0 && c.Resources.CpuPeriod != 0 {
|
2017-03-20 18:51:39 +08:00
|
|
|
cpuQuotaPerSecUSec := uint64(c.Resources.CpuQuota*1000000) / c.Resources.CpuPeriod
|
2017-02-24 17:44:02 +08:00
|
|
|
properties = append(properties,
|
2017-03-20 18:51:39 +08:00
|
|
|
newProp("CPUQuotaPerSecUSec", cpuQuotaPerSecUSec))
|
2017-02-24 17:44:02 +08:00
|
|
|
}
|
|
|
|
|
2015-12-15 08:26:29 +08:00
|
|
|
if c.Resources.BlkioWeight != 0 {
|
2015-01-27 20:54:19 +08:00
|
|
|
properties = append(properties,
|
2015-12-15 08:26:29 +08:00
|
|
|
newProp("BlockIOWeight", uint64(c.Resources.BlkioWeight)))
|
2015-01-27 20:54:19 +08:00
|
|
|
}
|
|
|
|
|
2016-04-29 01:49:29 +08:00
|
|
|
// We have to set kernel memory here, as we can't change it once
|
|
|
|
// processes have been attached to the cgroup.
|
|
|
|
if c.Resources.KernelMemory != 0 {
|
2015-06-18 15:44:30 +08:00
|
|
|
if err := setKernelMemory(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-19 05:49:14 +08:00
|
|
|
if _, err := theConn.StartTransientUnit(unitName, "replace", properties, nil); err != nil && !isUnitExists(err) {
|
2015-01-13 05:54:00 +08:00
|
|
|
return err
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2016-02-15 15:56:59 +08:00
|
|
|
if err := joinCgroups(c, pid); err != nil {
|
2015-04-08 14:11:29 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-14 07:25:18 +08:00
|
|
|
paths := make(map[string]string)
|
2015-10-17 02:32:19 +08:00
|
|
|
for _, s := range subsystems {
|
|
|
|
subsystemPath, err := getSubsystemPath(m.Cgroups, s.Name())
|
2014-08-14 07:25:18 +08:00
|
|
|
if err != nil {
|
|
|
|
// Don't fail if a cgroup hierarchy was not found, just skip this subsystem
|
2014-08-21 01:32:01 +08:00
|
|
|
if cgroups.IsNotFound(err) {
|
2014-08-14 07:25:18 +08:00
|
|
|
continue
|
|
|
|
}
|
2015-01-13 05:54:00 +08:00
|
|
|
return err
|
2014-08-14 07:25:18 +08:00
|
|
|
}
|
2015-10-17 02:32:19 +08:00
|
|
|
paths[s.Name()] = subsystemPath
|
2014-08-14 07:25:18 +08:00
|
|
|
}
|
2015-01-14 23:47:26 +08:00
|
|
|
m.Paths = paths
|
2015-01-13 05:54:00 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-14 23:23:42 +08:00
|
|
|
func (m *Manager) Destroy() error {
|
2016-01-12 05:12:51 +08:00
|
|
|
if m.Cgroups.Paths != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2015-05-26 02:29:09 +08:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2015-07-24 21:54:59 +08:00
|
|
|
theConn.StopUnit(getUnitName(m.Cgroups), "replace", nil)
|
2015-05-26 02:29:09 +08:00
|
|
|
if err := cgroups.RemovePaths(m.Paths); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.Paths = make(map[string]string)
|
|
|
|
return nil
|
2015-01-13 05:54:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Manager) GetPaths() map[string]string {
|
2015-05-26 02:29:09 +08:00
|
|
|
m.mu.Lock()
|
|
|
|
paths := m.Paths
|
|
|
|
m.mu.Unlock()
|
|
|
|
return paths
|
2014-08-13 14:18:55 +08:00
|
|
|
}
|
|
|
|
|
2015-04-02 11:00:35 +08:00
|
|
|
func join(c *configs.Cgroup, subsystem string, pid int) (string, error) {
|
|
|
|
path, err := getSubsystemPath(c, subsystem)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
Simplify and fix os.MkdirAll() usage
TL;DR: check for IsExist(err) after a failed MkdirAll() is both
redundant and wrong -- so two reasons to remove it.
Quoting MkdirAll documentation:
> MkdirAll creates a directory named path, along with any necessary
> parents, and returns nil, or else returns an error. If path
> is already a directory, MkdirAll does nothing and returns nil.
This means two things:
1. If a directory to be created already exists, no error is
returned.
2. If the error returned is IsExist (EEXIST), it means there exists
a non-directory with the same name as MkdirAll need to use for
directory. Example: we want to MkdirAll("a/b"), but file "a"
(or "a/b") already exists, so MkdirAll fails.
The above is a theory, based on quoted documentation and my UNIX
knowledge.
3. In practice, though, current MkdirAll implementation [1] returns
ENOTDIR in most of cases described in #2, with the exception when
there is a race between MkdirAll and someone else creating the
last component of MkdirAll argument as a file. In this very case
MkdirAll() will indeed return EEXIST.
Because of #1, IsExist check after MkdirAll is not needed.
Because of #2 and #3, ignoring IsExist error is just plain wrong,
as directory we require is not created. It's cleaner to report
the error now.
Note this error is all over the tree, I guess due to copy-paste,
or trying to follow the same usage pattern as for Mkdir(),
or some not quite correct examples on the Internet.
[1] https://github.com/golang/go/blob/f9ed2f75/src/os/path.go
Signed-off-by: Kir Kolyshkin <kir@openvz.org>
2015-07-30 09:01:41 +08:00
|
|
|
if err := os.MkdirAll(path, 0755); err != nil {
|
2015-04-02 11:00:35 +08:00
|
|
|
return "", err
|
|
|
|
}
|
2016-09-28 04:01:03 +08:00
|
|
|
if err := cgroups.WriteCgroupProc(path, pid); err != nil {
|
2015-04-02 11:00:35 +08:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return path, nil
|
|
|
|
}
|
|
|
|
|
2016-02-15 15:56:59 +08:00
|
|
|
func joinCgroups(c *configs.Cgroup, pid int) error {
|
|
|
|
for _, sys := range subsystems {
|
|
|
|
name := sys.Name()
|
|
|
|
switch name {
|
|
|
|
case "name=systemd":
|
|
|
|
// let systemd handle this
|
|
|
|
break
|
|
|
|
case "cpuset":
|
|
|
|
path, err := getSubsystemPath(c, name)
|
|
|
|
if err != nil && !cgroups.IsNotFound(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s := &fs.CpusetGroup{}
|
|
|
|
if err := s.ApplyDir(path, c, pid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
_, err := join(c, name, pid)
|
|
|
|
if err != nil {
|
|
|
|
// Even if it's `not found` error, we'll return err
|
|
|
|
// because devices cgroup is hard requirement for
|
|
|
|
// container security.
|
|
|
|
if name == "devices" {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// For other subsystems, omit the `not found` error
|
|
|
|
// because they are optional.
|
|
|
|
if !cgroups.IsNotFound(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-14 09:09:14 +08:00
|
|
|
}
|
2015-12-14 21:33:56 +08:00
|
|
|
|
2015-12-20 19:30:35 +08:00
|
|
|
return nil
|
2015-12-14 21:33:56 +08:00
|
|
|
}
|
|
|
|
|
2016-10-12 07:22:48 +08:00
|
|
|
// systemd represents slice hierarchy using `-`, so we need to follow suit when
|
2016-01-25 19:34:48 +08:00
|
|
|
// generating the path of slice. Essentially, test-a-b.slice becomes
|
|
|
|
// test.slice/test-a.slice/test-a-b.slice.
|
2016-09-28 04:01:03 +08:00
|
|
|
func ExpandSlice(slice string) (string, error) {
|
2016-01-25 19:34:48 +08:00
|
|
|
suffix := ".slice"
|
2016-01-27 16:00:52 +08:00
|
|
|
// Name has to end with ".slice", but can't be just ".slice".
|
|
|
|
if len(slice) < len(suffix) || !strings.HasSuffix(slice, suffix) {
|
|
|
|
return "", fmt.Errorf("invalid slice name: %s", slice)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Path-separators are not allowed.
|
|
|
|
if strings.Contains(slice, "/") {
|
|
|
|
return "", fmt.Errorf("invalid slice name: %s", slice)
|
|
|
|
}
|
2016-01-25 19:34:48 +08:00
|
|
|
|
|
|
|
var path, prefix string
|
2016-01-27 16:00:52 +08:00
|
|
|
sliceName := strings.TrimSuffix(slice, suffix)
|
2016-09-28 04:01:03 +08:00
|
|
|
// if input was -.slice, we should just return root now
|
|
|
|
if sliceName == "-" {
|
|
|
|
return "/", nil
|
|
|
|
}
|
2016-01-25 19:34:48 +08:00
|
|
|
for _, component := range strings.Split(sliceName, "-") {
|
|
|
|
// test--a.slice isn't permitted, nor is -test.slice.
|
|
|
|
if component == "" {
|
|
|
|
return "", fmt.Errorf("invalid slice name: %s", slice)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append the component to the path and to the prefix.
|
|
|
|
path += prefix + component + suffix + "/"
|
|
|
|
prefix += component + "-"
|
|
|
|
}
|
|
|
|
|
|
|
|
return path, nil
|
|
|
|
}
|
|
|
|
|
2015-02-01 11:56:27 +08:00
|
|
|
func getSubsystemPath(c *configs.Cgroup, subsystem string) (string, error) {
|
2014-06-20 21:13:56 +08:00
|
|
|
mountpoint, err := cgroups.FindCgroupMountpoint(subsystem)
|
2014-05-31 06:09:07 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2016-04-26 00:19:39 +08:00
|
|
|
initPath, err := cgroups.GetInitCgroup(subsystem)
|
2014-05-31 06:09:07 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-07-27 09:57:38 +08:00
|
|
|
// if pid 1 is systemd 226 or later, it will be in init.scope, not the root
|
|
|
|
initPath = strings.TrimSuffix(filepath.Clean(initPath), "init.scope")
|
2014-05-31 06:09:07 +08:00
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
slice := "system.slice"
|
2015-12-03 12:57:02 +08:00
|
|
|
if c.Parent != "" {
|
|
|
|
slice = c.Parent
|
2014-06-20 21:13:56 +08:00
|
|
|
}
|
2014-05-31 06:09:07 +08:00
|
|
|
|
2016-09-28 04:01:03 +08:00
|
|
|
slice, err = ExpandSlice(slice)
|
2016-01-25 19:34:48 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
return filepath.Join(mountpoint, initPath, slice, getUnitName(c)), nil
|
2014-05-31 06:09:07 +08:00
|
|
|
}
|
|
|
|
|
2015-02-01 11:56:27 +08:00
|
|
|
func (m *Manager) Freeze(state configs.FreezerState) error {
|
2015-01-13 19:52:14 +08:00
|
|
|
path, err := getSubsystemPath(m.Cgroups, "freezer")
|
2014-05-31 06:09:07 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-15 08:26:29 +08:00
|
|
|
prevState := m.Cgroups.Resources.Freezer
|
|
|
|
m.Cgroups.Resources.Freezer = state
|
2015-10-17 02:32:19 +08:00
|
|
|
freezer, err := subsystems.Get("freezer")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-02 10:22:38 +08:00
|
|
|
err = freezer.Set(path, m.Cgroups)
|
|
|
|
if err != nil {
|
2015-12-15 08:26:29 +08:00
|
|
|
m.Cgroups.Resources.Freezer = prevState
|
2014-06-04 08:25:07 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2014-05-31 06:09:07 +08:00
|
|
|
}
|
|
|
|
|
2015-01-13 05:54:00 +08:00
|
|
|
func (m *Manager) GetPids() ([]int, error) {
|
2015-10-13 05:28:31 +08:00
|
|
|
path, err := getSubsystemPath(m.Cgroups, "devices")
|
2014-05-22 04:48:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-13 05:28:31 +08:00
|
|
|
return cgroups.GetPids(path)
|
2014-05-22 04:48:06 +08:00
|
|
|
}
|
|
|
|
|
2016-01-09 03:37:18 +08:00
|
|
|
func (m *Manager) GetAllPids() ([]int, error) {
|
|
|
|
path, err := getSubsystemPath(m.Cgroups, "devices")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return cgroups.GetAllPids(path)
|
|
|
|
}
|
|
|
|
|
2015-01-13 05:54:00 +08:00
|
|
|
func (m *Manager) GetStats() (*cgroups.Stats, error) {
|
2015-05-26 02:29:09 +08:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2015-03-03 06:36:09 +08:00
|
|
|
stats := cgroups.NewStats()
|
|
|
|
for name, path := range m.Paths {
|
2015-10-17 02:32:19 +08:00
|
|
|
sys, err := subsystems.Get(name)
|
|
|
|
if err == errSubsystemDoesNotExist || !cgroups.PathExists(path) {
|
2015-03-03 06:36:09 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := sys.GetStats(path, stats); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stats, nil
|
2015-01-13 05:54:00 +08:00
|
|
|
}
|
|
|
|
|
2015-02-25 17:20:01 +08:00
|
|
|
func (m *Manager) Set(container *configs.Config) error {
|
2016-03-03 03:35:52 +08:00
|
|
|
// If Paths are set, then we are just joining cgroups paths
|
|
|
|
// and there is no need to set any values.
|
|
|
|
if m.Cgroups.Paths != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2015-12-17 17:13:06 +08:00
|
|
|
for _, sys := range subsystems {
|
|
|
|
// Get the subsystem path, but don't error out for not found cgroups.
|
|
|
|
path, err := getSubsystemPath(container.Cgroups, sys.Name())
|
|
|
|
if err != nil && !cgroups.IsNotFound(err) {
|
|
|
|
return err
|
2015-12-17 16:20:58 +08:00
|
|
|
}
|
2015-12-17 17:13:06 +08:00
|
|
|
|
2015-04-02 09:57:04 +08:00
|
|
|
if err := sys.Set(path, container.Cgroups); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-20 19:30:35 +08:00
|
|
|
if m.Paths["cpu"] != "" {
|
|
|
|
if err := fs.CheckCpushares(m.Paths["cpu"], container.Cgroups.Resources.CpuShares); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-04-02 09:57:04 +08:00
|
|
|
return nil
|
2015-02-25 17:20:01 +08:00
|
|
|
}
|
|
|
|
|
2015-02-01 11:56:27 +08:00
|
|
|
func getUnitName(c *configs.Cgroup) string {
|
2016-09-28 04:01:03 +08:00
|
|
|
// by default, we create a scope unless the user explicitly asks for a slice.
|
|
|
|
if !strings.HasSuffix(c.Name, ".slice") {
|
|
|
|
return fmt.Sprintf("%s-%s.scope", c.ScopePrefix, c.Name)
|
|
|
|
}
|
|
|
|
return c.Name
|
2014-05-22 04:48:06 +08:00
|
|
|
}
|
2014-06-20 21:13:56 +08:00
|
|
|
|
2015-06-18 15:44:30 +08:00
|
|
|
func setKernelMemory(c *configs.Cgroup) error {
|
2014-08-14 09:00:15 +08:00
|
|
|
path, err := getSubsystemPath(c, "memory")
|
2015-04-22 10:18:22 +08:00
|
|
|
if err != nil && !cgroups.IsNotFound(err) {
|
2014-08-14 09:00:15 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-07-23 08:04:37 +08:00
|
|
|
if err := os.MkdirAll(path, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return fs.EnableKernelMemoryAccounting(path)
|
2015-06-18 15:44:30 +08:00
|
|
|
}
|
2016-10-19 05:49:14 +08:00
|
|
|
|
|
|
|
// isUnitExists returns true if the error is that a systemd unit already exists.
|
|
|
|
func isUnitExists(err error) bool {
|
|
|
|
if err != nil {
|
|
|
|
if dbusError, ok := err.(dbus.Error); ok {
|
|
|
|
return strings.Contains(dbusError.Name, "org.freedesktop.systemd1.UnitExists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|