github.com/noxiouz/docker@v0.7.3-0.20160629055221-3d231c78e8c5/daemon/volumes_unix.go (about)

     1  // +build !windows
     2  
     3  package daemon
     4  
     5  import (
     6  	"os"
     7  	"sort"
     8  	"strconv"
     9  
    10  	"github.com/docker/docker/container"
    11  	"github.com/docker/docker/volume"
    12  )
    13  
    14  // setupMounts iterates through each of the mount points for a container and
    15  // calls Setup() on each. It also looks to see if is a network mount such as
    16  // /etc/resolv.conf, and if it is not, appends it to the array of mounts.
    17  func (daemon *Daemon) setupMounts(c *container.Container) ([]container.Mount, error) {
    18  	var mounts []container.Mount
    19  	// TODO: tmpfs mounts should be part of Mountpoints
    20  	tmpfsMounts := make(map[string]bool)
    21  	for _, m := range c.TmpfsMounts() {
    22  		tmpfsMounts[m.Destination] = true
    23  	}
    24  	for _, m := range c.MountPoints {
    25  		if tmpfsMounts[m.Destination] {
    26  			continue
    27  		}
    28  		if err := daemon.lazyInitializeVolume(c.ID, m); err != nil {
    29  			return nil, err
    30  		}
    31  		path, err := m.Setup(c.MountLabel)
    32  		if err != nil {
    33  			return nil, err
    34  		}
    35  		if !c.TrySetNetworkMount(m.Destination, path) {
    36  			mnt := container.Mount{
    37  				Source:      path,
    38  				Destination: m.Destination,
    39  				Writable:    m.RW,
    40  				Propagation: m.Propagation,
    41  			}
    42  			if m.Volume != nil {
    43  				attributes := map[string]string{
    44  					"driver":      m.Volume.DriverName(),
    45  					"container":   c.ID,
    46  					"destination": m.Destination,
    47  					"read/write":  strconv.FormatBool(m.RW),
    48  					"propagation": m.Propagation,
    49  				}
    50  				daemon.LogVolumeEvent(m.Volume.Name(), "mount", attributes)
    51  			}
    52  			mounts = append(mounts, mnt)
    53  		}
    54  	}
    55  
    56  	mounts = sortMounts(mounts)
    57  	netMounts := c.NetworkMounts()
    58  	// if we are going to mount any of the network files from container
    59  	// metadata, the ownership must be set properly for potential container
    60  	// remapped root (user namespaces)
    61  	rootUID, rootGID := daemon.GetRemappedUIDGID()
    62  	for _, mount := range netMounts {
    63  		if err := os.Chown(mount.Source, rootUID, rootGID); err != nil {
    64  			return nil, err
    65  		}
    66  	}
    67  	return append(mounts, netMounts...), nil
    68  }
    69  
    70  // sortMounts sorts an array of mounts in lexicographic order. This ensure that
    71  // when mounting, the mounts don't shadow other mounts. For example, if mounting
    72  // /etc and /etc/resolv.conf, /etc/resolv.conf must not be mounted first.
    73  func sortMounts(m []container.Mount) []container.Mount {
    74  	sort.Sort(mounts(m))
    75  	return m
    76  }
    77  
    78  // setBindModeIfNull is platform specific processing to ensure the
    79  // shared mode is set to 'z' if it is null. This is called in the case
    80  // of processing a named volume and not a typical bind.
    81  func setBindModeIfNull(bind *volume.MountPoint) *volume.MountPoint {
    82  	if bind.Mode == "" {
    83  		bind.Mode = "z"
    84  	}
    85  	return bind
    86  }