2014-02-21 13:35:43 +00:00
|
|
|
// +build linux
|
|
|
|
|
2014-04-19 04:30:08 +00:00
|
|
|
package systemd
|
2014-02-21 13:35:43 +00:00
|
|
|
|
|
|
|
import (
|
2014-04-19 04:30:08 +00:00
|
|
|
"io/ioutil"
|
2014-04-23 09:00:12 +00:00
|
|
|
"os"
|
2014-02-21 13:35:43 +00:00
|
|
|
"path/filepath"
|
2014-04-23 09:00:12 +00:00
|
|
|
"strconv"
|
2014-02-21 13:35:43 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2014-04-19 04:30:08 +00:00
|
|
|
|
|
|
|
systemd1 "github.com/coreos/go-systemd/dbus"
|
|
|
|
"github.com/dotcloud/docker/pkg/cgroups"
|
|
|
|
"github.com/dotcloud/docker/pkg/systemd"
|
|
|
|
"github.com/godbus/dbus"
|
2014-02-21 13:35:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type systemdCgroup struct {
|
2014-04-23 09:00:12 +00:00
|
|
|
cleanupDirs []string
|
2014-02-21 13:35:43 +00:00
|
|
|
}
|
|
|
|
|
2014-04-19 04:30:08 +00:00
|
|
|
type DeviceAllow struct {
|
|
|
|
Node string
|
|
|
|
Permissions string
|
|
|
|
}
|
|
|
|
|
2014-02-21 13:35:43 +00:00
|
|
|
var (
|
|
|
|
connLock sync.Mutex
|
|
|
|
theConn *systemd1.Conn
|
|
|
|
hasStartTransientUnit bool
|
|
|
|
)
|
|
|
|
|
2014-04-19 04:30:08 +00:00
|
|
|
func UseSystemd() bool {
|
2014-02-21 13:35:43 +00:00
|
|
|
if !systemd.SdBooted() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
connLock.Lock()
|
|
|
|
defer connLock.Unlock()
|
|
|
|
|
|
|
|
if theConn == nil {
|
|
|
|
var err error
|
|
|
|
theConn, err = systemd1.New()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assume we have StartTransientUnit
|
|
|
|
hasStartTransientUnit = true
|
|
|
|
|
|
|
|
// But if we get UnknownMethod error we don't
|
|
|
|
if _, err := theConn.StartTransientUnit("test.scope", "invalid"); err != nil {
|
|
|
|
if dbusError, ok := err.(dbus.Error); ok {
|
|
|
|
if dbusError.Name == "org.freedesktop.DBus.Error.UnknownMethod" {
|
|
|
|
hasStartTransientUnit = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hasStartTransientUnit
|
|
|
|
}
|
|
|
|
|
|
|
|
func getIfaceForUnit(unitName string) string {
|
|
|
|
if strings.HasSuffix(unitName, ".scope") {
|
|
|
|
return "Scope"
|
|
|
|
}
|
|
|
|
if strings.HasSuffix(unitName, ".service") {
|
|
|
|
return "Service"
|
|
|
|
}
|
|
|
|
return "Unit"
|
|
|
|
}
|
|
|
|
|
2014-04-23 09:00:12 +00:00
|
|
|
type cgroupArg struct {
|
|
|
|
File string
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
2014-04-19 04:30:08 +00:00
|
|
|
func Apply(c *cgroups.Cgroup, pid int) (cgroups.ActiveCgroup, error) {
|
|
|
|
var (
|
|
|
|
unitName = c.Parent + "-" + c.Name + ".scope"
|
|
|
|
slice = "system.slice"
|
|
|
|
properties []systemd1.Property
|
2014-04-23 09:00:12 +00:00
|
|
|
cpuArgs []cgroupArg
|
|
|
|
cpusetArgs []cgroupArg
|
|
|
|
memoryArgs []cgroupArg
|
|
|
|
res systemdCgroup
|
2014-04-19 04:30:08 +00:00
|
|
|
)
|
2014-02-21 13:35:43 +00:00
|
|
|
|
2014-04-23 09:00:12 +00:00
|
|
|
// First set up things not supported by systemd
|
|
|
|
|
|
|
|
// -1 disables memorySwap
|
|
|
|
if c.MemorySwap >= 0 && (c.Memory != 0 || c.MemorySwap > 0) {
|
|
|
|
memorySwap := c.MemorySwap
|
|
|
|
|
|
|
|
if memorySwap == 0 {
|
|
|
|
// By default, MemorySwap is set to twice the size of RAM.
|
|
|
|
memorySwap = c.Memory * 2
|
2014-02-21 13:35:43 +00:00
|
|
|
}
|
2014-04-23 09:00:12 +00:00
|
|
|
|
|
|
|
memoryArgs = append(memoryArgs, cgroupArg{"memory.memsw.limit_in_bytes", strconv.FormatInt(memorySwap, 10)})
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.CpusetCpus != "" {
|
|
|
|
cpusetArgs = append(cpusetArgs, cgroupArg{"cpuset.cpus", c.CpusetCpus})
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Slice != "" {
|
|
|
|
slice = c.Slice
|
2014-02-21 13:35:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
properties = append(properties,
|
|
|
|
systemd1.Property{"Slice", dbus.MakeVariant(slice)},
|
|
|
|
systemd1.Property{"Description", dbus.MakeVariant("docker container " + c.Name)},
|
2014-04-19 04:30:08 +00:00
|
|
|
systemd1.Property{"PIDs", dbus.MakeVariant([]uint32{uint32(pid)})},
|
|
|
|
)
|
2014-02-21 13:35:43 +00:00
|
|
|
|
|
|
|
if !c.DeviceAccess {
|
|
|
|
properties = append(properties,
|
|
|
|
systemd1.Property{"DevicePolicy", dbus.MakeVariant("strict")},
|
|
|
|
systemd1.Property{"DeviceAllow", dbus.MakeVariant([]DeviceAllow{
|
|
|
|
{"/dev/null", "rwm"},
|
|
|
|
{"/dev/zero", "rwm"},
|
|
|
|
{"/dev/full", "rwm"},
|
|
|
|
{"/dev/random", "rwm"},
|
|
|
|
{"/dev/urandom", "rwm"},
|
|
|
|
{"/dev/tty", "rwm"},
|
|
|
|
{"/dev/console", "rwm"},
|
|
|
|
{"/dev/tty0", "rwm"},
|
|
|
|
{"/dev/tty1", "rwm"},
|
|
|
|
{"/dev/pts/ptmx", "rwm"},
|
|
|
|
// There is no way to add /dev/pts/* here atm, so we hack this manually below
|
|
|
|
// /dev/pts/* (how to add this?)
|
|
|
|
// Same with tuntap, which doesn't exist as a node most of the time
|
|
|
|
})})
|
|
|
|
}
|
|
|
|
|
2014-04-23 09:00:12 +00:00
|
|
|
// Always enable accounting, this gets us the same behaviour as the fs implementation,
|
2014-04-10 18:08:56 +00:00
|
|
|
// plus the kernel has some problems with joining the memory cgroup at a later time.
|
|
|
|
properties = append(properties,
|
|
|
|
systemd1.Property{"MemoryAccounting", dbus.MakeVariant(true)},
|
2014-04-23 09:00:12 +00:00
|
|
|
systemd1.Property{"CPUAccounting", dbus.MakeVariant(true)},
|
|
|
|
systemd1.Property{"BlockIOAccounting", dbus.MakeVariant(true)})
|
2014-04-10 18:08:56 +00:00
|
|
|
|
2014-02-21 13:35:43 +00:00
|
|
|
if c.Memory != 0 {
|
|
|
|
properties = append(properties,
|
|
|
|
systemd1.Property{"MemoryLimit", dbus.MakeVariant(uint64(c.Memory))})
|
|
|
|
}
|
2014-04-24 05:11:43 +00:00
|
|
|
if c.MemoryReservation != 0 {
|
|
|
|
properties = append(properties,
|
|
|
|
systemd1.Property{"MemorySoftLimit", dbus.MakeVariant(uint64(c.MemoryReservation))})
|
|
|
|
}
|
2014-02-21 13:35:43 +00:00
|
|
|
// TODO: MemorySwap not available in systemd
|
|
|
|
|
|
|
|
if c.CpuShares != 0 {
|
|
|
|
properties = append(properties,
|
|
|
|
systemd1.Property{"CPUShares", dbus.MakeVariant(uint64(c.CpuShares))})
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := theConn.StartTransientUnit(unitName, "replace", properties...); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// To work around the lack of /dev/pts/* support above we need to manually add these
|
|
|
|
// so, ask systemd for the cgroup used
|
|
|
|
props, err := theConn.GetUnitTypeProperties(unitName, getIfaceForUnit(unitName))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cgroup := props["ControlGroup"].(string)
|
|
|
|
|
|
|
|
if !c.DeviceAccess {
|
2014-04-19 04:30:08 +00:00
|
|
|
mountpoint, err := cgroups.FindCgroupMountpoint("devices")
|
2014-02-21 13:35:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
path := filepath.Join(mountpoint, cgroup)
|
|
|
|
|
|
|
|
// /dev/pts/*
|
2014-04-19 04:30:08 +00:00
|
|
|
if err := ioutil.WriteFile(filepath.Join(path, "devices.allow"), []byte("c 136:* rwm"), 0700); err != nil {
|
2014-02-21 13:35:43 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// tuntap
|
2014-04-19 04:30:08 +00:00
|
|
|
if err := ioutil.WriteFile(filepath.Join(path, "devices.allow"), []byte("c 10:200 rwm"), 0700); err != nil {
|
2014-02-21 13:35:43 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2014-04-23 09:00:12 +00:00
|
|
|
|
|
|
|
if len(cpuArgs) != 0 {
|
|
|
|
mountpoint, err := cgroups.FindCgroupMountpoint("cpu")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
path := filepath.Join(mountpoint, cgroup)
|
|
|
|
|
|
|
|
for _, arg := range cpuArgs {
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(path, arg.File), []byte(arg.Value), 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(memoryArgs) != 0 {
|
|
|
|
mountpoint, err := cgroups.FindCgroupMountpoint("memory")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
path := filepath.Join(mountpoint, cgroup)
|
|
|
|
|
|
|
|
for _, arg := range memoryArgs {
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(path, arg.File), []byte(arg.Value), 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cpusetArgs) != 0 {
|
|
|
|
// 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",
|
|
|
|
// so we avoid using any hierarchies here, creating a toplevel directory.
|
|
|
|
mountpoint, err := cgroups.FindCgroupMountpoint("cpuset")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
initPath, err := cgroups.GetInitCgroupDir("cpuset")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rootPath := filepath.Join(mountpoint, initPath)
|
|
|
|
|
|
|
|
path := filepath.Join(mountpoint, initPath, c.Parent+"-"+c.Name)
|
|
|
|
|
|
|
|
res.cleanupDirs = append(res.cleanupDirs, path)
|
|
|
|
|
|
|
|
if err := os.MkdirAll(path, 0755); err != nil && !os.IsExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
foundCpus := false
|
|
|
|
foundMems := false
|
|
|
|
|
|
|
|
for _, arg := range cpusetArgs {
|
|
|
|
if arg.File == "cpuset.cpus" {
|
|
|
|
foundCpus = true
|
|
|
|
}
|
|
|
|
if arg.File == "cpuset.mems" {
|
|
|
|
foundMems = true
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(path, arg.File), []byte(arg.Value), 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// These are required, if not specified inherit from parent
|
|
|
|
if !foundCpus {
|
|
|
|
s, err := ioutil.ReadFile(filepath.Join(rootPath, "cpuset.cpus"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(path, "cpuset.cpus"), s, 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// These are required, if not specified inherit from parent
|
|
|
|
if !foundMems {
|
|
|
|
s, err := ioutil.ReadFile(filepath.Join(rootPath, "cpuset.mems"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(path, "cpuset.mems"), s, 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(path, "cgroup.procs"), []byte(strconv.Itoa(pid)), 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &res, nil
|
2014-02-21 13:35:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *systemdCgroup) Cleanup() error {
|
2014-04-23 09:00:12 +00:00
|
|
|
// systemd cleans up, we don't need to do much
|
|
|
|
|
|
|
|
for _, path := range c.cleanupDirs {
|
|
|
|
os.RemoveAll(path)
|
|
|
|
}
|
|
|
|
|
2014-02-21 13:35:43 +00:00
|
|
|
return nil
|
|
|
|
}
|