diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2020-11-15 17:11:44 +0100 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2020-11-21 14:22:40 +0100 |
| commit | 2f4d85f4fcb01363b05e03c7fe7d71a71a92c686 (patch) | |
| tree | de8fa23bb8b70371483de29537711febe20e5e01 | |
| parent | 647eaa71d93ba0981b6c25d8cce229c83b6c136c (diff) | |
pkg/build: support injected boot for linux/qemu
The way we assemble the image now from userspace files is not reasonable.
Support injected boot for qemu when we just copy image+kernel to output dir.
It makes it much easier to prepare images for other arches.
Since this mode is triggered only when UserspaceDir points to a file
it should not affect any existing users (since it used to be a dir).
| -rw-r--r-- | pkg/build/linux.go | 41 |
1 files changed, 27 insertions, 14 deletions
diff --git a/pkg/build/linux.go b/pkg/build/linux.go index a6de266d4..b7868c026 100644 --- a/pkg/build/linux.go +++ b/pkg/build/linux.go @@ -29,7 +29,18 @@ func (linux linux) build(params *Params) error { if err := linux.buildKernel(params); err != nil { return err } - if err := linux.createImage(params); err != nil { + kernelPath := filepath.Join(params.KernelDir, filepath.FromSlash(kernelBin(params.TargetArch))) + if fileInfo, err := os.Stat(params.UserspaceDir); err == nil && !fileInfo.IsDir() && params.VMType == "qemu" { + // If UserspaceDir is a file (image) and we use qemu, we just copy image and kernel to the output dir + // assuming that qemu will use injected kernel boot. In this mode we also assume password/key-less ssh. + // In future it would be good to switch to accepting complete disk image always and just replacing + // kernel in it for GCE VMs (assembling image from userspace files in createImage isn't reasonable). + if err := osutil.CopyFile(kernelPath, filepath.Join(params.OutputDir, "kernel")); err != nil { + return err + } + return osutil.CopyFile(params.UserspaceDir, filepath.Join(params.OutputDir, "image")) + } + if err := linux.createImage(params, kernelPath); err != nil { return err } return nil @@ -92,7 +103,7 @@ func (linux linux) buildKernel(params *Params) error { return nil } -func (linux) createImage(params *Params) error { +func (linux) createImage(params *Params, kernelPath string) error { tempDir, err := ioutil.TempDir("", "syz-build") if err != nil { return err @@ -102,18 +113,7 @@ func (linux) createImage(params *Params) error { if err := osutil.WriteExecFile(scriptFile, []byte(createImageScript)); err != nil { return fmt.Errorf("failed to write script file: %v", err) } - - var kernelImage string - switch params.TargetArch { - case targets.I386, targets.AMD64: - kernelImage = "arch/x86/boot/bzImage" - case targets.PPC64LE: - kernelImage = "arch/powerpc/boot/zImage.pseries" - case targets.S390x: - kernelImage = "arch/s390/boot/bzImage" - } - kernelImagePath := filepath.Join(params.KernelDir, filepath.FromSlash(kernelImage)) - cmd := osutil.Command(scriptFile, params.UserspaceDir, kernelImagePath, params.TargetArch) + cmd := osutil.Command(scriptFile, params.UserspaceDir, kernelPath, params.TargetArch) cmd.Dir = tempDir cmd.Env = append([]string{}, os.Environ()...) cmd.Env = append(cmd.Env, @@ -206,6 +206,19 @@ func LinuxMakeArgs(target *targets.Target, compiler, ccache, buildDir string) [] return args } +func kernelBin(arch string) string { + switch arch { + case targets.I386, targets.AMD64: + return "arch/x86/boot/bzImage" + case targets.PPC64LE: + return "arch/powerpc/boot/zImage.pseries" + case targets.S390x: + return "arch/s390/boot/bzImage" + default: + panic(fmt.Sprintf("unsupported arch %v", arch)) + } +} + // elfBinarySignature calculates signature of an elf binary aiming at runtime behavior // (text/data, debug info is ignored). func elfBinarySignature(bin string) (string, error) { |
