Lazily init DBus only when it is needed
OpenShift has some cAdvisor packages in its import chain. The init() here invokes Dbus start even when cAdvisor is not actively running or being used (in our packages). This change makes the systemd check and dbus initialization and connection lazy, occuring only the first time someone invokes UseSystemd().
This commit is contained in:
parent
19c6e473b2
commit
9ce178f707
@ -21,6 +21,7 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/docker/libcontainer/cgroups"
|
"github.com/docker/libcontainer/cgroups"
|
||||||
"github.com/docker/libcontainer/cgroups/systemd"
|
"github.com/docker/libcontainer/cgroups/systemd"
|
||||||
@ -43,23 +44,23 @@ var dockerRootDir = flag.String("docker_root", "/var/lib/docker", "Absolute path
|
|||||||
|
|
||||||
// Whether the system is using Systemd.
|
// Whether the system is using Systemd.
|
||||||
var useSystemd bool
|
var useSystemd bool
|
||||||
|
var check = sync.Once{}
|
||||||
func init() {
|
|
||||||
useSystemd = systemd.UseSystemd()
|
|
||||||
if !useSystemd {
|
|
||||||
// Second attempt at checking for systemd, check for a "name=systemd" cgroup.
|
|
||||||
mnt, err := cgroups.FindCgroupMountpoint("cpu")
|
|
||||||
if err == nil {
|
|
||||||
// systemd presence does not mean systemd controls cgroups.
|
|
||||||
// If system.slice cgroup exists, then systemd is taking control.
|
|
||||||
// This breaks if user creates system.slice manually :)
|
|
||||||
useSystemd = utils.FileExists(mnt + "/system.slice")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func UseSystemd() bool {
|
func UseSystemd() bool {
|
||||||
// init would run and initialize useSystemd before we can call this method.
|
check.Do(func() {
|
||||||
|
// run and initialize useSystemd
|
||||||
|
useSystemd = systemd.UseSystemd()
|
||||||
|
if !useSystemd {
|
||||||
|
// Second attempt at checking for systemd, check for a "name=systemd" cgroup.
|
||||||
|
mnt, err := cgroups.FindCgroupMountpoint("cpu")
|
||||||
|
if err == nil {
|
||||||
|
// systemd presence does not mean systemd controls cgroups.
|
||||||
|
// If system.slice cgroup exists, then systemd is taking control.
|
||||||
|
// This breaks if user creates system.slice manually :)
|
||||||
|
useSystemd = utils.FileExists(mnt + "/system.slice")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
return useSystemd
|
return useSystemd
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,7 +109,7 @@ func ContainerNameToDockerId(name string) string {
|
|||||||
id := path.Base(name)
|
id := path.Base(name)
|
||||||
|
|
||||||
// Turn systemd cgroup name into Docker ID.
|
// Turn systemd cgroup name into Docker ID.
|
||||||
if useSystemd {
|
if UseSystemd() {
|
||||||
id = strings.TrimPrefix(id, "docker-")
|
id = strings.TrimPrefix(id, "docker-")
|
||||||
id = strings.TrimSuffix(id, ".scope")
|
id = strings.TrimSuffix(id, ".scope")
|
||||||
}
|
}
|
||||||
@ -119,7 +120,7 @@ func ContainerNameToDockerId(name string) string {
|
|||||||
// Returns a full container name for the specified Docker ID.
|
// Returns a full container name for the specified Docker ID.
|
||||||
func FullContainerName(dockerId string) string {
|
func FullContainerName(dockerId string) string {
|
||||||
// Add the full container name.
|
// Add the full container name.
|
||||||
if useSystemd {
|
if UseSystemd() {
|
||||||
return path.Join("/system.slice", fmt.Sprintf("docker-%s.scope", dockerId))
|
return path.Join("/system.slice", fmt.Sprintf("docker-%s.scope", dockerId))
|
||||||
} else {
|
} else {
|
||||||
return path.Join("/docker", dockerId)
|
return path.Join("/docker", dockerId)
|
||||||
@ -207,7 +208,7 @@ func Register(factory info.MachineInfoFactory, fsInfo fs.FsInfo) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if useSystemd {
|
if UseSystemd() {
|
||||||
glog.Infof("System is using systemd")
|
glog.Infof("System is using systemd")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user