Lorenz Brun | 547b33f | 2020-04-23 15:27:06 +0200 | [diff] [blame] | 1 | // Copyright 2020 The Monogon Project Authors. |
| 2 | // |
| 3 | // SPDX-License-Identifier: Apache-2.0 |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | |
| 17 | // ktest is a test launcher for running tests inside a custom kernel and passes the results |
| 18 | // back out. |
| 19 | package main |
| 20 | |
| 21 | import ( |
| 22 | "crypto/rand" |
| 23 | "flag" |
| 24 | "fmt" |
| 25 | "io" |
| 26 | "log" |
| 27 | "net" |
| 28 | "os" |
| 29 | "os/exec" |
| 30 | "path/filepath" |
| 31 | ) |
| 32 | |
| 33 | var ( |
| 34 | kernelPath = flag.String("kernel-path", "", "Path of the Kernel ELF file") |
| 35 | initrdPath = flag.String("initrd-path", "", "Path of the initrd image") |
| 36 | cmdline = flag.String("cmdline", "", "Additional kernel command line options") |
| 37 | ) |
| 38 | |
| 39 | func main() { |
| 40 | flag.Parse() |
| 41 | |
| 42 | // Create a temporary socket for passing data (currently only exit code) |
| 43 | // TODO: Land https://patchwork.ozlabs.org/project/qemu-devel/patch/1357671226-11334-1-git-send-email-alexander_barabash@mentor.com/ |
| 44 | tmpDir := os.TempDir() |
| 45 | token := make([]byte, 16) |
| 46 | if _, err := io.ReadFull(rand.Reader, token); err != nil { |
| 47 | log.Fatal(err) |
| 48 | } |
| 49 | |
| 50 | socketPath := filepath.Join(tmpDir, fmt.Sprintf("qemu-io-%x", token)) |
| 51 | l, err := net.Listen("unix", socketPath) |
| 52 | if err != nil { |
| 53 | log.Fatal(err) |
| 54 | } |
| 55 | defer l.Close() |
| 56 | defer os.Remove(socketPath) |
| 57 | |
| 58 | // Start a QEMU microvm (https://github.com/qemu/qemu/blob/master/docs/microvm.rst) with only |
| 59 | // a RNG and two character devices (one for console, one for OOB communication) attached. |
| 60 | cmd := exec.Command("qemu-system-x86_64", "-nodefaults", "-no-user-config", "-nographic", "-no-reboot", |
| 61 | "-accel", "kvm", "-cpu", "host", |
| 62 | "-M", "microvm,x-option-roms=off,pic=off,pit=off,rtc=off,isa-serial=off", |
| 63 | "-kernel", *kernelPath, |
| 64 | "-append", "reboot=t console=hvc0 quiet "+*cmdline, |
| 65 | "-initrd", *initrdPath, |
| 66 | "-device", "virtio-rng-device,max-bytes=1024,period=1000", |
| 67 | "-device", "virtio-serial-device,max_ports=2", |
| 68 | "-chardev", "stdio,id=con0", "-device", "virtconsole,chardev=con0", |
| 69 | "-chardev", "socket,id=io,path="+socketPath, "-device", "virtserialport,chardev=io", |
| 70 | ) |
| 71 | |
| 72 | cmd.Stdout = os.Stdout |
| 73 | cmd.Stderr = os.Stderr |
| 74 | |
| 75 | exitCodeChan := make(chan uint8, 1) |
| 76 | |
| 77 | go func() { |
| 78 | conn, err := l.Accept() |
| 79 | if err != nil { |
| 80 | log.Fatal(err) |
| 81 | } |
| 82 | defer conn.Close() |
| 83 | |
| 84 | returnCode := make([]byte, 1) |
| 85 | if _, err := conn.Read(returnCode); err != nil && err != io.EOF { |
| 86 | log.Fatalf("Failed to read socket: %v", err) |
| 87 | } |
| 88 | exitCodeChan <- returnCode[0] |
| 89 | }() |
| 90 | |
| 91 | if err := cmd.Run(); err != nil { |
| 92 | log.Fatalf("Failed to run QEMU: %v", err) |
| 93 | } |
| 94 | select { |
| 95 | case exitCode := <-exitCodeChan: |
| 96 | os.Exit(int(exitCode)) |
| 97 | default: |
| 98 | log.Printf("Failed to get an error code back") |
| 99 | os.Exit(1) |
| 100 | } |
| 101 | } |