17244: Make -cgroup-parent-subsystem=X work in cgroups v2.
authorTom Clegg <tom@curii.com>
Tue, 18 Jul 2023 20:28:30 +0000 (16:28 -0400)
committerTom Clegg <tom@curii.com>
Wed, 19 Jul 2023 04:57:06 +0000 (00:57 -0400)
Arvados-DCO-1.1-Signed-off-by: Tom Clegg <tom@curii.com>

lib/crunchrun/cgroup.go
lib/crunchrun/cgroup_test.go
lib/crunchrun/crunchrun.go

index 48ec93b8768c0a117f0af60d8433bb30d8f4467c..34dc8e40f092981157555dc2c525cdabc3265105 100644 (file)
@@ -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
index 313ed9a254f7bdb3e4ba20ef053ada51d4ec002e..eb87456d14b0d1e0e60245460009d75cfe4a01b2 100644 (file)
@@ -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) {
index 04fc6c0d0c88a10935c28f19d09cc7410432d9dd..de79a29efda027c329551987b85ce87b0dd4b095 100644 (file)
@@ -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")