2f7dd1
From 5fb0f19ec8c52ed0c9bbb3551deb0016992ecc52 Mon Sep 17 00:00:00 2001
2f7dd1
From: Giuseppe Scrivano <gscrivan@redhat.com>
2f7dd1
Date: Thu, 3 Oct 2019 15:58:39 +0200
2f7dd1
Subject: [PATCH] cgroups: raise an error on cgroups v2
2f7dd1
2f7dd1
Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2f7dd1
---
2f7dd1
 create.go      |  8 ++++++++
2f7dd1
 run.go         |  8 ++++++++
2f7dd1
 utils_linux.go | 14 ++++++++++++++
2f7dd1
 3 files changed, 30 insertions(+)
2f7dd1
2f7dd1
diff --git a/create.go b/create.go
2f7dd1
index 5f3ac609..91d17d07 100644
2f7dd1
--- a/create.go
2f7dd1
+++ b/create.go
2f7dd1
@@ -1,6 +1,7 @@
2f7dd1
 package main
2f7dd1
 
2f7dd1
 import (
2f7dd1
+	"fmt"
2f7dd1
 	"os"
2f7dd1
 
2f7dd1
 	"github.com/urfave/cli"
2f7dd1
@@ -52,6 +53,13 @@ command(s) that get executed on start, edit the args parameter of the spec. See
2f7dd1
 		},
2f7dd1
 	},
2f7dd1
 	Action: func(context *cli.Context) error {
2f7dd1
+		unified, err := IsCgroup2UnifiedMode()
2f7dd1
+		if  err != nil {
2f7dd1
+			return err
2f7dd1
+		}
2f7dd1
+		if unified {
2f7dd1
+			return fmt.Errorf("this version of runc doesn't work on cgroups v2")
2f7dd1
+		}
2f7dd1
 		if err := checkArgs(context, 1, exactArgs); err != nil {
2f7dd1
 			return err
2f7dd1
 		}
2f7dd1
diff --git a/run.go b/run.go
2f7dd1
index f8d63178..3f29737b 100644
2f7dd1
--- a/run.go
2f7dd1
+++ b/run.go
2f7dd1
@@ -3,6 +3,7 @@
2f7dd1
 package main
2f7dd1
 
2f7dd1
 import (
2f7dd1
+	"fmt"
2f7dd1
 	"os"
2f7dd1
 
2f7dd1
 	"github.com/urfave/cli"
2f7dd1
@@ -63,6 +64,13 @@ command(s) that get executed on start, edit the args parameter of the spec. See
2f7dd1
 		},
2f7dd1
 	},
2f7dd1
 	Action: func(context *cli.Context) error {
2f7dd1
+		unified, err := IsCgroup2UnifiedMode()
2f7dd1
+		if  err != nil {
2f7dd1
+			return err
2f7dd1
+		}
2f7dd1
+		if unified {
2f7dd1
+			return fmt.Errorf("this version of runc doesn't work on cgroups v2")
2f7dd1
+		}
2f7dd1
 		if err := checkArgs(context, 1, exactArgs); err != nil {
2f7dd1
 			return err
2f7dd1
 		}
2f7dd1
diff --git a/utils_linux.go b/utils_linux.go
2f7dd1
index 984e6b0f..a5a03de9 100644
2f7dd1
--- a/utils_linux.go
2f7dd1
+++ b/utils_linux.go
2f7dd1
@@ -9,6 +9,7 @@ import (
2f7dd1
 	"os/exec"
2f7dd1
 	"path/filepath"
2f7dd1
 	"strconv"
2f7dd1
+	"syscall"
2f7dd1
 
2f7dd1
 	"github.com/opencontainers/runc/libcontainer"
2f7dd1
 	"github.com/opencontainers/runc/libcontainer/cgroups/systemd"
2f7dd1
@@ -26,6 +27,10 @@ import (
2f7dd1
 	"golang.org/x/sys/unix"
2f7dd1
 )
2f7dd1
 
2f7dd1
+const (
2f7dd1
+	_cgroup2SuperMagic = 0x63677270
2f7dd1
+)
2f7dd1
+
2f7dd1
 var errEmptyID = errors.New("container id cannot be empty")
2f7dd1
 
2f7dd1
 // loadFactory returns the configured factory instance for execing containers.
2f7dd1
@@ -451,3 +456,12 @@ func startContainer(context *cli.Context, spec *specs.Spec, action CtAct, criuOp
2f7dd1
 	}
2f7dd1
 	return r.run(spec.Process)
2f7dd1
 }
2f7dd1
+
2f7dd1
+// IsCgroup2UnifiedMode returns whether we are running in cgroup 2 cgroup2 mode.
2f7dd1
+func IsCgroup2UnifiedMode() (bool, error) {
2f7dd1
+	var st syscall.Statfs_t
2f7dd1
+	if err := syscall.Statfs("/sys/fs/cgroup", &st); err != nil {
2f7dd1
+		return false, err
2f7dd1
+	}
2f7dd1
+	return st.Type == _cgroup2SuperMagic, nil
2f7dd1
+}
2f7dd1
-- 
2f7dd1
2.21.0
2f7dd1