github.com/sijibomii/docker@v0.0.0-20231230191044-5cf6ca554647/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  	for _, m := range c.MountPoints {
    20  		if err := daemon.lazyInitializeVolume(c.ID, m); err != nil {
    21  			return nil, err
    22  		}
    23  		path, err := m.Setup()
    24  		if err != nil {
    25  			return nil, err
    26  		}
    27  		if !c.TrySetNetworkMount(m.Destination, path) {
    28  			mnt := container.Mount{
    29  				Source:      path,
    30  				Destination: m.Destination,
    31  				Writable:    m.RW,
    32  				Propagation: m.Propagation,
    33  			}
    34  			if m.Volume != nil {
    35  				attributes := map[string]string{
    36  					"driver":      m.Volume.DriverName(),
    37  					"container":   c.ID,
    38  					"destination": m.Destination,
    39  					"read/write":  strconv.FormatBool(m.RW),
    40  					"propagation": m.Propagation,
    41  				}
    42  				daemon.LogVolumeEvent(m.Volume.Name(), "mount", attributes)
    43  			}
    44  			mounts = append(mounts, mnt)
    45  		}
    46  	}
    47  
    48  	mounts = sortMounts(mounts)
    49  	netMounts := c.NetworkMounts()
    50  	// if we are going to mount any of the network files from container
    51  	// metadata, the ownership must be set properly for potential container
    52  	// remapped root (user namespaces)
    53  	rootUID, rootGID := daemon.GetRemappedUIDGID()
    54  	for _, mount := range netMounts {
    55  		if err := os.Chown(mount.Source, rootUID, rootGID); err != nil {
    56  			return nil, err
    57  		}
    58  	}
    59  	return append(mounts, netMounts...), nil
    60  }
    61  
    62  // sortMounts sorts an array of mounts in lexicographic order. This ensure that
    63  // when mounting, the mounts don't shadow other mounts. For example, if mounting
    64  // /etc and /etc/resolv.conf, /etc/resolv.conf must not be mounted first.
    65  func sortMounts(m []container.Mount) []container.Mount {
    66  	sort.Sort(mounts(m))
    67  	return m
    68  }
    69  
    70  // setBindModeIfNull is platform specific processing to ensure the
    71  // shared mode is set to 'z' if it is null. This is called in the case
    72  // of processing a named volume and not a typical bind.
    73  func setBindModeIfNull(bind *volume.MountPoint) *volume.MountPoint {
    74  	if bind.Mode == "" {
    75  		bind.Mode = "z"
    76  	}
    77  	return bind
    78  }