Skip to content

Commit

Permalink
Support storing Ollama layers
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaodong Ye <[email protected]>
  • Loading branch information
yeahdongcn committed Aug 26, 2024
1 parent 4e18d1f commit 0f767a7
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 17 deletions.
1 change: 1 addition & 0 deletions drivers/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type ApplyDiffOpts struct {
MountLabel string
IgnoreChownErrors bool
ForceMask *os.FileMode
LayerFilename *string
}

// ApplyDiffWithDifferOpts contains optional arguments for ApplyDiffWithDiffer methods.
Expand Down
47 changes: 36 additions & 11 deletions drivers/overlay/overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,23 @@ func supportsOverlay(home string, homeMagic graphdriver.FsMagic, rootUID, rootGI
return supportsDType, fmt.Errorf("'overlay' not found as a supported filesystem on this host. Please ensure kernel is new enough and has overlay support loaded.: %w", graphdriver.ErrNotSupported)
}

func cp(r io.Reader, dest string, filename string) error {
if seeker, ok := r.(io.Seeker); ok {
if _, err := seeker.Seek(0, io.SeekStart); err != nil {
return err
}
}

f, err := os.Create(filepath.Join(dest, filename))
if err != nil {
return err
}
defer f.Close()

_, err = io.Copy(f, r)
return err
}

func (d *Driver) useNaiveDiff() bool {
if d.usingComposefs {
return true
Expand Down Expand Up @@ -2329,17 +2346,25 @@ func (d *Driver) ApplyDiff(id, parent string, options graphdriver.ApplyDiffOpts)
return 0, err
}

logrus.Debugf("Applying tar in %s", applyDir)
// Overlay doesn't need the parent id to apply the diff
if err := untar(options.Diff, applyDir, &archive.TarOptions{
UIDMaps: idMappings.UIDs(),
GIDMaps: idMappings.GIDs(),
IgnoreChownErrors: d.options.ignoreChownErrors,
ForceMask: d.options.forceMask,
WhiteoutFormat: d.getWhiteoutFormat(),
InUserNS: unshare.IsRootless(),
}); err != nil {
return 0, err
if options.LayerFilename != nil {
logrus.Debugf("Applying file in %s", applyDir)
err := cp(options.Diff, applyDir, *options.LayerFilename)
if err != nil {
return 0, err
}
} else {
logrus.Debugf("Applying tar in %s", applyDir)
// Overlay doesn't need the parent id to apply the diff
if err := untar(options.Diff, applyDir, &archive.TarOptions{
UIDMaps: idMappings.UIDs(),
GIDMaps: idMappings.GIDs(),
IgnoreChownErrors: d.options.ignoreChownErrors,
ForceMask: d.options.forceMask,
WhiteoutFormat: d.getWhiteoutFormat(),
InUserNS: unshare.IsRootless(),
}); err != nil {
return 0, err
}
}

return directory.Size(applyDir)
Expand Down
25 changes: 19 additions & 6 deletions layers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2422,14 +2422,21 @@ func (r *layerStore) applyDiffWithOptions(to string, layerOptions *LayerOptions,
if uncompressedDigester != nil {
uncompressedWriter = io.MultiWriter(uncompressedWriter, uncompressedDigester.Hash())
}
payload, err := asm.NewInputTarStream(io.TeeReader(uncompressed, uncompressedWriter), metadata, storage.NewDiscardFilePutter())
if err != nil {
return -1, err

var payload io.Reader
if layerOptions != nil && layerOptions.LayerFilename != nil {
payload = diff
} else {
payload, err = asm.NewInputTarStream(io.TeeReader(uncompressed, uncompressedWriter), metadata, storage.NewDiscardFilePutter())
if err != nil {
return -1, err
}
}
options := drivers.ApplyDiffOpts{
Diff: payload,
Mappings: r.layerMappings(layer),
MountLabel: layer.MountLabel,
Diff: payload,
Mappings: r.layerMappings(layer),
MountLabel: layer.MountLabel,
LayerFilename: layerOptions.LayerFilename,
}
size, err := r.driver.ApplyDiff(layer.ID, layer.Parent, options)
if err != nil {
Expand Down Expand Up @@ -2468,6 +2475,12 @@ func (r *layerStore) applyDiffWithOptions(to string, layerOptions *LayerOptions,
layer.UncompressedDigest = uncompressedDigest
layer.UncompressedSize = uncompressedCounter.Count
layer.CompressionType = compression

if layerOptions != nil && layerOptions.LayerFilename != nil {
layer.CompressedSize = size
layer.UncompressedSize = size
}

layer.UIDs = make([]uint32, 0, len(uidLog))
for uid := range uidLog {
layer.UIDs = append(layer.UIDs, uid)
Expand Down
2 changes: 2 additions & 0 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,8 @@ type LayerOptions struct {
// Currently these can only be set when the layer record is created, but that
// could change in the future.
Flags map[string]interface{}
// LayerFilename is the target filename of the layer blob.
LayerFilename *string
}

type LayerBigDataOption struct {
Expand Down

0 comments on commit 0f767a7

Please sign in to comment.