github.com/imran-kn/cilium-fork@v1.6.9/pkg/cgroups/cgroups.go (about) 1 // Copyright 2018 Authors of Cilium 2 // 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 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package cgroups 16 17 import ( 18 "fmt" 19 "os" 20 "sync" 21 "syscall" 22 23 "github.com/cilium/cilium/pkg/defaults" 24 "github.com/cilium/cilium/pkg/logging" 25 "github.com/cilium/cilium/pkg/logging/logfields" 26 "github.com/cilium/cilium/pkg/mountinfo" 27 ) 28 29 var ( 30 // Path to where cgroup is mounted 31 cgroupRoot = defaults.DefaultCgroupRoot 32 33 // Only mount a single instance 34 cgrpMountOnce sync.Once 35 ) 36 37 var log = logging.DefaultLogger.WithField(logfields.LogSubsys, "cgroups") 38 39 // setCgroupRoot will set the path to mount cgroupv2 40 func setCgroupRoot(path string) { 41 cgroupRoot = path 42 } 43 44 // GetCgroupRoot returns the path for the cgroupv2 mount 45 func GetCgroupRoot() string { 46 return cgroupRoot 47 } 48 49 // mountCgroup mounts the Cgroup v2 filesystem into the desired cgroupRoot directory. 50 func mountCgroup() error { 51 cgroupRootStat, err := os.Stat(cgroupRoot) 52 if err != nil { 53 if os.IsNotExist(err) { 54 if err := os.MkdirAll(cgroupRoot, 0755); err != nil { 55 return fmt.Errorf("Unable to create cgroup mount directory: %s", err) 56 } 57 } else { 58 return fmt.Errorf("Failed to stat the mount path %s: %s", cgroupRoot, err) 59 } 60 } else if !cgroupRootStat.IsDir() { 61 return fmt.Errorf("%s is a file which is not a directory", cgroupRoot) 62 } 63 64 if err := syscall.Mount("none", cgroupRoot, mountinfo.FilesystemTypeCgroup2, 0, ""); err != nil { 65 return fmt.Errorf("failed to mount %s: %s", cgroupRoot, err) 66 } 67 68 return nil 69 } 70 71 // checkOrMountCustomLocation tries to check or mount the BPF filesystem in the 72 // given path. 73 func cgrpCheckOrMountLocation(cgroupRoot string) error { 74 setCgroupRoot(cgroupRoot) 75 76 // Check whether the custom location has a mount. 77 mounted, cgroupInstance, err := mountinfo.IsMountFS(mountinfo.FilesystemTypeCgroup2, cgroupRoot) 78 if err != nil { 79 return err 80 } 81 82 // If the custom location has no mount, let's mount there. 83 if !mounted { 84 if err := mountCgroup(); err != nil { 85 return err 86 } 87 } 88 89 if !cgroupInstance { 90 return fmt.Errorf("Mount in the custom directory %s has a different filesystem than cgroup2", cgroupRoot) 91 } 92 return nil 93 } 94 95 // CheckOrMountCgrpFS this checks if the cilium cgroup2 root mount point is 96 // mounted and if not mounts it. If mapRoot is "" it will mount the default 97 // location. It is harmless to have multiple cgroupv2 root mounts so unlike 98 // BPFFS case we simply mount at the cilium default regardless if the system 99 // has another mount created by systemd or otherwise. 100 func CheckOrMountCgrpFS(mapRoot string) { 101 cgrpMountOnce.Do(func() { 102 if mapRoot == "" { 103 mapRoot = cgroupRoot 104 } 105 err := cgrpCheckOrMountLocation(mapRoot) 106 // Failed cgroup2 mount is not a fatal error, sockmap will be disabled however 107 if err == nil { 108 log.Infof("Mounted cgroupv2 filesystem at %s", mapRoot) 109 } 110 }) 111 }