system: always move pause process when running on systemd

when running on a systemd with systemd, always try to move the pause
process to its own scope.

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
This commit is contained in:
Giuseppe Scrivano 2021-09-16 12:14:02 +02:00
parent fa9728c550
commit 9c1e27fdd5
No known key found for this signature in database
GPG key ID: E4730F97F60286ED
2 changed files with 19 additions and 11 deletions

View file

@ -9,7 +9,6 @@ import (
"os/exec"
"path/filepath"
"strconv"
"strings"
"github.com/containers/common/pkg/config"
"github.com/containers/podman/v3/libpod/define"
@ -71,11 +70,7 @@ func (ic *ContainerEngine) SetupRootless(_ context.Context, noMoveProcess bool)
if err != nil {
return err
}
initCommand, err := ioutil.ReadFile("/proc/1/comm")
// On errors, default to systemd
runsUnderSystemd := err != nil || strings.TrimRight(string(initCommand), "\n") == "systemd"
runsUnderSystemd := utils.RunsOnSystemd()
unitName := fmt.Sprintf("podman-%d.scope", os.Getpid())
if runsUnderSystemd || conf.Engine.CgroupManager == config.SystemdCgroupsManager {
if err := utils.RunUnderSystemdScope(os.Getpid(), "user.slice", unitName); err != nil {
@ -121,11 +116,7 @@ func (ic *ContainerEngine) SetupRootless(_ context.Context, noMoveProcess bool)
became, ret, err = rootless.TryJoinFromFilePaths(pausePidPath, true, paths)
if err := movePauseProcessToScope(pausePidPath); err != nil {
conf, err2 := ic.Config(context.Background())
if err2 != nil {
return err
}
if conf.Engine.CgroupManager == config.SystemdCgroupsManager {
if utils.RunsOnSystemd() {
logrus.Warnf("Failed to add pause process to systemd sandbox cgroup: %v", err)
} else {
logrus.Debugf("Failed to add pause process to systemd sandbox cgroup: %v", err)

View file

@ -4,10 +4,12 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"strconv"
"strings"
"sync"
"github.com/containers/podman/v3/libpod/define"
"github.com/containers/storage/pkg/archive"
@ -155,3 +157,18 @@ func RemoveScientificNotationFromFloat(x float64) (float64, error) {
}
return result, nil
}
var (
runsOnSystemdOnce sync.Once
runsOnSystemd bool
)
// RunsOnSystemd returns whether the system is using systemd
func RunsOnSystemd() bool {
runsOnSystemdOnce.Do(func() {
initCommand, err := ioutil.ReadFile("/proc/1/comm")
// On errors, default to systemd
runsOnSystemd = err != nil || strings.TrimRight(string(initCommand), "\n") == "systemd"
})
return runsOnSystemd
}