-
Notifications
You must be signed in to change notification settings - Fork 92
/
cgrouputil.go
114 lines (99 loc) · 3.34 KB
/
cgrouputil.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package cgrouputil
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/moby/sys/mountinfo"
"github.com/sirupsen/logrus"
)
// EvacuateCgroup2 evacuates cgroup2. Must be called in the parent PID namespace.
//
// When the current process belongs to "/foo" group (visible under "/sys/fs/cgroup/foo") and evac is like "bar",
// - All processes in the "/foo" group are moved to "/foo/bar" group, by writing PIDs into "/sys/fs/cgroup/foo/bar/cgroup.procs"
// - As many controllers as possible are enabled for "/foo/*" groups, by writing "/sys/fs/cgroup/foo/cgroup.subtree_control"
//
// Returns nil when cgroup2 is not enabled.
// Ported from https://github.com/rootless-containers/usernetes/commit/46ad812db7489914897ff8b1774f2fab0efda62b
func EvacuateCgroup2(evac string) error {
if evac == "" {
return errors.New("got empty evacuation group name")
}
if strings.Contains(evac, "/") {
return fmt.Errorf("unexpected evacuation group name %q: must not contain \"/\"", evac)
}
mountpoint := findCgroup2Mountpoint()
if mountpoint == "" {
logrus.Warn("cgroup2 is not mounted. cgroup2 evacuation is discarded.")
return nil
}
oldGroup := getCgroup2(os.Getpid())
if mountpoint == "" {
logrus.Warn("process is not running with cgroup2. cgroup2 evacuation is discarded.")
return nil
}
newGroup := filepath.Join(oldGroup, evac)
oldPath := filepath.Join(mountpoint, oldGroup)
newPath := filepath.Join(mountpoint, newGroup)
if err := os.MkdirAll(newPath, 0755); err != nil {
return err
}
// evacuate existing procs from oldGroup to newGroup, so that we can enable all controllers including threaded ones
cgroupProcsBytes, err := os.ReadFile(filepath.Join(oldPath, "cgroup.procs"))
if err != nil {
return err
}
for _, pidStr := range strings.Split(string(cgroupProcsBytes), "\n") {
if pidStr == "" || pidStr == "0" {
continue
}
if err := os.WriteFile(filepath.Join(newPath, "cgroup.procs"), []byte(pidStr), 0644); err != nil {
logrus.WithError(err).Warnf("failed to move process %s to cgroup %q", pidStr, newGroup)
}
}
// enable controllers for all subgroups under the oldGroup
controllerBytes, err := os.ReadFile(filepath.Join(oldPath, "cgroup.controllers"))
if err != nil {
return err
}
for _, controller := range strings.Fields(string(controllerBytes)) {
logrus.Debugf("enabling controller %q", controller)
if err := os.WriteFile(filepath.Join(oldPath, "cgroup.subtree_control"), []byte("+"+controller), 0644); err != nil {
logrus.WithError(err).Warnf("failed to enable controller %q", controller)
}
}
return nil
}
func findCgroup2Mountpoint() string {
f := mountinfoFSTypeFilter("cgroup2")
mounts, err := mountinfo.GetMounts(f)
if err != nil {
logrus.WithError(err).Warn("failed to find mountpoint for cgroup2")
return ""
}
if len(mounts) == 0 {
return ""
}
if len(mounts) != 1 {
logrus.Warnf("expected single mountpoint for cgroup2, got %d", len(mounts))
}
return mounts[0].Mountpoint
}
func getCgroup2(pid int) string {
p := fmt.Sprintf("/proc/%d/cgroup", pid)
b, err := os.ReadFile(p)
if err != nil {
logrus.WithError(err).Warnf("failed to read %q", p)
return ""
}
return getCgroup2FromProcPidCgroup(b)
}
func getCgroup2FromProcPidCgroup(b []byte) string {
for _, l := range strings.Split(string(b), "\n") {
if strings.HasPrefix(l, "0::") {
return strings.TrimPrefix(l, "0::")
}
}
return ""
}