2019-10-26 05:24:14 +08:00
|
|
|
// +build linux
|
2019-09-02 17:10:52 +08:00
|
|
|
|
|
|
|
package systemd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2020-03-01 21:52:48 +08:00
|
|
|
systemdDbus "github.com/coreos/go-systemd/v22/dbus"
|
2020-04-06 11:12:20 +08:00
|
|
|
securejoin "github.com/cyphar/filepath-securejoin"
|
2019-09-02 17:10:52 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
2019-11-07 16:25:49 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups/fs2"
|
2019-09-02 17:10:52 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2020-04-06 10:06:25 +08:00
|
|
|
type unifiedManager struct {
|
2019-09-02 17:10:52 +08:00
|
|
|
mu sync.Mutex
|
2020-04-06 10:06:25 +08:00
|
|
|
cgroups *configs.Cgroup
|
2020-04-05 02:19:05 +08:00
|
|
|
// path is like "/sys/fs/cgroup/user.slice/user-1001.slice/session-1.scope"
|
|
|
|
path string
|
|
|
|
rootless bool
|
|
|
|
}
|
|
|
|
|
2020-04-06 10:06:25 +08:00
|
|
|
func NewUnifiedManager(config *configs.Cgroup, path string, rootless bool) *unifiedManager {
|
|
|
|
return &unifiedManager{
|
|
|
|
cgroups: config,
|
2020-04-05 02:19:05 +08:00
|
|
|
path: path,
|
|
|
|
rootless: rootless,
|
|
|
|
}
|
2019-09-02 17:10:52 +08:00
|
|
|
}
|
|
|
|
|
2020-04-06 10:06:25 +08:00
|
|
|
func (m *unifiedManager) Apply(pid int) error {
|
2019-09-02 17:10:52 +08:00
|
|
|
var (
|
2020-04-06 10:06:25 +08:00
|
|
|
c = m.cgroups
|
2019-09-02 17:10:52 +08:00
|
|
|
unitName = getUnitName(c)
|
|
|
|
slice = "system.slice"
|
|
|
|
properties []systemdDbus.Property
|
|
|
|
)
|
|
|
|
|
|
|
|
if c.Paths != nil {
|
2020-04-05 02:19:05 +08:00
|
|
|
return cgroups.WriteCgroupProc(m.path, pid)
|
2019-09-02 17:10:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.Parent != "" {
|
|
|
|
slice = c.Parent
|
|
|
|
}
|
|
|
|
|
|
|
|
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") {
|
|
|
|
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)}))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we can delegate. This is only supported on systemd versions 218 and above.
|
|
|
|
if !strings.HasSuffix(unitName, ".slice") {
|
|
|
|
// Assume scopes always support delegation.
|
|
|
|
properties = append(properties, newProp("Delegate", true))
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
newProp("MemoryAccounting", true),
|
|
|
|
newProp("CPUAccounting", true),
|
2020-01-15 20:55:22 +08:00
|
|
|
newProp("IOAccounting", true))
|
2019-09-02 17:10:52 +08:00
|
|
|
|
|
|
|
// Assume DefaultDependencies= will always work (the check for it was previously broken.)
|
|
|
|
properties = append(properties,
|
|
|
|
newProp("DefaultDependencies", false))
|
|
|
|
|
|
|
|
if c.Resources.Memory != 0 {
|
|
|
|
properties = append(properties,
|
2020-01-15 20:55:22 +08:00
|
|
|
newProp("MemoryMax", uint64(c.Resources.Memory)))
|
2019-09-02 17:10:52 +08:00
|
|
|
}
|
|
|
|
|
2020-04-02 15:23:09 +08:00
|
|
|
swap, err := cgroups.ConvertMemorySwapToCgroupV2Value(c.Resources.MemorySwap, c.Resources.Memory)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if swap > 0 {
|
|
|
|
properties = append(properties,
|
|
|
|
newProp("MemorySwapMax", uint64(swap)))
|
|
|
|
}
|
|
|
|
|
2020-01-15 20:55:22 +08:00
|
|
|
if c.Resources.CpuWeight != 0 {
|
2019-09-02 17:10:52 +08:00
|
|
|
properties = append(properties,
|
2020-01-15 20:55:22 +08:00
|
|
|
newProp("CPUWeight", c.Resources.CpuWeight))
|
2019-09-02 17:10:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// cpu.cfs_quota_us and cpu.cfs_period_us are controlled by systemd.
|
|
|
|
if c.Resources.CpuQuota != 0 && c.Resources.CpuPeriod != 0 {
|
|
|
|
// corresponds to USEC_INFINITY in systemd
|
|
|
|
// if USEC_INFINITY is provided, CPUQuota is left unbound by systemd
|
|
|
|
// always setting a property value ensures we can apply a quota and remove it later
|
|
|
|
cpuQuotaPerSecUSec := uint64(math.MaxUint64)
|
|
|
|
if c.Resources.CpuQuota > 0 {
|
|
|
|
// systemd converts CPUQuotaPerSecUSec (microseconds per CPU second) to CPUQuota
|
|
|
|
// (integer percentage of CPU) internally. This means that if a fractional percent of
|
|
|
|
// CPU is indicated by Resources.CpuQuota, we need to round up to the nearest
|
|
|
|
// 10ms (1% of a second) such that child cgroups can set the cpu.cfs_quota_us they expect.
|
|
|
|
cpuQuotaPerSecUSec = uint64(c.Resources.CpuQuota*1000000) / c.Resources.CpuPeriod
|
|
|
|
if cpuQuotaPerSecUSec%10000 != 0 {
|
|
|
|
cpuQuotaPerSecUSec = ((cpuQuotaPerSecUSec / 10000) + 1) * 10000
|
|
|
|
}
|
|
|
|
}
|
|
|
|
properties = append(properties,
|
|
|
|
newProp("CPUQuotaPerSecUSec", cpuQuotaPerSecUSec))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Resources.PidsLimit > 0 {
|
|
|
|
properties = append(properties,
|
|
|
|
newProp("TasksAccounting", true),
|
|
|
|
newProp("TasksMax", uint64(c.Resources.PidsLimit)))
|
|
|
|
}
|
|
|
|
|
Support for setting systemd properties via annotations
In case systemd is used to set cgroups for the container,
it creates a scope unit dedicated to it (usually named
`runc-$ID.scope`).
This patch adds an ability to set arbitrary systemd properties
for the systemd unit via runtime spec annotations.
Initially this was developed as an ability to specify the
`TimeoutStopUSec` property, but later generalized to work with
arbitrary ones.
Example usage: add the following to runtime spec (config.json):
```
"annotations": {
"org.systemd.property.TimeoutStopUSec": "uint64 123456789",
"org.systemd.property.CollectMode":"'inactive-or-failed'"
},
```
and start the container (e.g. `runc --systemd-cgroup run $ID`).
The above will set the following systemd parameters:
* `TimeoutStopSec` to 2 minutes and 3 seconds,
* `CollectMode` to "inactive-or-failed".
The values are in the gvariant format (see [1]). To figure out
which type systemd expects for a particular parameter, see
systemd sources.
In particular, parameters with `USec` suffix require an `uint64`
typed argument, while gvariant assumes int32 for a numeric values,
therefore the explicit type is required.
NOTE that systemd receives the time-typed parameters as *USec
but shows them (in `systemctl show`) as *Sec. For example,
the stop timeout should be set as `TimeoutStopUSec` but
is shown as `TimeoutStopSec`.
[1] https://developer.gnome.org/glib/stable/gvariant-text.html
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-02-07 12:26:06 +08:00
|
|
|
properties = append(properties, c.SystemdProps...)
|
|
|
|
|
2020-03-26 10:52:28 +08:00
|
|
|
// ignore c.Resources.KernelMemory
|
2019-09-02 17:10:52 +08:00
|
|
|
|
2020-03-28 06:01:36 +08:00
|
|
|
dbusConnection, err := getDbusConnection()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-09-02 17:10:52 +08:00
|
|
|
statusChan := make(chan string, 1)
|
2020-03-28 06:01:36 +08:00
|
|
|
if _, err := dbusConnection.StartTransientUnit(unitName, "replace", properties, statusChan); err == nil {
|
2019-09-02 17:10:52 +08:00
|
|
|
select {
|
|
|
|
case <-statusChan:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
logrus.Warnf("Timed out while waiting for StartTransientUnit(%s) completion signal from dbus. Continuing...", unitName)
|
|
|
|
}
|
|
|
|
} else if !isUnitExists(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-05 02:19:05 +08:00
|
|
|
_, err = m.GetUnifiedPath()
|
2019-11-07 16:25:49 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-08 18:37:47 +08:00
|
|
|
if err := fs2.CreateCgroupPath(m.path, m.cgroups); err != nil {
|
2020-03-27 04:57:55 +08:00
|
|
|
return err
|
|
|
|
}
|
2019-09-02 17:10:52 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-06 10:06:25 +08:00
|
|
|
func (m *unifiedManager) Destroy() error {
|
|
|
|
if m.cgroups.Paths != nil {
|
2019-09-02 17:10:52 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2020-03-28 06:01:36 +08:00
|
|
|
|
|
|
|
dbusConnection, err := getDbusConnection()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-06 10:06:25 +08:00
|
|
|
dbusConnection.StopUnit(getUnitName(m.cgroups), "replace", nil)
|
2020-04-05 02:19:05 +08:00
|
|
|
|
|
|
|
// XXX this is probably not needed, systemd should handle it
|
|
|
|
err = os.Remove(m.path)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
2019-09-02 17:10:52 +08:00
|
|
|
return err
|
|
|
|
}
|
2020-04-05 02:19:05 +08:00
|
|
|
|
2019-09-02 17:10:52 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-05 02:19:05 +08:00
|
|
|
// this method is for v1 backward compatibility and will be removed
|
2020-04-06 10:06:25 +08:00
|
|
|
func (m *unifiedManager) GetPaths() map[string]string {
|
2020-04-05 02:19:05 +08:00
|
|
|
_, _ = m.GetUnifiedPath()
|
|
|
|
paths := map[string]string{
|
|
|
|
"pids": m.path,
|
|
|
|
"memory": m.path,
|
|
|
|
"io": m.path,
|
|
|
|
"cpu": m.path,
|
|
|
|
"devices": m.path,
|
|
|
|
"cpuset": m.path,
|
|
|
|
"freezer": m.path,
|
|
|
|
}
|
2019-09-02 17:10:52 +08:00
|
|
|
return paths
|
|
|
|
}
|
2020-04-05 02:19:05 +08:00
|
|
|
|
2020-04-06 10:06:25 +08:00
|
|
|
func (m *unifiedManager) GetUnifiedPath() (string, error) {
|
2019-10-19 00:40:46 +08:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2020-04-05 02:19:05 +08:00
|
|
|
if m.path != "" {
|
|
|
|
return m.path, nil
|
2019-10-19 00:40:46 +08:00
|
|
|
}
|
2020-03-27 04:35:51 +08:00
|
|
|
|
2020-04-06 10:06:25 +08:00
|
|
|
c := m.cgroups
|
2020-03-27 04:35:51 +08:00
|
|
|
slice := "system.slice"
|
|
|
|
if c.Parent != "" {
|
|
|
|
slice = c.Parent
|
|
|
|
}
|
|
|
|
|
|
|
|
slice, err := ExpandSlice(slice)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-04-06 11:12:20 +08:00
|
|
|
path := filepath.Join(slice, getUnitName(c))
|
|
|
|
path, err = securejoin.SecureJoin(fs2.UnifiedMountpoint, path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
m.path = path
|
|
|
|
|
2020-04-05 02:19:05 +08:00
|
|
|
return m.path, nil
|
2020-03-27 04:35:51 +08:00
|
|
|
}
|
|
|
|
|
2020-04-06 10:06:25 +08:00
|
|
|
func (m *unifiedManager) fsManager() (cgroups.Manager, error) {
|
2019-11-07 16:25:49 +08:00
|
|
|
path, err := m.GetUnifiedPath()
|
2019-09-02 17:10:52 +08:00
|
|
|
if err != nil {
|
2019-11-07 16:25:49 +08:00
|
|
|
return nil, err
|
2019-09-02 17:10:52 +08:00
|
|
|
}
|
2020-04-06 10:06:25 +08:00
|
|
|
return fs2.NewManager(m.cgroups, path, m.rootless)
|
2019-11-07 16:25:49 +08:00
|
|
|
}
|
|
|
|
|
2020-04-06 10:06:25 +08:00
|
|
|
func (m *unifiedManager) Freeze(state configs.FreezerState) error {
|
2019-11-07 16:25:49 +08:00
|
|
|
fsMgr, err := m.fsManager()
|
2019-09-02 17:10:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-07 16:25:49 +08:00
|
|
|
return fsMgr.Freeze(state)
|
2019-09-02 17:10:52 +08:00
|
|
|
}
|
|
|
|
|
2020-04-06 10:06:25 +08:00
|
|
|
func (m *unifiedManager) GetPids() ([]int, error) {
|
2019-10-19 00:40:46 +08:00
|
|
|
path, err := m.GetUnifiedPath()
|
2019-09-02 17:10:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return cgroups.GetPids(path)
|
|
|
|
}
|
|
|
|
|
2020-04-06 10:06:25 +08:00
|
|
|
func (m *unifiedManager) GetAllPids() ([]int, error) {
|
2019-10-19 00:40:46 +08:00
|
|
|
path, err := m.GetUnifiedPath()
|
2019-09-02 17:10:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return cgroups.GetAllPids(path)
|
|
|
|
}
|
|
|
|
|
2020-04-06 10:06:25 +08:00
|
|
|
func (m *unifiedManager) GetStats() (*cgroups.Stats, error) {
|
2019-11-07 16:25:49 +08:00
|
|
|
fsMgr, err := m.fsManager()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-09-02 17:10:52 +08:00
|
|
|
}
|
2019-11-07 16:25:49 +08:00
|
|
|
return fsMgr.GetStats()
|
2019-09-02 17:10:52 +08:00
|
|
|
}
|
|
|
|
|
2020-04-06 10:06:25 +08:00
|
|
|
func (m *unifiedManager) Set(container *configs.Config) error {
|
2019-11-07 16:25:49 +08:00
|
|
|
fsMgr, err := m.fsManager()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-09-02 17:10:52 +08:00
|
|
|
}
|
2019-11-07 16:25:49 +08:00
|
|
|
return fsMgr.Set(container)
|
2019-09-02 17:10:52 +08:00
|
|
|
}
|
2019-12-06 23:43:08 +08:00
|
|
|
|
2020-04-06 10:06:25 +08:00
|
|
|
func (m *unifiedManager) GetCgroups() (*configs.Cgroup, error) {
|
|
|
|
return m.cgroups, nil
|
2019-12-06 23:43:08 +08:00
|
|
|
}
|