Skip to content

Commit

Permalink
Merge pull request moby#7973 from LK4D4/persist_execdriver_dir
Browse files Browse the repository at this point in the history
Persist execdriver dir
  • Loading branch information
vieux committed Sep 23, 2014
2 parents b12f3a6 + 652cd6a commit 827634d
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 7 deletions.
4 changes: 4 additions & 0 deletions daemon/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ func (daemon *Daemon) Destroy(container *Container) error {
return fmt.Errorf("Unable to remove filesystem for %v: %v", container.ID, err)
}

if err := daemon.execDriver.Clean(container.ID); err != nil {
return fmt.Errorf("Unable to remove execdriver data for %s: %s", container.ID, err)
}

selinuxFreeLxcContexts(container.ProcessLabel)

return nil
Expand Down
1 change: 1 addition & 0 deletions daemon/execdriver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type Driver interface {
Info(id string) Info // "temporary" hack (until we move state from core to plugins)
GetPidsForContainer(id string) ([]int, error) // Returns a list of pids for the given container.
Terminate(c *Command) error // kill it with fire
Clean(id string) error // clean all traces of container exec
}

// Network settings of the container
Expand Down
5 changes: 5 additions & 0 deletions daemon/execdriver/lxc/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,11 @@ func (d *driver) generateEnvConfig(c *execdriver.Command) error {
return ioutil.WriteFile(p, data, 0600)
}

// Clean not implemented for lxc
func (d *driver) Clean(id string) error {
return nil
}

type TtyConsole struct {
MasterPty *os.File
SlavePty *os.File
Expand Down
17 changes: 10 additions & 7 deletions daemon/execdriver/native/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (d *driver) Run(c *execdriver.Command, pipes *execdriver.Pipes, startCallba
if err := d.createContainerRoot(c.ID); err != nil {
return -1, err
}
defer d.removeContainerRoot(c.ID)
defer d.cleanContainer(c.ID)

if err := d.writeContainerFile(container, c.ID); err != nil {
return -1, err
Expand Down Expand Up @@ -186,7 +186,7 @@ func (d *driver) Terminate(p *execdriver.Command) error {
err = syscall.Kill(p.ProcessConfig.Process.Pid, 9)
syscall.Wait4(p.ProcessConfig.Process.Pid, nil, 0, nil)
}
d.removeContainerRoot(p.ID)
d.cleanContainer(p.ID)

return err

Expand Down Expand Up @@ -227,15 +227,18 @@ func (d *driver) writeContainerFile(container *libcontainer.Config, id string) e
return ioutil.WriteFile(filepath.Join(d.root, id, "container.json"), data, 0655)
}

func (d *driver) createContainerRoot(id string) error {
return os.MkdirAll(filepath.Join(d.root, id), 0655)
}

func (d *driver) removeContainerRoot(id string) error {
func (d *driver) cleanContainer(id string) error {
d.Lock()
delete(d.activeContainers, id)
d.Unlock()
return os.RemoveAll(filepath.Join(d.root, id, "container.json"))
}

func (d *driver) createContainerRoot(id string) error {
return os.MkdirAll(filepath.Join(d.root, id), 0655)
}

func (d *driver) Clean(id string) error {
return os.RemoveAll(filepath.Join(d.root, id))
}

Expand Down
100 changes: 100 additions & 0 deletions integration-cli/docker_cli_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2062,3 +2062,103 @@ func TestRunMountOrdering(t *testing.T) {
deleteAllContainers()
logDone("run - volumes are mounted in the correct order")
}

func TestRunExecDir(t *testing.T) {
cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
out, _, err := runCommandWithOutput(cmd)
if err != nil {
t.Fatal(err, out)
}
id := strings.TrimSpace(out)
execDir := filepath.Join(execDriverPath, id)
stateFile := filepath.Join(execDir, "state.json")
contFile := filepath.Join(execDir, "container.json")

{
fi, err := os.Stat(execDir)
if err != nil {
t.Fatal(err)
}
if !fi.IsDir() {
t.Fatalf("%q must be a directory", execDir)
}
fi, err = os.Stat(stateFile)
if err != nil {
t.Fatal(err)
}
fi, err = os.Stat(contFile)
if err != nil {
t.Fatal(err)
}
}

stopCmd := exec.Command(dockerBinary, "stop", id)
out, _, err = runCommandWithOutput(stopCmd)
if err != nil {
t.Fatal(err, out)
}
{
fi, err := os.Stat(execDir)
if err != nil {
t.Fatal(err)
}
if !fi.IsDir() {
t.Fatalf("%q must be a directory", execDir)
}
fi, err = os.Stat(stateFile)
if err == nil {
t.Fatalf("Statefile %q is exists for stopped container!", stateFile)
}
if !os.IsNotExist(err) {
t.Fatalf("Error should be about non-existing, got %s", err)
}
fi, err = os.Stat(contFile)
if err == nil {
t.Fatalf("Container file %q is exists for stopped container!", contFile)
}
if !os.IsNotExist(err) {
t.Fatalf("Error should be about non-existing, got %s", err)
}
}
startCmd := exec.Command(dockerBinary, "start", id)
out, _, err = runCommandWithOutput(startCmd)
if err != nil {
t.Fatal(err, out)
}
{
fi, err := os.Stat(execDir)
if err != nil {
t.Fatal(err)
}
if !fi.IsDir() {
t.Fatalf("%q must be a directory", execDir)
}
fi, err = os.Stat(stateFile)
if err != nil {
t.Fatal(err)
}
fi, err = os.Stat(contFile)
if err != nil {
t.Fatal(err)
}
}
rmCmd := exec.Command(dockerBinary, "rm", "-f", id)
out, _, err = runCommandWithOutput(rmCmd)
if err != nil {
t.Fatal(err, out)
}
{
_, err := os.Stat(execDir)
if err == nil {
t.Fatal(err)
}
if err == nil {
t.Fatalf("Exec directory %q is exists for removed container!", execDir)
}
if !os.IsNotExist(err) {
t.Fatalf("Error should be about non-existing, got %s", err)
}
}

logDone("run - check execdriver dir behavior")
}
2 changes: 2 additions & 0 deletions integration-cli/docker_test_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ var registryImageName = "registry"
// the private registry to use for tests
var privateRegistryURL = "127.0.0.1:5000"

var execDriverPath = "/var/lib/docker/execdriver/native"

var workingDirectory string

func init() {
Expand Down

0 comments on commit 827634d

Please sign in to comment.