graph_windows.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // +build windows
  2. package graph
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "os"
  7. "github.com/Sirupsen/logrus"
  8. "github.com/docker/docker/daemon/graphdriver/windows"
  9. "github.com/docker/docker/pkg/archive"
  10. )
  11. // setupInitLayer populates a directory with mountpoints suitable
  12. // for bind-mounting dockerinit into the container. T
  13. func SetupInitLayer(initLayer string) error {
  14. return nil
  15. }
  16. func createRootFilesystemInDriver(graph *Graph, img *Image, layerData archive.ArchiveReader) error {
  17. if wd, ok := graph.driver.(*windows.WindowsGraphDriver); ok {
  18. if img.Container != "" && layerData == nil {
  19. logrus.Debugf("Copying from container %s.", img.Container)
  20. var ids []string
  21. if img.Parent != "" {
  22. parentImg, err := graph.Get(img.Parent)
  23. if err != nil {
  24. return err
  25. }
  26. ids, err = graph.ParentLayerIds(parentImg)
  27. if err != nil {
  28. return err
  29. }
  30. }
  31. if err := wd.CopyDiff(img.Container, img.ID, wd.LayerIdsToPaths(ids)); err != nil {
  32. return fmt.Errorf("Driver %s failed to copy image rootfs %s: %s", graph.driver, img.Container, err)
  33. }
  34. } else if img.Parent == "" {
  35. if err := graph.driver.Create(img.ID, img.Parent); err != nil {
  36. return fmt.Errorf("Driver %s failed to create image rootfs %s: %s", graph.driver, img.ID, err)
  37. }
  38. }
  39. } else {
  40. // This fallback allows the use of VFS during daemon development.
  41. if err := graph.driver.Create(img.ID, img.Parent); err != nil {
  42. return fmt.Errorf("Driver %s failed to create image rootfs %s: %s", graph.driver, img.ID, err)
  43. }
  44. }
  45. return nil
  46. }
  47. func (graph *Graph) restoreBaseImages() ([]string, error) {
  48. // TODO Windows. This needs implementing (@swernli)
  49. return nil, nil
  50. }
  51. // ParentLayerIds returns a list of all parent image IDs for the given image.
  52. func (graph *Graph) ParentLayerIds(img *Image) (ids []string, err error) {
  53. for i := img; i != nil && err == nil; i, err = graph.GetParent(i) {
  54. ids = append(ids, i.ID)
  55. }
  56. return
  57. }
  58. // storeImage stores file system layer data for the given image to the
  59. // graph's storage driver. Image metadata is stored in a file
  60. // at the specified root directory.
  61. func (graph *Graph) storeImage(img *Image, layerData archive.ArchiveReader, root string) (err error) {
  62. if wd, ok := graph.driver.(*windows.WindowsGraphDriver); ok {
  63. // Store the layer. If layerData is not nil and this isn't a base image,
  64. // unpack it into the new layer
  65. if layerData != nil && img.Parent != "" {
  66. var ids []string
  67. if img.Parent != "" {
  68. parentImg, err := graph.Get(img.Parent)
  69. if err != nil {
  70. return err
  71. }
  72. ids, err = graph.ParentLayerIds(parentImg)
  73. if err != nil {
  74. return err
  75. }
  76. }
  77. if img.Size, err = wd.Import(img.ID, layerData, wd.LayerIdsToPaths(ids)); err != nil {
  78. return err
  79. }
  80. }
  81. if err := graph.saveSize(root, int(img.Size)); err != nil {
  82. return err
  83. }
  84. f, err := os.OpenFile(jsonPath(root), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.FileMode(0600))
  85. if err != nil {
  86. return err
  87. }
  88. defer f.Close()
  89. return json.NewEncoder(f).Encode(img)
  90. } else {
  91. // We keep this functionality here so that we can still work with the
  92. // VFS driver during development. This will not be used for actual running
  93. // of Windows containers. Without this code, it would not be possible to
  94. // docker pull using the VFS driver.
  95. // Store the layer. If layerData is not nil, unpack it into the new layer
  96. if layerData != nil {
  97. if img.Size, err = graph.driver.ApplyDiff(img.ID, img.Parent, layerData); err != nil {
  98. return err
  99. }
  100. }
  101. if err := graph.saveSize(root, int(img.Size)); err != nil {
  102. return err
  103. }
  104. f, err := os.OpenFile(jsonPath(root), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.FileMode(0600))
  105. if err != nil {
  106. return err
  107. }
  108. defer f.Close()
  109. return json.NewEncoder(f).Encode(img)
  110. }
  111. }
  112. // TarLayer returns a tar archive of the image's filesystem layer.
  113. func (graph *Graph) TarLayer(img *Image) (arch archive.Archive, err error) {
  114. if wd, ok := graph.driver.(*windows.WindowsGraphDriver); ok {
  115. var ids []string
  116. if img.Parent != "" {
  117. parentImg, err := graph.Get(img.Parent)
  118. if err != nil {
  119. return nil, err
  120. }
  121. ids, err = graph.ParentLayerIds(parentImg)
  122. if err != nil {
  123. return nil, err
  124. }
  125. }
  126. return wd.Export(img.ID, wd.LayerIdsToPaths(ids))
  127. } else {
  128. // We keep this functionality here so that we can still work with the VFS
  129. // driver during development. VFS is not supported (and just will not work)
  130. // for Windows containers.
  131. return graph.driver.Diff(img.ID, img.Parent)
  132. }
  133. }