github.com/rhatdan/storage@v1.12.13/store.go (about) 1 package storage 2 3 import ( 4 "encoding/base64" 5 "fmt" 6 "io" 7 "io/ioutil" 8 "os" 9 "path/filepath" 10 "reflect" 11 "strings" 12 "sync" 13 "time" 14 15 // register all of the built-in drivers 16 _ "github.com/containers/storage/drivers/register" 17 18 "github.com/BurntSushi/toml" 19 drivers "github.com/containers/storage/drivers" 20 "github.com/containers/storage/pkg/archive" 21 "github.com/containers/storage/pkg/config" 22 "github.com/containers/storage/pkg/directory" 23 "github.com/containers/storage/pkg/idtools" 24 "github.com/containers/storage/pkg/ioutils" 25 "github.com/containers/storage/pkg/parsers" 26 "github.com/containers/storage/pkg/stringid" 27 "github.com/containers/storage/pkg/stringutils" 28 digest "github.com/opencontainers/go-digest" 29 "github.com/opencontainers/selinux/go-selinux/label" 30 "github.com/pkg/errors" 31 ) 32 33 var ( 34 // DefaultStoreOptions is a reasonable default set of options. 35 defaultStoreOptions StoreOptions 36 stores []*store 37 storesLock sync.Mutex 38 ) 39 40 // ROFileBasedStore wraps up the methods of the various types of file-based 41 // data stores that we implement which are needed for both read-only and 42 // read-write files. 43 type ROFileBasedStore interface { 44 Locker 45 46 // Load reloads the contents of the store from disk. It should be called 47 // with the lock held. 48 Load() error 49 } 50 51 // RWFileBasedStore wraps up the methods of various types of file-based data 52 // stores that we implement using read-write files. 53 type RWFileBasedStore interface { 54 // Save saves the contents of the store to disk. It should be called with 55 // the lock held, and Touch() should be called afterward before releasing the 56 // lock. 57 Save() error 58 } 59 60 // FileBasedStore wraps up the common methods of various types of file-based 61 // data stores that we implement. 62 type FileBasedStore interface { 63 ROFileBasedStore 64 RWFileBasedStore 65 } 66 67 // ROMetadataStore wraps a method for reading metadata associated with an ID. 68 type ROMetadataStore interface { 69 // Metadata reads metadata associated with an item with the specified ID. 70 Metadata(id string) (string, error) 71 } 72 73 // RWMetadataStore wraps a method for setting metadata associated with an ID. 74 type RWMetadataStore interface { 75 // SetMetadata updates the metadata associated with the item with the specified ID. 76 SetMetadata(id, metadata string) error 77 } 78 79 // MetadataStore wraps up methods for getting and setting metadata associated with IDs. 80 type MetadataStore interface { 81 ROMetadataStore 82 RWMetadataStore 83 } 84 85 // An ROBigDataStore wraps up the read-only big-data related methods of the 86 // various types of file-based lookaside stores that we implement. 87 type ROBigDataStore interface { 88 // BigData retrieves a (potentially large) piece of data associated with 89 // this ID, if it has previously been set. 90 BigData(id, key string) ([]byte, error) 91 92 // BigDataSize retrieves the size of a (potentially large) piece of 93 // data associated with this ID, if it has previously been set. 94 BigDataSize(id, key string) (int64, error) 95 96 // BigDataDigest retrieves the digest of a (potentially large) piece of 97 // data associated with this ID, if it has previously been set. 98 BigDataDigest(id, key string) (digest.Digest, error) 99 100 // BigDataNames() returns a list of the names of previously-stored pieces of 101 // data. 102 BigDataNames(id string) ([]string, error) 103 } 104 105 // A RWImageBigDataStore wraps up how we store big-data associated with images. 106 type RWImageBigDataStore interface { 107 // SetBigData stores a (potentially large) piece of data associated 108 // with this ID. 109 // Pass github.com/containers/image/manifest.Digest as digestManifest 110 // to allow ByDigest to find images by their correct digests. 111 SetBigData(id, key string, data []byte, digestManifest func([]byte) (digest.Digest, error)) error 112 } 113 114 // A ContainerBigDataStore wraps up how we store big-data associated with containers. 115 type ContainerBigDataStore interface { 116 ROBigDataStore 117 // SetBigData stores a (potentially large) piece of data associated 118 // with this ID. 119 SetBigData(id, key string, data []byte) error 120 } 121 122 // A FlaggableStore can have flags set and cleared on items which it manages. 123 type FlaggableStore interface { 124 // ClearFlag removes a named flag from an item in the store. 125 ClearFlag(id string, flag string) error 126 127 // SetFlag sets a named flag and its value on an item in the store. 128 SetFlag(id string, flag string, value interface{}) error 129 } 130 131 // StoreOptions is used for passing initialization options to GetStore(), for 132 // initializing a Store object and the underlying storage that it controls. 133 type StoreOptions struct { 134 // RunRoot is the filesystem path under which we can store run-time 135 // information, such as the locations of active mount points, that we 136 // want to lose if the host is rebooted. 137 RunRoot string `json:"runroot,omitempty"` 138 // GraphRoot is the filesystem path under which we will store the 139 // contents of layers, images, and containers. 140 GraphRoot string `json:"root,omitempty"` 141 // GraphDriverName is the underlying storage driver that we'll be 142 // using. It only needs to be specified the first time a Store is 143 // initialized for a given RunRoot and GraphRoot. 144 GraphDriverName string `json:"driver,omitempty"` 145 // GraphDriverOptions are driver-specific options. 146 GraphDriverOptions []string `json:"driver-options,omitempty"` 147 // UIDMap and GIDMap are used for setting up a container's root filesystem 148 // for use inside of a user namespace where UID mapping is being used. 149 UIDMap []idtools.IDMap `json:"uidmap,omitempty"` 150 GIDMap []idtools.IDMap `json:"gidmap,omitempty"` 151 } 152 153 // Store wraps up the various types of file-based stores that we use into a 154 // singleton object that initializes and manages them all together. 155 type Store interface { 156 // RunRoot, GraphRoot, GraphDriverName, and GraphOptions retrieve 157 // settings that were passed to GetStore() when the object was created. 158 RunRoot() string 159 GraphRoot() string 160 GraphDriverName() string 161 GraphOptions() []string 162 UIDMap() []idtools.IDMap 163 GIDMap() []idtools.IDMap 164 165 // GraphDriver obtains and returns a handle to the graph Driver object used 166 // by the Store. 167 GraphDriver() (drivers.Driver, error) 168 169 // CreateLayer creates a new layer in the underlying storage driver, 170 // optionally having the specified ID (one will be assigned if none is 171 // specified), with the specified layer (or no layer) as its parent, 172 // and with optional names. (The writeable flag is ignored.) 173 CreateLayer(id, parent string, names []string, mountLabel string, writeable bool, options *LayerOptions) (*Layer, error) 174 175 // PutLayer combines the functions of CreateLayer and ApplyDiff, 176 // marking the layer for automatic removal if applying the diff fails 177 // for any reason. 178 // 179 // Note that we do some of this work in a child process. The calling 180 // process's main() function needs to import our pkg/reexec package and 181 // should begin with something like this in order to allow us to 182 // properly start that child process: 183 // if reexec.Init() { 184 // return 185 // } 186 PutLayer(id, parent string, names []string, mountLabel string, writeable bool, options *LayerOptions, diff io.Reader) (*Layer, int64, error) 187 188 // CreateImage creates a new image, optionally with the specified ID 189 // (one will be assigned if none is specified), with optional names, 190 // referring to a specified image, and with optional metadata. An 191 // image is a record which associates the ID of a layer with a 192 // additional bookkeeping information which the library stores for the 193 // convenience of its caller. 194 CreateImage(id string, names []string, layer, metadata string, options *ImageOptions) (*Image, error) 195 196 // CreateContainer creates a new container, optionally with the 197 // specified ID (one will be assigned if none is specified), with 198 // optional names, using the specified image's top layer as the basis 199 // for the container's layer, and assigning the specified ID to that 200 // layer (one will be created if none is specified). A container is a 201 // layer which is associated with additional bookkeeping information 202 // which the library stores for the convenience of its caller. 203 CreateContainer(id string, names []string, image, layer, metadata string, options *ContainerOptions) (*Container, error) 204 205 // Metadata retrieves the metadata which is associated with a layer, 206 // image, or container (whichever the passed-in ID refers to). 207 Metadata(id string) (string, error) 208 209 // SetMetadata updates the metadata which is associated with a layer, 210 // image, or container (whichever the passed-in ID refers to) to match 211 // the specified value. The metadata value can be retrieved at any 212 // time using Metadata, or using Layer, Image, or Container and reading 213 // the object directly. 214 SetMetadata(id, metadata string) error 215 216 // Exists checks if there is a layer, image, or container which has the 217 // passed-in ID or name. 218 Exists(id string) bool 219 220 // Status asks for a status report, in the form of key-value pairs, 221 // from the underlying storage driver. The contents vary from driver 222 // to driver. 223 Status() ([][2]string, error) 224 225 // Delete removes the layer, image, or container which has the 226 // passed-in ID or name. Note that no safety checks are performed, so 227 // this can leave images with references to layers which do not exist, 228 // and layers with references to parents which no longer exist. 229 Delete(id string) error 230 231 // DeleteLayer attempts to remove the specified layer. If the layer is the 232 // parent of any other layer, or is referred to by any images, it will return 233 // an error. 234 DeleteLayer(id string) error 235 236 // DeleteImage removes the specified image if it is not referred to by 237 // any containers. If its top layer is then no longer referred to by 238 // any other images and is not the parent of any other layers, its top 239 // layer will be removed. If that layer's parent is no longer referred 240 // to by any other images and is not the parent of any other layers, 241 // then it, too, will be removed. This procedure will be repeated 242 // until a layer which should not be removed, or the base layer, is 243 // reached, at which point the list of removed layers is returned. If 244 // the commit argument is false, the image and layers are not removed, 245 // but the list of layers which would be removed is still returned. 246 DeleteImage(id string, commit bool) (layers []string, err error) 247 248 // DeleteContainer removes the specified container and its layer. If 249 // there is no matching container, or if the container exists but its 250 // layer does not, an error will be returned. 251 DeleteContainer(id string) error 252 253 // Wipe removes all known layers, images, and containers. 254 Wipe() error 255 256 // Mount attempts to mount a layer, image, or container for access, and 257 // returns the pathname if it succeeds. 258 // Note if the mountLabel == "", the default label for the container 259 // will be used. 260 // 261 // Note that we do some of this work in a child process. The calling 262 // process's main() function needs to import our pkg/reexec package and 263 // should begin with something like this in order to allow us to 264 // properly start that child process: 265 // if reexec.Init() { 266 // return 267 // } 268 Mount(id, mountLabel string) (string, error) 269 270 // Unmount attempts to unmount a layer, image, or container, given an ID, a 271 // name, or a mount path. Returns whether or not the layer is still mounted. 272 Unmount(id string, force bool) (bool, error) 273 274 // Mounted returns number of times the layer has been mounted. 275 Mounted(id string) (int, error) 276 277 // Changes returns a summary of the changes which would need to be made 278 // to one layer to make its contents the same as a second layer. If 279 // the first layer is not specified, the second layer's parent is 280 // assumed. Each Change structure contains a Path relative to the 281 // layer's root directory, and a Kind which is either ChangeAdd, 282 // ChangeModify, or ChangeDelete. 283 Changes(from, to string) ([]archive.Change, error) 284 285 // DiffSize returns a count of the size of the tarstream which would 286 // specify the changes returned by Changes. 287 DiffSize(from, to string) (int64, error) 288 289 // Diff returns the tarstream which would specify the changes returned 290 // by Changes. If options are passed in, they can override default 291 // behaviors. 292 Diff(from, to string, options *DiffOptions) (io.ReadCloser, error) 293 294 // ApplyDiff applies a tarstream to a layer. Information about the 295 // tarstream is cached with the layer. Typically, a layer which is 296 // populated using a tarstream will be expected to not be modified in 297 // any other way, either before or after the diff is applied. 298 // 299 // Note that we do some of this work in a child process. The calling 300 // process's main() function needs to import our pkg/reexec package and 301 // should begin with something like this in order to allow us to 302 // properly start that child process: 303 // if reexec.Init() { 304 // return 305 // } 306 ApplyDiff(to string, diff io.Reader) (int64, error) 307 308 // LayersByCompressedDigest returns a slice of the layers with the 309 // specified compressed digest value recorded for them. 310 LayersByCompressedDigest(d digest.Digest) ([]Layer, error) 311 312 // LayersByUncompressedDigest returns a slice of the layers with the 313 // specified uncompressed digest value recorded for them. 314 LayersByUncompressedDigest(d digest.Digest) ([]Layer, error) 315 316 // LayerSize returns a cached approximation of the layer's size, or -1 317 // if we don't have a value on hand. 318 LayerSize(id string) (int64, error) 319 320 // LayerParentOwners returns the UIDs and GIDs of owners of parents of 321 // the layer's mountpoint for which the layer's UID and GID maps (if 322 // any are defined) don't contain corresponding IDs. 323 LayerParentOwners(id string) ([]int, []int, error) 324 325 // Layers returns a list of the currently known layers. 326 Layers() ([]Layer, error) 327 328 // Images returns a list of the currently known images. 329 Images() ([]Image, error) 330 331 // Containers returns a list of the currently known containers. 332 Containers() ([]Container, error) 333 334 // Names returns the list of names for a layer, image, or container. 335 Names(id string) ([]string, error) 336 337 // SetNames changes the list of names for a layer, image, or container. 338 // Duplicate names are removed from the list automatically. 339 SetNames(id string, names []string) error 340 341 // ListImageBigData retrieves a list of the (possibly large) chunks of 342 // named data associated with an image. 343 ListImageBigData(id string) ([]string, error) 344 345 // ImageBigData retrieves a (possibly large) chunk of named data 346 // associated with an image. 347 ImageBigData(id, key string) ([]byte, error) 348 349 // ImageBigDataSize retrieves the size of a (possibly large) chunk 350 // of named data associated with an image. 351 ImageBigDataSize(id, key string) (int64, error) 352 353 // ImageBigDataDigest retrieves the digest of a (possibly large) chunk 354 // of named data associated with an image. 355 ImageBigDataDigest(id, key string) (digest.Digest, error) 356 357 // SetImageBigData stores a (possibly large) chunk of named data 358 // associated with an image. Pass 359 // github.com/containers/image/manifest.Digest as digestManifest to 360 // allow ImagesByDigest to find images by their correct digests. 361 SetImageBigData(id, key string, data []byte, digestManifest func([]byte) (digest.Digest, error)) error 362 363 // ImageSize computes the size of the image's layers and ancillary data. 364 ImageSize(id string) (int64, error) 365 366 // ListContainerBigData retrieves a list of the (possibly large) chunks of 367 // named data associated with a container. 368 ListContainerBigData(id string) ([]string, error) 369 370 // ContainerBigData retrieves a (possibly large) chunk of named data 371 // associated with a container. 372 ContainerBigData(id, key string) ([]byte, error) 373 374 // ContainerBigDataSize retrieves the size of a (possibly large) 375 // chunk of named data associated with a container. 376 ContainerBigDataSize(id, key string) (int64, error) 377 378 // ContainerBigDataDigest retrieves the digest of a (possibly large) 379 // chunk of named data associated with a container. 380 ContainerBigDataDigest(id, key string) (digest.Digest, error) 381 382 // SetContainerBigData stores a (possibly large) chunk of named data 383 // associated with a container. 384 SetContainerBigData(id, key string, data []byte) error 385 386 // ContainerSize computes the size of the container's layer and ancillary 387 // data. Warning: this is a potentially expensive operation. 388 ContainerSize(id string) (int64, error) 389 390 // Layer returns a specific layer. 391 Layer(id string) (*Layer, error) 392 393 // Image returns a specific image. 394 Image(id string) (*Image, error) 395 396 // ImagesByTopLayer returns a list of images which reference the specified 397 // layer as their top layer. They will have different IDs and names 398 // and may have different metadata, big data items, and flags. 399 ImagesByTopLayer(id string) ([]*Image, error) 400 401 // ImagesByDigest returns a list of images which contain a big data item 402 // named ImageDigestBigDataKey whose contents have the specified digest. 403 ImagesByDigest(d digest.Digest) ([]*Image, error) 404 405 // Container returns a specific container. 406 Container(id string) (*Container, error) 407 408 // ContainerByLayer returns a specific container based on its layer ID or 409 // name. 410 ContainerByLayer(id string) (*Container, error) 411 412 // ContainerDirectory returns a path of a directory which the caller 413 // can use to store data, specific to the container, which the library 414 // does not directly manage. The directory will be deleted when the 415 // container is deleted. 416 ContainerDirectory(id string) (string, error) 417 418 // SetContainerDirectoryFile is a convenience function which stores 419 // a piece of data in the specified file relative to the container's 420 // directory. 421 SetContainerDirectoryFile(id, file string, data []byte) error 422 423 // FromContainerDirectory is a convenience function which reads 424 // the contents of the specified file relative to the container's 425 // directory. 426 FromContainerDirectory(id, file string) ([]byte, error) 427 428 // ContainerRunDirectory returns a path of a directory which the 429 // caller can use to store data, specific to the container, which the 430 // library does not directly manage. The directory will be deleted 431 // when the host system is restarted. 432 ContainerRunDirectory(id string) (string, error) 433 434 // SetContainerRunDirectoryFile is a convenience function which stores 435 // a piece of data in the specified file relative to the container's 436 // run directory. 437 SetContainerRunDirectoryFile(id, file string, data []byte) error 438 439 // FromContainerRunDirectory is a convenience function which reads 440 // the contents of the specified file relative to the container's run 441 // directory. 442 FromContainerRunDirectory(id, file string) ([]byte, error) 443 444 // ContainerParentOwners returns the UIDs and GIDs of owners of parents 445 // of the container's layer's mountpoint for which the layer's UID and 446 // GID maps (if any are defined) don't contain corresponding IDs. 447 ContainerParentOwners(id string) ([]int, []int, error) 448 449 // Lookup returns the ID of a layer, image, or container with the specified 450 // name or ID. 451 Lookup(name string) (string, error) 452 453 // Shutdown attempts to free any kernel resources which are being used 454 // by the underlying driver. If "force" is true, any mounted (i.e., in 455 // use) layers are unmounted beforehand. If "force" is not true, then 456 // layers being in use is considered to be an error condition. A list 457 // of still-mounted layers is returned along with possible errors. 458 Shutdown(force bool) (layers []string, err error) 459 460 // Version returns version information, in the form of key-value pairs, from 461 // the storage package. 462 Version() ([][2]string, error) 463 464 // GetDigestLock returns digest-specific Locker. 465 GetDigestLock(digest.Digest) (Locker, error) 466 } 467 468 // IDMappingOptions are used for specifying how ID mapping should be set up for 469 // a layer or container. 470 type IDMappingOptions struct { 471 // UIDMap and GIDMap are used for setting up a layer's root filesystem 472 // for use inside of a user namespace where ID mapping is being used. 473 // If HostUIDMapping/HostGIDMapping is true, no mapping of the 474 // respective type will be used. Otherwise, if UIDMap and/or GIDMap 475 // contain at least one mapping, one or both will be used. By default, 476 // if neither of those conditions apply, if the layer has a parent 477 // layer, the parent layer's mapping will be used, and if it does not 478 // have a parent layer, the mapping which was passed to the Store 479 // object when it was initialized will be used. 480 HostUIDMapping bool 481 HostGIDMapping bool 482 UIDMap []idtools.IDMap 483 GIDMap []idtools.IDMap 484 } 485 486 // LayerOptions is used for passing options to a Store's CreateLayer() and PutLayer() methods. 487 type LayerOptions struct { 488 // IDMappingOptions specifies the type of ID mapping which should be 489 // used for this layer. If nothing is specified, the layer will 490 // inherit settings from its parent layer or, if it has no parent 491 // layer, the Store object. 492 IDMappingOptions 493 // TemplateLayer is the ID of a layer whose contents will be used to 494 // initialize this layer. If set, it should be a child of the layer 495 // which we want to use as the parent of the new layer. 496 TemplateLayer string 497 } 498 499 // ImageOptions is used for passing options to a Store's CreateImage() method. 500 type ImageOptions struct { 501 // CreationDate, if not zero, will override the default behavior of marking the image as having been 502 // created when CreateImage() was called, recording CreationDate instead. 503 CreationDate time.Time 504 // Digest is a hard-coded digest value that we can use to look up the image. It is optional. 505 Digest digest.Digest 506 } 507 508 // ContainerOptions is used for passing options to a Store's CreateContainer() method. 509 type ContainerOptions struct { 510 // IDMappingOptions specifies the type of ID mapping which should be 511 // used for this container's layer. If nothing is specified, the 512 // container's layer will inherit settings from the image's top layer 513 // or, if it is not being created based on an image, the Store object. 514 IDMappingOptions 515 LabelOpts []string 516 Flags map[string]interface{} 517 MountOpts []string 518 } 519 520 type store struct { 521 lastLoaded time.Time 522 runRoot string 523 graphLock Locker 524 graphRoot string 525 graphDriverName string 526 graphOptions []string 527 uidMap []idtools.IDMap 528 gidMap []idtools.IDMap 529 graphDriver drivers.Driver 530 layerStore LayerStore 531 roLayerStores []ROLayerStore 532 imageStore ImageStore 533 roImageStores []ROImageStore 534 containerStore ContainerStore 535 digestLockRoot string 536 } 537 538 // GetStore attempts to find an already-created Store object matching the 539 // specified location and graph driver, and if it can't, it creates and 540 // initializes a new Store object, and the underlying storage that it controls. 541 // 542 // If StoreOptions `options` haven't been fully populated, then DefaultStoreOptions are used. 543 // 544 // These defaults observe environment variables: 545 // * `STORAGE_DRIVER` for the name of the storage driver to attempt to use 546 // * `STORAGE_OPTS` for the string of options to pass to the driver 547 // 548 // Note that we do some of this work in a child process. The calling process's 549 // main() function needs to import our pkg/reexec package and should begin with 550 // something like this in order to allow us to properly start that child 551 // process: 552 // if reexec.Init() { 553 // return 554 // } 555 func GetStore(options StoreOptions) (Store, error) { 556 if options.RunRoot == "" && options.GraphRoot == "" && options.GraphDriverName == "" && len(options.GraphDriverOptions) == 0 { 557 options = defaultStoreOptions 558 } 559 560 if options.GraphRoot != "" { 561 dir, err := filepath.Abs(options.GraphRoot) 562 if err != nil { 563 return nil, errors.Wrapf(err, "error deriving an absolute path from %q", options.GraphRoot) 564 } 565 options.GraphRoot = dir 566 } 567 if options.RunRoot != "" { 568 dir, err := filepath.Abs(options.RunRoot) 569 if err != nil { 570 return nil, errors.Wrapf(err, "error deriving an absolute path from %q", options.RunRoot) 571 } 572 options.RunRoot = dir 573 } 574 575 storesLock.Lock() 576 defer storesLock.Unlock() 577 578 for _, s := range stores { 579 if s.graphRoot == options.GraphRoot && (options.GraphDriverName == "" || s.graphDriverName == options.GraphDriverName) { 580 return s, nil 581 } 582 } 583 584 if options.GraphRoot == "" { 585 return nil, errors.Wrap(ErrIncompleteOptions, "no storage root specified") 586 } 587 if options.RunRoot == "" { 588 return nil, errors.Wrap(ErrIncompleteOptions, "no storage runroot specified") 589 } 590 591 if err := os.MkdirAll(options.RunRoot, 0700); err != nil && !os.IsExist(err) { 592 return nil, err 593 } 594 if err := os.MkdirAll(options.GraphRoot, 0700); err != nil && !os.IsExist(err) { 595 return nil, err 596 } 597 for _, subdir := range []string{"mounts", "tmp", options.GraphDriverName} { 598 if err := os.MkdirAll(filepath.Join(options.GraphRoot, subdir), 0700); err != nil && !os.IsExist(err) { 599 return nil, err 600 } 601 } 602 603 graphLock, err := GetLockfile(filepath.Join(options.GraphRoot, "storage.lock")) 604 if err != nil { 605 return nil, err 606 } 607 s := &store{ 608 runRoot: options.RunRoot, 609 graphLock: graphLock, 610 graphRoot: options.GraphRoot, 611 graphDriverName: options.GraphDriverName, 612 graphOptions: options.GraphDriverOptions, 613 uidMap: copyIDMap(options.UIDMap), 614 gidMap: copyIDMap(options.GIDMap), 615 } 616 if err := s.load(); err != nil { 617 return nil, err 618 } 619 620 stores = append(stores, s) 621 622 return s, nil 623 } 624 625 func copyIDMap(idmap []idtools.IDMap) []idtools.IDMap { 626 m := []idtools.IDMap{} 627 if idmap != nil { 628 m = make([]idtools.IDMap, len(idmap)) 629 copy(m, idmap) 630 } 631 if len(m) > 0 { 632 return m[:] 633 } 634 return nil 635 } 636 637 func (s *store) RunRoot() string { 638 return s.runRoot 639 } 640 641 func (s *store) GraphDriverName() string { 642 return s.graphDriverName 643 } 644 645 func (s *store) GraphRoot() string { 646 return s.graphRoot 647 } 648 649 func (s *store) GraphOptions() []string { 650 return s.graphOptions 651 } 652 653 func (s *store) UIDMap() []idtools.IDMap { 654 return copyIDMap(s.uidMap) 655 } 656 657 func (s *store) GIDMap() []idtools.IDMap { 658 return copyIDMap(s.gidMap) 659 } 660 661 func (s *store) load() error { 662 driver, err := s.GraphDriver() 663 if err != nil { 664 return err 665 } 666 s.graphDriver = driver 667 s.graphDriverName = driver.String() 668 driverPrefix := s.graphDriverName + "-" 669 670 rls, err := s.LayerStore() 671 if err != nil { 672 return err 673 } 674 s.layerStore = rls 675 if _, err := s.ROLayerStores(); err != nil { 676 return err 677 } 678 679 gipath := filepath.Join(s.graphRoot, driverPrefix+"images") 680 if err := os.MkdirAll(gipath, 0700); err != nil { 681 return err 682 } 683 ris, err := newImageStore(gipath) 684 if err != nil { 685 return err 686 } 687 s.imageStore = ris 688 if _, err := s.ROImageStores(); err != nil { 689 return err 690 } 691 692 gcpath := filepath.Join(s.graphRoot, driverPrefix+"containers") 693 if err := os.MkdirAll(gcpath, 0700); err != nil { 694 return err 695 } 696 rcs, err := newContainerStore(gcpath) 697 if err != nil { 698 return err 699 } 700 rcpath := filepath.Join(s.runRoot, driverPrefix+"containers") 701 if err := os.MkdirAll(rcpath, 0700); err != nil { 702 return err 703 } 704 s.containerStore = rcs 705 706 s.digestLockRoot = filepath.Join(s.runRoot, driverPrefix+"locks") 707 if err := os.MkdirAll(s.digestLockRoot, 0700); err != nil { 708 return err 709 } 710 711 return nil 712 } 713 714 // GetDigestLock returns a digest-specific Locker. 715 func (s *store) GetDigestLock(d digest.Digest) (Locker, error) { 716 return GetLockfile(filepath.Join(s.digestLockRoot, d.String())) 717 } 718 719 func (s *store) getGraphDriver() (drivers.Driver, error) { 720 if s.graphDriver != nil { 721 return s.graphDriver, nil 722 } 723 config := drivers.Options{ 724 Root: s.graphRoot, 725 RunRoot: s.runRoot, 726 DriverOptions: s.graphOptions, 727 UIDMaps: s.uidMap, 728 GIDMaps: s.gidMap, 729 } 730 driver, err := drivers.New(s.graphDriverName, config) 731 if err != nil { 732 return nil, err 733 } 734 s.graphDriver = driver 735 s.graphDriverName = driver.String() 736 return driver, nil 737 } 738 739 func (s *store) GraphDriver() (drivers.Driver, error) { 740 s.graphLock.Lock() 741 defer s.graphLock.Unlock() 742 if s.graphLock.TouchedSince(s.lastLoaded) { 743 s.graphDriver = nil 744 s.layerStore = nil 745 s.lastLoaded = time.Now() 746 } 747 return s.getGraphDriver() 748 } 749 750 // LayerStore obtains and returns a handle to the writeable layer store object 751 // used by the Store. Accessing this store directly will bypass locking and 752 // synchronization, so it is not a part of the exported Store interface. 753 func (s *store) LayerStore() (LayerStore, error) { 754 s.graphLock.Lock() 755 defer s.graphLock.Unlock() 756 if s.graphLock.TouchedSince(s.lastLoaded) { 757 s.graphDriver = nil 758 s.layerStore = nil 759 s.lastLoaded = time.Now() 760 } 761 if s.layerStore != nil { 762 return s.layerStore, nil 763 } 764 driver, err := s.getGraphDriver() 765 if err != nil { 766 return nil, err 767 } 768 driverPrefix := s.graphDriverName + "-" 769 rlpath := filepath.Join(s.runRoot, driverPrefix+"layers") 770 if err := os.MkdirAll(rlpath, 0700); err != nil { 771 return nil, err 772 } 773 glpath := filepath.Join(s.graphRoot, driverPrefix+"layers") 774 if err := os.MkdirAll(glpath, 0700); err != nil { 775 return nil, err 776 } 777 rls, err := newLayerStore(rlpath, glpath, driver, s.uidMap, s.gidMap) 778 if err != nil { 779 return nil, err 780 } 781 s.layerStore = rls 782 return s.layerStore, nil 783 } 784 785 // ROLayerStores obtains additional read/only layer store objects used by the 786 // Store. Accessing these stores directly will bypass locking and 787 // synchronization, so it is not part of the exported Store interface. 788 func (s *store) ROLayerStores() ([]ROLayerStore, error) { 789 s.graphLock.Lock() 790 defer s.graphLock.Unlock() 791 if s.roLayerStores != nil { 792 return s.roLayerStores, nil 793 } 794 driver, err := s.getGraphDriver() 795 if err != nil { 796 return nil, err 797 } 798 driverPrefix := s.graphDriverName + "-" 799 rlpath := filepath.Join(s.runRoot, driverPrefix+"layers") 800 if err := os.MkdirAll(rlpath, 0700); err != nil { 801 return nil, err 802 } 803 for _, store := range driver.AdditionalImageStores() { 804 glpath := filepath.Join(store, driverPrefix+"layers") 805 rls, err := newROLayerStore(rlpath, glpath, driver) 806 if err != nil { 807 return nil, err 808 } 809 s.roLayerStores = append(s.roLayerStores, rls) 810 } 811 return s.roLayerStores, nil 812 } 813 814 // ImageStore obtains and returns a handle to the writable image store object 815 // used by the Store. Accessing this store directly will bypass locking and 816 // synchronization, so it is not a part of the exported Store interface. 817 func (s *store) ImageStore() (ImageStore, error) { 818 if s.imageStore != nil { 819 return s.imageStore, nil 820 } 821 return nil, ErrLoadError 822 } 823 824 // ROImageStores obtains additional read/only image store objects used by the 825 // Store. Accessing these stores directly will bypass locking and 826 // synchronization, so it is not a part of the exported Store interface. 827 func (s *store) ROImageStores() ([]ROImageStore, error) { 828 if len(s.roImageStores) != 0 { 829 return s.roImageStores, nil 830 } 831 driver, err := s.getGraphDriver() 832 if err != nil { 833 return nil, err 834 } 835 driverPrefix := s.graphDriverName + "-" 836 for _, store := range driver.AdditionalImageStores() { 837 gipath := filepath.Join(store, driverPrefix+"images") 838 ris, err := newROImageStore(gipath) 839 if err != nil { 840 return nil, err 841 } 842 s.roImageStores = append(s.roImageStores, ris) 843 } 844 return s.roImageStores, nil 845 } 846 847 // ContainerStore obtains and returns a handle to the container store object 848 // used by the Store. Accessing this store directly will bypass locking and 849 // synchronization, so it is not a part of the exported Store interface. 850 func (s *store) ContainerStore() (ContainerStore, error) { 851 if s.containerStore != nil { 852 return s.containerStore, nil 853 } 854 return nil, ErrLoadError 855 } 856 857 func (s *store) PutLayer(id, parent string, names []string, mountLabel string, writeable bool, options *LayerOptions, diff io.Reader) (*Layer, int64, error) { 858 var parentLayer *Layer 859 rlstore, err := s.LayerStore() 860 if err != nil { 861 return nil, -1, err 862 } 863 rlstores, err := s.ROLayerStores() 864 if err != nil { 865 return nil, -1, err 866 } 867 rcstore, err := s.ContainerStore() 868 if err != nil { 869 return nil, -1, err 870 } 871 rlstore.Lock() 872 defer rlstore.Unlock() 873 if modified, err := rlstore.Modified(); modified || err != nil { 874 if err = rlstore.Load(); err != nil { 875 return nil, -1, err 876 } 877 } 878 rcstore.Lock() 879 defer rcstore.Unlock() 880 if modified, err := rcstore.Modified(); modified || err != nil { 881 if err = rcstore.Load(); err != nil { 882 return nil, -1, err 883 } 884 } 885 if id == "" { 886 id = stringid.GenerateRandomID() 887 } 888 if options == nil { 889 options = &LayerOptions{} 890 } 891 if options.HostUIDMapping { 892 options.UIDMap = nil 893 } 894 if options.HostGIDMapping { 895 options.GIDMap = nil 896 } 897 uidMap := options.UIDMap 898 gidMap := options.GIDMap 899 if parent != "" { 900 var ilayer *Layer 901 for _, l := range append([]ROLayerStore{rlstore}, rlstores...) { 902 lstore := l 903 if lstore != rlstore { 904 lstore.Lock() 905 defer lstore.Unlock() 906 if modified, err := lstore.Modified(); modified || err != nil { 907 if err = lstore.Load(); err != nil { 908 return nil, -1, err 909 } 910 } 911 } 912 if l, err := lstore.Get(parent); err == nil && l != nil { 913 ilayer = l 914 parent = ilayer.ID 915 break 916 } 917 } 918 if ilayer == nil { 919 return nil, -1, ErrLayerUnknown 920 } 921 parentLayer = ilayer 922 containers, err := rcstore.Containers() 923 if err != nil { 924 return nil, -1, err 925 } 926 for _, container := range containers { 927 if container.LayerID == parent { 928 return nil, -1, ErrParentIsContainer 929 } 930 } 931 if !options.HostUIDMapping && len(options.UIDMap) == 0 { 932 uidMap = ilayer.UIDMap 933 } 934 if !options.HostGIDMapping && len(options.GIDMap) == 0 { 935 gidMap = ilayer.GIDMap 936 } 937 } else { 938 if !options.HostUIDMapping && len(options.UIDMap) == 0 { 939 uidMap = s.uidMap 940 } 941 if !options.HostGIDMapping && len(options.GIDMap) == 0 { 942 gidMap = s.gidMap 943 } 944 } 945 var layerOptions *LayerOptions 946 if s.graphDriver.SupportsShifting() { 947 layerOptions = &LayerOptions{IDMappingOptions: IDMappingOptions{HostUIDMapping: true, HostGIDMapping: true, UIDMap: nil, GIDMap: nil}} 948 } else { 949 layerOptions = &LayerOptions{ 950 IDMappingOptions: IDMappingOptions{ 951 HostUIDMapping: options.HostUIDMapping, 952 HostGIDMapping: options.HostGIDMapping, 953 UIDMap: copyIDMap(uidMap), 954 GIDMap: copyIDMap(gidMap), 955 }, 956 } 957 } 958 return rlstore.Put(id, parentLayer, names, mountLabel, nil, layerOptions, writeable, nil, diff) 959 } 960 961 func (s *store) CreateLayer(id, parent string, names []string, mountLabel string, writeable bool, options *LayerOptions) (*Layer, error) { 962 layer, _, err := s.PutLayer(id, parent, names, mountLabel, writeable, options, nil) 963 return layer, err 964 } 965 966 func (s *store) CreateImage(id string, names []string, layer, metadata string, options *ImageOptions) (*Image, error) { 967 if id == "" { 968 id = stringid.GenerateRandomID() 969 } 970 971 if layer != "" { 972 lstore, err := s.LayerStore() 973 if err != nil { 974 return nil, err 975 } 976 lstores, err := s.ROLayerStores() 977 if err != nil { 978 return nil, err 979 } 980 var ilayer *Layer 981 for _, s := range append([]ROLayerStore{lstore}, lstores...) { 982 store := s 983 store.Lock() 984 defer store.Unlock() 985 if modified, err := store.Modified(); modified || err != nil { 986 if err = store.Load(); err != nil { 987 return nil, err 988 } 989 } 990 ilayer, err = store.Get(layer) 991 if err == nil { 992 break 993 } 994 } 995 if ilayer == nil { 996 return nil, ErrLayerUnknown 997 } 998 layer = ilayer.ID 999 } 1000 1001 ristore, err := s.ImageStore() 1002 if err != nil { 1003 return nil, err 1004 } 1005 ristore.Lock() 1006 defer ristore.Unlock() 1007 if modified, err := ristore.Modified(); modified || err != nil { 1008 if err = ristore.Load(); err != nil { 1009 return nil, err 1010 } 1011 } 1012 1013 creationDate := time.Now().UTC() 1014 if options != nil && !options.CreationDate.IsZero() { 1015 creationDate = options.CreationDate 1016 } 1017 1018 return ristore.Create(id, names, layer, metadata, creationDate, options.Digest) 1019 } 1020 1021 func (s *store) imageTopLayerForMapping(image *Image, ristore ROImageStore, createMappedLayer bool, rlstore LayerStore, lstores []ROLayerStore, options IDMappingOptions) (*Layer, error) { 1022 layerMatchesMappingOptions := func(layer *Layer, options IDMappingOptions) bool { 1023 // If the driver supports shifting and the layer has no mappings, we can use it. 1024 if s.graphDriver.SupportsShifting() && len(layer.UIDMap) == 0 && len(layer.GIDMap) == 0 { 1025 return true 1026 } 1027 // If we want host mapping, and the layer uses mappings, it's not the best match. 1028 if options.HostUIDMapping && len(layer.UIDMap) != 0 { 1029 return false 1030 } 1031 if options.HostGIDMapping && len(layer.GIDMap) != 0 { 1032 return false 1033 } 1034 // If we don't care about the mapping, it's fine. 1035 if len(options.UIDMap) == 0 && len(options.GIDMap) == 0 { 1036 return true 1037 } 1038 // Compare the maps. 1039 return reflect.DeepEqual(layer.UIDMap, options.UIDMap) && reflect.DeepEqual(layer.GIDMap, options.GIDMap) 1040 } 1041 var layer, parentLayer *Layer 1042 allStores := append([]ROLayerStore{rlstore}, lstores...) 1043 // Locate the image's top layer and its parent, if it has one. 1044 for _, s := range allStores { 1045 store := s 1046 if store != rlstore { 1047 store.Lock() 1048 defer store.Unlock() 1049 if modified, err := store.Modified(); modified || err != nil { 1050 if err = store.Load(); err != nil { 1051 return nil, err 1052 } 1053 } 1054 } 1055 // Walk the top layer list. 1056 for _, candidate := range append([]string{image.TopLayer}, image.MappedTopLayers...) { 1057 if cLayer, err := store.Get(candidate); err == nil { 1058 // We want the layer's parent, too, if it has one. 1059 var cParentLayer *Layer 1060 if cLayer.Parent != "" { 1061 // Its parent should be in one of the stores, somewhere. 1062 for _, ps := range allStores { 1063 if cParentLayer, err = ps.Get(cLayer.Parent); err == nil { 1064 break 1065 } 1066 } 1067 if cParentLayer == nil { 1068 continue 1069 } 1070 } 1071 // If the layer matches the desired mappings, it's a perfect match, 1072 // so we're actually done here. 1073 if layerMatchesMappingOptions(cLayer, options) { 1074 return cLayer, nil 1075 } 1076 // Record the first one that we found, even if it's not ideal, so that 1077 // we have a starting point. 1078 if layer == nil { 1079 layer = cLayer 1080 parentLayer = cParentLayer 1081 } 1082 } 1083 } 1084 } 1085 if layer == nil { 1086 return nil, ErrLayerUnknown 1087 } 1088 // The top layer's mappings don't match the ones we want, but it's in a read-only 1089 // image store, so we can't create and add a mapped copy of the layer to the image. 1090 // We'll have to do the mapping for the container itself, elsewhere. 1091 if !createMappedLayer { 1092 return layer, nil 1093 } 1094 // The top layer's mappings don't match the ones we want, and it's in an image store 1095 // that lets us edit image metadata... 1096 if istore, ok := ristore.(*imageStore); ok { 1097 // ... so create a duplicate of the layer with the desired mappings, and 1098 // register it as an alternate top layer in the image. 1099 var layerOptions LayerOptions 1100 if s.graphDriver.SupportsShifting() { 1101 layerOptions = LayerOptions{ 1102 IDMappingOptions: IDMappingOptions{ 1103 HostUIDMapping: true, 1104 HostGIDMapping: true, 1105 UIDMap: nil, 1106 GIDMap: nil, 1107 }, 1108 } 1109 } else { 1110 layerOptions = LayerOptions{ 1111 IDMappingOptions: IDMappingOptions{ 1112 HostUIDMapping: options.HostUIDMapping, 1113 HostGIDMapping: options.HostGIDMapping, 1114 UIDMap: copyIDMap(options.UIDMap), 1115 GIDMap: copyIDMap(options.GIDMap), 1116 }, 1117 } 1118 } 1119 layerOptions.TemplateLayer = layer.ID 1120 mappedLayer, _, err := rlstore.Put("", parentLayer, nil, layer.MountLabel, nil, &layerOptions, false, nil, nil) 1121 if err != nil { 1122 return nil, errors.Wrapf(err, "error creating an ID-mapped copy of layer %q", layer.ID) 1123 } 1124 if err = istore.addMappedTopLayer(image.ID, mappedLayer.ID); err != nil { 1125 if err2 := rlstore.Delete(mappedLayer.ID); err2 != nil { 1126 err = errors.WithMessage(err, fmt.Sprintf("error deleting layer %q: %v", mappedLayer.ID, err2)) 1127 } 1128 return nil, errors.Wrapf(err, "error registering ID-mapped layer with image %q", image.ID) 1129 } 1130 layer = mappedLayer 1131 } 1132 return layer, nil 1133 } 1134 1135 func (s *store) CreateContainer(id string, names []string, image, layer, metadata string, options *ContainerOptions) (*Container, error) { 1136 if options == nil { 1137 options = &ContainerOptions{} 1138 } 1139 if options.HostUIDMapping { 1140 options.UIDMap = nil 1141 } 1142 if options.HostGIDMapping { 1143 options.GIDMap = nil 1144 } 1145 rlstore, err := s.LayerStore() 1146 if err != nil { 1147 return nil, err 1148 } 1149 if id == "" { 1150 id = stringid.GenerateRandomID() 1151 } 1152 1153 var imageTopLayer *Layer 1154 imageID := "" 1155 uidMap := options.UIDMap 1156 gidMap := options.GIDMap 1157 1158 idMappingsOptions := options.IDMappingOptions 1159 if image != "" { 1160 var imageHomeStore ROImageStore 1161 lstores, err := s.ROLayerStores() 1162 if err != nil { 1163 return nil, err 1164 } 1165 istore, err := s.ImageStore() 1166 if err != nil { 1167 return nil, err 1168 } 1169 istores, err := s.ROImageStores() 1170 if err != nil { 1171 return nil, err 1172 } 1173 rlstore.Lock() 1174 defer rlstore.Unlock() 1175 if modified, err := rlstore.Modified(); modified || err != nil { 1176 if err = rlstore.Load(); err != nil { 1177 return nil, err 1178 } 1179 } 1180 var cimage *Image 1181 for _, s := range append([]ROImageStore{istore}, istores...) { 1182 store := s 1183 store.Lock() 1184 defer store.Unlock() 1185 if modified, err := store.Modified(); modified || err != nil { 1186 if err = store.Load(); err != nil { 1187 return nil, err 1188 } 1189 } 1190 cimage, err = store.Get(image) 1191 if err == nil { 1192 imageHomeStore = store 1193 break 1194 } 1195 } 1196 if cimage == nil { 1197 return nil, ErrImageUnknown 1198 } 1199 imageID = cimage.ID 1200 1201 if cimage.TopLayer != "" { 1202 createMappedLayer := imageHomeStore == istore 1203 ilayer, err := s.imageTopLayerForMapping(cimage, imageHomeStore, createMappedLayer, rlstore, lstores, idMappingsOptions) 1204 if err != nil { 1205 return nil, err 1206 } 1207 imageTopLayer = ilayer 1208 1209 if !options.HostUIDMapping && len(options.UIDMap) == 0 { 1210 uidMap = ilayer.UIDMap 1211 } 1212 if !options.HostGIDMapping && len(options.GIDMap) == 0 { 1213 gidMap = ilayer.GIDMap 1214 } 1215 } 1216 } else { 1217 rlstore.Lock() 1218 defer rlstore.Unlock() 1219 if modified, err := rlstore.Modified(); modified || err != nil { 1220 if err = rlstore.Load(); err != nil { 1221 return nil, err 1222 } 1223 } 1224 if !options.HostUIDMapping && len(options.UIDMap) == 0 { 1225 uidMap = s.uidMap 1226 } 1227 if !options.HostGIDMapping && len(options.GIDMap) == 0 { 1228 gidMap = s.gidMap 1229 } 1230 } 1231 var layerOptions *LayerOptions 1232 if s.graphDriver.SupportsShifting() { 1233 layerOptions = &LayerOptions{ 1234 IDMappingOptions: IDMappingOptions{ 1235 HostUIDMapping: true, 1236 HostGIDMapping: true, 1237 UIDMap: nil, 1238 GIDMap: nil, 1239 }, 1240 } 1241 } else { 1242 layerOptions = &LayerOptions{ 1243 IDMappingOptions: IDMappingOptions{ 1244 HostUIDMapping: idMappingsOptions.HostUIDMapping, 1245 HostGIDMapping: idMappingsOptions.HostGIDMapping, 1246 UIDMap: copyIDMap(uidMap), 1247 GIDMap: copyIDMap(gidMap), 1248 }, 1249 } 1250 } 1251 if options.Flags == nil { 1252 options.Flags = make(map[string]interface{}) 1253 } 1254 plabel, _ := options.Flags["ProcessLabel"].(string) 1255 mlabel, _ := options.Flags["MountLabel"].(string) 1256 if (plabel == "" && mlabel != "") || 1257 (plabel != "" && mlabel == "") { 1258 return nil, errors.Errorf("ProcessLabel and Mountlabel must either not be specified or both specified") 1259 } 1260 1261 if plabel == "" { 1262 processLabel, mountLabel, err := label.InitLabels(options.LabelOpts) 1263 if err != nil { 1264 return nil, err 1265 } 1266 options.Flags["ProcessLabel"] = processLabel 1267 options.Flags["MountLabel"] = mountLabel 1268 } 1269 1270 clayer, err := rlstore.Create(layer, imageTopLayer, nil, options.Flags["MountLabel"].(string), nil, layerOptions, true) 1271 if err != nil { 1272 return nil, err 1273 } 1274 layer = clayer.ID 1275 rcstore, err := s.ContainerStore() 1276 if err != nil { 1277 return nil, err 1278 } 1279 rcstore.Lock() 1280 defer rcstore.Unlock() 1281 if modified, err := rcstore.Modified(); modified || err != nil { 1282 if err = rcstore.Load(); err != nil { 1283 return nil, err 1284 } 1285 } 1286 options.IDMappingOptions = IDMappingOptions{ 1287 HostUIDMapping: len(options.UIDMap) == 0, 1288 HostGIDMapping: len(options.GIDMap) == 0, 1289 UIDMap: copyIDMap(options.UIDMap), 1290 GIDMap: copyIDMap(options.GIDMap), 1291 } 1292 container, err := rcstore.Create(id, names, imageID, layer, metadata, options) 1293 if err != nil || container == nil { 1294 rlstore.Delete(layer) 1295 } 1296 return container, err 1297 } 1298 1299 func (s *store) SetMetadata(id, metadata string) error { 1300 rlstore, err := s.LayerStore() 1301 if err != nil { 1302 return err 1303 } 1304 ristore, err := s.ImageStore() 1305 if err != nil { 1306 return err 1307 } 1308 rcstore, err := s.ContainerStore() 1309 if err != nil { 1310 return err 1311 } 1312 1313 rlstore.Lock() 1314 defer rlstore.Unlock() 1315 if modified, err := rlstore.Modified(); modified || err != nil { 1316 if err = rlstore.Load(); err != nil { 1317 return err 1318 } 1319 } 1320 ristore.Lock() 1321 defer ristore.Unlock() 1322 if modified, err := ristore.Modified(); modified || err != nil { 1323 if err := ristore.Load(); err != nil { 1324 return err 1325 } 1326 } 1327 rcstore.Lock() 1328 defer rcstore.Unlock() 1329 if modified, err := rcstore.Modified(); modified || err != nil { 1330 if err = rcstore.Load(); err != nil { 1331 return err 1332 } 1333 } 1334 1335 if rlstore.Exists(id) { 1336 return rlstore.SetMetadata(id, metadata) 1337 } 1338 if ristore.Exists(id) { 1339 return ristore.SetMetadata(id, metadata) 1340 } 1341 if rcstore.Exists(id) { 1342 return rcstore.SetMetadata(id, metadata) 1343 } 1344 return ErrNotAnID 1345 } 1346 1347 func (s *store) Metadata(id string) (string, error) { 1348 lstore, err := s.LayerStore() 1349 if err != nil { 1350 return "", err 1351 } 1352 lstores, err := s.ROLayerStores() 1353 if err != nil { 1354 return "", err 1355 } 1356 for _, s := range append([]ROLayerStore{lstore}, lstores...) { 1357 store := s 1358 store.RLock() 1359 defer store.Unlock() 1360 if modified, err := store.Modified(); modified || err != nil { 1361 if err = store.Load(); err != nil { 1362 return "", err 1363 } 1364 } 1365 if store.Exists(id) { 1366 return store.Metadata(id) 1367 } 1368 } 1369 1370 istore, err := s.ImageStore() 1371 if err != nil { 1372 return "", err 1373 } 1374 istores, err := s.ROImageStores() 1375 if err != nil { 1376 return "", err 1377 } 1378 for _, s := range append([]ROImageStore{istore}, istores...) { 1379 store := s 1380 store.RLock() 1381 defer store.Unlock() 1382 if modified, err := store.Modified(); modified || err != nil { 1383 if err = store.Load(); err != nil { 1384 return "", err 1385 } 1386 } 1387 if store.Exists(id) { 1388 return store.Metadata(id) 1389 } 1390 } 1391 1392 cstore, err := s.ContainerStore() 1393 if err != nil { 1394 return "", err 1395 } 1396 cstore.RLock() 1397 defer cstore.Unlock() 1398 if modified, err := cstore.Modified(); modified || err != nil { 1399 if err = cstore.Load(); err != nil { 1400 return "", err 1401 } 1402 } 1403 if cstore.Exists(id) { 1404 return cstore.Metadata(id) 1405 } 1406 return "", ErrNotAnID 1407 } 1408 1409 func (s *store) ListImageBigData(id string) ([]string, error) { 1410 istore, err := s.ImageStore() 1411 if err != nil { 1412 return nil, err 1413 } 1414 istores, err := s.ROImageStores() 1415 if err != nil { 1416 return nil, err 1417 } 1418 for _, s := range append([]ROImageStore{istore}, istores...) { 1419 store := s 1420 store.RLock() 1421 defer store.Unlock() 1422 if modified, err := store.Modified(); modified || err != nil { 1423 if err = store.Load(); err != nil { 1424 return nil, err 1425 } 1426 } 1427 bigDataNames, err := store.BigDataNames(id) 1428 if err == nil { 1429 return bigDataNames, err 1430 } 1431 } 1432 return nil, ErrImageUnknown 1433 } 1434 1435 func (s *store) ImageBigDataSize(id, key string) (int64, error) { 1436 istore, err := s.ImageStore() 1437 if err != nil { 1438 return -1, err 1439 } 1440 istores, err := s.ROImageStores() 1441 if err != nil { 1442 return -1, err 1443 } 1444 for _, s := range append([]ROImageStore{istore}, istores...) { 1445 store := s 1446 store.RLock() 1447 defer store.Unlock() 1448 if modified, err := store.Modified(); modified || err != nil { 1449 if err = store.Load(); err != nil { 1450 return -1, err 1451 } 1452 } 1453 size, err := store.BigDataSize(id, key) 1454 if err == nil { 1455 return size, nil 1456 } 1457 } 1458 return -1, ErrSizeUnknown 1459 } 1460 1461 func (s *store) ImageBigDataDigest(id, key string) (digest.Digest, error) { 1462 ristore, err := s.ImageStore() 1463 if err != nil { 1464 return "", err 1465 } 1466 stores, err := s.ROImageStores() 1467 if err != nil { 1468 return "", err 1469 } 1470 stores = append([]ROImageStore{ristore}, stores...) 1471 for _, r := range stores { 1472 ristore := r 1473 ristore.RLock() 1474 defer ristore.Unlock() 1475 if modified, err := ristore.Modified(); modified || err != nil { 1476 if err = ristore.Load(); err != nil { 1477 return "", nil 1478 } 1479 } 1480 d, err := ristore.BigDataDigest(id, key) 1481 if err == nil && d.Validate() == nil { 1482 return d, nil 1483 } 1484 } 1485 return "", ErrDigestUnknown 1486 } 1487 1488 func (s *store) ImageBigData(id, key string) ([]byte, error) { 1489 istore, err := s.ImageStore() 1490 if err != nil { 1491 return nil, err 1492 } 1493 istores, err := s.ROImageStores() 1494 if err != nil { 1495 return nil, err 1496 } 1497 for _, s := range append([]ROImageStore{istore}, istores...) { 1498 store := s 1499 store.RLock() 1500 defer store.Unlock() 1501 if modified, err := store.Modified(); modified || err != nil { 1502 if err = store.Load(); err != nil { 1503 return nil, err 1504 } 1505 } 1506 data, err := store.BigData(id, key) 1507 if err == nil { 1508 return data, nil 1509 } 1510 } 1511 return nil, ErrImageUnknown 1512 } 1513 1514 func (s *store) SetImageBigData(id, key string, data []byte, digestManifest func([]byte) (digest.Digest, error)) error { 1515 ristore, err := s.ImageStore() 1516 if err != nil { 1517 return err 1518 } 1519 1520 ristore.Lock() 1521 defer ristore.Unlock() 1522 if modified, err := ristore.Modified(); modified || err != nil { 1523 if err = ristore.Load(); err != nil { 1524 return nil 1525 } 1526 } 1527 1528 return ristore.SetBigData(id, key, data, digestManifest) 1529 } 1530 1531 func (s *store) ImageSize(id string) (int64, error) { 1532 var image *Image 1533 1534 lstore, err := s.LayerStore() 1535 if err != nil { 1536 return -1, errors.Wrapf(err, "error loading primary layer store data") 1537 } 1538 lstores, err := s.ROLayerStores() 1539 if err != nil { 1540 return -1, errors.Wrapf(err, "error loading additional layer stores") 1541 } 1542 for _, s := range append([]ROLayerStore{lstore}, lstores...) { 1543 store := s 1544 store.RLock() 1545 defer store.Unlock() 1546 if modified, err := store.Modified(); modified || err != nil { 1547 if err = store.Load(); err != nil { 1548 return -1, err 1549 } 1550 } 1551 } 1552 1553 var imageStore ROBigDataStore 1554 istore, err := s.ImageStore() 1555 if err != nil { 1556 return -1, errors.Wrapf(err, "error loading primary image store data") 1557 } 1558 istores, err := s.ROImageStores() 1559 if err != nil { 1560 return -1, errors.Wrapf(err, "error loading additional image stores") 1561 } 1562 1563 // Look for the image's record. 1564 for _, s := range append([]ROImageStore{istore}, istores...) { 1565 store := s 1566 store.RLock() 1567 defer store.Unlock() 1568 if modified, err := store.Modified(); modified || err != nil { 1569 if err = store.Load(); err != nil { 1570 return -1, err 1571 } 1572 } 1573 if image, err = store.Get(id); err == nil { 1574 imageStore = store 1575 break 1576 } 1577 } 1578 if image == nil { 1579 return -1, errors.Wrapf(ErrImageUnknown, "error locating image with ID %q", id) 1580 } 1581 1582 // Start with a list of the image's top layers. 1583 queue := make(map[string]struct{}) 1584 for _, layerID := range append([]string{image.TopLayer}, image.MappedTopLayers...) { 1585 queue[layerID] = struct{}{} 1586 } 1587 visited := make(map[string]struct{}) 1588 // Walk all of the layers. 1589 var size int64 1590 for len(visited) < len(queue) { 1591 for layerID := range queue { 1592 // Visit each layer only once. 1593 if _, ok := visited[layerID]; ok { 1594 continue 1595 } 1596 visited[layerID] = struct{}{} 1597 // Look for the layer and the store that knows about it. 1598 var layerStore ROLayerStore 1599 var layer *Layer 1600 for _, store := range append([]ROLayerStore{lstore}, lstores...) { 1601 if layer, err = store.Get(layerID); err == nil { 1602 layerStore = store 1603 break 1604 } 1605 } 1606 if layer == nil { 1607 return -1, errors.Wrapf(ErrLayerUnknown, "error locating layer with ID %q", layerID) 1608 } 1609 // The UncompressedSize is only valid if there's a digest to go with it. 1610 n := layer.UncompressedSize 1611 if layer.UncompressedDigest == "" { 1612 // Compute the size. 1613 n, err = layerStore.DiffSize("", layer.ID) 1614 if err != nil { 1615 return -1, errors.Wrapf(err, "size/digest of layer with ID %q could not be calculated", layerID) 1616 } 1617 } 1618 // Count this layer. 1619 size += n 1620 // Make a note to visit the layer's parent if we haven't already. 1621 if layer.Parent != "" { 1622 queue[layer.Parent] = struct{}{} 1623 } 1624 } 1625 } 1626 1627 // Count big data items. 1628 names, err := imageStore.BigDataNames(id) 1629 if err != nil { 1630 return -1, errors.Wrapf(err, "error reading list of big data items for image %q", id) 1631 } 1632 for _, name := range names { 1633 n, err := imageStore.BigDataSize(id, name) 1634 if err != nil { 1635 return -1, errors.Wrapf(err, "error reading size of big data item %q for image %q", name, id) 1636 } 1637 size += n 1638 } 1639 1640 return size, nil 1641 } 1642 1643 func (s *store) ContainerSize(id string) (int64, error) { 1644 lstore, err := s.LayerStore() 1645 if err != nil { 1646 return -1, err 1647 } 1648 lstores, err := s.ROLayerStores() 1649 if err != nil { 1650 return -1, err 1651 } 1652 for _, s := range append([]ROLayerStore{lstore}, lstores...) { 1653 store := s 1654 store.RLock() 1655 defer store.Unlock() 1656 if modified, err := store.Modified(); modified || err != nil { 1657 if err = store.Load(); err != nil { 1658 return -1, err 1659 } 1660 } 1661 } 1662 1663 // Get the location of the container directory and container run directory. 1664 // Do it before we lock the container store because they do, too. 1665 cdir, err := s.ContainerDirectory(id) 1666 if err != nil { 1667 return -1, err 1668 } 1669 rdir, err := s.ContainerRunDirectory(id) 1670 if err != nil { 1671 return -1, err 1672 } 1673 1674 rcstore, err := s.ContainerStore() 1675 if err != nil { 1676 return -1, err 1677 } 1678 rcstore.RLock() 1679 defer rcstore.Unlock() 1680 if modified, err := rcstore.Modified(); modified || err != nil { 1681 if err = rcstore.Load(); err != nil { 1682 return -1, err 1683 } 1684 } 1685 1686 // Read the container record. 1687 container, err := rcstore.Get(id) 1688 if err != nil { 1689 return -1, err 1690 } 1691 1692 // Read the container's layer's size. 1693 var layer *Layer 1694 var size int64 1695 for _, store := range append([]ROLayerStore{lstore}, lstores...) { 1696 if layer, err = store.Get(container.LayerID); err == nil { 1697 size, err = store.DiffSize("", layer.ID) 1698 if err != nil { 1699 return -1, errors.Wrapf(err, "error determining size of layer with ID %q", layer.ID) 1700 } 1701 break 1702 } 1703 } 1704 if layer == nil { 1705 return -1, errors.Wrapf(ErrLayerUnknown, "error locating layer with ID %q", container.LayerID) 1706 } 1707 1708 // Count big data items. 1709 names, err := rcstore.BigDataNames(id) 1710 if err != nil { 1711 return -1, errors.Wrapf(err, "error reading list of big data items for container %q", container.ID) 1712 } 1713 for _, name := range names { 1714 n, err := rcstore.BigDataSize(id, name) 1715 if err != nil { 1716 return -1, errors.Wrapf(err, "error reading size of big data item %q for container %q", name, id) 1717 } 1718 size += n 1719 } 1720 1721 // Count the size of our container directory and container run directory. 1722 n, err := directory.Size(cdir) 1723 if err != nil { 1724 return -1, err 1725 } 1726 size += n 1727 n, err = directory.Size(rdir) 1728 if err != nil { 1729 return -1, err 1730 } 1731 size += n 1732 1733 return size, nil 1734 } 1735 1736 func (s *store) ListContainerBigData(id string) ([]string, error) { 1737 rcstore, err := s.ContainerStore() 1738 if err != nil { 1739 return nil, err 1740 } 1741 1742 rcstore.RLock() 1743 defer rcstore.Unlock() 1744 if modified, err := rcstore.Modified(); modified || err != nil { 1745 if err = rcstore.Load(); err != nil { 1746 return nil, err 1747 } 1748 } 1749 1750 return rcstore.BigDataNames(id) 1751 } 1752 1753 func (s *store) ContainerBigDataSize(id, key string) (int64, error) { 1754 rcstore, err := s.ContainerStore() 1755 if err != nil { 1756 return -1, err 1757 } 1758 rcstore.RLock() 1759 defer rcstore.Unlock() 1760 if modified, err := rcstore.Modified(); modified || err != nil { 1761 if err = rcstore.Load(); err != nil { 1762 return -1, err 1763 } 1764 } 1765 return rcstore.BigDataSize(id, key) 1766 } 1767 1768 func (s *store) ContainerBigDataDigest(id, key string) (digest.Digest, error) { 1769 rcstore, err := s.ContainerStore() 1770 if err != nil { 1771 return "", err 1772 } 1773 rcstore.RLock() 1774 defer rcstore.Unlock() 1775 if modified, err := rcstore.Modified(); modified || err != nil { 1776 if err = rcstore.Load(); err != nil { 1777 return "", err 1778 } 1779 } 1780 return rcstore.BigDataDigest(id, key) 1781 } 1782 1783 func (s *store) ContainerBigData(id, key string) ([]byte, error) { 1784 rcstore, err := s.ContainerStore() 1785 if err != nil { 1786 return nil, err 1787 } 1788 rcstore.RLock() 1789 defer rcstore.Unlock() 1790 if modified, err := rcstore.Modified(); modified || err != nil { 1791 if err = rcstore.Load(); err != nil { 1792 return nil, err 1793 } 1794 } 1795 return rcstore.BigData(id, key) 1796 } 1797 1798 func (s *store) SetContainerBigData(id, key string, data []byte) error { 1799 rcstore, err := s.ContainerStore() 1800 if err != nil { 1801 return err 1802 } 1803 rcstore.Lock() 1804 defer rcstore.Unlock() 1805 if modified, err := rcstore.Modified(); modified || err != nil { 1806 if err = rcstore.Load(); err != nil { 1807 return err 1808 } 1809 } 1810 return rcstore.SetBigData(id, key, data) 1811 } 1812 1813 func (s *store) Exists(id string) bool { 1814 lstore, err := s.LayerStore() 1815 if err != nil { 1816 return false 1817 } 1818 lstores, err := s.ROLayerStores() 1819 if err != nil { 1820 return false 1821 } 1822 for _, s := range append([]ROLayerStore{lstore}, lstores...) { 1823 store := s 1824 store.RLock() 1825 defer store.Unlock() 1826 if modified, err := store.Modified(); modified || err != nil { 1827 if err = store.Load(); err != nil { 1828 return false 1829 } 1830 } 1831 if store.Exists(id) { 1832 return true 1833 } 1834 } 1835 1836 istore, err := s.ImageStore() 1837 if err != nil { 1838 return false 1839 } 1840 istores, err := s.ROImageStores() 1841 if err != nil { 1842 return false 1843 } 1844 for _, s := range append([]ROImageStore{istore}, istores...) { 1845 store := s 1846 store.RLock() 1847 defer store.Unlock() 1848 if modified, err := store.Modified(); modified || err != nil { 1849 if err = store.Load(); err != nil { 1850 return false 1851 } 1852 } 1853 if store.Exists(id) { 1854 return true 1855 } 1856 } 1857 1858 rcstore, err := s.ContainerStore() 1859 if err != nil { 1860 return false 1861 } 1862 rcstore.RLock() 1863 defer rcstore.Unlock() 1864 if modified, err := rcstore.Modified(); modified || err != nil { 1865 if err = rcstore.Load(); err != nil { 1866 return false 1867 } 1868 } 1869 if rcstore.Exists(id) { 1870 return true 1871 } 1872 1873 return false 1874 } 1875 1876 func dedupeNames(names []string) []string { 1877 seen := make(map[string]bool) 1878 deduped := make([]string, 0, len(names)) 1879 for _, name := range names { 1880 if _, wasSeen := seen[name]; !wasSeen { 1881 seen[name] = true 1882 deduped = append(deduped, name) 1883 } 1884 } 1885 return deduped 1886 } 1887 1888 func (s *store) SetNames(id string, names []string) error { 1889 deduped := dedupeNames(names) 1890 1891 rlstore, err := s.LayerStore() 1892 if err != nil { 1893 return err 1894 } 1895 rlstore.Lock() 1896 defer rlstore.Unlock() 1897 if modified, err := rlstore.Modified(); modified || err != nil { 1898 if err = rlstore.Load(); err != nil { 1899 return err 1900 } 1901 } 1902 if rlstore.Exists(id) { 1903 return rlstore.SetNames(id, deduped) 1904 } 1905 1906 ristore, err := s.ImageStore() 1907 if err != nil { 1908 return err 1909 } 1910 ristore.Lock() 1911 defer ristore.Unlock() 1912 if modified, err := ristore.Modified(); modified || err != nil { 1913 if err = ristore.Load(); err != nil { 1914 return err 1915 } 1916 } 1917 if ristore.Exists(id) { 1918 return ristore.SetNames(id, deduped) 1919 } 1920 1921 rcstore, err := s.ContainerStore() 1922 if err != nil { 1923 return err 1924 } 1925 rcstore.Lock() 1926 defer rcstore.Unlock() 1927 if modified, err := rcstore.Modified(); modified || err != nil { 1928 if err = rcstore.Load(); err != nil { 1929 return err 1930 } 1931 } 1932 if rcstore.Exists(id) { 1933 return rcstore.SetNames(id, deduped) 1934 } 1935 return ErrLayerUnknown 1936 } 1937 1938 func (s *store) Names(id string) ([]string, error) { 1939 lstore, err := s.LayerStore() 1940 if err != nil { 1941 return nil, err 1942 } 1943 lstores, err := s.ROLayerStores() 1944 if err != nil { 1945 return nil, err 1946 } 1947 for _, s := range append([]ROLayerStore{lstore}, lstores...) { 1948 store := s 1949 store.RLock() 1950 defer store.Unlock() 1951 if modified, err := store.Modified(); modified || err != nil { 1952 if err = store.Load(); err != nil { 1953 return nil, err 1954 } 1955 } 1956 if l, err := store.Get(id); l != nil && err == nil { 1957 return l.Names, nil 1958 } 1959 } 1960 1961 istore, err := s.ImageStore() 1962 if err != nil { 1963 return nil, err 1964 } 1965 istores, err := s.ROImageStores() 1966 if err != nil { 1967 return nil, err 1968 } 1969 for _, s := range append([]ROImageStore{istore}, istores...) { 1970 store := s 1971 store.RLock() 1972 defer store.Unlock() 1973 if modified, err := store.Modified(); modified || err != nil { 1974 if err = store.Load(); err != nil { 1975 return nil, err 1976 } 1977 } 1978 if i, err := store.Get(id); i != nil && err == nil { 1979 return i.Names, nil 1980 } 1981 } 1982 1983 rcstore, err := s.ContainerStore() 1984 if err != nil { 1985 return nil, err 1986 } 1987 rcstore.RLock() 1988 defer rcstore.Unlock() 1989 if modified, err := rcstore.Modified(); modified || err != nil { 1990 if err = rcstore.Load(); err != nil { 1991 return nil, err 1992 } 1993 } 1994 if c, err := rcstore.Get(id); c != nil && err == nil { 1995 return c.Names, nil 1996 } 1997 return nil, ErrLayerUnknown 1998 } 1999 2000 func (s *store) Lookup(name string) (string, error) { 2001 lstore, err := s.LayerStore() 2002 if err != nil { 2003 return "", err 2004 } 2005 lstores, err := s.ROLayerStores() 2006 if err != nil { 2007 return "", err 2008 } 2009 for _, s := range append([]ROLayerStore{lstore}, lstores...) { 2010 store := s 2011 store.RLock() 2012 defer store.Unlock() 2013 if modified, err := store.Modified(); modified || err != nil { 2014 if err = store.Load(); err != nil { 2015 return "", err 2016 } 2017 } 2018 if l, err := store.Get(name); l != nil && err == nil { 2019 return l.ID, nil 2020 } 2021 } 2022 2023 istore, err := s.ImageStore() 2024 if err != nil { 2025 return "", err 2026 } 2027 istores, err := s.ROImageStores() 2028 if err != nil { 2029 return "", err 2030 } 2031 for _, s := range append([]ROImageStore{istore}, istores...) { 2032 store := s 2033 store.RLock() 2034 defer store.Unlock() 2035 if modified, err := store.Modified(); modified || err != nil { 2036 if err = store.Load(); err != nil { 2037 return "", err 2038 } 2039 } 2040 if i, err := store.Get(name); i != nil && err == nil { 2041 return i.ID, nil 2042 } 2043 } 2044 2045 cstore, err := s.ContainerStore() 2046 if err != nil { 2047 return "", err 2048 } 2049 cstore.RLock() 2050 defer cstore.Unlock() 2051 if modified, err := cstore.Modified(); modified || err != nil { 2052 if err = cstore.Load(); err != nil { 2053 return "", err 2054 } 2055 } 2056 if c, err := cstore.Get(name); c != nil && err == nil { 2057 return c.ID, nil 2058 } 2059 2060 return "", ErrLayerUnknown 2061 } 2062 2063 func (s *store) DeleteLayer(id string) error { 2064 rlstore, err := s.LayerStore() 2065 if err != nil { 2066 return err 2067 } 2068 ristore, err := s.ImageStore() 2069 if err != nil { 2070 return err 2071 } 2072 rcstore, err := s.ContainerStore() 2073 if err != nil { 2074 return err 2075 } 2076 2077 rlstore.Lock() 2078 defer rlstore.Unlock() 2079 if modified, err := rlstore.Modified(); modified || err != nil { 2080 if err = rlstore.Load(); err != nil { 2081 return err 2082 } 2083 } 2084 ristore.Lock() 2085 defer ristore.Unlock() 2086 if modified, err := ristore.Modified(); modified || err != nil { 2087 if err = ristore.Load(); err != nil { 2088 return err 2089 } 2090 } 2091 rcstore.Lock() 2092 defer rcstore.Unlock() 2093 if modified, err := rcstore.Modified(); modified || err != nil { 2094 if err = rcstore.Load(); err != nil { 2095 return err 2096 } 2097 } 2098 2099 if rlstore.Exists(id) { 2100 if l, err := rlstore.Get(id); err != nil { 2101 id = l.ID 2102 } 2103 layers, err := rlstore.Layers() 2104 if err != nil { 2105 return err 2106 } 2107 for _, layer := range layers { 2108 if layer.Parent == id { 2109 return ErrLayerHasChildren 2110 } 2111 } 2112 images, err := ristore.Images() 2113 if err != nil { 2114 return err 2115 } 2116 for _, image := range images { 2117 if image.TopLayer == id || stringutils.InSlice(image.MappedTopLayers, id) { 2118 return errors.Wrapf(ErrLayerUsedByImage, "Layer %v used by image %v", id, image.ID) 2119 } 2120 } 2121 containers, err := rcstore.Containers() 2122 if err != nil { 2123 return err 2124 } 2125 for _, container := range containers { 2126 if container.LayerID == id { 2127 return errors.Wrapf(ErrLayerUsedByContainer, "Layer %v used by container %v", id, container.ID) 2128 } 2129 } 2130 return rlstore.Delete(id) 2131 } 2132 return ErrNotALayer 2133 } 2134 2135 func (s *store) DeleteImage(id string, commit bool) (layers []string, err error) { 2136 rlstore, err := s.LayerStore() 2137 if err != nil { 2138 return nil, err 2139 } 2140 ristore, err := s.ImageStore() 2141 if err != nil { 2142 return nil, err 2143 } 2144 rcstore, err := s.ContainerStore() 2145 if err != nil { 2146 return nil, err 2147 } 2148 2149 rlstore.Lock() 2150 defer rlstore.Unlock() 2151 if modified, err := rlstore.Modified(); modified || err != nil { 2152 if err = rlstore.Load(); err != nil { 2153 return nil, err 2154 } 2155 } 2156 ristore.Lock() 2157 defer ristore.Unlock() 2158 if modified, err := ristore.Modified(); modified || err != nil { 2159 if err = ristore.Load(); err != nil { 2160 return nil, err 2161 } 2162 } 2163 rcstore.Lock() 2164 defer rcstore.Unlock() 2165 if modified, err := rcstore.Modified(); modified || err != nil { 2166 if err = rcstore.Load(); err != nil { 2167 return nil, err 2168 } 2169 } 2170 layersToRemove := []string{} 2171 if ristore.Exists(id) { 2172 image, err := ristore.Get(id) 2173 if err != nil { 2174 return nil, err 2175 } 2176 id = image.ID 2177 containers, err := rcstore.Containers() 2178 if err != nil { 2179 return nil, err 2180 } 2181 aContainerByImage := make(map[string]string) 2182 for _, container := range containers { 2183 aContainerByImage[container.ImageID] = container.ID 2184 } 2185 if container, ok := aContainerByImage[id]; ok { 2186 return nil, errors.Wrapf(ErrImageUsedByContainer, "Image used by %v", container) 2187 } 2188 images, err := ristore.Images() 2189 if err != nil { 2190 return nil, err 2191 } 2192 layers, err := rlstore.Layers() 2193 if err != nil { 2194 return nil, err 2195 } 2196 childrenByParent := make(map[string]*[]string) 2197 for _, layer := range layers { 2198 parent := layer.Parent 2199 if list, ok := childrenByParent[parent]; ok { 2200 newList := append(*list, layer.ID) 2201 childrenByParent[parent] = &newList 2202 } else { 2203 childrenByParent[parent] = &([]string{layer.ID}) 2204 } 2205 } 2206 otherImagesByTopLayer := make(map[string]string) 2207 for _, img := range images { 2208 if img.ID != id { 2209 otherImagesByTopLayer[img.TopLayer] = img.ID 2210 for _, layerID := range img.MappedTopLayers { 2211 otherImagesByTopLayer[layerID] = img.ID 2212 } 2213 } 2214 } 2215 if commit { 2216 if err = ristore.Delete(id); err != nil { 2217 return nil, err 2218 } 2219 } 2220 layer := image.TopLayer 2221 lastRemoved := "" 2222 for layer != "" { 2223 if rcstore.Exists(layer) { 2224 break 2225 } 2226 if _, ok := otherImagesByTopLayer[layer]; ok { 2227 break 2228 } 2229 parent := "" 2230 if l, err := rlstore.Get(layer); err == nil { 2231 parent = l.Parent 2232 } 2233 hasOtherRefs := func() bool { 2234 layersToCheck := []string{layer} 2235 if layer == image.TopLayer { 2236 layersToCheck = append(layersToCheck, image.MappedTopLayers...) 2237 } 2238 for _, layer := range layersToCheck { 2239 if childList, ok := childrenByParent[layer]; ok && childList != nil { 2240 children := *childList 2241 for _, child := range children { 2242 if child != lastRemoved { 2243 return true 2244 } 2245 } 2246 } 2247 } 2248 return false 2249 } 2250 if hasOtherRefs() { 2251 break 2252 } 2253 lastRemoved = layer 2254 if layer == image.TopLayer { 2255 layersToRemove = append(layersToRemove, image.MappedTopLayers...) 2256 } 2257 layersToRemove = append(layersToRemove, lastRemoved) 2258 layer = parent 2259 } 2260 } else { 2261 return nil, ErrNotAnImage 2262 } 2263 if commit { 2264 for _, layer := range layersToRemove { 2265 if err = rlstore.Delete(layer); err != nil { 2266 return nil, err 2267 } 2268 } 2269 } 2270 return layersToRemove, nil 2271 } 2272 2273 func (s *store) DeleteContainer(id string) error { 2274 rlstore, err := s.LayerStore() 2275 if err != nil { 2276 return err 2277 } 2278 ristore, err := s.ImageStore() 2279 if err != nil { 2280 return err 2281 } 2282 rcstore, err := s.ContainerStore() 2283 if err != nil { 2284 return err 2285 } 2286 2287 rlstore.Lock() 2288 defer rlstore.Unlock() 2289 if modified, err := rlstore.Modified(); modified || err != nil { 2290 if err = rlstore.Load(); err != nil { 2291 return err 2292 } 2293 } 2294 ristore.Lock() 2295 defer ristore.Unlock() 2296 if modified, err := ristore.Modified(); modified || err != nil { 2297 if err = ristore.Load(); err != nil { 2298 return err 2299 } 2300 } 2301 rcstore.Lock() 2302 defer rcstore.Unlock() 2303 if modified, err := rcstore.Modified(); modified || err != nil { 2304 if err = rcstore.Load(); err != nil { 2305 return err 2306 } 2307 } 2308 2309 if rcstore.Exists(id) { 2310 if container, err := rcstore.Get(id); err == nil { 2311 if rlstore.Exists(container.LayerID) { 2312 if err = rlstore.Delete(container.LayerID); err != nil { 2313 return err 2314 } 2315 } 2316 if err = rcstore.Delete(id); err != nil { 2317 return err 2318 } 2319 middleDir := s.graphDriverName + "-containers" 2320 gcpath := filepath.Join(s.GraphRoot(), middleDir, container.ID) 2321 if err = os.RemoveAll(gcpath); err != nil { 2322 return err 2323 } 2324 rcpath := filepath.Join(s.RunRoot(), middleDir, container.ID) 2325 if err = os.RemoveAll(rcpath); err != nil { 2326 return err 2327 } 2328 return nil 2329 } 2330 } 2331 return ErrNotAContainer 2332 } 2333 2334 func (s *store) Delete(id string) error { 2335 rlstore, err := s.LayerStore() 2336 if err != nil { 2337 return err 2338 } 2339 ristore, err := s.ImageStore() 2340 if err != nil { 2341 return err 2342 } 2343 rcstore, err := s.ContainerStore() 2344 if err != nil { 2345 return err 2346 } 2347 2348 rlstore.Lock() 2349 defer rlstore.Unlock() 2350 if modified, err := rlstore.Modified(); modified || err != nil { 2351 if err = rlstore.Load(); err != nil { 2352 return err 2353 } 2354 } 2355 ristore.Lock() 2356 defer ristore.Unlock() 2357 if modified, err := ristore.Modified(); modified || err != nil { 2358 if err := ristore.Load(); err != nil { 2359 return err 2360 } 2361 } 2362 rcstore.Lock() 2363 defer rcstore.Unlock() 2364 if modified, err := rcstore.Modified(); modified || err != nil { 2365 if err = rcstore.Load(); err != nil { 2366 return err 2367 } 2368 } 2369 2370 if rcstore.Exists(id) { 2371 if container, err := rcstore.Get(id); err == nil { 2372 if rlstore.Exists(container.LayerID) { 2373 if err = rlstore.Delete(container.LayerID); err != nil { 2374 return err 2375 } 2376 if err = rcstore.Delete(id); err != nil { 2377 return err 2378 } 2379 middleDir := s.graphDriverName + "-containers" 2380 gcpath := filepath.Join(s.GraphRoot(), middleDir, container.ID, "userdata") 2381 if err = os.RemoveAll(gcpath); err != nil { 2382 return err 2383 } 2384 rcpath := filepath.Join(s.RunRoot(), middleDir, container.ID, "userdata") 2385 if err = os.RemoveAll(rcpath); err != nil { 2386 return err 2387 } 2388 return nil 2389 } 2390 return ErrNotALayer 2391 } 2392 } 2393 if ristore.Exists(id) { 2394 return ristore.Delete(id) 2395 } 2396 if rlstore.Exists(id) { 2397 return rlstore.Delete(id) 2398 } 2399 return ErrLayerUnknown 2400 } 2401 2402 func (s *store) Wipe() error { 2403 rcstore, err := s.ContainerStore() 2404 if err != nil { 2405 return err 2406 } 2407 ristore, err := s.ImageStore() 2408 if err != nil { 2409 return err 2410 } 2411 rlstore, err := s.LayerStore() 2412 if err != nil { 2413 return err 2414 } 2415 2416 rlstore.Lock() 2417 defer rlstore.Unlock() 2418 if modified, err := rlstore.Modified(); modified || err != nil { 2419 if err = rlstore.Load(); err != nil { 2420 return err 2421 } 2422 } 2423 ristore.Lock() 2424 defer ristore.Unlock() 2425 if modified, err := ristore.Modified(); modified || err != nil { 2426 if err = ristore.Load(); err != nil { 2427 return err 2428 } 2429 } 2430 rcstore.Lock() 2431 defer rcstore.Unlock() 2432 if modified, err := rcstore.Modified(); modified || err != nil { 2433 if err = rcstore.Load(); err != nil { 2434 return err 2435 } 2436 } 2437 2438 if err = rcstore.Wipe(); err != nil { 2439 return err 2440 } 2441 if err = ristore.Wipe(); err != nil { 2442 return err 2443 } 2444 return rlstore.Wipe() 2445 } 2446 2447 func (s *store) Status() ([][2]string, error) { 2448 rlstore, err := s.LayerStore() 2449 if err != nil { 2450 return nil, err 2451 } 2452 return rlstore.Status() 2453 } 2454 2455 func (s *store) Version() ([][2]string, error) { 2456 return [][2]string{}, nil 2457 } 2458 2459 func (s *store) Mount(id, mountLabel string) (string, error) { 2460 container, err := s.Container(id) 2461 var ( 2462 uidMap, gidMap []idtools.IDMap 2463 mountOpts []string 2464 ) 2465 if err == nil { 2466 uidMap, gidMap = container.UIDMap, container.GIDMap 2467 id = container.LayerID 2468 mountOpts = container.MountOpts() 2469 } 2470 rlstore, err := s.LayerStore() 2471 if err != nil { 2472 return "", err 2473 } 2474 rlstore.Lock() 2475 defer rlstore.Unlock() 2476 if modified, err := rlstore.Modified(); modified || err != nil { 2477 if err = rlstore.Load(); err != nil { 2478 return "", err 2479 } 2480 } 2481 if rlstore.Exists(id) { 2482 options := drivers.MountOpts{ 2483 MountLabel: mountLabel, 2484 UidMaps: uidMap, 2485 GidMaps: gidMap, 2486 Options: mountOpts, 2487 } 2488 return rlstore.Mount(id, options) 2489 } 2490 return "", ErrLayerUnknown 2491 } 2492 2493 func (s *store) Mounted(id string) (int, error) { 2494 if layerID, err := s.ContainerLayerID(id); err == nil { 2495 id = layerID 2496 } 2497 rlstore, err := s.LayerStore() 2498 if err != nil { 2499 return 0, err 2500 } 2501 rlstore.RLock() 2502 defer rlstore.Unlock() 2503 if modified, err := rlstore.Modified(); modified || err != nil { 2504 if err = rlstore.Load(); err != nil { 2505 return 0, err 2506 } 2507 } 2508 2509 return rlstore.Mounted(id) 2510 } 2511 2512 func (s *store) Unmount(id string, force bool) (bool, error) { 2513 if layerID, err := s.ContainerLayerID(id); err == nil { 2514 id = layerID 2515 } 2516 rlstore, err := s.LayerStore() 2517 if err != nil { 2518 return false, err 2519 } 2520 rlstore.Lock() 2521 defer rlstore.Unlock() 2522 if modified, err := rlstore.Modified(); modified || err != nil { 2523 if err = rlstore.Load(); err != nil { 2524 return false, err 2525 } 2526 } 2527 if rlstore.Exists(id) { 2528 return rlstore.Unmount(id, force) 2529 } 2530 return false, ErrLayerUnknown 2531 } 2532 2533 func (s *store) Changes(from, to string) ([]archive.Change, error) { 2534 lstore, err := s.LayerStore() 2535 if err != nil { 2536 return nil, err 2537 } 2538 lstores, err := s.ROLayerStores() 2539 if err != nil { 2540 return nil, err 2541 } 2542 for _, s := range append([]ROLayerStore{lstore}, lstores...) { 2543 store := s 2544 store.RLock() 2545 defer store.Unlock() 2546 if modified, err := store.Modified(); modified || err != nil { 2547 if err = store.Load(); err != nil { 2548 return nil, err 2549 } 2550 } 2551 if store.Exists(to) { 2552 return store.Changes(from, to) 2553 } 2554 } 2555 return nil, ErrLayerUnknown 2556 } 2557 2558 func (s *store) DiffSize(from, to string) (int64, error) { 2559 lstore, err := s.LayerStore() 2560 if err != nil { 2561 return -1, err 2562 } 2563 lstores, err := s.ROLayerStores() 2564 if err != nil { 2565 return -1, err 2566 } 2567 for _, s := range append([]ROLayerStore{lstore}, lstores...) { 2568 store := s 2569 store.RLock() 2570 defer store.Unlock() 2571 if modified, err := store.Modified(); modified || err != nil { 2572 if err = store.Load(); err != nil { 2573 return -1, err 2574 } 2575 } 2576 if store.Exists(to) { 2577 return store.DiffSize(from, to) 2578 } 2579 } 2580 return -1, ErrLayerUnknown 2581 } 2582 2583 func (s *store) Diff(from, to string, options *DiffOptions) (io.ReadCloser, error) { 2584 lstore, err := s.LayerStore() 2585 if err != nil { 2586 return nil, err 2587 } 2588 lstores, err := s.ROLayerStores() 2589 if err != nil { 2590 return nil, err 2591 } 2592 for _, s := range append([]ROLayerStore{lstore}, lstores...) { 2593 store := s 2594 store.RLock() 2595 if modified, err := store.Modified(); modified || err != nil { 2596 if err = store.Load(); err != nil { 2597 return nil, err 2598 } 2599 } 2600 if store.Exists(to) { 2601 rc, err := store.Diff(from, to, options) 2602 if rc != nil && err == nil { 2603 wrapped := ioutils.NewReadCloserWrapper(rc, func() error { 2604 err := rc.Close() 2605 store.Unlock() 2606 return err 2607 }) 2608 return wrapped, nil 2609 } 2610 store.Unlock() 2611 return rc, err 2612 } 2613 store.Unlock() 2614 } 2615 return nil, ErrLayerUnknown 2616 } 2617 2618 func (s *store) ApplyDiff(to string, diff io.Reader) (int64, error) { 2619 rlstore, err := s.LayerStore() 2620 if err != nil { 2621 return -1, err 2622 } 2623 rlstore.Lock() 2624 defer rlstore.Unlock() 2625 if modified, err := rlstore.Modified(); modified || err != nil { 2626 if err = rlstore.Load(); err != nil { 2627 return -1, err 2628 } 2629 } 2630 if rlstore.Exists(to) { 2631 return rlstore.ApplyDiff(to, diff) 2632 } 2633 return -1, ErrLayerUnknown 2634 } 2635 2636 func (s *store) layersByMappedDigest(m func(ROLayerStore, digest.Digest) ([]Layer, error), d digest.Digest) ([]Layer, error) { 2637 var layers []Layer 2638 lstore, err := s.LayerStore() 2639 if err != nil { 2640 return nil, err 2641 } 2642 2643 lstores, err := s.ROLayerStores() 2644 if err != nil { 2645 return nil, err 2646 } 2647 for _, s := range append([]ROLayerStore{lstore}, lstores...) { 2648 store := s 2649 store.RLock() 2650 defer store.Unlock() 2651 if modified, err := store.Modified(); modified || err != nil { 2652 if err = store.Load(); err != nil { 2653 return nil, err 2654 } 2655 } 2656 storeLayers, err := m(store, d) 2657 if err != nil { 2658 if errors.Cause(err) != ErrLayerUnknown { 2659 return nil, err 2660 } 2661 continue 2662 } 2663 layers = append(layers, storeLayers...) 2664 } 2665 if len(layers) == 0 { 2666 return nil, ErrLayerUnknown 2667 } 2668 return layers, nil 2669 } 2670 2671 func (s *store) LayersByCompressedDigest(d digest.Digest) ([]Layer, error) { 2672 if err := d.Validate(); err != nil { 2673 return nil, errors.Wrapf(err, "error looking for compressed layers matching digest %q", d) 2674 } 2675 return s.layersByMappedDigest(func(r ROLayerStore, d digest.Digest) ([]Layer, error) { return r.LayersByCompressedDigest(d) }, d) 2676 } 2677 2678 func (s *store) LayersByUncompressedDigest(d digest.Digest) ([]Layer, error) { 2679 if err := d.Validate(); err != nil { 2680 return nil, errors.Wrapf(err, "error looking for layers matching digest %q", d) 2681 } 2682 return s.layersByMappedDigest(func(r ROLayerStore, d digest.Digest) ([]Layer, error) { return r.LayersByUncompressedDigest(d) }, d) 2683 } 2684 2685 func (s *store) LayerSize(id string) (int64, error) { 2686 lstore, err := s.LayerStore() 2687 if err != nil { 2688 return -1, err 2689 } 2690 lstores, err := s.ROLayerStores() 2691 if err != nil { 2692 return -1, err 2693 } 2694 for _, s := range append([]ROLayerStore{lstore}, lstores...) { 2695 store := s 2696 store.RLock() 2697 defer store.Unlock() 2698 if modified, err := store.Modified(); modified || err != nil { 2699 if err = store.Load(); err != nil { 2700 return -1, err 2701 } 2702 } 2703 if store.Exists(id) { 2704 return store.Size(id) 2705 } 2706 } 2707 return -1, ErrLayerUnknown 2708 } 2709 2710 func (s *store) LayerParentOwners(id string) ([]int, []int, error) { 2711 rlstore, err := s.LayerStore() 2712 if err != nil { 2713 return nil, nil, err 2714 } 2715 rlstore.RLock() 2716 defer rlstore.Unlock() 2717 if modified, err := rlstore.Modified(); modified || err != nil { 2718 if err = rlstore.Load(); err != nil { 2719 return nil, nil, err 2720 } 2721 } 2722 if rlstore.Exists(id) { 2723 return rlstore.ParentOwners(id) 2724 } 2725 return nil, nil, ErrLayerUnknown 2726 } 2727 2728 func (s *store) ContainerParentOwners(id string) ([]int, []int, error) { 2729 rlstore, err := s.LayerStore() 2730 if err != nil { 2731 return nil, nil, err 2732 } 2733 rcstore, err := s.ContainerStore() 2734 if err != nil { 2735 return nil, nil, err 2736 } 2737 rlstore.RLock() 2738 defer rlstore.Unlock() 2739 if modified, err := rlstore.Modified(); modified || err != nil { 2740 if err = rlstore.Load(); err != nil { 2741 return nil, nil, err 2742 } 2743 } 2744 rcstore.RLock() 2745 defer rcstore.Unlock() 2746 if modified, err := rcstore.Modified(); modified || err != nil { 2747 if err = rcstore.Load(); err != nil { 2748 return nil, nil, err 2749 } 2750 } 2751 container, err := rcstore.Get(id) 2752 if err != nil { 2753 return nil, nil, err 2754 } 2755 if rlstore.Exists(container.LayerID) { 2756 return rlstore.ParentOwners(container.LayerID) 2757 } 2758 return nil, nil, ErrLayerUnknown 2759 } 2760 2761 func (s *store) Layers() ([]Layer, error) { 2762 var layers []Layer 2763 lstore, err := s.LayerStore() 2764 if err != nil { 2765 return nil, err 2766 } 2767 2768 lstores, err := s.ROLayerStores() 2769 if err != nil { 2770 return nil, err 2771 } 2772 2773 for _, s := range append([]ROLayerStore{lstore}, lstores...) { 2774 store := s 2775 store.RLock() 2776 defer store.Unlock() 2777 if modified, err := store.Modified(); modified || err != nil { 2778 if err = store.Load(); err != nil { 2779 return nil, err 2780 } 2781 } 2782 storeLayers, err := store.Layers() 2783 if err != nil { 2784 return nil, err 2785 } 2786 layers = append(layers, storeLayers...) 2787 } 2788 return layers, nil 2789 } 2790 2791 func (s *store) Images() ([]Image, error) { 2792 var images []Image 2793 istore, err := s.ImageStore() 2794 if err != nil { 2795 return nil, err 2796 } 2797 2798 istores, err := s.ROImageStores() 2799 if err != nil { 2800 return nil, err 2801 } 2802 for _, s := range append([]ROImageStore{istore}, istores...) { 2803 store := s 2804 store.RLock() 2805 defer store.Unlock() 2806 if modified, err := store.Modified(); modified || err != nil { 2807 if err = store.Load(); err != nil { 2808 return nil, err 2809 } 2810 } 2811 storeImages, err := store.Images() 2812 if err != nil { 2813 return nil, err 2814 } 2815 images = append(images, storeImages...) 2816 } 2817 return images, nil 2818 } 2819 2820 func (s *store) Containers() ([]Container, error) { 2821 rcstore, err := s.ContainerStore() 2822 if err != nil { 2823 return nil, err 2824 } 2825 2826 rcstore.RLock() 2827 defer rcstore.Unlock() 2828 if modified, err := rcstore.Modified(); modified || err != nil { 2829 if err = rcstore.Load(); err != nil { 2830 return nil, err 2831 } 2832 } 2833 2834 return rcstore.Containers() 2835 } 2836 2837 func (s *store) Layer(id string) (*Layer, error) { 2838 lstore, err := s.LayerStore() 2839 if err != nil { 2840 return nil, err 2841 } 2842 lstores, err := s.ROLayerStores() 2843 if err != nil { 2844 return nil, err 2845 } 2846 for _, s := range append([]ROLayerStore{lstore}, lstores...) { 2847 store := s 2848 store.RLock() 2849 defer store.Unlock() 2850 if modified, err := store.Modified(); modified || err != nil { 2851 if err = store.Load(); err != nil { 2852 return nil, err 2853 } 2854 } 2855 layer, err := store.Get(id) 2856 if err == nil { 2857 return layer, nil 2858 } 2859 } 2860 return nil, ErrLayerUnknown 2861 } 2862 2863 func (s *store) Image(id string) (*Image, error) { 2864 istore, err := s.ImageStore() 2865 if err != nil { 2866 return nil, err 2867 } 2868 istores, err := s.ROImageStores() 2869 if err != nil { 2870 return nil, err 2871 } 2872 for _, s := range append([]ROImageStore{istore}, istores...) { 2873 store := s 2874 store.RLock() 2875 defer store.Unlock() 2876 if modified, err := store.Modified(); modified || err != nil { 2877 if err = store.Load(); err != nil { 2878 return nil, err 2879 } 2880 } 2881 image, err := store.Get(id) 2882 if err == nil { 2883 return image, nil 2884 } 2885 } 2886 return nil, ErrImageUnknown 2887 } 2888 2889 func (s *store) ImagesByTopLayer(id string) ([]*Image, error) { 2890 images := []*Image{} 2891 layer, err := s.Layer(id) 2892 if err != nil { 2893 return nil, err 2894 } 2895 2896 istore, err := s.ImageStore() 2897 if err != nil { 2898 return nil, err 2899 } 2900 2901 istores, err := s.ROImageStores() 2902 if err != nil { 2903 return nil, err 2904 } 2905 for _, s := range append([]ROImageStore{istore}, istores...) { 2906 store := s 2907 store.RLock() 2908 defer store.Unlock() 2909 if modified, err := store.Modified(); modified || err != nil { 2910 if err = store.Load(); err != nil { 2911 return nil, err 2912 } 2913 } 2914 imageList, err := store.Images() 2915 if err != nil { 2916 return nil, err 2917 } 2918 for _, image := range imageList { 2919 if image.TopLayer == layer.ID || stringutils.InSlice(image.MappedTopLayers, layer.ID) { 2920 images = append(images, &image) 2921 } 2922 } 2923 } 2924 return images, nil 2925 } 2926 2927 func (s *store) ImagesByDigest(d digest.Digest) ([]*Image, error) { 2928 images := []*Image{} 2929 2930 istore, err := s.ImageStore() 2931 if err != nil { 2932 return nil, err 2933 } 2934 2935 istores, err := s.ROImageStores() 2936 if err != nil { 2937 return nil, err 2938 } 2939 for _, store := range append([]ROImageStore{istore}, istores...) { 2940 store.RLock() 2941 defer store.Unlock() 2942 if modified, err := store.Modified(); modified || err != nil { 2943 if err = store.Load(); err != nil { 2944 return nil, err 2945 } 2946 } 2947 imageList, err := store.ByDigest(d) 2948 if err != nil && err != ErrImageUnknown { 2949 return nil, err 2950 } 2951 images = append(images, imageList...) 2952 } 2953 return images, nil 2954 } 2955 2956 func (s *store) Container(id string) (*Container, error) { 2957 rcstore, err := s.ContainerStore() 2958 if err != nil { 2959 return nil, err 2960 } 2961 rcstore.RLock() 2962 defer rcstore.Unlock() 2963 if modified, err := rcstore.Modified(); modified || err != nil { 2964 if err = rcstore.Load(); err != nil { 2965 return nil, err 2966 } 2967 } 2968 2969 return rcstore.Get(id) 2970 } 2971 2972 func (s *store) ContainerLayerID(id string) (string, error) { 2973 rcstore, err := s.ContainerStore() 2974 if err != nil { 2975 return "", err 2976 } 2977 rcstore.RLock() 2978 defer rcstore.Unlock() 2979 if modified, err := rcstore.Modified(); modified || err != nil { 2980 if err = rcstore.Load(); err != nil { 2981 return "", err 2982 } 2983 } 2984 container, err := rcstore.Get(id) 2985 if err != nil { 2986 return "", err 2987 } 2988 return container.LayerID, nil 2989 } 2990 2991 func (s *store) ContainerByLayer(id string) (*Container, error) { 2992 layer, err := s.Layer(id) 2993 if err != nil { 2994 return nil, err 2995 } 2996 rcstore, err := s.ContainerStore() 2997 if err != nil { 2998 return nil, err 2999 } 3000 rcstore.RLock() 3001 defer rcstore.Unlock() 3002 if modified, err := rcstore.Modified(); modified || err != nil { 3003 if err = rcstore.Load(); err != nil { 3004 return nil, err 3005 } 3006 } 3007 containerList, err := rcstore.Containers() 3008 if err != nil { 3009 return nil, err 3010 } 3011 for _, container := range containerList { 3012 if container.LayerID == layer.ID { 3013 return &container, nil 3014 } 3015 } 3016 3017 return nil, ErrContainerUnknown 3018 } 3019 3020 func (s *store) ContainerDirectory(id string) (string, error) { 3021 rcstore, err := s.ContainerStore() 3022 if err != nil { 3023 return "", err 3024 } 3025 rcstore.RLock() 3026 defer rcstore.Unlock() 3027 if modified, err := rcstore.Modified(); modified || err != nil { 3028 if err = rcstore.Load(); err != nil { 3029 return "", err 3030 } 3031 } 3032 3033 id, err = rcstore.Lookup(id) 3034 if err != nil { 3035 return "", err 3036 } 3037 3038 middleDir := s.graphDriverName + "-containers" 3039 gcpath := filepath.Join(s.GraphRoot(), middleDir, id, "userdata") 3040 if err := os.MkdirAll(gcpath, 0700); err != nil { 3041 return "", err 3042 } 3043 return gcpath, nil 3044 } 3045 3046 func (s *store) ContainerRunDirectory(id string) (string, error) { 3047 rcstore, err := s.ContainerStore() 3048 if err != nil { 3049 return "", err 3050 } 3051 3052 rcstore.RLock() 3053 defer rcstore.Unlock() 3054 if modified, err := rcstore.Modified(); modified || err != nil { 3055 if err = rcstore.Load(); err != nil { 3056 return "", err 3057 } 3058 } 3059 3060 id, err = rcstore.Lookup(id) 3061 if err != nil { 3062 return "", err 3063 } 3064 3065 middleDir := s.graphDriverName + "-containers" 3066 rcpath := filepath.Join(s.RunRoot(), middleDir, id, "userdata") 3067 if err := os.MkdirAll(rcpath, 0700); err != nil { 3068 return "", err 3069 } 3070 return rcpath, nil 3071 } 3072 3073 func (s *store) SetContainerDirectoryFile(id, file string, data []byte) error { 3074 dir, err := s.ContainerDirectory(id) 3075 if err != nil { 3076 return err 3077 } 3078 err = os.MkdirAll(filepath.Dir(filepath.Join(dir, file)), 0700) 3079 if err != nil { 3080 return err 3081 } 3082 return ioutils.AtomicWriteFile(filepath.Join(dir, file), data, 0600) 3083 } 3084 3085 func (s *store) FromContainerDirectory(id, file string) ([]byte, error) { 3086 dir, err := s.ContainerDirectory(id) 3087 if err != nil { 3088 return nil, err 3089 } 3090 return ioutil.ReadFile(filepath.Join(dir, file)) 3091 } 3092 3093 func (s *store) SetContainerRunDirectoryFile(id, file string, data []byte) error { 3094 dir, err := s.ContainerRunDirectory(id) 3095 if err != nil { 3096 return err 3097 } 3098 err = os.MkdirAll(filepath.Dir(filepath.Join(dir, file)), 0700) 3099 if err != nil { 3100 return err 3101 } 3102 return ioutils.AtomicWriteFile(filepath.Join(dir, file), data, 0600) 3103 } 3104 3105 func (s *store) FromContainerRunDirectory(id, file string) ([]byte, error) { 3106 dir, err := s.ContainerRunDirectory(id) 3107 if err != nil { 3108 return nil, err 3109 } 3110 return ioutil.ReadFile(filepath.Join(dir, file)) 3111 } 3112 3113 func (s *store) Shutdown(force bool) ([]string, error) { 3114 mounted := []string{} 3115 modified := false 3116 3117 rlstore, err := s.LayerStore() 3118 if err != nil { 3119 return mounted, err 3120 } 3121 3122 s.graphLock.Lock() 3123 defer s.graphLock.Unlock() 3124 3125 rlstore.Lock() 3126 defer rlstore.Unlock() 3127 if modified, err := rlstore.Modified(); modified || err != nil { 3128 if err = rlstore.Load(); err != nil { 3129 return nil, err 3130 } 3131 } 3132 3133 layers, err := rlstore.Layers() 3134 if err != nil { 3135 return mounted, err 3136 } 3137 for _, layer := range layers { 3138 if layer.MountCount == 0 { 3139 continue 3140 } 3141 mounted = append(mounted, layer.ID) 3142 if force { 3143 for layer.MountCount > 0 { 3144 _, err2 := rlstore.Unmount(layer.ID, force) 3145 if err2 != nil { 3146 if err == nil { 3147 err = err2 3148 } 3149 break 3150 } 3151 modified = true 3152 } 3153 } 3154 } 3155 if len(mounted) > 0 && err == nil { 3156 err = errors.Wrap(ErrLayerUsedByContainer, "A layer is mounted") 3157 } 3158 if err == nil { 3159 err = s.graphDriver.Cleanup() 3160 s.graphLock.Touch() 3161 modified = true 3162 } 3163 if modified { 3164 rlstore.Touch() 3165 } 3166 return mounted, err 3167 } 3168 3169 // Convert a BigData key name into an acceptable file name. 3170 func makeBigDataBaseName(key string) string { 3171 reader := strings.NewReader(key) 3172 for reader.Len() > 0 { 3173 ch, size, err := reader.ReadRune() 3174 if err != nil || size != 1 { 3175 break 3176 } 3177 if ch != '.' && !(ch >= '0' && ch <= '9') && !(ch >= 'a' && ch <= 'z') { 3178 break 3179 } 3180 } 3181 if reader.Len() > 0 { 3182 return "=" + base64.StdEncoding.EncodeToString([]byte(key)) 3183 } 3184 return key 3185 } 3186 3187 func stringSliceWithoutValue(slice []string, value string) []string { 3188 modified := make([]string, 0, len(slice)) 3189 for _, v := range slice { 3190 if v == value { 3191 continue 3192 } 3193 modified = append(modified, v) 3194 } 3195 return modified 3196 } 3197 3198 func copyStringSlice(slice []string) []string { 3199 if len(slice) == 0 { 3200 return nil 3201 } 3202 ret := make([]string, len(slice)) 3203 copy(ret, slice) 3204 return ret 3205 } 3206 3207 func copyStringInt64Map(m map[string]int64) map[string]int64 { 3208 ret := make(map[string]int64, len(m)) 3209 for k, v := range m { 3210 ret[k] = v 3211 } 3212 return ret 3213 } 3214 3215 func copyStringDigestMap(m map[string]digest.Digest) map[string]digest.Digest { 3216 ret := make(map[string]digest.Digest, len(m)) 3217 for k, v := range m { 3218 ret[k] = v 3219 } 3220 return ret 3221 } 3222 3223 func copyDigestSlice(slice []digest.Digest) []digest.Digest { 3224 if len(slice) == 0 { 3225 return nil 3226 } 3227 ret := make([]digest.Digest, len(slice)) 3228 copy(ret, slice) 3229 return ret 3230 } 3231 3232 // copyStringInterfaceMap still forces us to assume that the interface{} is 3233 // a non-pointer scalar value 3234 func copyStringInterfaceMap(m map[string]interface{}) map[string]interface{} { 3235 ret := make(map[string]interface{}, len(m)) 3236 for k, v := range m { 3237 ret[k] = v 3238 } 3239 return ret 3240 } 3241 3242 // defaultConfigFile path to the system wide storage.conf file 3243 const defaultConfigFile = "/etc/containers/storage.conf" 3244 3245 // DefaultConfigFile returns the path to the storage config file used 3246 func DefaultConfigFile(rootless bool) (string, error) { 3247 if rootless { 3248 home, err := homeDir() 3249 if err != nil { 3250 return "", errors.Wrapf(err, "cannot determine users homedir") 3251 } 3252 return filepath.Join(home, ".config/containers/storage.conf"), nil 3253 } 3254 return defaultConfigFile, nil 3255 } 3256 3257 // TOML-friendly explicit tables used for conversions. 3258 type tomlConfig struct { 3259 Storage struct { 3260 Driver string `toml:"driver"` 3261 RunRoot string `toml:"runroot"` 3262 GraphRoot string `toml:"graphroot"` 3263 Options struct{ config.OptionsConfig } `toml:"options"` 3264 } `toml:"storage"` 3265 } 3266 3267 // ReloadConfigurationFile parses the specified configuration file and overrides 3268 // the configuration in storeOptions. 3269 func ReloadConfigurationFile(configFile string, storeOptions *StoreOptions) { 3270 data, err := ioutil.ReadFile(configFile) 3271 if err != nil { 3272 if !os.IsNotExist(err) { 3273 fmt.Printf("Failed to read %s %v\n", configFile, err.Error()) 3274 return 3275 } 3276 } 3277 3278 config := new(tomlConfig) 3279 3280 if _, err := toml.Decode(string(data), config); err != nil { 3281 fmt.Printf("Failed to parse %s %v\n", configFile, err.Error()) 3282 return 3283 } 3284 if config.Storage.Driver != "" { 3285 storeOptions.GraphDriverName = config.Storage.Driver 3286 } 3287 if config.Storage.RunRoot != "" { 3288 storeOptions.RunRoot = config.Storage.RunRoot 3289 } 3290 if config.Storage.GraphRoot != "" { 3291 storeOptions.GraphRoot = config.Storage.GraphRoot 3292 } 3293 if config.Storage.Options.Thinpool.AutoExtendPercent != "" { 3294 storeOptions.GraphDriverOptions = append(storeOptions.GraphDriverOptions, fmt.Sprintf("dm.thinp_autoextend_percent=%s", config.Storage.Options.Thinpool.AutoExtendPercent)) 3295 } 3296 3297 if config.Storage.Options.Thinpool.AutoExtendThreshold != "" { 3298 storeOptions.GraphDriverOptions = append(storeOptions.GraphDriverOptions, fmt.Sprintf("dm.thinp_autoextend_threshold=%s", config.Storage.Options.Thinpool.AutoExtendThreshold)) 3299 } 3300 3301 if config.Storage.Options.Thinpool.BaseSize != "" { 3302 storeOptions.GraphDriverOptions = append(storeOptions.GraphDriverOptions, fmt.Sprintf("dm.basesize=%s", config.Storage.Options.Thinpool.BaseSize)) 3303 } 3304 if config.Storage.Options.Thinpool.BlockSize != "" { 3305 storeOptions.GraphDriverOptions = append(storeOptions.GraphDriverOptions, fmt.Sprintf("dm.blocksize=%s", config.Storage.Options.Thinpool.BlockSize)) 3306 } 3307 if config.Storage.Options.Thinpool.DirectLvmDevice != "" { 3308 storeOptions.GraphDriverOptions = append(storeOptions.GraphDriverOptions, fmt.Sprintf("dm.directlvm_device=%s", config.Storage.Options.Thinpool.DirectLvmDevice)) 3309 } 3310 if config.Storage.Options.Thinpool.DirectLvmDeviceForce != "" { 3311 storeOptions.GraphDriverOptions = append(storeOptions.GraphDriverOptions, fmt.Sprintf("dm.directlvm_device_force=%s", config.Storage.Options.Thinpool.DirectLvmDeviceForce)) 3312 } 3313 if config.Storage.Options.Thinpool.Fs != "" { 3314 storeOptions.GraphDriverOptions = append(storeOptions.GraphDriverOptions, fmt.Sprintf("dm.fs=%s", config.Storage.Options.Thinpool.Fs)) 3315 } 3316 if config.Storage.Options.Thinpool.LogLevel != "" { 3317 storeOptions.GraphDriverOptions = append(storeOptions.GraphDriverOptions, fmt.Sprintf("dm.libdm_log_level=%s", config.Storage.Options.Thinpool.LogLevel)) 3318 } 3319 if config.Storage.Options.Thinpool.MinFreeSpace != "" { 3320 storeOptions.GraphDriverOptions = append(storeOptions.GraphDriverOptions, fmt.Sprintf("dm.min_free_space=%s", config.Storage.Options.Thinpool.MinFreeSpace)) 3321 } 3322 if config.Storage.Options.Thinpool.MkfsArg != "" { 3323 storeOptions.GraphDriverOptions = append(storeOptions.GraphDriverOptions, fmt.Sprintf("dm.mkfsarg=%s", config.Storage.Options.Thinpool.MkfsArg)) 3324 } 3325 if config.Storage.Options.Thinpool.MountOpt != "" { 3326 storeOptions.GraphDriverOptions = append(storeOptions.GraphDriverOptions, fmt.Sprintf("%s.mountopt=%s", config.Storage.Driver, config.Storage.Options.Thinpool.MountOpt)) 3327 } 3328 if config.Storage.Options.Thinpool.UseDeferredDeletion != "" { 3329 storeOptions.GraphDriverOptions = append(storeOptions.GraphDriverOptions, fmt.Sprintf("dm.use_deferred_deletion=%s", config.Storage.Options.Thinpool.UseDeferredDeletion)) 3330 } 3331 if config.Storage.Options.Thinpool.UseDeferredRemoval != "" { 3332 storeOptions.GraphDriverOptions = append(storeOptions.GraphDriverOptions, fmt.Sprintf("dm.use_deferred_removal=%s", config.Storage.Options.Thinpool.UseDeferredRemoval)) 3333 } 3334 if config.Storage.Options.Thinpool.XfsNoSpaceMaxRetries != "" { 3335 storeOptions.GraphDriverOptions = append(storeOptions.GraphDriverOptions, fmt.Sprintf("dm.xfs_nospace_max_retries=%s", config.Storage.Options.Thinpool.XfsNoSpaceMaxRetries)) 3336 } 3337 for _, s := range config.Storage.Options.AdditionalImageStores { 3338 storeOptions.GraphDriverOptions = append(storeOptions.GraphDriverOptions, fmt.Sprintf("%s.imagestore=%s", config.Storage.Driver, s)) 3339 } 3340 if config.Storage.Options.Size != "" { 3341 storeOptions.GraphDriverOptions = append(storeOptions.GraphDriverOptions, fmt.Sprintf("%s.size=%s", config.Storage.Driver, config.Storage.Options.Size)) 3342 } 3343 if config.Storage.Options.OstreeRepo != "" { 3344 storeOptions.GraphDriverOptions = append(storeOptions.GraphDriverOptions, fmt.Sprintf("%s.ostree_repo=%s", config.Storage.Driver, config.Storage.Options.OstreeRepo)) 3345 } 3346 if config.Storage.Options.SkipMountHome != "" { 3347 storeOptions.GraphDriverOptions = append(storeOptions.GraphDriverOptions, fmt.Sprintf("%s.skip_mount_home=%s", config.Storage.Driver, config.Storage.Options.SkipMountHome)) 3348 } 3349 if config.Storage.Options.MountProgram != "" { 3350 storeOptions.GraphDriverOptions = append(storeOptions.GraphDriverOptions, fmt.Sprintf("%s.mount_program=%s", config.Storage.Driver, config.Storage.Options.MountProgram)) 3351 } 3352 if config.Storage.Options.MountOpt != "" { 3353 storeOptions.GraphDriverOptions = append(storeOptions.GraphDriverOptions, fmt.Sprintf("%s.mountopt=%s", config.Storage.Driver, config.Storage.Options.MountOpt)) 3354 } 3355 if config.Storage.Options.RemapUser != "" && config.Storage.Options.RemapGroup == "" { 3356 config.Storage.Options.RemapGroup = config.Storage.Options.RemapUser 3357 } 3358 if config.Storage.Options.RemapGroup != "" && config.Storage.Options.RemapUser == "" { 3359 config.Storage.Options.RemapUser = config.Storage.Options.RemapGroup 3360 } 3361 if config.Storage.Options.RemapUser != "" && config.Storage.Options.RemapGroup != "" { 3362 mappings, err := idtools.NewIDMappings(config.Storage.Options.RemapUser, config.Storage.Options.RemapGroup) 3363 if err != nil { 3364 fmt.Printf("Error initializing ID mappings for %s:%s %v\n", config.Storage.Options.RemapUser, config.Storage.Options.RemapGroup, err) 3365 return 3366 } 3367 storeOptions.UIDMap = mappings.UIDs() 3368 storeOptions.GIDMap = mappings.GIDs() 3369 } 3370 3371 uidmap, err := idtools.ParseIDMap([]string{config.Storage.Options.RemapUIDs}, "remap-uids") 3372 if err != nil { 3373 fmt.Print(err) 3374 } else { 3375 storeOptions.UIDMap = append(storeOptions.UIDMap, uidmap...) 3376 } 3377 gidmap, err := idtools.ParseIDMap([]string{config.Storage.Options.RemapGIDs}, "remap-gids") 3378 if err != nil { 3379 fmt.Print(err) 3380 } else { 3381 storeOptions.GIDMap = append(storeOptions.GIDMap, gidmap...) 3382 } 3383 if os.Getenv("STORAGE_DRIVER") != "" { 3384 storeOptions.GraphDriverName = os.Getenv("STORAGE_DRIVER") 3385 } 3386 if os.Getenv("STORAGE_OPTS") != "" { 3387 storeOptions.GraphDriverOptions = append(storeOptions.GraphDriverOptions, strings.Split(os.Getenv("STORAGE_OPTS"), ",")...) 3388 } 3389 if len(storeOptions.GraphDriverOptions) == 1 && storeOptions.GraphDriverOptions[0] == "" { 3390 storeOptions.GraphDriverOptions = nil 3391 } 3392 } 3393 3394 func init() { 3395 defaultStoreOptions.RunRoot = "/var/run/containers/storage" 3396 defaultStoreOptions.GraphRoot = "/var/lib/containers/storage" 3397 defaultStoreOptions.GraphDriverName = "" 3398 3399 ReloadConfigurationFile(defaultConfigFile, &defaultStoreOptions) 3400 } 3401 3402 // GetDefaultMountOptions returns the default mountoptions defined in container/storage 3403 func GetDefaultMountOptions() ([]string, error) { 3404 return GetMountOptions(defaultStoreOptions.GraphDriverName, defaultStoreOptions.GraphDriverOptions) 3405 } 3406 3407 // GetMountOptions returns the mountoptions for the specified driver and graphDriverOptions 3408 func GetMountOptions(driver string, graphDriverOptions []string) ([]string, error) { 3409 mountOpts := []string{ 3410 ".mountopt", 3411 fmt.Sprintf("%s.mountopt", driver), 3412 } 3413 for _, option := range graphDriverOptions { 3414 key, val, err := parsers.ParseKeyValueOpt(option) 3415 if err != nil { 3416 return nil, err 3417 } 3418 key = strings.ToLower(key) 3419 for _, m := range mountOpts { 3420 if m == key { 3421 return strings.Split(val, ","), nil 3422 } 3423 } 3424 } 3425 return nil, nil 3426 }