2014-05-15 06:21:44 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package systemd
|
|
|
|
|
|
|
|
import (
|
2014-06-04 08:25:07 +08:00
|
|
|
"bytes"
|
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
|
|
|
|
2014-08-19 20:27:31 +08:00
|
|
|
systemd "github.com/coreos/go-systemd/dbus"
|
2014-06-10 23:14:16 +08:00
|
|
|
"github.com/docker/libcontainer/cgroups"
|
2014-06-20 21:13:56 +08:00
|
|
|
"github.com/docker/libcontainer/cgroups/fs"
|
2014-05-15 06:21:44 +08:00
|
|
|
"github.com/godbus/dbus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type systemdCgroup struct {
|
2014-08-14 09:00:15 +08:00
|
|
|
cgroup *cgroups.Cgroup
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
type subsystem interface {
|
|
|
|
GetStats(string, *cgroups.Stats) error
|
|
|
|
}
|
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
var (
|
|
|
|
connLock sync.Mutex
|
2014-08-19 20:27:31 +08:00
|
|
|
theConn *systemd.Conn
|
2014-05-15 06:21:44 +08:00
|
|
|
hasStartTransientUnit bool
|
|
|
|
)
|
|
|
|
|
2014-11-06 00:56:47 +08:00
|
|
|
func newProp(name string, units interface{}) systemd.Property {
|
|
|
|
return systemd.Property{
|
|
|
|
Name: name,
|
|
|
|
Value: dbus.MakeVariant(units),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
func UseSystemd() bool {
|
2014-08-19 20:27:31 +08:00
|
|
|
s, err := os.Stat("/run/systemd/system")
|
|
|
|
if err != nil || !s.IsDir() {
|
2014-05-15 06:21:44 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
connLock.Lock()
|
|
|
|
defer connLock.Unlock()
|
|
|
|
|
|
|
|
if theConn == nil {
|
|
|
|
var err error
|
2014-08-19 20:27:31 +08:00
|
|
|
theConn, err = systemd.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
|
|
|
|
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-11-15 09:22:10 +08:00
|
|
|
func Apply(c *cgroups.Cgroup, pid int) (map[string]string, error) {
|
2014-05-15 06:21:44 +08:00
|
|
|
var (
|
2014-05-22 04:48:06 +08:00
|
|
|
unitName = getUnitName(c)
|
2014-05-15 06:21:44 +08:00
|
|
|
slice = "system.slice"
|
2014-08-19 20:27:31 +08:00
|
|
|
properties []systemd.Property
|
2014-08-14 09:00:15 +08:00
|
|
|
res = &systemdCgroup{}
|
2014-05-15 06:21:44 +08:00
|
|
|
)
|
|
|
|
|
2014-08-14 07:25:18 +08:00
|
|
|
res.cgroup = c
|
2014-05-15 06:21:44 +08:00
|
|
|
|
|
|
|
if c.Slice != "" {
|
|
|
|
slice = c.Slice
|
|
|
|
}
|
|
|
|
|
|
|
|
properties = append(properties,
|
2014-11-06 00:56:47 +08:00
|
|
|
systemd.PropSlice(slice),
|
|
|
|
systemd.PropDescription("docker container "+c.Name),
|
|
|
|
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
|
|
|
|
|
|
|
if c.Memory != 0 {
|
|
|
|
properties = append(properties,
|
2014-11-06 00:56:47 +08:00
|
|
|
newProp("MemoryLimit", uint64(c.Memory)))
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
// TODO: MemoryReservation and MemorySwap not available in systemd
|
|
|
|
|
|
|
|
if c.CpuShares != 0 {
|
|
|
|
properties = append(properties,
|
2014-11-06 00:56:47 +08:00
|
|
|
newProp("CPUShares", uint64(c.CpuShares)))
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := theConn.StartTransientUnit(unitName, "replace", properties...); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-02-18 07:14:30 +08:00
|
|
|
if !c.AllowAllDevices {
|
2014-08-14 09:00:15 +08:00
|
|
|
if err := joinDevices(c, pid); err != nil {
|
2014-05-15 06:21:44 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-14 09:00:15 +08:00
|
|
|
// -1 disables memorySwap
|
|
|
|
if c.MemorySwap >= 0 && (c.Memory != 0 || c.MemorySwap > 0) {
|
|
|
|
if err := joinMemory(c, pid); err != nil {
|
2014-05-15 06:21:44 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-31 06:09:07 +08:00
|
|
|
// we need to manually join the freezer cgroup in systemd 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 {
|
2014-05-31 06:09:07 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-05-15 06:21:44 +08:00
|
|
|
|
2014-08-14 09:00:15 +08:00
|
|
|
if c.CpusetCpus != "" {
|
|
|
|
if err := joinCpuset(c, pid); err != nil {
|
2014-05-15 06:21:44 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-14 07:25:18 +08:00
|
|
|
paths := make(map[string]string)
|
2014-11-15 09:03:40 +08:00
|
|
|
for _, sysname := range []string{
|
|
|
|
"devices",
|
|
|
|
"memory",
|
|
|
|
"cpu",
|
|
|
|
"cpuset",
|
|
|
|
"cpuacct",
|
|
|
|
"blkio",
|
|
|
|
"perf_event",
|
|
|
|
"freezer",
|
|
|
|
} {
|
2014-11-15 09:22:10 +08:00
|
|
|
subsystemPath, err := getSubsystemPath(res.cgroup, sysname)
|
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
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
paths[sysname] = subsystemPath
|
|
|
|
}
|
|
|
|
return paths, nil
|
2014-08-13 14:18:55 +08:00
|
|
|
}
|
|
|
|
|
2014-11-15 09:22:10 +08:00
|
|
|
func writeFile(dir, file, data string) error {
|
|
|
|
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
|
|
|
|
2014-08-14 09:00:15 +08:00
|
|
|
func joinFreezer(c *cgroups.Cgroup, pid int) error {
|
2014-06-20 21:13:56 +08:00
|
|
|
path, err := getSubsystemPath(c, "freezer")
|
2014-05-31 06:09:07 +08:00
|
|
|
if err != nil {
|
2014-08-14 09:00:15 +08:00
|
|
|
return err
|
2014-05-31 06:09:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(path, 0755); err != nil && !os.IsExist(err) {
|
2014-08-14 09:00:15 +08:00
|
|
|
return err
|
2014-05-31 06:09:07 +08:00
|
|
|
}
|
|
|
|
|
2014-08-14 09:00:15 +08:00
|
|
|
return ioutil.WriteFile(filepath.Join(path, "cgroup.procs"), []byte(strconv.Itoa(pid)), 0700)
|
2014-05-31 06:09:07 +08:00
|
|
|
}
|
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
func getSubsystemPath(c *cgroups.Cgroup, subsystem string) (string, error) {
|
|
|
|
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"
|
|
|
|
if c.Slice != "" {
|
|
|
|
slice = c.Slice
|
|
|
|
}
|
2014-05-31 06:09:07 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func Freeze(c *cgroups.Cgroup, state cgroups.FreezerState) error {
|
2014-06-20 21:13:56 +08:00
|
|
|
path, err := getSubsystemPath(c, "freezer")
|
2014-05-31 06:09:07 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-06-04 08:25:07 +08:00
|
|
|
if err := ioutil.WriteFile(filepath.Join(path, "freezer.state"), []byte(state), 0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
state_, err := ioutil.ReadFile(filepath.Join(path, "freezer.state"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if string(state) == string(bytes.TrimSpace(state_)) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(1 * time.Millisecond)
|
|
|
|
}
|
|
|
|
return nil
|
2014-05-31 06:09:07 +08:00
|
|
|
}
|
|
|
|
|
2014-05-22 04:48:06 +08:00
|
|
|
func GetPids(c *cgroups.Cgroup) ([]int, error) {
|
2014-08-14 09:00:15 +08:00
|
|
|
path, err := getSubsystemPath(c, "cpu")
|
2014-05-22 04:48:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-08-14 09:00:15 +08:00
|
|
|
return cgroups.ReadProcsFile(path)
|
2014-05-22 04:48:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func getUnitName(c *cgroups.Cgroup) string {
|
|
|
|
return fmt.Sprintf("%s-%s.scope", c.Parent, c.Name)
|
|
|
|
}
|
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
|
|
|
|
// in wide use. When both these are availalable we will be able to switch, but need to keep the old
|
|
|
|
// 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.
|
|
|
|
func joinDevices(c *cgroups.Cgroup, pid int) error {
|
|
|
|
path, err := getSubsystemPath(c, "devices")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.MkdirAll(path, 0755); err != nil && !os.IsExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(path, "cgroup.procs"), []byte(strconv.Itoa(pid)), 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := writeFile(path, "devices.deny", "a"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dev := range c.AllowedDevices {
|
|
|
|
if err := writeFile(path, "devices.allow", dev.GetCgroupAllowString()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-15 04:31:23 +08:00
|
|
|
// Symmetrical public function to update device based cgroups. Also available
|
|
|
|
// in the fs implementation.
|
|
|
|
func ApplyDevices(c *cgroups.Cgroup, pid int) error {
|
|
|
|
return joinDevices(c, pid)
|
|
|
|
}
|
|
|
|
|
2014-08-14 09:00:15 +08:00
|
|
|
func joinMemory(c *cgroups.Cgroup, pid int) error {
|
|
|
|
memorySwap := c.MemorySwap
|
|
|
|
|
|
|
|
if memorySwap == 0 {
|
|
|
|
// By default, MemorySwap is set to twice the size of RAM.
|
|
|
|
memorySwap = c.Memory * 2
|
|
|
|
}
|
|
|
|
|
|
|
|
path, err := getSubsystemPath(c, "memory")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioutil.WriteFile(filepath.Join(path, "memory.memsw.limit_in_bytes"), []byte(strconv.FormatInt(memorySwap, 10)), 0700)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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"
|
|
|
|
func joinCpuset(c *cgroups.Cgroup, pid int) error {
|
|
|
|
path, err := getSubsystemPath(c, "cpuset")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &fs.CpusetGroup{}
|
|
|
|
|
|
|
|
return s.SetDir(path, c.CpusetCpus, pid)
|
|
|
|
}
|