diff options
| author | Kris Alder <kalder@google.com> | 2022-05-03 11:05:34 -0700 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2022-05-06 08:59:20 +0200 |
| commit | e60b110364688cf334ec68e073edf8d9cebc9fcb (patch) | |
| tree | e10026e4c1246c6b4d7206da4a7cd3acbff8343e /pkg/build/android.go | |
| parent | abda9b1a98d331fa672f556e33f18df2eed424bd (diff) | |
pkg/build: refactor cuttlefish image code to embedFiles()
Since most of the image mounting code is duplicated, we can instead
extract it to an embedFiles() function that takes a callback. The Linux-
or Android-specific code can be in the callback.
Diffstat (limited to 'pkg/build/android.go')
| -rw-r--r-- | pkg/build/android.go | 69 |
1 files changed, 65 insertions, 4 deletions
diff --git a/pkg/build/android.go b/pkg/build/android.go index 9fb38bd5c..cc4e041da 100644 --- a/pkg/build/android.go +++ b/pkg/build/android.go @@ -4,7 +4,11 @@ package build import ( + "archive/tar" + "compress/gzip" "fmt" + "io/ioutil" + "os" "path/filepath" "time" @@ -27,6 +31,40 @@ func (a android) runBuild(kernelDir, buildConfig string) error { return err } +func (a android) readCompiler(archivePath string) (string, error) { + f, err := os.Open(archivePath) + if err != nil { + return "", err + } + defer f.Close() + + gr, err := gzip.NewReader(f) + if err != nil { + return "", err + } + defer gr.Close() + + tr := tar.NewReader(gr) + + h, err := tr.Next() + for ; err == nil; h, err = tr.Next() { + if filepath.Base(h.Name) == "compile.h" { + bytes, err := ioutil.ReadAll(tr) + if err != nil { + return "", err + } + result := linuxCompilerRegexp.FindSubmatch(bytes) + if result == nil { + return "", fmt.Errorf("include/generated/compile.h does not contain build information") + } + + return string(result[1]), nil + } + } + + return "", fmt.Errorf("archive %s doesn't contain include/generated/compile.h", archivePath) +} + func (a android) build(params Params) (ImageDetails, error) { var details ImageDetails @@ -44,13 +82,37 @@ func (a android) build(params Params) (ImageDetails, error) { return details, fmt.Errorf("failed to build modules: %s", err) } - buildOutDir := filepath.Join(params.KernelDir, "out/dist") + buildOutDir := filepath.Join(params.KernelDir, "out", "dist") bzImage := filepath.Join(buildOutDir, "bzImage") vmlinux := filepath.Join(buildOutDir, "vmlinux") initramfs := filepath.Join(buildOutDir, "initramfs.img") - if err := buildCuttlefishImage(params, bzImage, vmlinux, initramfs); err != nil { - return details, fmt.Errorf("failed to build image: %s", err) + var err error + details.CompilerID, err = a.readCompiler(filepath.Join(buildOutDir, "kernel-headers.tar.gz")) + if err != nil { + return details, err + } + + if err := embedFiles(params, func(mountDir string) error { + homeDir := filepath.Join(mountDir, "root") + + if _, err := osutil.RunCmd(time.Hour, homeDir, "./fetchcvd"); err != nil { + return err + } + + if err := osutil.CopyFile(bzImage, filepath.Join(homeDir, "bzImage")); err != nil { + return err + } + if err := osutil.CopyFile(vmlinux, filepath.Join(homeDir, "vmlinux")); err != nil { + return err + } + if err := osutil.CopyFile(initramfs, filepath.Join(homeDir, "initramfs.img")); err != nil { + return err + } + + return nil + }); err != nil { + return details, err } if err := osutil.CopyFile(vmlinux, filepath.Join(params.OutputDir, "kernel")); err != nil { @@ -60,7 +122,6 @@ func (a android) build(params Params) (ImageDetails, error) { return details, err } - var err error details.Signature, err = elfBinarySignature(vmlinux) if err != nil { return details, fmt.Errorf("failed to generate signature: %s", err) |
