-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
mount.go
66 lines (55 loc) · 1.21 KB
/
mount.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
//go:build linux
package cgroupslib
import (
"bufio"
"io"
"os"
"path/filepath"
"strings"
"github.com/hashicorp/go-set/v2"
)
func detect() Mode {
if os.Geteuid() > 0 {
return OFF
}
f, err := os.Open("/proc/self/mountinfo")
if err != nil {
return OFF
}
defer func() {
_ = f.Close()
}()
mode := scan(f)
if mode == CG2 && !functionalCgroups2() {
return OFF
}
return mode
}
func functionalCgroups2() bool {
const controllersFile = "cgroup.controllers"
requiredCgroup2Controllers := []string{"cpuset", "cpu", "io", "memory", "pids"}
controllersRootPath := filepath.Join(root, controllersFile)
content, err := os.ReadFile(controllersRootPath)
if err != nil {
return false
}
rootSubtreeControllers := set.From[string](strings.Fields(string(content)))
return rootSubtreeControllers.ContainsSlice(requiredCgroup2Controllers)
}
func scan(in io.Reader) Mode {
scanner := bufio.NewScanner(in)
for scanner.Scan() {
tokens := set.From(strings.Fields(scanner.Text()))
if tokens.Contains("/sys/fs/cgroup") {
if tokens.Contains("tmpfs") {
return CG1
}
if tokens.Contains("cgroup2") {
return CG2
}
}
}
return OFF
}