From 3c6c907299adba1582b6e9eb766eca34e1ba2a0a Mon Sep 17 00:00:00 2001 From: Antonio Murdaca Date: Mon, 9 May 2016 17:06:46 +0200 Subject: [PATCH] pkg: chrootarchive: chroot_linux: fix docker build The path we're trying to remove doesn't exist after a successful chroot+chdir because a / is only appended after pivot_root is successful and so we can't cleanup anymore with the old path. Also fix leaking .pivot_root dirs under /var/lib/docker/tmp/docker-builder* on error. Fix https://github.com/docker/docker/issues/22587 Introduced by https://github.com/docker/docker/pull/22506 Signed-off-by: Antonio Murdaca --- chrootarchive/chroot_linux.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/chrootarchive/chroot_linux.go b/chrootarchive/chroot_linux.go index a3af0f4..fb74ee7 100644 --- a/chrootarchive/chroot_linux.go +++ b/chrootarchive/chroot_linux.go @@ -43,7 +43,9 @@ func chroot(path string) (err error) { } errCleanup := os.Remove(pivotDir) - if errCleanup != nil { + // pivotDir doesn't exist if pivot_root failed and chroot+chdir was successful + // but we already cleaned it up on failed pivot_root + if errCleanup != nil && !os.IsNotExist(errCleanup) { errCleanup = fmt.Errorf("Error cleaning up after pivot: %v", errCleanup) if err == nil { err = errCleanup @@ -52,7 +54,10 @@ func chroot(path string) (err error) { }() if err := syscall.PivotRoot(path, pivotDir); err != nil { - // If pivot fails, fall back to the normal chroot + // If pivot fails, fall back to the normal chroot after cleaning up temp dir for pivot_root + if err := os.Remove(pivotDir); err != nil { + return fmt.Errorf("Error cleaning up after failed pivot: %v", err) + } return realChroot(path) } mounted = true @@ -84,7 +89,7 @@ func realChroot(path string) error { return fmt.Errorf("Error after fallback to chroot: %v", err) } if err := syscall.Chdir("/"); err != nil { - return fmt.Errorf("Error chaning to new root after chroot: %v", err) + return fmt.Errorf("Error changing to new root after chroot: %v", err) } return nil }