Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unify apk installed db for base layer #3879

Merged
merged 1 commit into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
86 changes: 86 additions & 0 deletions src/cmd/linuxkit/moby/apk_tarwriter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package moby

import (
"archive/tar"
"bytes"
)

// apkTarWriter apk-aware tar writer that consolidates installed database, so that
// it can be called multiple times and will do the union of all such databases,
// rather than overwriting the previous one.
const apkInstalledPath = "lib/apk/db/installed"

type apkTarWriter struct {
*tar.Writer
dbs [][]byte
current *bytes.Buffer
}

func newAPKTarWriter(w *tar.Writer) *apkTarWriter {
return &apkTarWriter{
Writer: w,
}
}

func (a *apkTarWriter) WriteHeader(hdr *tar.Header) error {
if a.current != nil {
a.dbs = append(a.dbs, a.current.Bytes())
a.current = nil
}
if hdr.Name == apkInstalledPath {
a.current = new(bytes.Buffer)
}
return a.Writer.WriteHeader(hdr)
}
func (a *apkTarWriter) Write(b []byte) (int, error) {
if a.current != nil {
a.current.Write(b)
}
return a.Writer.Write(b)
}

func (a *apkTarWriter) Close() error {
// before closing, write out the union of all the databases
if a.current != nil {
a.dbs = append(a.dbs, a.current.Bytes())
a.current = nil
}
if err := a.WriteAPKDB(); err != nil {
return err
}
return a.Writer.Close()
}

func (a *apkTarWriter) WriteAPKDB() error {
if len(a.dbs) > 1 {
// consolidate the databases
// calculate the size of the new database
var size int
for _, db := range a.dbs {
size += len(db)
size += 2 // 2 trailing newlines for each db
}
hdr := &tar.Header{
Name: apkInstalledPath,
Mode: 0o644,
Uid: 0,
Gid: 0,
Typeflag: tar.TypeReg,
Size: int64(size),
}
if err := a.Writer.WriteHeader(hdr); err != nil {
return err
}
for _, db := range a.dbs {
if _, err := a.Writer.Write(db); err != nil {
return err
}
if _, err := a.Writer.Write([]byte{'\n', '\n'}); err != nil {
return err
}
}
}
// once complete, clear the databases
a.dbs = nil
return nil
}
6 changes: 5 additions & 1 deletion src/cmd/linuxkit/moby/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,17 @@ func Build(m Moby, w io.Writer, opts BuildOpts) error {
if len(m.Init) != 0 {
log.Infof("Add init containers:")
}
apkTar := newAPKTarWriter(iw)
for _, ii := range m.initRefs {
log.Infof("Process init image: %s", ii)
err := ImageTar(ii, "", iw, resolvconfSymlink, opts)
err := ImageTar(ii, "", apkTar, resolvconfSymlink, opts)
if err != nil {
return fmt.Errorf("Failed to build init tarball from %s: %v", ii, err)
}
}
if err := apkTar.WriteAPKDB(); err != nil {
return err
}

if len(m.Onboot) != 0 {
log.Infof("Add onboot containers:")
Expand Down