From 6419afbb779b26af853b46d8ad79cfe52b6f7805 Mon Sep 17 00:00:00 2001 From: Greg Steuck Date: Tue, 27 Nov 2018 04:14:06 -0800 Subject: openbsd: run on gce * build/openbsd: minor cleanup (use tuples instead of maps) * Grammar nits in comments. * Simplify openbsd.Create, will defer when there's more than one error exit. * pkg/build: Support copying kernel into GCE image * Simple test for openbsd image copy build. * Cleanup in case something failed before. * Support multi-processor VMs on GCE. * More debug * Reformat * OpenBSD gce image needs to be raw. * GC * Force format to GNU directly on Go 1.10 or newer. * Use vmType passed as a parameter inside openbsd.go * gofmt * more fmt * Can't use GENERIC.mp just yet. * capitalize * Copyright --- pkg/build/build.go | 2 +- pkg/build/openbsd.go | 64 +++++++++++++++++++++++++++++--------- tools/create-openbsd-vmm-worker.sh | 12 ++++--- vm/gce/gce.go | 13 +++----- vm/gce/tar_go1.10.go | 14 +++++++++ vm/gce/tar_go1.9.go | 19 +++++++++++ vm/vm.go | 2 +- vm/vmimpl/vmimpl.go | 4 +-- vm/vmm/vmm.go | 10 ++---- 9 files changed, 101 insertions(+), 39 deletions(-) create mode 100644 vm/gce/tar_go1.10.go create mode 100644 vm/gce/tar_go1.9.go diff --git a/pkg/build/build.go b/pkg/build/build.go index 34015f3ab..7ce21e9b0 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -71,7 +71,7 @@ func getBuilder(targetOS, targetArch, vmType string) (builder, error) { return fuchsia{}, nil case targetOS == "akaros" && targetArch == "amd64" && vmType == "qemu": return akaros{}, nil - case targetOS == "openbsd" && targetArch == "amd64" && vmType == "vmm": + case targetOS == "openbsd" && targetArch == "amd64" && (vmType == "gce" || vmType == "vmm"): return openbsd{}, nil default: return nil, fmt.Errorf("unsupported image type %v/%v/%v", targetOS, targetArch, vmType) diff --git a/pkg/build/openbsd.go b/pkg/build/openbsd.go index 1ec255ac6..319891cbc 100644 --- a/pkg/build/openbsd.go +++ b/pkg/build/openbsd.go @@ -10,6 +10,7 @@ import ( "strconv" "time" + "github.com/google/syzkaller/pkg/log" "github.com/google/syzkaller/pkg/osutil" ) @@ -21,25 +22,29 @@ func (ctx openbsd) build(targetArch, vmType, kernelDir, outputDir, compiler, use confDir := fmt.Sprintf("%v/sys/arch/%v/conf", kernelDir, targetArch) compileDir := fmt.Sprintf("%v/sys/arch/%v/compile/%v", kernelDir, targetArch, kernelName) - if err := ctx.configure(confDir, compileDir, kernelName); err != nil { + useGCE := vmType == "gce" + if err := ctx.configure(confDir, compileDir, kernelName, useGCE); err != nil { return err } if err := ctx.make(compileDir, "all"); err != nil { return err } - - for src, dst := range map[string]string{ - filepath.Join(compileDir, "obj/bsd"): "kernel", - filepath.Join(compileDir, "obj/bsd.gdb"): "obj/bsd.gdb", - filepath.Join(userspaceDir, "image"): "image", - filepath.Join(userspaceDir, "key"): "key", + for _, s := range []struct{ dir, src, dst string }{ + {compileDir, "obj/bsd", "kernel"}, + {compileDir, "obj/bsd.gdb", "obj/bsd.gdb"}, + {userspaceDir, "image", "image"}, + {userspaceDir, "key", "key"}, } { - fullDst := filepath.Join(outputDir, dst) - if err := osutil.CopyFile(src, fullDst); err != nil { - return fmt.Errorf("failed to copy %v -> %v: %v", src, fullDst, err) + fullSrc := filepath.Join(s.dir, s.src) + fullDst := filepath.Join(outputDir, s.dst) + if err := osutil.CopyFile(fullSrc, fullDst); err != nil { + return fmt.Errorf("failed to copy %v -> %v: %v", fullSrc, fullDst, err) } } + if useGCE { + return CopyKernelToImage(outputDir) + } return nil } @@ -47,11 +52,18 @@ func (ctx openbsd) clean(kernelDir string) error { return ctx.make(kernelDir, "", "clean") } -func (ctx openbsd) configure(confDir, compileDir, kernelName string) error { - conf := []byte(` -include "arch/amd64/conf/GENERIC" +func (ctx openbsd) configure(confDir, compileDir, kernelName string, useGCE bool) error { + baseConfig := "GENERIC" + if useGCE { + // GCE supports multiple CPUs. + // TODO(gnezdo): Switch to GENERIC.MP once kernel crash is solved. + // http://openbsd-archive.7691.n7.nabble.com/option-kcov-GENERIC-MP-gt-silent-crash-tc355807.html + baseConfig = "GENERIC" + } + conf := []byte(fmt.Sprintf(` +include "arch/amd64/conf/%v" pseudo-device kcov 1 -`) +`, baseConfig)) if err := osutil.WriteFile(filepath.Join(confDir, kernelName), conf); err != nil { return err } @@ -78,3 +90,27 @@ func (ctx openbsd) make(kernelDir string, args ...string) error { _, err := osutil.RunCmd(10*time.Minute, kernelDir, "make", args...) return err } + +// The easiest way to make an openbsd image that boots the given +// kernel on GCE is to simply overwrite it inside the disk image. +// Ideally a user space tool capable of understanding FFS should +// implement this directly, but vnd(4) device would do in a pinch. +// Assumes that the outputDir contains the appropriately named files. +func CopyKernelToImage(outputDir string) error { + script := `set -eux +# Cleanup in case something failed before. +doas umount /altroot || true +doas vnconfig -u vnd0 || true + +doas /sbin/vnconfig vnd0 image +doas mount /dev/vnd0a /altroot +doas cp kernel /altroot/bsd +doas umount /altroot +doas vnconfig -u vnd0 +` + debugOut, err := osutil.RunCmd(10*time.Minute, outputDir, "/bin/sh", "-c", script) + if err != nil { + log.Logf(0, "Error copying kernel into image %v\n%v\n", outputDir, debugOut) + } + return err +} diff --git a/tools/create-openbsd-vmm-worker.sh b/tools/create-openbsd-vmm-worker.sh index cce3ad8ef..1baa3c687 100755 --- a/tools/create-openbsd-vmm-worker.sh +++ b/tools/create-openbsd-vmm-worker.sh @@ -41,6 +41,10 @@ perl -i.bak -pne 's/^(ttyC.*)vt220.*/$1unknown off/' /etc/ttys touch root/.hushlogin home/syzkaller/.hushlogin EOF +cat >etc/sysctl.conf <etc/installurl <&1 <" @@ -165,5 +169,5 @@ expect { EOF cat <