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.

Fixes: #580

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
  • Loading branch information
rhatdan committed May 26, 2021
1 parent 386234b commit 7c0d472
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
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 @@ -195,7 +195,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
}
37 changes: 36 additions & 1 deletion pkg/config/systemd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@
package config

import (
"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
Expand All @@ -16,5 +26,30 @@ 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
}

0 comments on commit 7c0d472

Please sign in to comment.