From e032047c343138df9ce2cc06698977ad5b03c9ee Mon Sep 17 00:00:00 2001 From: Daniel J Walsh Date: Wed, 26 May 2021 15:07:25 -0400 Subject: [PATCH] Don't use systemd defaults if /proc/1/comm != systemd 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 Signed-off-by: Valentin Rothberg --- Makefile | 2 +- pkg/config/config_test.go | 12 ++++++++++-- pkg/config/default.go | 2 +- pkg/config/nosystemd.go | 10 +++++++++- pkg/config/systemd.go | 40 ++++++++++++++++++++++++++++++++++++++- 5 files changed, 60 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 92de0e5a5..10edc57a4 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index f6f6e7aba..e482b03e8 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -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) @@ -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() { diff --git a/pkg/config/default.go b/pkg/config/default.go index 133f9f073..794efc32e 100644 --- a/pkg/config/default.go +++ b/pkg/config/default.go @@ -191,7 +191,7 @@ func DefaultConfig() (*Config, error) { Init: false, InitPath: "", IPCNS: "private", - LogDriver: DefaultLogDriver, + LogDriver: defaultLogDriver(), LogSizeMax: DefaultLogSizeMax, NetNS: netns, NoHosts: false, diff --git a/pkg/config/nosystemd.go b/pkg/config/nosystemd.go index 5b82b1389..6e39a6ccd 100644 --- a/pkg/config/nosystemd.go +++ b/pkg/config/nosystemd.go @@ -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 +} diff --git a/pkg/config/systemd.go b/pkg/config/systemd.go index 02e5c4ac2..ed014126b 100644 --- a/pkg/config/systemd.go +++ b/pkg/config/systemd.go @@ -3,11 +3,23 @@ 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 @@ -15,6 +27,32 @@ func defaultCgroupManager() string { 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 }