2015-04-27 16:25:38 +00:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2015-05-19 20:05:25 +00:00
|
|
|
"sort"
|
2015-12-22 00:45:31 +00:00
|
|
|
"strconv"
|
2015-04-27 16:25:38 +00:00
|
|
|
|
2015-11-12 19:55:17 +00:00
|
|
|
"github.com/docker/docker/container"
|
2015-06-03 16:26:41 +00:00
|
|
|
"github.com/docker/docker/volume"
|
2015-04-27 16:25:38 +00:00
|
|
|
)
|
|
|
|
|
2015-07-30 23:10:56 +00:00
|
|
|
// setupMounts iterates through each of the mount points for a container and
|
|
|
|
// calls Setup() on each. It also looks to see if is a network mount such as
|
|
|
|
// /etc/resolv.conf, and if it is not, appends it to the array of mounts.
|
2016-03-18 18:50:19 +00:00
|
|
|
func (daemon *Daemon) setupMounts(c *container.Container) ([]container.Mount, error) {
|
|
|
|
var mounts []container.Mount
|
2016-06-06 09:57:11 +00:00
|
|
|
// TODO: tmpfs mounts should be part of Mountpoints
|
|
|
|
tmpfsMounts := make(map[string]bool)
|
|
|
|
for _, m := range c.TmpfsMounts() {
|
|
|
|
tmpfsMounts[m.Destination] = true
|
|
|
|
}
|
2016-03-18 18:50:19 +00:00
|
|
|
for _, m := range c.MountPoints {
|
2016-06-06 09:57:11 +00:00
|
|
|
if tmpfsMounts[m.Destination] {
|
|
|
|
continue
|
|
|
|
}
|
2016-03-18 18:50:19 +00:00
|
|
|
if err := daemon.lazyInitializeVolume(c.ID, m); err != nil {
|
2016-01-12 22:18:57 +00:00
|
|
|
return nil, err
|
2015-12-09 19:39:31 +00:00
|
|
|
}
|
2016-05-26 18:39:46 +00:00
|
|
|
path, err := m.Setup(c.MountLabel)
|
2015-05-19 20:05:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-18 18:50:19 +00:00
|
|
|
if !c.TrySetNetworkMount(m.Destination, path) {
|
|
|
|
mnt := container.Mount{
|
2015-07-14 00:54:00 +00:00
|
|
|
Source: path,
|
|
|
|
Destination: m.Destination,
|
|
|
|
Writable: m.RW,
|
2015-10-23 20:57:57 +00:00
|
|
|
Propagation: m.Propagation,
|
|
|
|
}
|
2015-12-22 00:45:31 +00:00
|
|
|
if m.Volume != nil {
|
|
|
|
attributes := map[string]string{
|
|
|
|
"driver": m.Volume.DriverName(),
|
2016-03-18 18:50:19 +00:00
|
|
|
"container": c.ID,
|
2015-12-22 00:45:31 +00:00
|
|
|
"destination": m.Destination,
|
|
|
|
"read/write": strconv.FormatBool(m.RW),
|
|
|
|
"propagation": m.Propagation,
|
|
|
|
}
|
|
|
|
daemon.LogVolumeEvent(m.Volume.Name(), "mount", attributes)
|
|
|
|
}
|
2015-10-23 20:57:57 +00:00
|
|
|
mounts = append(mounts, mnt)
|
2015-07-14 00:54:00 +00:00
|
|
|
}
|
2015-04-29 22:53:35 +00:00
|
|
|
}
|
2015-05-19 20:05:25 +00:00
|
|
|
|
|
|
|
mounts = sortMounts(mounts)
|
2016-03-18 18:50:19 +00:00
|
|
|
netMounts := c.NetworkMounts()
|
2015-10-08 15:51:41 +00:00
|
|
|
// if we are going to mount any of the network files from container
|
|
|
|
// metadata, the ownership must be set properly for potential container
|
|
|
|
// remapped root (user namespaces)
|
2015-11-03 19:25:22 +00:00
|
|
|
rootUID, rootGID := daemon.GetRemappedUIDGID()
|
2015-10-08 15:51:41 +00:00
|
|
|
for _, mount := range netMounts {
|
|
|
|
if err := os.Chown(mount.Source, rootUID, rootGID); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return append(mounts, netMounts...), nil
|
2015-04-29 22:53:35 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 23:10:56 +00:00
|
|
|
// sortMounts sorts an array of mounts in lexicographic order. This ensure that
|
|
|
|
// when mounting, the mounts don't shadow other mounts. For example, if mounting
|
|
|
|
// /etc and /etc/resolv.conf, /etc/resolv.conf must not be mounted first.
|
2016-03-18 18:50:19 +00:00
|
|
|
func sortMounts(m []container.Mount) []container.Mount {
|
2015-05-19 20:05:25 +00:00
|
|
|
sort.Sort(mounts(m))
|
|
|
|
return m
|
|
|
|
}
|
2015-04-29 22:53:35 +00:00
|
|
|
|
2015-09-10 02:23:06 +00:00
|
|
|
// setBindModeIfNull is platform specific processing to ensure the
|
|
|
|
// shared mode is set to 'z' if it is null. This is called in the case
|
|
|
|
// of processing a named volume and not a typical bind.
|
|
|
|
func setBindModeIfNull(bind *volume.MountPoint) *volume.MountPoint {
|
|
|
|
if bind.Mode == "" {
|
|
|
|
bind.Mode = "z"
|
2015-07-16 21:14:58 +00:00
|
|
|
}
|
2015-09-10 02:23:06 +00:00
|
|
|
return bind
|
2015-07-16 21:14:58 +00:00
|
|
|
}
|