Adding --docekr_root to customize where Docker stores state.

Fixes #199
This commit is contained in:
Victor Marmol 2014-08-29 14:54:40 -07:00
parent 4706361784
commit b75dea4021
2 changed files with 23 additions and 16 deletions

View File

@ -31,6 +31,9 @@ import (
var ArgDockerEndpoint = flag.String("docker", "unix:///var/run/docker.sock", "docker endpoint") var ArgDockerEndpoint = flag.String("docker", "unix:///var/run/docker.sock", "docker endpoint")
// Basepath to all container specific information that libcontainer stores.
var dockerRootDir = flag.String("docker_root", "/var/lib/docker", "Absolute path to the Docker state root directory (default: /var/lib/docker)")
type dockerFactory struct { type dockerFactory struct {
machineInfoFactory info.MachineInfoFactory machineInfoFactory info.MachineInfoFactory
@ -54,6 +57,7 @@ func (self *dockerFactory) NewContainerHandler(name string) (handler container.C
name, name,
self.machineInfoFactory, self.machineInfoFactory,
self.useSystemd, self.useSystemd,
*dockerRootDir,
) )
return return
} }

View File

@ -34,8 +34,8 @@ import (
"github.com/google/cadvisor/utils" "github.com/google/cadvisor/utils"
) )
// Basepath to all container specific information that libcontainer stores. // Relative path from Docker root to the libcontainer per-container state.
const dockerRootDir = "/var/lib/docker/execdriver/native" const pathToLibcontainerState = "execdriver/native"
var fileNotFound = errors.New("file not found") var fileNotFound = errors.New("file not found")
@ -47,6 +47,7 @@ type dockerContainerHandler struct {
aliases []string aliases []string
machineInfoFactory info.MachineInfoFactory machineInfoFactory info.MachineInfoFactory
useSystemd bool useSystemd bool
libcontainerStateDir string
} }
func newDockerContainerHandler( func newDockerContainerHandler(
@ -54,12 +55,14 @@ func newDockerContainerHandler(
name string, name string,
machineInfoFactory info.MachineInfoFactory, machineInfoFactory info.MachineInfoFactory,
useSystemd bool, useSystemd bool,
dockerRootDir string,
) (container.ContainerHandler, error) { ) (container.ContainerHandler, error) {
handler := &dockerContainerHandler{ handler := &dockerContainerHandler{
client: client, client: client,
name: name, name: name,
machineInfoFactory: machineInfoFactory, machineInfoFactory: machineInfoFactory,
useSystemd: useSystemd, useSystemd: useSystemd,
libcontainerStateDir: path.Join(dockerRootDir, pathToLibcontainerState),
} }
if handler.isDockerRoot() { if handler.isDockerRoot() {
return handler, nil return handler, nil
@ -89,7 +92,7 @@ func (self *dockerContainerHandler) isDockerRoot() bool {
// TODO(vmarmol): Switch to getting this from libcontainer once we have a solid API. // TODO(vmarmol): Switch to getting this from libcontainer once we have a solid API.
func (self *dockerContainerHandler) readLibcontainerConfig() (config *libcontainer.Config, err error) { func (self *dockerContainerHandler) readLibcontainerConfig() (config *libcontainer.Config, err error) {
configPath := path.Join(dockerRootDir, self.id, "container.json") configPath := path.Join(self.libcontainerStateDir, self.id, "container.json")
if !utils.FileExists(configPath) { if !utils.FileExists(configPath) {
// TODO(vishh): Return file name as well once we have a better error interface. // TODO(vishh): Return file name as well once we have a better error interface.
err = fileNotFound err = fileNotFound
@ -116,11 +119,11 @@ func (self *dockerContainerHandler) readLibcontainerConfig() (config *libcontain
} }
func (self *dockerContainerHandler) readLibcontainerState() (state *libcontainer.State, err error) { func (self *dockerContainerHandler) readLibcontainerState() (state *libcontainer.State, err error) {
statePath := path.Join(dockerRootDir, self.id, "state.json") statePath := path.Join(self.libcontainerStateDir, self.id, "state.json")
if !utils.FileExists(statePath) { if !utils.FileExists(statePath) {
// TODO(vmarmol): Remove this once we can depend on a newer Docker. // TODO(vmarmol): Remove this once we can depend on a newer Docker.
// Libcontainer changed how its state was stored, try the old way of a "pid" file // Libcontainer changed how its state was stored, try the old way of a "pid" file
if utils.FileExists(path.Join(dockerRootDir, self.id, "pid")) { if utils.FileExists(path.Join(self.libcontainerStateDir, self.id, "pid")) {
// We don't need the old state, return an empty state and we'll gracefully degrade. // We don't need the old state, return an empty state and we'll gracefully degrade.
state = new(libcontainer.State) state = new(libcontainer.State)
return return