graph_windows.go 4.5 KB

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