volumes.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. package daemon
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "syscall"
  9. "github.com/dotcloud/docker/archive"
  10. "github.com/dotcloud/docker/daemon/execdriver"
  11. "github.com/dotcloud/docker/pkg/symlink"
  12. )
  13. type BindMap struct {
  14. SrcPath string
  15. DstPath string
  16. Mode string
  17. }
  18. func prepareVolumesForContainer(container *Container) error {
  19. if container.Volumes == nil || len(container.Volumes) == 0 {
  20. container.Volumes = make(map[string]string)
  21. container.VolumesRW = make(map[string]bool)
  22. if err := applyVolumesFrom(container); err != nil {
  23. return err
  24. }
  25. }
  26. if err := createVolumes(container); err != nil {
  27. return err
  28. }
  29. return nil
  30. }
  31. func setupMountsForContainer(container *Container) error {
  32. mounts := []execdriver.Mount{
  33. {container.daemon.sysInitPath, "/.dockerinit", false, true},
  34. {container.ResolvConfPath, "/etc/resolv.conf", false, true},
  35. }
  36. if container.HostnamePath != "" && container.HostsPath != "" {
  37. mounts = append(mounts, execdriver.Mount{container.HostnamePath, "/etc/hostname", false, true})
  38. mounts = append(mounts, execdriver.Mount{container.HostsPath, "/etc/hosts", false, true})
  39. }
  40. // Mount user specified volumes
  41. // Note, these are not private because you may want propagation of (un)mounts from host
  42. // volumes. For instance if you use -v /usr:/usr and the host later mounts /usr/share you
  43. // want this new mount in the container
  44. for r, v := range container.Volumes {
  45. mounts = append(mounts, execdriver.Mount{v, r, container.VolumesRW[r], false})
  46. }
  47. container.command.Mounts = mounts
  48. return nil
  49. }
  50. func applyVolumesFrom(container *Container) error {
  51. volumesFrom := container.hostConfig.VolumesFrom
  52. if len(volumesFrom) > 0 {
  53. for _, containerSpec := range volumesFrom {
  54. var (
  55. mountRW = true
  56. specParts = strings.SplitN(containerSpec, ":", 2)
  57. )
  58. switch len(specParts) {
  59. case 0:
  60. return fmt.Errorf("Malformed volumes-from specification: %s", containerSpec)
  61. case 2:
  62. switch specParts[1] {
  63. case "ro":
  64. mountRW = false
  65. case "rw": // mountRW is already true
  66. default:
  67. return fmt.Errorf("Malformed volumes-from specification: %s", containerSpec)
  68. }
  69. }
  70. c := container.daemon.Get(specParts[0])
  71. if c == nil {
  72. return fmt.Errorf("Container %s not found. Impossible to mount its volumes", specParts[0])
  73. }
  74. if err := c.Mount(); err != nil {
  75. return fmt.Errorf("Container %s failed to mount. Impossible to mount its volumes", specParts[0])
  76. }
  77. defer c.Unmount()
  78. for volPath, id := range c.Volumes {
  79. if _, exists := container.Volumes[volPath]; exists {
  80. continue
  81. }
  82. stat, err := os.Stat(c.getResourcePath(volPath))
  83. if err != nil {
  84. return err
  85. }
  86. if err := createIfNotExists(container.getResourcePath(volPath), stat.IsDir()); err != nil {
  87. return err
  88. }
  89. container.Volumes[volPath] = id
  90. if isRW, exists := c.VolumesRW[volPath]; exists {
  91. container.VolumesRW[volPath] = isRW && mountRW
  92. }
  93. }
  94. }
  95. }
  96. return nil
  97. }
  98. func getBindMap(container *Container) (map[string]BindMap, error) {
  99. var (
  100. // Create the requested bind mounts
  101. binds = make(map[string]BindMap)
  102. // Define illegal container destinations
  103. illegalDsts = []string{"/", "."}
  104. )
  105. for _, bind := range container.hostConfig.Binds {
  106. // FIXME: factorize bind parsing in parseBind
  107. var (
  108. src, dst, mode string
  109. arr = strings.Split(bind, ":")
  110. )
  111. if len(arr) == 2 {
  112. src = arr[0]
  113. dst = arr[1]
  114. mode = "rw"
  115. } else if len(arr) == 3 {
  116. src = arr[0]
  117. dst = arr[1]
  118. mode = arr[2]
  119. } else {
  120. return nil, fmt.Errorf("Invalid bind specification: %s", bind)
  121. }
  122. // Bail if trying to mount to an illegal destination
  123. for _, illegal := range illegalDsts {
  124. if dst == illegal {
  125. return nil, fmt.Errorf("Illegal bind destination: %s", dst)
  126. }
  127. }
  128. bindMap := BindMap{
  129. SrcPath: src,
  130. DstPath: dst,
  131. Mode: mode,
  132. }
  133. binds[filepath.Clean(dst)] = bindMap
  134. }
  135. return binds, nil
  136. }
  137. func createVolumes(container *Container) error {
  138. binds, err := getBindMap(container)
  139. if err != nil {
  140. return err
  141. }
  142. volumesDriver := container.daemon.volumes.Driver()
  143. // Create the requested volumes if they don't exist
  144. for volPath := range container.Config.Volumes {
  145. volPath = filepath.Clean(volPath)
  146. volIsDir := true
  147. // Skip existing volumes
  148. if _, exists := container.Volumes[volPath]; exists {
  149. continue
  150. }
  151. var srcPath string
  152. var isBindMount bool
  153. srcRW := false
  154. // If an external bind is defined for this volume, use that as a source
  155. if bindMap, exists := binds[volPath]; exists {
  156. isBindMount = true
  157. srcPath = bindMap.SrcPath
  158. if !filepath.IsAbs(srcPath) {
  159. return fmt.Errorf("%s must be an absolute path", srcPath)
  160. }
  161. if strings.ToLower(bindMap.Mode) == "rw" {
  162. srcRW = true
  163. }
  164. if stat, err := os.Stat(bindMap.SrcPath); err != nil {
  165. return err
  166. } else {
  167. volIsDir = stat.IsDir()
  168. }
  169. // Otherwise create an directory in $ROOT/volumes/ and use that
  170. } else {
  171. // Do not pass a container as the parameter for the volume creation.
  172. // The graph driver using the container's information ( Image ) to
  173. // create the parent.
  174. c, err := container.daemon.volumes.Create(nil, "", "", "", "", nil, nil)
  175. if err != nil {
  176. return err
  177. }
  178. srcPath, err = volumesDriver.Get(c.ID, "")
  179. if err != nil {
  180. return fmt.Errorf("Driver %s failed to get volume rootfs %s: %s", volumesDriver, c.ID, err)
  181. }
  182. srcRW = true // RW by default
  183. }
  184. if p, err := filepath.EvalSymlinks(srcPath); err != nil {
  185. return err
  186. } else {
  187. srcPath = p
  188. }
  189. // Create the mountpoint
  190. rootVolPath, err := symlink.FollowSymlinkInScope(filepath.Join(container.basefs, volPath), container.basefs)
  191. if err != nil {
  192. return err
  193. }
  194. newVolPath, err := filepath.Rel(container.basefs, rootVolPath)
  195. if err != nil {
  196. return err
  197. }
  198. newVolPath = "/" + newVolPath
  199. if volPath != newVolPath {
  200. delete(container.Volumes, volPath)
  201. delete(container.VolumesRW, volPath)
  202. }
  203. container.Volumes[newVolPath] = srcPath
  204. container.VolumesRW[newVolPath] = srcRW
  205. if err := createIfNotExists(rootVolPath, volIsDir); err != nil {
  206. return err
  207. }
  208. // Do not copy or change permissions if we are mounting from the host
  209. if srcRW && !isBindMount {
  210. volList, err := ioutil.ReadDir(rootVolPath)
  211. if err != nil {
  212. return err
  213. }
  214. if len(volList) > 0 {
  215. srcList, err := ioutil.ReadDir(srcPath)
  216. if err != nil {
  217. return err
  218. }
  219. if len(srcList) == 0 {
  220. // If the source volume is empty copy files from the root into the volume
  221. if err := archive.CopyWithTar(rootVolPath, srcPath); err != nil {
  222. return err
  223. }
  224. }
  225. }
  226. var stat syscall.Stat_t
  227. if err := syscall.Stat(rootVolPath, &stat); err != nil {
  228. return err
  229. }
  230. var srcStat syscall.Stat_t
  231. if err := syscall.Stat(srcPath, &srcStat); err != nil {
  232. return err
  233. }
  234. // Change the source volume's ownership if it differs from the root
  235. // files that were just copied
  236. if stat.Uid != srcStat.Uid || stat.Gid != srcStat.Gid {
  237. if err := os.Chown(srcPath, int(stat.Uid), int(stat.Gid)); err != nil {
  238. return err
  239. }
  240. }
  241. }
  242. }
  243. return nil
  244. }
  245. func createIfNotExists(path string, isDir bool) error {
  246. if _, err := os.Stat(path); err != nil {
  247. if os.IsNotExist(err) {
  248. if isDir {
  249. if err := os.MkdirAll(path, 0755); err != nil {
  250. return err
  251. }
  252. } else {
  253. if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
  254. return err
  255. }
  256. f, err := os.OpenFile(path, os.O_CREATE, 0755)
  257. if err != nil {
  258. return err
  259. }
  260. defer f.Close()
  261. }
  262. }
  263. }
  264. return nil
  265. }