graph_unix.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // +build !windows
  2. package graph
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "syscall"
  10. "github.com/docker/docker/pkg/archive"
  11. "github.com/docker/docker/pkg/system"
  12. )
  13. // setupInitLayer populates a directory with mountpoints suitable
  14. // for bind-mounting dockerinit into the container. The mountpoint is simply an
  15. // empty file at /.dockerinit
  16. //
  17. // This extra layer is used by all containers as the top-most ro layer. It protects
  18. // the container from unwanted side-effects on the rw layer.
  19. func SetupInitLayer(initLayer string) error {
  20. for pth, typ := range map[string]string{
  21. "/dev/pts": "dir",
  22. "/dev/shm": "dir",
  23. "/proc": "dir",
  24. "/sys": "dir",
  25. "/.dockerinit": "file",
  26. "/.dockerenv": "file",
  27. "/etc/resolv.conf": "file",
  28. "/etc/hosts": "file",
  29. "/etc/hostname": "file",
  30. "/dev/console": "file",
  31. "/etc/mtab": "/proc/mounts",
  32. } {
  33. parts := strings.Split(pth, "/")
  34. prev := "/"
  35. for _, p := range parts[1:] {
  36. prev = filepath.Join(prev, p)
  37. syscall.Unlink(filepath.Join(initLayer, prev))
  38. }
  39. if _, err := os.Stat(filepath.Join(initLayer, pth)); err != nil {
  40. if os.IsNotExist(err) {
  41. if err := system.MkdirAll(filepath.Join(initLayer, filepath.Dir(pth)), 0755); err != nil {
  42. return err
  43. }
  44. switch typ {
  45. case "dir":
  46. if err := system.MkdirAll(filepath.Join(initLayer, pth), 0755); err != nil {
  47. return err
  48. }
  49. case "file":
  50. f, err := os.OpenFile(filepath.Join(initLayer, pth), os.O_CREATE, 0755)
  51. if err != nil {
  52. return err
  53. }
  54. f.Close()
  55. default:
  56. if err := os.Symlink(typ, filepath.Join(initLayer, pth)); err != nil {
  57. return err
  58. }
  59. }
  60. } else {
  61. return err
  62. }
  63. }
  64. }
  65. // Layer is ready to use, if it wasn't before.
  66. return nil
  67. }
  68. func createRootFilesystemInDriver(graph *Graph, img *Image, layerData archive.ArchiveReader) error {
  69. if err := graph.driver.Create(img.ID, img.Parent); err != nil {
  70. return fmt.Errorf("Driver %s failed to create image rootfs %s: %s", graph.driver, img.ID, err)
  71. }
  72. return nil
  73. }
  74. func (graph *Graph) restoreBaseImages() ([]string, error) {
  75. return nil, nil
  76. }
  77. // storeImage stores file system layer data for the given image to the
  78. // graph's storage driver. Image metadata is stored in a file
  79. // at the specified root directory.
  80. func (graph *Graph) storeImage(img *Image, layerData archive.ArchiveReader, root string) (err error) {
  81. // Store the layer. If layerData is not nil, unpack it into the new layer
  82. if layerData != nil {
  83. if img.Size, err = graph.driver.ApplyDiff(img.ID, img.Parent, layerData); err != nil {
  84. return err
  85. }
  86. }
  87. if err := graph.saveSize(root, int(img.Size)); err != nil {
  88. return err
  89. }
  90. f, err := os.OpenFile(jsonPath(root), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.FileMode(0600))
  91. if err != nil {
  92. return err
  93. }
  94. defer f.Close()
  95. return json.NewEncoder(f).Encode(img)
  96. }
  97. // TarLayer returns a tar archive of the image's filesystem layer.
  98. func (graph *Graph) TarLayer(img *Image) (arch archive.Archive, err error) {
  99. return graph.driver.Diff(img.ID, img.Parent)
  100. }