Skip to content

Commit

Permalink
service: do not run under the root cgroup
Browse files Browse the repository at this point in the history
at startup, when running on a cgroup v2 system, check if the current
process is running in the root cgroup and move it to a sub-cgroup,
otherwise Podman is not able to create cgroups and move processes
there.

Closes: #14573

[NO NEW TESTS NEEDED] it needs nested podman

Signed-off-by: Giuseppe Scrivano <[email protected]>
  • Loading branch information
giuseppe committed Jun 30, 2022
1 parent 5c39797 commit bd51410
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions cmd/podman/system/service_abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,40 @@ import (
"os"
"path/filepath"

"github.com/containers/common/pkg/cgroups"
"github.com/containers/podman/v4/cmd/podman/registry"
api "github.com/containers/podman/v4/pkg/api/server"
"github.com/containers/podman/v4/pkg/domain/entities"
"github.com/containers/podman/v4/pkg/domain/infra"
"github.com/containers/podman/v4/pkg/servicereaper"
"github.com/containers/podman/v4/utils"
"github.com/coreos/go-systemd/v22/activation"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"golang.org/x/sys/unix"
)

// maybeMoveToSubCgroup moves the current process in a sub cgroup when
// it is running in the root cgroup on a system that uses cgroupv2.
func maybeMoveToSubCgroup() error {
unifiedMode, err := cgroups.IsCgroup2UnifiedMode()
if err != nil {
return err
}
if !unifiedMode {
return nil
}
cgroup, err := utils.GetOwnCgroup()
if err != nil {
return err
}
if cgroup == "/" {
return utils.MoveUnderCgroupSubtree("init")
}
return nil
}

func restService(flags *pflag.FlagSet, cfg *entities.PodmanConfig, opts entities.ServiceOptions) error {
var (
listener net.Listener
Expand Down Expand Up @@ -103,6 +125,10 @@ func restService(flags *pflag.FlagSet, cfg *entities.PodmanConfig, opts entities
return err
}

if err := maybeMoveToSubCgroup(); err != nil {
return err
}

servicereaper.Start()
infra.StartWatcher(libpodRuntime)
server, err := api.NewServerWithSettings(libpodRuntime, listener, opts)
Expand Down

0 comments on commit bd51410

Please sign in to comment.