Skip to content

Commit

Permalink
fix setup workDir will change the dir ownership on host
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Wan <zirenwan@gmail.com>
  • Loading branch information
HusterWan authored and rudyfly committed May 10, 2019
1 parent 19851cc commit 84a1817
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
16 changes: 8 additions & 8 deletions daemon/mgr/container_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -720,16 +720,16 @@ func (mgr *ContainerManager) initContainerStorage(ctx context.Context, c *Contai
}
}()

// try to setup container working directory
if err := mgr.SetupWorkingDirectory(ctx, c); err != nil {
return errors.Wrapf(err, "failed to setup container %s working directory", c.ID)
}

// parse volume config
if err = mgr.generateMountPoints(ctx, c); err != nil {
return errors.Wrap(err, "failed to parse volume argument")
}

// try to setup container working directory
if err := mgr.SetupWorkingDirectory(ctx, c); err != nil {
return errors.Wrapf(err, "failed to setup container %s working directory", c.ID)
}

// set mount point disk quota
if err = mgr.setDiskQuota(ctx, c, true); err != nil {
// just ignore failed to set disk quota
Expand All @@ -754,11 +754,11 @@ func (mgr *ContainerManager) SetupWorkingDirectory(ctx context.Context, c *Conta
mgr.setMountFS(ctx, c)
}

c.Config.WorkingDir = filepath.Clean(c.Config.WorkingDir)
workingDir := filepath.Clean(c.Config.WorkingDir)
resourcePath := c.GetResourcePath(c.MountFS, workingDir)

path := filepath.Join(c.MountFS, c.Config.WorkingDir)
// TODO(ziren): not care about File mode
err := os.MkdirAll(path, 0755)
err := os.MkdirAll(resourcePath, 0755)
if err != nil && !os.IsExist(err) {
return err
}
Expand Down
27 changes: 27 additions & 0 deletions daemon/mgr/container_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,33 @@ func (c *Container) CleanRootfsSnapshotDirs() error {
return nil
}

// GetResourcePath is to determine the real host path of dir inside a container.
// If the dir has no volume covered, then just return BaseFS/dir,
// else we should return the real path inside volume.
func (c *Container) GetResourcePath(baseFS, path string) string {
var (
rootPath string
)

// first check if the dir in volume
for _, mp := range c.Mounts {
if !strings.HasPrefix(path, mp.Destination) {
continue
}

if mp.Source == mp.Destination {
rootPath = "/"
} else {
rootPath = strings.TrimSuffix(mp.Source, mp.Destination)
}
}

if rootPath != "" {
return filepath.Join(rootPath, path)
}
return filepath.Join(baseFS, path)
}

// ContainerRestartPolicy represents the policy is used to manage container.
type ContainerRestartPolicy types.RestartPolicy

Expand Down

0 comments on commit 84a1817

Please sign in to comment.