From 28a733a8823fedadc34a560935abdd17039cb100 Mon Sep 17 00:00:00 2001 From: Tom Clegg Date: Tue, 18 Jul 2023 16:28:30 -0400 Subject: [PATCH] 17244: Make -cgroup-parent-subsystem=X work in cgroups v2. Arvados-DCO-1.1-Signed-off-by: Tom Clegg --- lib/crunchrun/cgroup.go | 7 +++++++ lib/crunchrun/cgroup_test.go | 8 -------- lib/crunchrun/crunchrun.go | 2 +- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/crunchrun/cgroup.go b/lib/crunchrun/cgroup.go index 48ec93b876..34dc8e40f0 100644 --- a/lib/crunchrun/cgroup.go +++ b/lib/crunchrun/cgroup.go @@ -11,6 +11,9 @@ import ( ) // 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") @@ -22,6 +25,10 @@ func findCgroup(subsystem string) (string, error) { 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 diff --git a/lib/crunchrun/cgroup_test.go b/lib/crunchrun/cgroup_test.go index 313ed9a254..eb87456d14 100644 --- a/lib/crunchrun/cgroup_test.go +++ b/lib/crunchrun/cgroup_test.go @@ -5,9 +5,6 @@ package crunchrun import ( - "fmt" - "os/exec" - . "gopkg.in/check.v1" ) @@ -16,11 +13,6 @@ type CgroupSuite struct{} 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) { diff --git a/lib/crunchrun/crunchrun.go b/lib/crunchrun/crunchrun.go index 04fc6c0d0c..de79a29efd 100644 --- a/lib/crunchrun/crunchrun.go +++ b/lib/crunchrun/crunchrun.go @@ -1903,7 +1903,7 @@ func (command) RunCommand(prog string, args []string, stdin io.Reader, stdout, s 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") -- 2.39.5