Skip to content

Commit

Permalink
Don't use systemd defaults if /proc/1/comm != systemd
Browse files Browse the repository at this point in the history
Currently we have users failing to run containers within containers
or on systems without systemd support.  This change will give us
better defaults on these systems.

BZ: https://bugzilla.redhat.com/show_bug.cgi?id=1979497
Signed-off-by: Daniel J Walsh <[email protected]>
Signed-off-by: Valentin Rothberg <[email protected]>
  • Loading branch information
rhatdan authored and vrothberg committed Jul 6, 2021
1 parent 9904ee5 commit e032047
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ GO_BUILD=$(GO) build
ifeq ($(shell go help mod >/dev/null 2>&1 && echo true), true)
GO_BUILD=GO111MODULE=on $(GO) build -mod=vendor
endif
BUILDTAGS := containers_image_openpgp
BUILDTAGS := containers_image_openpgp,systemd
DESTDIR ?=
PREFIX := /usr/local
CONFIGDIR := ${PREFIX}/share/containers
Expand Down
12 changes: 10 additions & 2 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ var _ = Describe("Config", func() {
gomega.Expect(defaultConfig.GetDefaultEnvEx(false, true)).To(gomega.BeEquivalentTo(httpEnvs))
gomega.Expect(strings.Join(defaultConfig.GetDefaultEnvEx(true, true), ",")).To(gomega.ContainSubstring("HTTP_PROXY"))
gomega.Expect(strings.Join(defaultConfig.GetDefaultEnvEx(true, true), ",")).To(gomega.ContainSubstring("foo"))

// Undo that
if proxyEnvSet {
os.Setenv("HTTP_PROXY", oldProxy)
Expand Down Expand Up @@ -292,7 +291,16 @@ var _ = Describe("Config", func() {
gomega.Expect(config.Network.CNIPluginDirs).To(gomega.Equal(pluginDirs))
gomega.Expect(config.Engine.NumLocks).To(gomega.BeEquivalentTo(2048))
gomega.Expect(config.Engine.OCIRuntimes["runc"]).To(gomega.Equal(OCIRuntimeMap["runc"]))
gomega.Expect(config.LogDriver()).To(gomega.Equal("k8s-file"))
if useSystemd() {
gomega.Expect(config.Engine.CgroupManager).To(gomega.BeEquivalentTo("systemd"))
gomega.Expect(config.Engine.EventsLogger).To(gomega.BeEquivalentTo("journald"))
gomega.Expect(config.Containers.LogDriver).To(gomega.BeEquivalentTo("k8s-file"))
} else {
gomega.Expect(config.Engine.CgroupManager).To(gomega.BeEquivalentTo("cgroupfs"))
gomega.Expect(config.Engine.EventsLogger).To(gomega.BeEquivalentTo("file"))
gomega.Expect(config.Containers.LogDriver).To(gomega.BeEquivalentTo("k8s-file"))
}

})

It("should success with valid user file path", func() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func DefaultConfig() (*Config, error) {
Init: false,
InitPath: "",
IPCNS: "private",
LogDriver: DefaultLogDriver,
LogDriver: defaultLogDriver(),
LogSizeMax: DefaultLogSizeMax,
NetNS: netns,
NoHosts: false,
Expand Down
10 changes: 9 additions & 1 deletion pkg/config/nosystemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@
package config

func defaultCgroupManager() string {
return "cgroupfs"
return CgroupfsCgroupsManager
}

func defaultEventsLogger() string {
return "file"
}

func defaultLogDriver() string {
return DefaultLogDriver
}

func useSystemd() bool {
return false
}
40 changes: 39 additions & 1 deletion pkg/config/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,56 @@
package config

import (
"io/ioutil"
"strings"
"sync"

"github.com/containers/common/pkg/cgroupv2"
"github.com/containers/storage/pkg/unshare"
)

var (
systemdOnce sync.Once
usesSystemd bool
)

func defaultCgroupManager() string {
if !useSystemd() {
return CgroupfsCgroupsManager
}
enabled, err := cgroupv2.Enabled()
if err == nil && !enabled && unshare.IsRootless() {
return CgroupfsCgroupsManager
}

return SystemdCgroupsManager
}

func defaultEventsLogger() string {
return "journald"
if useSystemd() {
return "journald"
}
return "file"
}

func defaultLogDriver() string {
// If we decide to change the default for logdriver, it should be done here.
if useSystemd() {
return DefaultLogDriver
}

return DefaultLogDriver

}

func useSystemd() bool {
systemdOnce.Do(func() {
dat, err := ioutil.ReadFile("/proc/1/comm")
if err == nil {
val := strings.TrimSuffix(string(dat), "\n")
usesSystemd = (val == "systemd")
}
return
})
return usesSystemd
}

0 comments on commit e032047

Please sign in to comment.