This repository has been archived by the owner on Mar 20, 2024. It is now read-only.
forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgroups.go
111 lines (95 loc) · 3.25 KB
/
cgroups.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
// Copyright 2018 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cgroups
import (
"fmt"
"os"
"sync"
"syscall"
"github.com/cilium/cilium/pkg/defaults"
"github.com/cilium/cilium/pkg/logging"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/mountinfo"
)
var (
// Path to where cgroup is mounted
cgroupRoot = defaults.DefaultCgroupRoot
// Only mount a single instance
cgrpMountOnce sync.Once
)
var log = logging.DefaultLogger.WithField(logfields.LogSubsys, "cgroups")
// setCgroupRoot will set the path to mount cgroupv2
func setCgroupRoot(path string) {
cgroupRoot = path
}
// GetCgroupRoot returns the path for the cgroupv2 mount
func GetCgroupRoot() string {
return cgroupRoot
}
// mountCgroup mounts the Cgroup v2 filesystem into the desired cgroupRoot directory.
func mountCgroup() error {
cgroupRootStat, err := os.Stat(cgroupRoot)
if err != nil {
if os.IsNotExist(err) {
if err := os.MkdirAll(cgroupRoot, 0755); err != nil {
return fmt.Errorf("Unable to create cgroup mount directory: %s", err)
}
} else {
return fmt.Errorf("Failed to stat the mount path %s: %s", cgroupRoot, err)
}
} else if !cgroupRootStat.IsDir() {
return fmt.Errorf("%s is a file which is not a directory", cgroupRoot)
}
if err := syscall.Mount("none", cgroupRoot, mountinfo.FilesystemTypeCgroup2, 0, ""); err != nil {
return fmt.Errorf("failed to mount %s: %s", cgroupRoot, err)
}
return nil
}
// checkOrMountCustomLocation tries to check or mount the BPF filesystem in the
// given path.
func cgrpCheckOrMountLocation(cgroupRoot string) error {
setCgroupRoot(cgroupRoot)
// Check whether the custom location has a mount.
mounted, cgroupInstance, err := mountinfo.IsMountFS(mountinfo.FilesystemTypeCgroup2, cgroupRoot)
if err != nil {
return err
}
// If the custom location has no mount, let's mount there.
if !mounted {
if err := mountCgroup(); err != nil {
return err
}
}
if !cgroupInstance {
return fmt.Errorf("Mount in the custom directory %s has a different filesystem than cgroup2", cgroupRoot)
}
return nil
}
// CheckOrMountCgrpFS this checks if the cilium cgroup2 root mount point is
// mounted and if not mounts it. If mapRoot is "" it will mount the default
// location. It is harmless to have multiple cgroupv2 root mounts so unlike
// BPFFS case we simply mount at the cilium default regardless if the system
// has another mount created by systemd or otherwise.
func CheckOrMountCgrpFS(mapRoot string) {
cgrpMountOnce.Do(func() {
if mapRoot == "" {
mapRoot = cgroupRoot
}
err := cgrpCheckOrMountLocation(mapRoot)
// Failed cgroup2 mount is not a fatal error, sockmap will be disabled however
if err == nil {
log.Infof("Mounted cgroupv2 filesystem at %s", mapRoot)
}
})
}