volumes_unix.go 2.5 KB

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