diff options
| author | Kris Alder <kalder@google.com> | 2022-04-29 15:24:59 -0700 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2022-05-06 08:59:20 +0200 |
| commit | abda9b1a98d331fa672f556e33f18df2eed424bd (patch) | |
| tree | 5671d162b54faf776af37409a20478037f56a914 /pkg/build/android_linux.go | |
| parent | efeff0a5c583d6c8158481970f1b0dd581358d5a (diff) | |
pkg/build: build Android kernel and embed in Cuttlefish image
Android uses some config files (specified with the BUILD_CONFIG
environment variable) to configure kernel builds. KASan configurations
already exist.
We want to mount the image, install a new Cuttlefish emulator binary
(using fetch_cvd) and copy over the kernel that we've built.
Diffstat (limited to 'pkg/build/android_linux.go')
| -rw-r--r-- | pkg/build/android_linux.go | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/pkg/build/android_linux.go b/pkg/build/android_linux.go new file mode 100644 index 000000000..a6e4e086c --- /dev/null +++ b/pkg/build/android_linux.go @@ -0,0 +1,67 @@ +// Copyright 2022 syzkaller project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +package build + +import ( + "fmt" + "io/ioutil" + "os" + "path/filepath" + "time" + + "golang.org/x/sys/unix" + + "github.com/google/syzkaller/pkg/osutil" +) + +// buildCuttlefishImage mounts a disk image, fetches and installs a Cuttlefish emulator binary, +// and copies in the required kernel artifacts. +func buildCuttlefishImage(params Params, bzImage, vmlinux, initramfs string) error { + tempDir, err := ioutil.TempDir("", "syz-build") + if err != nil { + return err + } + defer os.RemoveAll(tempDir) + imageFile := filepath.Join(tempDir, "image") + if err := osutil.CopyFile(params.UserspaceDir, imageFile); err != nil { + return err + } + loop, loopFile, err := linuxSetupLoop(imageFile) + if err != nil { + return err + } + defer func() { + unix.IoctlGetInt(loop, unix.LOOP_CLR_FD) + unix.Close(loop) + }() + mountDir := filepath.Join(tempDir, "mnt") + if err := osutil.MkdirAll(mountDir); err != nil { + return err + } + if err := tryMount(loopFile+"p1", mountDir); err != nil { + return fmt.Errorf("mount(%vp1, %v) failed: %v", loopFile, mountDir, err) + } + defer unix.Unmount(mountDir, 0) + + imageHomeDir := filepath.Join(mountDir, "root") + if _, err := osutil.RunCmd(time.Hour, imageHomeDir, "./fetchcvd"); err != nil { + return fmt.Errorf("run fetch_cvd: %s", err) + } + + if err := osutil.CopyFile(bzImage, filepath.Join(imageHomeDir, "bzImage")); err != nil { + return err + } + if err := osutil.CopyFile(vmlinux, filepath.Join(imageHomeDir, "vmlinux")); err != nil { + return err + } + if err := osutil.CopyFile(initramfs, filepath.Join(imageHomeDir, "initramfs.img")); err != nil { + return err + } + + if err := unix.Unmount(mountDir, 0); err != nil { + return err + } + + return osutil.CopyFile(imageFile, filepath.Join(params.OutputDir, "image")) +} |
