)
// Return the current process's cgroup for the given subsystem.
+//
+// If the host has cgroups v2 and not v1 (i.e., unified mode), return
+// the current process's cgroup.
func findCgroup(subsystem string) (string, error) {
subsys := []byte(subsystem)
cgroups, err := ioutil.ReadFile("/proc/self/cgroup")
if len(toks) < 3 {
continue
}
+ if len(toks[1]) == 0 && string(toks[0]) == "0" {
+ // cgroups v2: "0::$PATH"
+ return string(toks[2]), nil
+ }
for _, s := range bytes.Split(toks[1], []byte(",")) {
if bytes.Compare(s, subsys) == 0 {
return string(toks[2]), nil
package crunchrun
import (
- "fmt"
- "os/exec"
-
. "gopkg.in/check.v1"
)
var _ = Suite(&CgroupSuite{})
func (s *CgroupSuite) TestFindCgroup(c *C) {
- if buf, err := exec.Command("stat", "-ftc", "%T", "/sys/fs/cgroup").CombinedOutput(); err != nil {
- c.Skip(fmt.Sprintf("cannot stat /sys/fs/cgroup: %s", err))
- } else if string(buf) == "cgroup2fs\n" {
- c.Skip("cannot test cgroups v1 feature because this system is using cgroups v2 unified mode")
- }
for _, s := range []string{"devices", "cpu", "cpuset"} {
g, err := findCgroup(s)
if c.Check(err, IsNil) {
statInterval := flags.Duration("crunchstat-interval", 10*time.Second, "sampling period for periodic resource usage reporting")
flags.String("cgroup-root", "/sys/fs/cgroup", "path to sysfs cgroup tree (obsolete, ignored)")
flags.String("cgroup-parent", "docker", "name of container's parent cgroup (obsolete, ignored)")
- cgroupParentSubsystem := flags.String("cgroup-parent-subsystem", "", "use current cgroup for given subsystem as parent cgroup for container (cgroups v1 only)")
+ cgroupParentSubsystem := flags.String("cgroup-parent-subsystem", "", "use current cgroup for given `subsystem` as parent cgroup for container (subsystem argument is only relevant for cgroups v1; in cgroups v2 / unified mode, any non-empty value means use current cgroup)")
caCertsPath := flags.String("ca-certs", "", "Path to TLS root certificates")
detach := flags.Bool("detach", false, "Detach from parent process and run in the background")
stdinConfig := flags.Bool("stdin-config", false, "Load config and environment variables from JSON message on stdin")