systemd.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. Copyright The containerd Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cgroups
  14. import (
  15. "fmt"
  16. "path/filepath"
  17. "strings"
  18. "sync"
  19. systemdDbus "github.com/coreos/go-systemd/dbus"
  20. "github.com/godbus/dbus"
  21. specs "github.com/opencontainers/runtime-spec/specs-go"
  22. )
  23. const (
  24. SystemdDbus Name = "systemd"
  25. defaultSlice = "system.slice"
  26. )
  27. func Systemd() ([]Subsystem, error) {
  28. root, err := v1MountPoint()
  29. if err != nil {
  30. return nil, err
  31. }
  32. defaultSubsystems, err := defaults(root)
  33. if err != nil {
  34. return nil, err
  35. }
  36. s, err := NewSystemd(root)
  37. if err != nil {
  38. return nil, err
  39. }
  40. // make sure the systemd controller is added first
  41. return append([]Subsystem{s}, defaultSubsystems...), nil
  42. }
  43. func Slice(slice, name string) Path {
  44. if slice == "" {
  45. slice = defaultSlice
  46. }
  47. return func(subsystem Name) (string, error) {
  48. return filepath.Join(slice, unitName(name)), nil
  49. }
  50. }
  51. func NewSystemd(root string) (*SystemdController, error) {
  52. return &SystemdController{
  53. root: root,
  54. }, nil
  55. }
  56. type SystemdController struct {
  57. mu sync.Mutex
  58. root string
  59. }
  60. func (s *SystemdController) Name() Name {
  61. return SystemdDbus
  62. }
  63. func (s *SystemdController) Create(path string, resources *specs.LinuxResources) error {
  64. conn, err := systemdDbus.New()
  65. if err != nil {
  66. return err
  67. }
  68. defer conn.Close()
  69. slice, name := splitName(path)
  70. properties := []systemdDbus.Property{
  71. systemdDbus.PropDescription(fmt.Sprintf("cgroup %s", name)),
  72. systemdDbus.PropWants(slice),
  73. newProperty("DefaultDependencies", false),
  74. newProperty("Delegate", true),
  75. newProperty("MemoryAccounting", true),
  76. newProperty("CPUAccounting", true),
  77. newProperty("BlockIOAccounting", true),
  78. }
  79. ch := make(chan string)
  80. _, err = conn.StartTransientUnit(name, "replace", properties, ch)
  81. if err != nil {
  82. return err
  83. }
  84. <-ch
  85. return nil
  86. }
  87. func (s *SystemdController) Delete(path string) error {
  88. conn, err := systemdDbus.New()
  89. if err != nil {
  90. return err
  91. }
  92. defer conn.Close()
  93. _, name := splitName(path)
  94. ch := make(chan string)
  95. _, err = conn.StopUnit(name, "replace", ch)
  96. if err != nil {
  97. return err
  98. }
  99. <-ch
  100. return nil
  101. }
  102. func newProperty(name string, units interface{}) systemdDbus.Property {
  103. return systemdDbus.Property{
  104. Name: name,
  105. Value: dbus.MakeVariant(units),
  106. }
  107. }
  108. func unitName(name string) string {
  109. return fmt.Sprintf("%s.slice", name)
  110. }
  111. func splitName(path string) (slice string, unit string) {
  112. slice, unit = filepath.Split(path)
  113. return strings.TrimSuffix(slice, "/"), unit
  114. }