volumes_unix.go 2.3 KB

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