in metric/system/cgroup/util.go [205:278]
func SubsystemMountpoints(rootfs resolve.Resolver, subsystems map[string]struct{}) (Mountpoints, error) {
// TODO: will we run into mount namespace issues if we use /proc/self/mountinfo?
mountinfo, err := os.Open(rootfs.ResolveHostFS("/proc/self/mountinfo"))
if err != nil {
return Mountpoints{}, err
}
defer mountinfo.Close()
mounts := map[string]string{}
mountInfo := Mountpoints{}
sc := bufio.NewScanner(mountinfo)
possibleV2Paths := []string{}
for sc.Scan() {
// https://www.kernel.org/doc/Documentation/filesystems/proc.txt
// Example:
// 25 21 0:20 / /cgroup/cpu rw,relatime - cgroup cgroup rw,cpu
line := strings.TrimSpace(sc.Text())
if line == "" {
continue
}
mount, err := parseMountinfoLine(line)
if err != nil {
return Mountpoints{}, err
}
// if the mountpoint from the subsystem has a different root than ours, it probably belongs to something else.
if !strings.HasPrefix(mount.mountpoint, rootfs.ResolveHostFS("")) {
continue
}
// cgroupv1 option
if mount.filesystemType == "cgroup" {
for _, opt := range mount.superOptions {
// Sometimes the subsystem name is written like "name=blkio".
fields := strings.SplitN(opt, "=", 2)
if len(fields) > 1 {
opt = fields[1]
}
// Test if option is a subsystem name.
if _, found := subsystems[opt]; found {
// Add the subsystem mount if it does not already exist.
if _, exists := mounts[opt]; !exists {
mounts[opt] = mount.mountpoint
}
}
}
}
// V2 option
if mount.filesystemType == "cgroup2" {
possibleV2Paths = append(possibleV2Paths, mount.mountpoint)
}
}
mountInfo.V2Loc = getProperV2Paths(rootfs, possibleV2Paths)
mountInfo.V1Mounts = mounts
// we only care about a contanerized root path if we're trying to monitor a host system
// from inside a container
// This logic helps us proper fetch the cgroup path when we're running inside a container
// with a private namespace
if mountInfo.V2Loc != "" && rootfs.IsSet() && cgroupNSStateFetch() {
mountInfo.ContainerizedRootMount, err = guessContainerCgroupPath(mountInfo.V2Loc, os.Getpid())
// treat this as a non-fatal error. If we end up needing this value, the lookups will fail down the line
if err != nil {
logp.L().Debugf("could not fetch cgroup path inside container: %w", err)
}
}
return mountInfo, sc.Err()
}