1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Get rid of panic in stats for lxc

Fix containers dir

Docker-DCO-1.1-Signed-off-by: Jessie Frazelle <jess@docker.com> (github: jfrazelle)
This commit is contained in:
Jessica Frazelle 2015-03-24 17:11:49 -07:00
parent 22dba32b4d
commit 7dce902494
4 changed files with 16 additions and 11 deletions

View file

@ -1015,7 +1015,7 @@ func NewDaemonFromDirectory(config *Config, eng *engine.Engine) (*Daemon, error)
sysInfo := sysinfo.New(false) sysInfo := sysinfo.New(false)
const runDir = "/var/run/docker" const runDir = "/var/run/docker"
ed, err := execdrivers.NewDriver(config.ExecDriver, runDir, sysInitPath, sysInfo) ed, err := execdrivers.NewDriver(config.ExecDriver, runDir, config.Root, sysInitPath, sysInfo)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -10,13 +10,13 @@ import (
"github.com/docker/docker/pkg/sysinfo" "github.com/docker/docker/pkg/sysinfo"
) )
func NewDriver(name, root, initPath string, sysInfo *sysinfo.SysInfo) (execdriver.Driver, error) { func NewDriver(name, root, libPath, initPath string, sysInfo *sysinfo.SysInfo) (execdriver.Driver, error) {
switch name { switch name {
case "lxc": case "lxc":
// we want to give the lxc driver the full docker root because it needs // we want to give the lxc driver the full docker root because it needs
// to access and write config and template files in /var/lib/docker/containers/* // to access and write config and template files in /var/lib/docker/containers/*
// to be backwards compatible // to be backwards compatible
return lxc.NewDriver(root, initPath, sysInfo.AppArmor) return lxc.NewDriver(root, libPath, initPath, sysInfo.AppArmor)
case "native": case "native":
return native.NewDriver(path.Join(root, "execdriver", "native"), initPath) return native.NewDriver(path.Join(root, "execdriver", "native"), initPath)
} }

View file

@ -36,6 +36,7 @@ var ErrExec = errors.New("Unsupported: Exec is not supported by the lxc driver")
type driver struct { type driver struct {
root string // root path for the driver to use root string // root path for the driver to use
libPath string
initPath string initPath string
apparmor bool apparmor bool
sharedRoot bool sharedRoot bool
@ -49,7 +50,7 @@ type activeContainer struct {
cmd *exec.Cmd cmd *exec.Cmd
} }
func NewDriver(root, initPath string, apparmor bool) (*driver, error) { func NewDriver(root, libPath, initPath string, apparmor bool) (*driver, error) {
if err := os.MkdirAll(root, 0700); err != nil { if err := os.MkdirAll(root, 0700); err != nil {
return nil, err return nil, err
} }
@ -64,6 +65,7 @@ func NewDriver(root, initPath string, apparmor bool) (*driver, error) {
return &driver{ return &driver{
apparmor: apparmor, apparmor: apparmor,
root: root, root: root,
libPath: libPath,
initPath: initPath, initPath: initPath,
sharedRoot: rootIsShared(), sharedRoot: rootIsShared(),
activeContainers: make(map[string]*activeContainer), activeContainers: make(map[string]*activeContainer),
@ -669,7 +671,7 @@ func rootIsShared() bool {
} }
func (d *driver) containerDir(containerId string) string { func (d *driver) containerDir(containerId string) string {
return path.Join(d.root, "containers", containerId) return path.Join(d.libPath, "containers", containerId)
} }
func (d *driver) generateLXCConfig(c *execdriver.Command) (string, error) { func (d *driver) generateLXCConfig(c *execdriver.Command) (string, error) {
@ -699,7 +701,7 @@ func (d *driver) generateEnvConfig(c *execdriver.Command) error {
if err != nil { if err != nil {
return err return err
} }
p := path.Join(d.root, "containers", c.ID, "config.env") p := path.Join(d.libPath, "containers", c.ID, "config.env")
c.Mounts = append(c.Mounts, execdriver.Mount{ c.Mounts = append(c.Mounts, execdriver.Mount{
Source: p, Source: p,
Destination: "/.dockerenv", Destination: "/.dockerenv",
@ -791,5 +793,8 @@ func (d *driver) Exec(c *execdriver.Command, processConfig *execdriver.ProcessCo
} }
func (d *driver) Stats(id string) (*execdriver.ResourceStats, error) { func (d *driver) Stats(id string) (*execdriver.ResourceStats, error) {
if _, ok := d.activeContainers[id]; !ok {
return nil, fmt.Errorf("%s is not a key in active containers", id)
}
return execdriver.Stats(d.containerDir(id), d.activeContainers[id].container.Cgroups.Memory, d.machineMemory) return execdriver.Stats(d.containerDir(id), d.activeContainers[id].container.Cgroups.Memory, d.machineMemory)
} }

View file

@ -39,7 +39,7 @@ func TestLXCConfig(t *testing.T) {
cpu = cpuMin + rand.Intn(cpuMax-cpuMin) cpu = cpuMin + rand.Intn(cpuMax-cpuMin)
) )
driver, err := NewDriver(root, "", false) driver, err := NewDriver(root, root, "", false)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -76,7 +76,7 @@ func TestCustomLxcConfig(t *testing.T) {
os.MkdirAll(path.Join(root, "containers", "1"), 0777) os.MkdirAll(path.Join(root, "containers", "1"), 0777)
driver, err := NewDriver(root, "", false) driver, err := NewDriver(root, root, "", false)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -194,7 +194,7 @@ func TestCustomLxcConfigMounts(t *testing.T) {
} }
os.MkdirAll(path.Join(root, "containers", "1"), 0777) os.MkdirAll(path.Join(root, "containers", "1"), 0777)
driver, err := NewDriver(root, "", false) driver, err := NewDriver(root, root, "", false)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -248,7 +248,7 @@ func TestCustomLxcConfigMisc(t *testing.T) {
} }
defer os.RemoveAll(root) defer os.RemoveAll(root)
os.MkdirAll(path.Join(root, "containers", "1"), 0777) os.MkdirAll(path.Join(root, "containers", "1"), 0777)
driver, err := NewDriver(root, "", true) driver, err := NewDriver(root, root, "", true)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -313,7 +313,7 @@ func TestCustomLxcConfigMiscOverride(t *testing.T) {
} }
defer os.RemoveAll(root) defer os.RemoveAll(root)
os.MkdirAll(path.Join(root, "containers", "1"), 0777) os.MkdirAll(path.Join(root, "containers", "1"), 0777)
driver, err := NewDriver(root, "", false) driver, err := NewDriver(root, root, "", false)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }