mirror of
https://github.com/containers/podman.git
synced 2025-05-21 17:16:22 +08:00
Merge pull request #14308 from n1hility/root-cgroup
Support running podman under a root v2 cgroup
This commit is contained in:
@ -3109,7 +3109,7 @@ func (c *Container) getOCICgroupPath() (string, error) {
|
|||||||
case c.config.NoCgroups:
|
case c.config.NoCgroups:
|
||||||
return "", nil
|
return "", nil
|
||||||
case c.config.CgroupsMode == cgroupSplit:
|
case c.config.CgroupsMode == cgroupSplit:
|
||||||
selfCgroup, err := utils.GetOwnCgroup()
|
selfCgroup, err := utils.GetOwnCgroupDisallowRoot()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package generate
|
package generate
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
@ -166,6 +167,14 @@ func verifyContainerResourcesCgroupV2(s *specgen.SpecGenerator) ([]string, error
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return warnings, err
|
return warnings, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if own == "/" {
|
||||||
|
// If running under the root cgroup try to create or reuse a "probe" cgroup to read memory values
|
||||||
|
own = "podman_probe"
|
||||||
|
_ = os.MkdirAll(filepath.Join("/sys/fs/cgroup", own), 0o755)
|
||||||
|
_ = ioutil.WriteFile("/sys/fs/cgroup/cgroup.subtree_control", []byte("+memory"), 0o644)
|
||||||
|
}
|
||||||
|
|
||||||
memoryMax := filepath.Join("/sys/fs/cgroup", own, "memory.max")
|
memoryMax := filepath.Join("/sys/fs/cgroup", own, "memory.max")
|
||||||
memorySwapMax := filepath.Join("/sys/fs/cgroup", own, "memory.swap.max")
|
memorySwapMax := filepath.Join("/sys/fs/cgroup", own, "memory.swap.max")
|
||||||
_, errMemoryMax := os.Stat(memoryMax)
|
_, errMemoryMax := os.Stat(memoryMax)
|
||||||
|
0
utils/testdata/cgroup.empty
vendored
Normal file
0
utils/testdata/cgroup.empty
vendored
Normal file
1
utils/testdata/cgroup.other
vendored
Normal file
1
utils/testdata/cgroup.other
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
0::/other
|
1
utils/testdata/cgroup.root
vendored
Normal file
1
utils/testdata/cgroup.root
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
0::/
|
@ -64,7 +64,7 @@ func RunUnderSystemdScope(pid int, slice string, unitName string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getCgroupProcess(procFile string) (string, error) {
|
func getCgroupProcess(procFile string, allowRoot bool) (string, error) {
|
||||||
f, err := os.Open(procFile)
|
f, err := os.Open(procFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
@ -72,7 +72,7 @@ func getCgroupProcess(procFile string) (string, error) {
|
|||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
scanner := bufio.NewScanner(f)
|
scanner := bufio.NewScanner(f)
|
||||||
cgroup := "/"
|
cgroup := ""
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := scanner.Text()
|
line := scanner.Text()
|
||||||
parts := strings.SplitN(line, ":", 3)
|
parts := strings.SplitN(line, ":", 3)
|
||||||
@ -87,7 +87,7 @@ func getCgroupProcess(procFile string) (string, error) {
|
|||||||
cgroup = parts[2]
|
cgroup = parts[2]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if cgroup == "/" {
|
if len(cgroup) == 0 || (!allowRoot && cgroup == "/") {
|
||||||
return "", errors.Errorf("could not find cgroup mount in %q", procFile)
|
return "", errors.Errorf("could not find cgroup mount in %q", procFile)
|
||||||
}
|
}
|
||||||
return cgroup, nil
|
return cgroup, nil
|
||||||
@ -95,12 +95,16 @@ func getCgroupProcess(procFile string) (string, error) {
|
|||||||
|
|
||||||
// GetOwnCgroup returns the cgroup for the current process.
|
// GetOwnCgroup returns the cgroup for the current process.
|
||||||
func GetOwnCgroup() (string, error) {
|
func GetOwnCgroup() (string, error) {
|
||||||
return getCgroupProcess("/proc/self/cgroup")
|
return getCgroupProcess("/proc/self/cgroup", true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetOwnCgroupDisallowRoot() (string, error) {
|
||||||
|
return getCgroupProcess("/proc/self/cgroup", false)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetCgroupProcess returns the cgroup for the specified process process.
|
// GetCgroupProcess returns the cgroup for the specified process process.
|
||||||
func GetCgroupProcess(pid int) (string, error) {
|
func GetCgroupProcess(pid int) (string, error) {
|
||||||
return getCgroupProcess(fmt.Sprintf("/proc/%d/cgroup", pid))
|
return getCgroupProcess(fmt.Sprintf("/proc/%d/cgroup", pid), true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MoveUnderCgroupSubtree moves the PID under a cgroup subtree.
|
// MoveUnderCgroupSubtree moves the PID under a cgroup subtree.
|
||||||
|
26
utils/utils_test.go
Normal file
26
utils/utils_test.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
//go:build linux || darwin
|
||||||
|
// +build linux darwin
|
||||||
|
|
||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCgroupProcess(t *testing.T) {
|
||||||
|
val, err := getCgroupProcess("testdata/cgroup.root", true)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, "/", val)
|
||||||
|
|
||||||
|
_, err = getCgroupProcess("testdata/cgroup.root", false)
|
||||||
|
assert.NotNil(t, err)
|
||||||
|
|
||||||
|
val, err = getCgroupProcess("testdata/cgroup.other", true)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, "/other", val)
|
||||||
|
|
||||||
|
_, err = getCgroupProcess("testdata/cgroup.empty", true)
|
||||||
|
assert.NotNil(t, err)
|
||||||
|
}
|
@ -17,6 +17,10 @@ func GetOwnCgroup() (string, error) {
|
|||||||
return "", errors.New("not implemented for windows")
|
return "", errors.New("not implemented for windows")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetOwnCgroupDisallowRoot() (string, error) {
|
||||||
|
return "", errors.New("not implemented for windows")
|
||||||
|
}
|
||||||
|
|
||||||
func GetCgroupProcess(pid int) (string, error) {
|
func GetCgroupProcess(pid int) (string, error) {
|
||||||
return "", errors.New("not implemented for windows")
|
return "", errors.New("not implemented for windows")
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user