Skip to content

Commit

Permalink
Cleanup docker tmp dir on start
Browse files Browse the repository at this point in the history
(backport from moby#31741)

Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com>
Signed-off-by: Wentao Zhang <zhangwentao234@huawei.com>
(cherry-pick from 9c451ad)

Conflicts:
	daemon/daemon.go
  • Loading branch information
mlaventure authored and moypray committed Jul 10, 2017
1 parent 58eb497 commit dfe2416
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ func NewDaemon(config *Config, registryService *registry.Service, containerdRemo
}

// set up the tmpDir to use a canonical path
tmp, err := tempDir(config.Root, rootUID, rootGID)
tmp, err := prepareTempDir(config.Root, rootUID, rootGID)
if err != nil {
return nil, fmt.Errorf("Unable to get the TempDir under %s: %s", config.Root, err)
}
Expand Down Expand Up @@ -1363,12 +1363,29 @@ func (daemon *Daemon) GetCachedImageOnBuild(imgID string, cfg *containertypes.Co
return cache.ID().String(), nil
}

// tempDir returns the default directory to use for temporary files.
func tempDir(rootDir string, rootUID, rootGID int) (string, error) {
// prepareTempDir prepares and returns the default directory to use
// for temporary files.
// If it doesn't exist, it is created. If it exists, its content is removed.
func prepareTempDir(rootDir string, rootUID, rootGID int) (string, error) {
var tmpDir string
if tmpDir = os.Getenv("DOCKER_TMPDIR"); tmpDir == "" {
tmpDir = filepath.Join(rootDir, "tmp")
newName := tmpDir + "-old"
if err := os.Rename(tmpDir, newName); err != nil {
go func() {
if err := os.RemoveAll(newName); err != nil {
logrus.Warnf("failed to delete old tmp directory: %s", newName)
}
}()
} else {
logrus.Warnf("failed to rename %s for background deletion: %s. Deleting synchronously", tmpDir, err)
if err := os.RemoveAll(tmpDir); err != nil {
logrus.Warnf("failed to delete old tmp directory: %s", tmpDir)
}
}
}
// We don't remove the content of tmpdir if it's not the default,
// it may hold things that do not belong to us.
return tmpDir, idtools.MkdirAllAs(tmpDir, 0700, rootUID, rootGID)
}

Expand Down

0 comments on commit dfe2416

Please sign in to comment.