Skip to content

Commit f5bf741

Browse files
committed
pkg/utils: Add function to get the cgroups version
Based on the IsCgroup2UnifiedMode function: https://github.com/containers/libpod/tree/master/pkg/cgroups As suggested by Matthew Heon. containers#318
1 parent aaf81a5 commit f5bf741

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ go 1.13
55
require (
66
github.com/sirupsen/logrus v1.5.0
77
github.com/spf13/cobra v0.0.6
8+
golang.org/x/sys v0.0.0-20190422165155-953cdadca894
89
)

src/pkg/utils/utils.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/containers/toolbox/pkg/shell"
2727
"github.com/sirupsen/logrus"
28+
"golang.org/x/sys/unix"
2829
)
2930

3031
var (
@@ -83,6 +84,25 @@ func ForwardToHost() (int, error) {
8384
return exitCode, nil
8485
}
8586

87+
// GetCgroupsVersion returns the cgroups version of the host
88+
//
89+
// Based on the IsCgroup2UnifiedMode function in:
90+
// https://github.com/containers/libpod/tree/master/pkg/cgroups
91+
func GetCgroupsVersion() (int, error) {
92+
var st syscall.Statfs_t
93+
94+
if err := syscall.Statfs("/sys/fs/cgroup", &st); err != nil {
95+
return -1, err
96+
}
97+
98+
version := 1
99+
if st.Type == unix.CGROUP2_SUPER_MAGIC {
100+
version = 2
101+
}
102+
103+
return version, nil
104+
}
105+
86106
func GetEnvOptionsForPreservedVariables() []string {
87107
logrus.Debug("Creating list of environment variables to forward")
88108

0 commit comments

Comments
 (0)