func()

in validators/cgroup_validator_linux.go [50:85]


func (c *CgroupsValidator) Validate(spec SysSpec) (warns, errs []error) {
	// Get the subsystems from /sys/fs/cgroup/cgroup.controllers when cgroup v2 is used.
	// /proc/cgroups is meaningless for v2
	// https://github.com/torvalds/linux/blob/v5.3/Documentation/admin-guide/cgroup-v2.rst#deprecated-v1-core-features
	var st unix.Statfs_t
	var err error
	if err := unix.Statfs(unifiedMountpoint, &st); err != nil {
		return nil, []error{errors.Wrap(err, "cannot statfs the cgroupv2 root")}
	}
	var requiredCgroupSpec []string
	var optionalCgroupSpec []string
	var subsystems []string
	if st.Type == unix.CGROUP2_SUPER_MAGIC {
		subsystems, err = c.getCgroupV2Subsystems()
		if err != nil {
			return nil, []error{errors.Wrap(err, "failed to get cgroup v2 subsystems")}
		}
		requiredCgroupSpec = spec.CgroupsV2
		optionalCgroupSpec = spec.CgroupsV2Optional
	} else {
		subsystems, err = c.getCgroupV1Subsystems()
		if err != nil {
			return nil, []error{errors.Wrap(err, "failed to get cgroup v1 subsystems")}
		}
		requiredCgroupSpec = spec.Cgroups
		optionalCgroupSpec = spec.CgroupsOptional
	}

	if missingRequired := c.validateCgroupSubsystems(requiredCgroupSpec, subsystems, true); len(missingRequired) != 0 {
		errs = []error{errors.Errorf("missing required cgroups: %s", strings.Join(missingRequired, " "))}
	}
	if missingOptional := c.validateCgroupSubsystems(optionalCgroupSpec, subsystems, false); len(missingOptional) != 0 {
		warns = []error{errors.Errorf("missing optional cgroups: %s", strings.Join(missingOptional, " "))}
	}
	return
}