diff options
| author | Siddharth M <siddharth.muralee@gmail.com> | 2019-02-22 17:48:44 +0530 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2019-02-22 13:18:44 +0100 |
| commit | 7fa29a58950476516166f4cd66d66036fb182be2 (patch) | |
| tree | 2f4fda0db863bb05c5f7c5f64414f83cf2508f79 /pkg | |
| parent | 6a5fcca423a42e14346de8637cc30d79530bf034 (diff) | |
pkg/build: added feature to copy kernel image for netbsd
* Modified pkg/build/netbsd.go
1. Made kernel build incremental
2. Added code to copy kernel to disk image
* Fix formating issues
* Fixed basic issues
* Fixed copy kernel to disk
1. Added CPU option to default
2. Added snapshot option to vm/qemu/qemu.go
3. Hacky solution to get the build working
* Fixed issues and added vm.MonitorExecution
* Added sync instead of poweroff
Diffstat (limited to 'pkg')
| -rw-r--r-- | pkg/build/build.go | 2 | ||||
| -rw-r--r-- | pkg/build/netbsd.go | 81 |
2 files changed, 73 insertions, 10 deletions
diff --git a/pkg/build/build.go b/pkg/build/build.go index 64f32f29e..130a4108a 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -74,7 +74,7 @@ func getBuilder(targetOS, targetArch, vmType string) (builder, error) { return akaros{}, nil case targetOS == "openbsd" && targetArch == "amd64" && (vmType == "gce" || vmType == "vmm"): return openbsd{}, nil - case targetOS == "netbsd" && targetArch == "amd64" && vmType == "qemu": + case targetOS == "netbsd" && targetArch == "amd64" && (vmType == "qemu" || vmType == "gce"): return netbsd{}, nil default: return nil, fmt.Errorf("unsupported image type %v/%v/%v", targetOS, targetArch, vmType) diff --git a/pkg/build/netbsd.go b/pkg/build/netbsd.go index cab291119..31d44598a 100644 --- a/pkg/build/netbsd.go +++ b/pkg/build/netbsd.go @@ -4,13 +4,17 @@ package build import ( + "encoding/json" "fmt" "path/filepath" "runtime" "strconv" "time" + "github.com/google/syzkaller/pkg/mgrconfig" "github.com/google/syzkaller/pkg/osutil" + "github.com/google/syzkaller/pkg/report" + "github.com/google/syzkaller/vm" ) type netbsd struct{} @@ -33,29 +37,33 @@ no options SVS if err := osutil.WriteFile(filepath.Join(confDir, kernelName), conf); err != nil { return err } - // Build tools before building kernel if _, err := osutil.RunCmd(10*time.Minute, kernelDir, "./build.sh", "-m", targetArch, - "-U", "-j"+strconv.Itoa(runtime.NumCPU()), "tools"); err != nil { + "-U", "-u", "-j"+strconv.Itoa(runtime.NumCPU()), "tools"); err != nil { return extractRootCause(err) } // Build kernel if _, err := osutil.RunCmd(10*time.Minute, kernelDir, "./build.sh", "-m", targetArch, - "-U", "-j"+strconv.Itoa(runtime.NumCPU()), "kernel="+kernelName); err != nil { + "-U", "-u", "-j"+strconv.Itoa(runtime.NumCPU()), "kernel="+kernelName); err != nil { return extractRootCause(err) } - - for _, s := range []struct{ dir, src, dst string }{ - {compileDir, "netbsd", "kernel"}, - {compileDir, "netbsd.gdb", "netbsd.gdb"}, + for _, s := range []struct{ dir, obj string }{ + {compileDir, "netbsd"}, + {compileDir, "netbsd.gdb"}, + {userspaceDir, "image"}, + {userspaceDir, "key"}, } { - fullSrc := filepath.Join(s.dir, s.src) - fullDst := filepath.Join(outputDir, s.dst) + fullSrc := filepath.Join(s.dir, s.obj) + fullDst := filepath.Join(outputDir, s.obj) if err := osutil.CopyFile(fullSrc, fullDst); err != nil { return fmt.Errorf("failed to copy %v -> %v: %v", fullSrc, fullDst, err) } } + + if vmType == "qemu" || vmType == "gce" { + return CopyKernelToDisk(outputDir) + } return nil } @@ -66,3 +74,58 @@ func (ctx netbsd) clean(kernelDir string) error { // machine. return nil } + +// Copy the compiled kernel to the qemu disk image using ssh +func CopyKernelToDisk(outputDir string) error { + temp := []byte(` +{ + "snapshot": false, + "mem": 1024 +} `) + VMconfig := (*json.RawMessage)(&temp) + // Create config for booting the disk image + cfg := &mgrconfig.Config{ + Workdir: outputDir, + Image: filepath.Join(outputDir, "image"), + SSHKey: filepath.Join(outputDir, "key"), + SSHUser: "root", + TargetOS: "netbsd", + TargetArch: "amd64", + TargetVMArch: "amd64", + Type: "qemu", + VM: *VMconfig, + } + // Create a VM pool + pool, err := vm.Create(cfg, false) + if err != nil { + return fmt.Errorf("failed to create a VM Pool : %v", err) + } + // Create a new reporter instance + reporter, err := report.NewReporter(cfg) + if err != nil { + return fmt.Errorf("failed to create a Reporter : %v", err) + } + // Create a VM instance (We need only one) + inst, err := pool.Create(0) + if err != nil { + return fmt.Errorf("failed to create the VM Instance : %v", err) + } + defer inst.Close() + // Copy the kernel into the disk image and replace it + // This makes use of the fact that the file is copied by default to / + kernel, err := inst.Copy(filepath.Join(outputDir, "netbsd")) + if err != nil { + return fmt.Errorf("error Copying the kernel %v: %v", kernel, err) + } + // Run sync so that the copied image is stored properly + outc, errc, err := inst.Run(time.Minute, nil, "sync") + if err != nil { + return fmt.Errorf("error syncing the instance %v", err) + } + // Make sure that the command has executed properly + rep := inst.MonitorExecution(outc, errc, reporter, vm.ExitNormal) + if rep != nil { + return fmt.Errorf("error executng poweroff : %v", rep.Title) + } + return nil +} |
