Skip to content

Commit 4e81474

Browse files
committed
Support storing Ollama layers
Signed-off-by: Xiaodong Ye <[email protected]>
1 parent 2db1d73 commit 4e81474

File tree

4 files changed

+58
-17
lines changed

4 files changed

+58
-17
lines changed

drivers/driver.go

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ type ApplyDiffOpts struct {
7272
MountLabel string
7373
IgnoreChownErrors bool
7474
ForceMask *os.FileMode
75+
LayerFilename *string
7576
}
7677

7778
// ApplyDiffWithDifferOpts contains optional arguments for ApplyDiffWithDiffer methods.

drivers/overlay/overlay.go

+36-11
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,23 @@ func supportsOverlay(home string, homeMagic graphdriver.FsMagic, rootUID, rootGI
790790
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)
791791
}
792792

793+
func cp(r io.Reader, dest string, filename string) error {
794+
if seeker, ok := r.(io.Seeker); ok {
795+
if _, err := seeker.Seek(0, io.SeekStart); err != nil {
796+
return err
797+
}
798+
}
799+
800+
f, err := os.Create(filepath.Join(dest, filename))
801+
if err != nil {
802+
return err
803+
}
804+
defer f.Close()
805+
806+
_, err = io.Copy(f, r)
807+
return err
808+
}
809+
793810
func (d *Driver) useNaiveDiff() bool {
794811
if d.usingComposefs {
795812
return true
@@ -2329,17 +2346,25 @@ func (d *Driver) ApplyDiff(id, parent string, options graphdriver.ApplyDiffOpts)
23292346
return 0, err
23302347
}
23312348

2332-
logrus.Debugf("Applying tar in %s", applyDir)
2333-
// Overlay doesn't need the parent id to apply the diff
2334-
if err := untar(options.Diff, applyDir, &archive.TarOptions{
2335-
UIDMaps: idMappings.UIDs(),
2336-
GIDMaps: idMappings.GIDs(),
2337-
IgnoreChownErrors: d.options.ignoreChownErrors,
2338-
ForceMask: d.options.forceMask,
2339-
WhiteoutFormat: d.getWhiteoutFormat(),
2340-
InUserNS: unshare.IsRootless(),
2341-
}); err != nil {
2342-
return 0, err
2349+
if options.LayerFilename != nil {
2350+
logrus.Debugf("Applying file in %s", applyDir)
2351+
err := cp(options.Diff, applyDir, *options.LayerFilename)
2352+
if err != nil {
2353+
return 0, err
2354+
}
2355+
} else {
2356+
logrus.Debugf("Applying tar in %s", applyDir)
2357+
// Overlay doesn't need the parent id to apply the diff
2358+
if err := untar(options.Diff, applyDir, &archive.TarOptions{
2359+
UIDMaps: idMappings.UIDs(),
2360+
GIDMaps: idMappings.GIDs(),
2361+
IgnoreChownErrors: d.options.ignoreChownErrors,
2362+
ForceMask: d.options.forceMask,
2363+
WhiteoutFormat: d.getWhiteoutFormat(),
2364+
InUserNS: unshare.IsRootless(),
2365+
}); err != nil {
2366+
return 0, err
2367+
}
23432368
}
23442369

23452370
return directory.Size(applyDir)

layers.go

+19-6
Original file line numberDiff line numberDiff line change
@@ -2422,14 +2422,21 @@ func (r *layerStore) applyDiffWithOptions(to string, layerOptions *LayerOptions,
24222422
if uncompressedDigester != nil {
24232423
uncompressedWriter = io.MultiWriter(uncompressedWriter, uncompressedDigester.Hash())
24242424
}
2425-
payload, err := asm.NewInputTarStream(io.TeeReader(uncompressed, uncompressedWriter), metadata, storage.NewDiscardFilePutter())
2426-
if err != nil {
2427-
return -1, err
2425+
2426+
var payload io.Reader
2427+
if layerOptions != nil && layerOptions.LayerFilename != nil {
2428+
payload = diff
2429+
} else {
2430+
payload, err = asm.NewInputTarStream(io.TeeReader(uncompressed, uncompressedWriter), metadata, storage.NewDiscardFilePutter())
2431+
if err != nil {
2432+
return -1, err
2433+
}
24282434
}
24292435
options := drivers.ApplyDiffOpts{
2430-
Diff: payload,
2431-
Mappings: r.layerMappings(layer),
2432-
MountLabel: layer.MountLabel,
2436+
Diff: payload,
2437+
Mappings: r.layerMappings(layer),
2438+
MountLabel: layer.MountLabel,
2439+
LayerFilename: layerOptions.LayerFilename,
24332440
}
24342441
size, err := r.driver.ApplyDiff(layer.ID, layer.Parent, options)
24352442
if err != nil {
@@ -2468,6 +2475,12 @@ func (r *layerStore) applyDiffWithOptions(to string, layerOptions *LayerOptions,
24682475
layer.UncompressedDigest = uncompressedDigest
24692476
layer.UncompressedSize = uncompressedCounter.Count
24702477
layer.CompressionType = compression
2478+
2479+
if layerOptions != nil && layerOptions.LayerFilename != nil {
2480+
layer.CompressedSize = size
2481+
layer.UncompressedSize = size
2482+
}
2483+
24712484
layer.UIDs = make([]uint32, 0, len(uidLog))
24722485
for uid := range uidLog {
24732486
layer.UIDs = append(layer.UIDs, uid)

store.go

+2
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,8 @@ type LayerOptions struct {
641641
// Currently these can only be set when the layer record is created, but that
642642
// could change in the future.
643643
Flags map[string]interface{}
644+
// LayerFilename is the target filename of the layer blob.
645+
LayerFilename *string
644646
}
645647

646648
type LayerBigDataOption struct {

0 commit comments

Comments
 (0)