github.com/endophage/docker@v1.4.2-0.20161027011718-242853499895/pkg/chrootarchive/chroot_linux.go (about)

     1  package chrootarchive
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"syscall"
     9  
    10  	"github.com/docker/docker/pkg/mount"
    11  	rsystem "github.com/opencontainers/runc/libcontainer/system"
    12  )
    13  
    14  // chroot on linux uses pivot_root instead of chroot
    15  // pivot_root takes a new root and an old root.
    16  // Old root must be a sub-dir of new root, it is where the current rootfs will reside after the call to pivot_root.
    17  // New root is where the new rootfs is set to.
    18  // Old root is removed after the call to pivot_root so it is no longer available under the new root.
    19  // This is similar to how libcontainer sets up a container's rootfs
    20  func chroot(path string) (err error) {
    21  	// if the engine is running in a user namespace we need to use actual chroot
    22  	if rsystem.RunningInUserNS() {
    23  		return realChroot(path)
    24  	}
    25  	if err := syscall.Unshare(syscall.CLONE_NEWNS); err != nil {
    26  		return fmt.Errorf("Error creating mount namespace before pivot: %v", err)
    27  	}
    28  
    29  	// make everything in new ns private
    30  	if err := mount.MakeRPrivate("/"); err != nil {
    31  		return err
    32  	}
    33  	// ensure path is a mountpoint
    34  	if err := mount.MakePrivate(path); err != nil {
    35  		return err
    36  	}
    37  
    38  	// setup oldRoot for pivot_root
    39  	pivotDir, err := ioutil.TempDir(path, ".pivot_root")
    40  	if err != nil {
    41  		return fmt.Errorf("Error setting up pivot dir: %v", err)
    42  	}
    43  
    44  	var mounted bool
    45  	defer func() {
    46  		if mounted {
    47  			// make sure pivotDir is not mounted before we try to remove it
    48  			if errCleanup := syscall.Unmount(pivotDir, syscall.MNT_DETACH); errCleanup != nil {
    49  				if err == nil {
    50  					err = errCleanup
    51  				}
    52  				return
    53  			}
    54  		}
    55  
    56  		errCleanup := os.Remove(pivotDir)
    57  		// pivotDir doesn't exist if pivot_root failed and chroot+chdir was successful
    58  		// because we already cleaned it up on failed pivot_root
    59  		if errCleanup != nil && !os.IsNotExist(errCleanup) {
    60  			errCleanup = fmt.Errorf("Error cleaning up after pivot: %v", errCleanup)
    61  			if err == nil {
    62  				err = errCleanup
    63  			}
    64  		}
    65  	}()
    66  
    67  	if err := syscall.PivotRoot(path, pivotDir); err != nil {
    68  		// If pivot fails, fall back to the normal chroot after cleaning up temp dir
    69  		if err := os.Remove(pivotDir); err != nil {
    70  			return fmt.Errorf("Error cleaning up after failed pivot: %v", err)
    71  		}
    72  		return realChroot(path)
    73  	}
    74  	mounted = true
    75  
    76  	// This is the new path for where the old root (prior to the pivot) has been moved to
    77  	// This dir contains the rootfs of the caller, which we need to remove so it is not visible during extraction
    78  	pivotDir = filepath.Join("/", filepath.Base(pivotDir))
    79  
    80  	if err := syscall.Chdir("/"); err != nil {
    81  		return fmt.Errorf("Error changing to new root: %v", err)
    82  	}
    83  
    84  	// Make the pivotDir (where the old root lives) private so it can be unmounted without propagating to the host
    85  	if err := syscall.Mount("", pivotDir, "", syscall.MS_PRIVATE|syscall.MS_REC, ""); err != nil {
    86  		return fmt.Errorf("Error making old root private after pivot: %v", err)
    87  	}
    88  
    89  	// Now unmount the old root so it's no longer visible from the new root
    90  	if err := syscall.Unmount(pivotDir, syscall.MNT_DETACH); err != nil {
    91  		return fmt.Errorf("Error while unmounting old root after pivot: %v", err)
    92  	}
    93  	mounted = false
    94  
    95  	return nil
    96  }
    97  
    98  func realChroot(path string) error {
    99  	if err := syscall.Chroot(path); err != nil {
   100  		return fmt.Errorf("Error after fallback to chroot: %v", err)
   101  	}
   102  	if err := syscall.Chdir("/"); err != nil {
   103  		return fmt.Errorf("Error changing to new root after chroot: %v", err)
   104  	}
   105  	return nil
   106  }