Blob Blame History Raw
From 5fb0f19ec8c52ed0c9bbb3551deb0016992ecc52 Mon Sep 17 00:00:00 2001
From: Giuseppe Scrivano <gscrivan@redhat.com>
Date: Thu, 3 Oct 2019 15:58:39 +0200
Subject: [PATCH] cgroups: raise an error on cgroups v2

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
---
 create.go      |  8 ++++++++
 run.go         |  8 ++++++++
 utils_linux.go | 14 ++++++++++++++
 3 files changed, 30 insertions(+)

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