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
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"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
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
hasTransientDefaultDependencies 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)
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
return hasStartTransientUnit
|
|
|
|
}
|
|
|
|
|
|
|
|
func getIfaceForUnit(unitName string) string {
|
|
|
|
if strings.HasSuffix(unitName, ".scope") {
|
|
|
|
return "Scope"
|
|
|
|
}
|
|
|
|
if strings.HasSuffix(unitName, ".service") {
|
|
|
|
return "Service"
|
|
|
|
}
|
|
|
|
return "Unit"
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
properties = append(properties,
|
2015-07-25 07:35:48 +08:00
|
|
|
systemdDbus.PropSlice(slice),
|
|
|
|
systemdDbus.PropDescription("docker container "+c.Name),
|
2014-11-06 00:56:47 +08:00
|
|
|
newProp("PIDs", []uint32{uint32(pid)}),
|
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,
|
2015-12-15 08:26:29 +08:00
|
|
|
newProp("MemoryLimit", uint64(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,
|
2015-12-15 08:26:29 +08:00
|
|
|
newProp("CPUShares", uint64(c.Resources.CpuShares)))
|
2014-05-15 06:21:44 +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
|
|
|
}
|
|
|
|
|
2015-06-18 15:44:30 +08:00
|
|
|
// We need to set kernel memory before processes join cgroup because
|
|
|
|
// kmem.limit_in_bytes can only be set when the cgroup is empty.
|
|
|
|
// And swap memory limit needs to be set after memory limit, only
|
|
|
|
// memory limit is handled by systemd, so it's kind of ugly here.
|
2015-12-15 08:26:29 +08:00
|
|
|
if c.Resources.KernelMemory > 0 {
|
2015-06-18 15:44:30 +08:00
|
|
|
if err := setKernelMemory(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-24 21:54:59 +08:00
|
|
|
if _, err := theConn.StartTransientUnit(unitName, "replace", properties, nil); err != nil {
|
2015-01-13 05:54:00 +08:00
|
|
|
return err
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2015-01-22 09:53:30 +08:00
|
|
|
if err := joinDevices(c, pid); err != nil {
|
2015-02-17 05:09:00 +08:00
|
|
|
return err
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2015-02-13 14:33:03 +08:00
|
|
|
// TODO: CpuQuota and CpuPeriod not available in systemd
|
|
|
|
// we need to manually join the cpu.cfs_quota_us and cpu.cfs_period_us
|
|
|
|
if err := joinCpu(c, pid); err != nil {
|
2015-02-20 07:59:50 +08:00
|
|
|
return err
|
2015-02-13 14:33:03 +08:00
|
|
|
}
|
|
|
|
|
2015-09-29 10:02:12 +08:00
|
|
|
// TODO: MemoryReservation and MemorySwap not available in systemd
|
2015-05-15 14:24:56 +08:00
|
|
|
if err := joinMemory(c, pid); err != nil {
|
|
|
|
return err
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2015-12-14 21:33:56 +08:00
|
|
|
// we need to manually join the freezer, net_cls, net_prio, pids and cpuset cgroup in systemd
|
2014-11-14 08:15:47 +08:00
|
|
|
// because it does not currently support it via the dbus api.
|
2014-08-14 09:00:15 +08:00
|
|
|
if err := joinFreezer(c, pid); err != nil {
|
2015-01-13 05:54:00 +08:00
|
|
|
return err
|
2014-05-31 06:09:07 +08:00
|
|
|
}
|
2014-05-15 06:21:44 +08:00
|
|
|
|
2015-05-14 10:48:46 +08:00
|
|
|
if err := joinNetPrio(c, pid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-14 09:09:14 +08:00
|
|
|
if err := joinNetCls(c, pid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-14 10:48:46 +08:00
|
|
|
|
2015-12-14 21:33:56 +08:00
|
|
|
if err := joinPids(c, pid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-14 08:15:47 +08:00
|
|
|
if err := joinCpuset(c, pid); err != nil {
|
2015-01-13 05:54:00 +08:00
|
|
|
return err
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2015-04-27 16:34:36 +08:00
|
|
|
if err := joinHugetlb(c, pid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-29 15:42:29 +08:00
|
|
|
|
|
|
|
if err := joinPerfEvent(c, pid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-08 14:11:29 +08:00
|
|
|
// FIXME: Systemd does have `BlockIODeviceWeight` property, but we got problem
|
2015-06-22 10:29:59 +08:00
|
|
|
// using that (at least on systemd 208, see https://github.com/opencontainers/runc/libcontainer/pull/354),
|
2015-04-08 14:11:29 +08:00
|
|
|
// so use fs work around for now.
|
|
|
|
if err := joinBlkio(c, pid); err != nil {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-11-15 09:22:10 +08:00
|
|
|
func writeFile(dir, file, data string) error {
|
2015-04-22 10:18:22 +08:00
|
|
|
// Normally dir should not be empty, one case is that cgroup subsystem
|
|
|
|
// is not mounted, we will get empty dir, and we want it fail here.
|
|
|
|
if dir == "" {
|
|
|
|
return fmt.Errorf("no such directory for %s.", file)
|
|
|
|
}
|
2014-11-15 09:22:10 +08:00
|
|
|
return ioutil.WriteFile(filepath.Join(dir, file), []byte(data), 0700)
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
2014-05-22 04:48:06 +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
|
|
|
|
}
|
|
|
|
if err := writeFile(path, "cgroup.procs", strconv.Itoa(pid)); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return path, nil
|
|
|
|
}
|
|
|
|
|
2015-02-20 07:59:50 +08:00
|
|
|
func joinCpu(c *configs.Cgroup, pid int) error {
|
2015-12-20 19:30:35 +08:00
|
|
|
_, err := join(c, "cpu", pid)
|
2015-04-22 10:18:22 +08:00
|
|
|
if err != nil && !cgroups.IsNotFound(err) {
|
2015-02-13 14:33:03 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-01 11:56:27 +08:00
|
|
|
func joinFreezer(c *configs.Cgroup, pid int) error {
|
2015-12-20 19:30:35 +08:00
|
|
|
_, err := join(c, "freezer", pid)
|
2015-05-15 10:39:04 +08:00
|
|
|
if err != nil && !cgroups.IsNotFound(err) {
|
2014-08-14 09:00:15 +08:00
|
|
|
return err
|
2014-05-31 06:09:07 +08:00
|
|
|
}
|
2015-12-20 19:30:35 +08:00
|
|
|
return nil
|
2014-05-31 06:09:07 +08:00
|
|
|
}
|
|
|
|
|
2015-05-14 10:48:46 +08:00
|
|
|
func joinNetPrio(c *configs.Cgroup, pid int) error {
|
2015-12-20 19:30:35 +08:00
|
|
|
_, err := join(c, "net_prio", pid)
|
2015-05-14 10:48:46 +08:00
|
|
|
if err != nil && !cgroups.IsNotFound(err) {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-20 19:30:35 +08:00
|
|
|
return nil
|
2015-05-14 10:48:46 +08:00
|
|
|
}
|
|
|
|
|
2015-05-14 09:09:14 +08:00
|
|
|
func joinNetCls(c *configs.Cgroup, pid int) error {
|
2015-12-20 19:30:35 +08:00
|
|
|
_, err := join(c, "net_cls", pid)
|
2015-05-14 09:09:14 +08:00
|
|
|
if err != nil && !cgroups.IsNotFound(err) {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-20 19:30:35 +08:00
|
|
|
return nil
|
2015-12-14 21:33:56 +08:00
|
|
|
}
|
|
|
|
|
2015-12-14 21:33:56 +08:00
|
|
|
func joinPids(c *configs.Cgroup, pid int) error {
|
2015-12-20 19:30:35 +08:00
|
|
|
_, err := join(c, "pids", pid)
|
2015-12-14 21:33:56 +08:00
|
|
|
if err != nil && !cgroups.IsNotFound(err) {
|
2015-12-14 21:33:56 +08:00
|
|
|
return err
|
|
|
|
}
|
2015-12-20 19:30:35 +08:00
|
|
|
return nil
|
2015-12-14 21:33:56 +08:00
|
|
|
}
|
|
|
|
|
2016-01-25 19:34:48 +08:00
|
|
|
// systemd represents slice heirarchy using `-`, so we need to follow suit when
|
|
|
|
// generating the path of slice. Essentially, test-a-b.slice becomes
|
|
|
|
// test.slice/test-a.slice/test-a-b.slice.
|
|
|
|
func expandSlice(slice string) (string, error) {
|
|
|
|
suffix := ".slice"
|
|
|
|
sliceName := strings.TrimSuffix(slice, suffix)
|
|
|
|
|
|
|
|
var path, prefix string
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
initPath, err := cgroups.GetInitCgroupDir(subsystem)
|
2014-05-31 06:09:07 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
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-01-25 19:34:48 +08:00
|
|
|
slice, err = expandSlice(slice)
|
|
|
|
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 {
|
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 {
|
2015-12-03 12:57:02 +08:00
|
|
|
return fmt.Sprintf("%s-%s.scope", c.ScopePrefix, c.Name)
|
2014-05-22 04:48:06 +08:00
|
|
|
}
|
2014-06-20 21:13:56 +08:00
|
|
|
|
2014-08-14 09:00:15 +08:00
|
|
|
// Atm we can't use the systemd device support because of two missing things:
|
|
|
|
// * Support for wildcards to allow mknod on any device
|
|
|
|
// * Support for wildcards to allow /dev/pts support
|
|
|
|
//
|
|
|
|
// The second is available in more recent systemd as "char-pts", but not in e.g. v208 which is
|
2015-04-20 10:35:51 +08:00
|
|
|
// in wide use. When both these are available we will be able to switch, but need to keep the old
|
2014-08-14 09:00:15 +08:00
|
|
|
// implementation for backwards compat.
|
|
|
|
//
|
|
|
|
// Note: we can't use systemd to set up the initial limits, and then change the cgroup
|
|
|
|
// because systemd will re-write the device settings if it needs to re-apply the cgroup context.
|
|
|
|
// This happens at least for v208 when any sibling unit is started.
|
2015-02-01 11:56:27 +08:00
|
|
|
func joinDevices(c *configs.Cgroup, pid int) error {
|
2015-12-20 19:30:35 +08:00
|
|
|
_, err := join(c, "devices", pid)
|
2015-04-22 10:18:22 +08:00
|
|
|
// Even if it's `not found` error, we'll return err because devices cgroup
|
|
|
|
// is hard requirement for container security.
|
2014-08-14 09:00:15 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-20 19:30:35 +08:00
|
|
|
return nil
|
2014-08-14 09:00:15 +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
|
|
|
|
}
|
|
|
|
|
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-06-18 15:44:30 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-13 22:05:08 +08:00
|
|
|
// This doesn't get called by manager.Set, so we need to do it here.
|
|
|
|
s := &fs.MemoryGroup{}
|
|
|
|
return s.SetKernelMemory(path, c)
|
2015-06-18 15:44:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func joinMemory(c *configs.Cgroup, pid int) error {
|
2016-01-13 22:05:08 +08:00
|
|
|
_, err := join(c, "memory", pid)
|
2015-06-18 15:44:30 +08:00
|
|
|
if err != nil && !cgroups.IsNotFound(err) {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-15 14:24:56 +08:00
|
|
|
return nil
|
2014-08-14 09:00:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// systemd does not atm set up the cpuset controller, so we must manually
|
|
|
|
// join it. Additionally that is a very finicky controller where each
|
|
|
|
// level must have a full setup as the default for a new directory is "no cpus"
|
2015-02-01 11:56:27 +08:00
|
|
|
func joinCpuset(c *configs.Cgroup, pid int) error {
|
2014-08-14 09:00:15 +08:00
|
|
|
path, err := getSubsystemPath(c, "cpuset")
|
2015-04-22 10:18:22 +08:00
|
|
|
if err != nil && !cgroups.IsNotFound(err) {
|
2014-08-14 09:00:15 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &fs.CpusetGroup{}
|
|
|
|
|
2015-02-27 12:09:42 +08:00
|
|
|
return s.ApplyDir(path, c, pid)
|
2014-08-14 09:00:15 +08:00
|
|
|
}
|
2015-04-08 14:11:29 +08:00
|
|
|
|
|
|
|
// `BlockIODeviceWeight` property of systemd does not work properly, and systemd
|
|
|
|
// expects device path instead of major minor numbers, which is also confusing
|
|
|
|
// for users. So we use fs work around for now.
|
|
|
|
func joinBlkio(c *configs.Cgroup, pid int) error {
|
2015-12-20 19:30:35 +08:00
|
|
|
_, err := join(c, "blkio", pid)
|
2015-04-08 14:11:29 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-04-27 16:34:36 +08:00
|
|
|
|
|
|
|
func joinHugetlb(c *configs.Cgroup, pid int) error {
|
2015-12-20 19:30:35 +08:00
|
|
|
_, err := join(c, "hugetlb", pid)
|
2015-04-27 16:34:36 +08:00
|
|
|
if err != nil && !cgroups.IsNotFound(err) {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-20 19:30:35 +08:00
|
|
|
return nil
|
2015-04-27 16:34:36 +08:00
|
|
|
}
|
2015-09-29 15:42:29 +08:00
|
|
|
|
|
|
|
func joinPerfEvent(c *configs.Cgroup, pid int) error {
|
2015-12-20 19:30:35 +08:00
|
|
|
_, err := join(c, "perf_event", pid)
|
2015-09-29 15:42:29 +08:00
|
|
|
if err != nil && !cgroups.IsNotFound(err) {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-20 19:30:35 +08:00
|
|
|
return nil
|
2015-09-29 15:42:29 +08:00
|
|
|
}
|