graph.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. package graph
  2. import (
  3. "compress/gzip"
  4. "crypto/sha256"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "io"
  9. "io/ioutil"
  10. "os"
  11. "path/filepath"
  12. "runtime"
  13. "strconv"
  14. "strings"
  15. "sync"
  16. "time"
  17. "github.com/Sirupsen/logrus"
  18. "github.com/docker/distribution/digest"
  19. "github.com/docker/docker/autogen/dockerversion"
  20. "github.com/docker/docker/daemon/graphdriver"
  21. "github.com/docker/docker/image"
  22. "github.com/docker/docker/pkg/archive"
  23. "github.com/docker/docker/pkg/progressreader"
  24. "github.com/docker/docker/pkg/streamformatter"
  25. "github.com/docker/docker/pkg/stringid"
  26. "github.com/docker/docker/pkg/system"
  27. "github.com/docker/docker/pkg/truncindex"
  28. "github.com/docker/docker/runconfig"
  29. "github.com/vbatts/tar-split/tar/asm"
  30. "github.com/vbatts/tar-split/tar/storage"
  31. )
  32. // The type is used to protect pulling or building related image
  33. // layers from deleteing when filtered by dangling=true
  34. // The key of layers is the images ID which is pulling or building
  35. // The value of layers is a slice which hold layer IDs referenced to
  36. // pulling or building images
  37. type retainedLayers struct {
  38. layerHolders map[string]map[string]struct{} // map[layerID]map[sessionID]
  39. sync.Mutex
  40. }
  41. func (r *retainedLayers) Add(sessionID string, layerIDs []string) {
  42. r.Lock()
  43. defer r.Unlock()
  44. for _, layerID := range layerIDs {
  45. if r.layerHolders[layerID] == nil {
  46. r.layerHolders[layerID] = map[string]struct{}{}
  47. }
  48. r.layerHolders[layerID][sessionID] = struct{}{}
  49. }
  50. }
  51. func (r *retainedLayers) Delete(sessionID string, layerIDs []string) {
  52. r.Lock()
  53. defer r.Unlock()
  54. for _, layerID := range layerIDs {
  55. holders, ok := r.layerHolders[layerID]
  56. if !ok {
  57. continue
  58. }
  59. delete(holders, sessionID)
  60. if len(holders) == 0 {
  61. delete(r.layerHolders, layerID) // Delete any empty reference set.
  62. }
  63. }
  64. }
  65. func (r *retainedLayers) Exists(layerID string) bool {
  66. r.Lock()
  67. _, exists := r.layerHolders[layerID]
  68. r.Unlock()
  69. return exists
  70. }
  71. // A Graph is a store for versioned filesystem images and the relationship between them.
  72. type Graph struct {
  73. root string
  74. idIndex *truncindex.TruncIndex
  75. driver graphdriver.Driver
  76. imageMutex imageMutex // protect images in driver.
  77. retained *retainedLayers
  78. }
  79. // file names for ./graph/<ID>/
  80. const (
  81. jsonFileName = "json"
  82. layersizeFileName = "layersize"
  83. digestFileName = "checksum"
  84. tarDataFileName = "tar-data.json.gz"
  85. )
  86. var (
  87. // ErrDigestNotSet is used when request the digest for a layer
  88. // but the layer has no digest value or content to compute the
  89. // the digest.
  90. ErrDigestNotSet = errors.New("digest is not set for layer")
  91. )
  92. // NewGraph instantiates a new graph at the given root path in the filesystem.
  93. // `root` will be created if it doesn't exist.
  94. func NewGraph(root string, driver graphdriver.Driver) (*Graph, error) {
  95. abspath, err := filepath.Abs(root)
  96. if err != nil {
  97. return nil, err
  98. }
  99. // Create the root directory if it doesn't exists
  100. if err := system.MkdirAll(root, 0700); err != nil && !os.IsExist(err) {
  101. return nil, err
  102. }
  103. graph := &Graph{
  104. root: abspath,
  105. idIndex: truncindex.NewTruncIndex([]string{}),
  106. driver: driver,
  107. retained: &retainedLayers{layerHolders: make(map[string]map[string]struct{})},
  108. }
  109. if err := graph.restore(); err != nil {
  110. return nil, err
  111. }
  112. return graph, nil
  113. }
  114. // IsHeld returns whether the given layerID is being used by an ongoing pull or build.
  115. func (graph *Graph) IsHeld(layerID string) bool {
  116. return graph.retained.Exists(layerID)
  117. }
  118. func (graph *Graph) restore() error {
  119. dir, err := ioutil.ReadDir(graph.root)
  120. if err != nil {
  121. return err
  122. }
  123. var ids = []string{}
  124. for _, v := range dir {
  125. id := v.Name()
  126. if graph.driver.Exists(id) {
  127. ids = append(ids, id)
  128. }
  129. }
  130. baseIds, err := graph.restoreBaseImages()
  131. if err != nil {
  132. return err
  133. }
  134. ids = append(ids, baseIds...)
  135. graph.idIndex = truncindex.NewTruncIndex(ids)
  136. logrus.Debugf("Restored %d elements", len(ids))
  137. return nil
  138. }
  139. // IsNotExist detects whether an image exists by parsing the incoming error message.
  140. // FIXME: Implement error subclass instead of looking at the error text
  141. // Note: This is the way golang implements os.IsNotExists on Plan9
  142. func (graph *Graph) IsNotExist(err error, id string) bool {
  143. return err != nil && (strings.Contains(strings.ToLower(err.Error()), "does not exist") || strings.Contains(strings.ToLower(err.Error()), "no such")) && strings.Contains(err.Error(), id)
  144. }
  145. // Exists returns true if an image is registered at the given id.
  146. // If the image doesn't exist or if an error is encountered, false is returned.
  147. func (graph *Graph) Exists(id string) bool {
  148. if _, err := graph.Get(id); err != nil {
  149. return false
  150. }
  151. return true
  152. }
  153. // Get returns the image with the given id, or an error if the image doesn't exist.
  154. func (graph *Graph) Get(name string) (*image.Image, error) {
  155. id, err := graph.idIndex.Get(name)
  156. if err != nil {
  157. return nil, fmt.Errorf("could not find image: %v", err)
  158. }
  159. img, err := graph.loadImage(id)
  160. if err != nil {
  161. return nil, err
  162. }
  163. if img.ID != id {
  164. return nil, fmt.Errorf("Image stored at '%s' has wrong id '%s'", id, img.ID)
  165. }
  166. if img.Size < 0 {
  167. size, err := graph.driver.DiffSize(img.ID, img.Parent)
  168. if err != nil {
  169. return nil, fmt.Errorf("unable to calculate size of image id %q: %s", img.ID, err)
  170. }
  171. img.Size = size
  172. if err := graph.saveSize(graph.imageRoot(id), int(img.Size)); err != nil {
  173. return nil, err
  174. }
  175. }
  176. return img, nil
  177. }
  178. // Create creates a new image and registers it in the graph.
  179. func (graph *Graph) Create(layerData archive.ArchiveReader, containerID, containerImage, comment, author string, containerConfig, config *runconfig.Config) (*image.Image, error) {
  180. img := &image.Image{
  181. ID: stringid.GenerateRandomID(),
  182. Comment: comment,
  183. Created: time.Now().UTC(),
  184. DockerVersion: dockerversion.VERSION,
  185. Author: author,
  186. Config: config,
  187. Architecture: runtime.GOARCH,
  188. OS: runtime.GOOS,
  189. }
  190. if containerID != "" {
  191. img.Parent = containerImage
  192. img.Container = containerID
  193. img.ContainerConfig = *containerConfig
  194. }
  195. if err := graph.Register(img, layerData); err != nil {
  196. return nil, err
  197. }
  198. return img, nil
  199. }
  200. // Register imports a pre-existing image into the graph.
  201. func (graph *Graph) Register(img *image.Image, layerData archive.ArchiveReader) (err error) {
  202. if err := image.ValidateID(img.ID); err != nil {
  203. return err
  204. }
  205. // We need this entire operation to be atomic within the engine. Note that
  206. // this doesn't mean Register is fully safe yet.
  207. graph.imageMutex.Lock(img.ID)
  208. defer graph.imageMutex.Unlock(img.ID)
  209. // The returned `error` must be named in this function's signature so that
  210. // `err` is not shadowed in this deferred cleanup.
  211. defer func() {
  212. // If any error occurs, remove the new dir from the driver.
  213. // Don't check for errors since the dir might not have been created.
  214. if err != nil {
  215. graph.driver.Remove(img.ID)
  216. }
  217. }()
  218. // (This is a convenience to save time. Race conditions are taken care of by os.Rename)
  219. if graph.Exists(img.ID) {
  220. return fmt.Errorf("Image %s already exists", img.ID)
  221. }
  222. // Ensure that the image root does not exist on the filesystem
  223. // when it is not registered in the graph.
  224. // This is common when you switch from one graph driver to another
  225. if err := os.RemoveAll(graph.imageRoot(img.ID)); err != nil && !os.IsNotExist(err) {
  226. return err
  227. }
  228. // If the driver has this ID but the graph doesn't, remove it from the driver to start fresh.
  229. // (the graph is the source of truth).
  230. // Ignore errors, since we don't know if the driver correctly returns ErrNotExist.
  231. // (FIXME: make that mandatory for drivers).
  232. graph.driver.Remove(img.ID)
  233. tmp, err := graph.mktemp("")
  234. defer os.RemoveAll(tmp)
  235. if err != nil {
  236. return fmt.Errorf("mktemp failed: %s", err)
  237. }
  238. // Create root filesystem in the driver
  239. if err := createRootFilesystemInDriver(graph, img, layerData); err != nil {
  240. return err
  241. }
  242. // Apply the diff/layer
  243. if err := graph.storeImage(img, layerData, tmp); err != nil {
  244. return err
  245. }
  246. // Commit
  247. if err := os.Rename(tmp, graph.imageRoot(img.ID)); err != nil {
  248. return err
  249. }
  250. graph.idIndex.Add(img.ID)
  251. return nil
  252. }
  253. // TempLayerArchive creates a temporary archive of the given image's filesystem layer.
  254. // The archive is stored on disk and will be automatically deleted as soon as has been read.
  255. // If output is not nil, a human-readable progress bar will be written to it.
  256. func (graph *Graph) TempLayerArchive(id string, sf *streamformatter.StreamFormatter, output io.Writer) (*archive.TempArchive, error) {
  257. image, err := graph.Get(id)
  258. if err != nil {
  259. return nil, err
  260. }
  261. tmp, err := graph.mktemp("")
  262. if err != nil {
  263. return nil, err
  264. }
  265. a, err := graph.TarLayer(image)
  266. if err != nil {
  267. return nil, err
  268. }
  269. progressReader := progressreader.New(progressreader.Config{
  270. In: a,
  271. Out: output,
  272. Formatter: sf,
  273. Size: 0,
  274. NewLines: false,
  275. ID: stringid.TruncateID(id),
  276. Action: "Buffering to disk",
  277. })
  278. defer progressReader.Close()
  279. return archive.NewTempArchive(progressReader, tmp)
  280. }
  281. // mktemp creates a temporary sub-directory inside the graph's filesystem.
  282. func (graph *Graph) mktemp(id string) (string, error) {
  283. dir := filepath.Join(graph.root, "_tmp", stringid.GenerateNonCryptoID())
  284. if err := system.MkdirAll(dir, 0700); err != nil {
  285. return "", err
  286. }
  287. return dir, nil
  288. }
  289. func (graph *Graph) newTempFile() (*os.File, error) {
  290. tmp, err := graph.mktemp("")
  291. if err != nil {
  292. return nil, err
  293. }
  294. return ioutil.TempFile(tmp, "")
  295. }
  296. func bufferToFile(f *os.File, src io.Reader) (int64, digest.Digest, error) {
  297. var (
  298. h = sha256.New()
  299. w = gzip.NewWriter(io.MultiWriter(f, h))
  300. )
  301. _, err := io.Copy(w, src)
  302. w.Close()
  303. if err != nil {
  304. return 0, "", err
  305. }
  306. n, err := f.Seek(0, os.SEEK_CUR)
  307. if err != nil {
  308. return 0, "", err
  309. }
  310. if _, err := f.Seek(0, 0); err != nil {
  311. return 0, "", err
  312. }
  313. return n, digest.NewDigest("sha256", h), nil
  314. }
  315. // Delete atomically removes an image from the graph.
  316. func (graph *Graph) Delete(name string) error {
  317. id, err := graph.idIndex.Get(name)
  318. if err != nil {
  319. return err
  320. }
  321. tmp, err := graph.mktemp("")
  322. graph.idIndex.Delete(id)
  323. if err == nil {
  324. if err := os.Rename(graph.imageRoot(id), tmp); err != nil {
  325. // On err make tmp point to old dir and cleanup unused tmp dir
  326. os.RemoveAll(tmp)
  327. tmp = graph.imageRoot(id)
  328. }
  329. } else {
  330. // On err make tmp point to old dir for cleanup
  331. tmp = graph.imageRoot(id)
  332. }
  333. // Remove rootfs data from the driver
  334. graph.driver.Remove(id)
  335. // Remove the trashed image directory
  336. return os.RemoveAll(tmp)
  337. }
  338. // Map returns a list of all images in the graph, addressable by ID.
  339. func (graph *Graph) Map() map[string]*image.Image {
  340. images := make(map[string]*image.Image)
  341. graph.walkAll(func(image *image.Image) {
  342. images[image.ID] = image
  343. })
  344. return images
  345. }
  346. // walkAll iterates over each image in the graph, and passes it to a handler.
  347. // The walking order is undetermined.
  348. func (graph *Graph) walkAll(handler func(*image.Image)) {
  349. graph.idIndex.Iterate(func(id string) {
  350. if img, err := graph.Get(id); err != nil {
  351. return
  352. } else if handler != nil {
  353. handler(img)
  354. }
  355. })
  356. }
  357. // ByParent returns a lookup table of images by their parent.
  358. // If an image of id ID has 3 children images, then the value for key ID
  359. // will be a list of 3 images.
  360. // If an image has no children, it will not have an entry in the table.
  361. func (graph *Graph) ByParent() map[string][]*image.Image {
  362. byParent := make(map[string][]*image.Image)
  363. graph.walkAll(func(img *image.Image) {
  364. parent, err := graph.Get(img.Parent)
  365. if err != nil {
  366. return
  367. }
  368. if children, exists := byParent[parent.ID]; exists {
  369. byParent[parent.ID] = append(children, img)
  370. } else {
  371. byParent[parent.ID] = []*image.Image{img}
  372. }
  373. })
  374. return byParent
  375. }
  376. // Retain keeps the images and layers that are in pulling chain so that they are not deleted.
  377. // If not, they may be deleted by rmi with dangling condition.
  378. func (graph *Graph) Retain(sessionID string, layerIDs ...string) {
  379. graph.retained.Add(sessionID, layerIDs)
  380. }
  381. // Release removes the referenced image id from the provided set of layers.
  382. func (graph *Graph) Release(sessionID string, layerIDs ...string) {
  383. graph.retained.Delete(sessionID, layerIDs)
  384. }
  385. // Heads returns all heads in the graph, keyed by id.
  386. // A head is an image which is not the parent of another image in the graph.
  387. func (graph *Graph) Heads() map[string]*image.Image {
  388. heads := make(map[string]*image.Image)
  389. byParent := graph.ByParent()
  390. graph.walkAll(func(image *image.Image) {
  391. // If it's not in the byParent lookup table, then
  392. // it's not a parent -> so it's a head!
  393. if _, exists := byParent[image.ID]; !exists {
  394. heads[image.ID] = image
  395. }
  396. })
  397. return heads
  398. }
  399. func (graph *Graph) imageRoot(id string) string {
  400. return filepath.Join(graph.root, id)
  401. }
  402. // loadImage fetches the image with the given id from the graph.
  403. func (graph *Graph) loadImage(id string) (*image.Image, error) {
  404. root := graph.imageRoot(id)
  405. // Open the JSON file to decode by streaming
  406. jsonSource, err := os.Open(jsonPath(root))
  407. if err != nil {
  408. return nil, err
  409. }
  410. defer jsonSource.Close()
  411. img := &image.Image{}
  412. dec := json.NewDecoder(jsonSource)
  413. // Decode the JSON data
  414. if err := dec.Decode(img); err != nil {
  415. return nil, err
  416. }
  417. if err := image.ValidateID(img.ID); err != nil {
  418. return nil, err
  419. }
  420. if buf, err := ioutil.ReadFile(filepath.Join(root, layersizeFileName)); err != nil {
  421. if !os.IsNotExist(err) {
  422. return nil, err
  423. }
  424. // If the layersize file does not exist then set the size to a negative number
  425. // because a layer size of 0 (zero) is valid
  426. img.Size = -1
  427. } else {
  428. // Using Atoi here instead would temporarily convert the size to a machine
  429. // dependent integer type, which causes images larger than 2^31 bytes to
  430. // display negative sizes on 32-bit machines:
  431. size, err := strconv.ParseInt(string(buf), 10, 64)
  432. if err != nil {
  433. return nil, err
  434. }
  435. img.Size = int64(size)
  436. }
  437. return img, nil
  438. }
  439. // saveSize stores the `size` in the provided graph `img` directory `root`.
  440. func (graph *Graph) saveSize(root string, size int) error {
  441. if err := ioutil.WriteFile(filepath.Join(root, layersizeFileName), []byte(strconv.Itoa(size)), 0600); err != nil {
  442. return fmt.Errorf("Error storing image size in %s/%s: %s", root, layersizeFileName, err)
  443. }
  444. return nil
  445. }
  446. // SetDigest sets the digest for the image layer to the provided value.
  447. func (graph *Graph) SetDigest(id string, dgst digest.Digest) error {
  448. root := graph.imageRoot(id)
  449. if err := ioutil.WriteFile(filepath.Join(root, digestFileName), []byte(dgst.String()), 0600); err != nil {
  450. return fmt.Errorf("Error storing digest in %s/%s: %s", root, digestFileName, err)
  451. }
  452. return nil
  453. }
  454. // GetDigest gets the digest for the provide image layer id.
  455. func (graph *Graph) GetDigest(id string) (digest.Digest, error) {
  456. root := graph.imageRoot(id)
  457. cs, err := ioutil.ReadFile(filepath.Join(root, digestFileName))
  458. if err != nil {
  459. if os.IsNotExist(err) {
  460. return "", ErrDigestNotSet
  461. }
  462. return "", err
  463. }
  464. return digest.ParseDigest(string(cs))
  465. }
  466. // RawJSON returns the JSON representation for an image as a byte array.
  467. func (graph *Graph) RawJSON(id string) ([]byte, error) {
  468. root := graph.imageRoot(id)
  469. buf, err := ioutil.ReadFile(jsonPath(root))
  470. if err != nil {
  471. return nil, fmt.Errorf("Failed to read json for image %s: %s", id, err)
  472. }
  473. return buf, nil
  474. }
  475. func jsonPath(root string) string {
  476. return filepath.Join(root, jsonFileName)
  477. }
  478. func (graph *Graph) disassembleAndApplyTarLayer(img *image.Image, layerData archive.ArchiveReader, root string) error {
  479. // this is saving the tar-split metadata
  480. mf, err := os.OpenFile(filepath.Join(root, tarDataFileName), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.FileMode(0600))
  481. if err != nil {
  482. return err
  483. }
  484. mfz := gzip.NewWriter(mf)
  485. metaPacker := storage.NewJSONPacker(mfz)
  486. defer mf.Close()
  487. defer mfz.Close()
  488. inflatedLayerData, err := archive.DecompressStream(layerData)
  489. if err != nil {
  490. return err
  491. }
  492. // we're passing nil here for the file putter, because the ApplyDiff will
  493. // handle the extraction of the archive
  494. rdr, err := asm.NewInputTarStream(inflatedLayerData, metaPacker, nil)
  495. if err != nil {
  496. return err
  497. }
  498. if img.Size, err = graph.driver.ApplyDiff(img.ID, img.Parent, archive.ArchiveReader(rdr)); err != nil {
  499. return err
  500. }
  501. return nil
  502. }
  503. func (graph *Graph) assembleTarLayer(img *image.Image) (archive.Archive, error) {
  504. root := graph.imageRoot(img.ID)
  505. mFileName := filepath.Join(root, tarDataFileName)
  506. mf, err := os.Open(mFileName)
  507. if err != nil {
  508. if !os.IsNotExist(err) {
  509. logrus.Errorf("failed to open %q: %s", mFileName, err)
  510. }
  511. return nil, err
  512. }
  513. pR, pW := io.Pipe()
  514. // this will need to be in a goroutine, as we are returning the stream of a
  515. // tar archive, but can not close the metadata reader early (when this
  516. // function returns)...
  517. go func() {
  518. defer mf.Close()
  519. // let's reassemble!
  520. logrus.Debugf("[graph] TarLayer with reassembly: %s", img.ID)
  521. mfz, err := gzip.NewReader(mf)
  522. if err != nil {
  523. pW.CloseWithError(fmt.Errorf("[graph] error with %s: %s", mFileName, err))
  524. return
  525. }
  526. defer mfz.Close()
  527. // get our relative path to the container
  528. fsLayer, err := graph.driver.Get(img.ID, "")
  529. if err != nil {
  530. pW.CloseWithError(err)
  531. return
  532. }
  533. defer graph.driver.Put(img.ID)
  534. metaUnpacker := storage.NewJSONUnpacker(mfz)
  535. fileGetter := storage.NewPathFileGetter(fsLayer)
  536. logrus.Debugf("[graph] %s is at %q", img.ID, fsLayer)
  537. ots := asm.NewOutputTarStream(fileGetter, metaUnpacker)
  538. defer ots.Close()
  539. if _, err := io.Copy(pW, ots); err != nil {
  540. pW.CloseWithError(err)
  541. return
  542. }
  543. pW.Close()
  544. }()
  545. return pR, nil
  546. }