Skip to content

Commit

Permalink
Merge pull request #3104 from giuseppe/initial-cgroup2
Browse files Browse the repository at this point in the history
rootless: allow resource isolation with cgroup v2
  • Loading branch information
openshift-merge-robot authored May 17, 2019
2 parents 41c4721 + 0e8f4dd commit 144244a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
6 changes: 5 additions & 1 deletion cmd/podman/libpodruntime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ func getRuntime(ctx context.Context, c *cliconfig.PodmanCommand, renumber bool,
if c.Flags().Changed("cgroup-manager") {
options = append(options, libpod.WithCgroupManager(c.GlobalFlags.CGroupManager))
} else {
if rootless.IsRootless() {
unified, err := util.IsCgroup2UnifiedMode()
if err != nil {
return nil, err
}
if rootless.IsRootless() && !unified {
options = append(options, libpod.WithCgroupManager("cgroupfs"))
}
}
Expand Down
7 changes: 7 additions & 0 deletions cmd/podman/shared/create_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/containers/libpod/cmd/podman/shared/parse"
cc "github.com/containers/libpod/pkg/spec"
"github.com/containers/libpod/pkg/sysinfo"
"github.com/containers/libpod/pkg/util"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -76,6 +77,12 @@ func addWarning(warnings []string, msg string) []string {

func verifyContainerResources(config *cc.CreateConfig, update bool) ([]string, error) {
warnings := []string{}

cgroup2, err := util.IsCgroup2UnifiedMode()
if err != nil || cgroup2 {
return warnings, err
}

sysInfo := sysinfo.New(true)

// memory subsystem checks and adjustments
Expand Down
10 changes: 7 additions & 3 deletions pkg/spec/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/containers/libpod/libpod"
"github.com/containers/libpod/pkg/rootless"
"github.com/containers/libpod/pkg/util"
pmount "github.com/containers/storage/pkg/mount"
"github.com/docker/docker/oci/caps"
"github.com/docker/go-units"
Expand Down Expand Up @@ -347,10 +348,13 @@ func (config *CreateConfig) createConfigToOCISpec(runtime *libpod.Runtime, userM
}

if rootless.IsRootless() {
if addedResources {
return nil, errors.New("invalid configuration, cannot set resources with rootless containers")
cgroup2, err := util.IsCgroup2UnifiedMode()
if err != nil {
return nil, err
}
if addedResources && !cgroup2 {
return nil, errors.New("invalid configuration, cannot set resources with rootless containers not using cgroups v2 unified mode")
}
configSpec.Linux.Resources = &spec.LinuxResources{}
}

// Make sure that the bind mounts keep options like nosuid, noexec, nodev.
Expand Down
24 changes: 24 additions & 0 deletions pkg/util/utils_supported.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,33 @@ import (
"github.com/pkg/errors"
"os"
"path/filepath"
"sync"
"syscall"
)

const (
_cgroup2SuperMagic = 0x63677270
)

var (
isUnifiedOnce sync.Once
isUnified bool
isUnifiedErr error
)

// IsCgroup2UnifiedMode returns whether we are running in cgroup 2 unified mode.
func IsCgroup2UnifiedMode() (bool, error) {
isUnifiedOnce.Do(func() {
var st syscall.Statfs_t
if err := syscall.Statfs("/sys/fs/cgroup", &st); err != nil {
isUnified, isUnifiedErr = false, err
} else {
isUnified, isUnifiedErr = st.Type == _cgroup2SuperMagic, nil
}
})
return isUnified, isUnifiedErr
}

// GetRootlessRuntimeDir returns the runtime directory when running as non root
func GetRootlessRuntimeDir() (string, error) {
var rootlessRuntimeDirError error
Expand Down
5 changes: 5 additions & 0 deletions pkg/util/utils_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ import (
func GetRootlessRuntimeDir() (string, error) {
return "", errors.New("this function is not implemented for windows")
}

// IsCgroup2UnifiedMode returns whether we are running in cgroup 2 unified mode.
func IsCgroup2UnifiedMode() (bool, error) {
return false, errors.New("this function is not implemented for windows")
}

0 comments on commit 144244a

Please sign in to comment.