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 | // ktestinit is an init designed to run inside a lightweight VM for running tests in there. |
| 18 | // It performs basic platform initialization like mounting kernel filesystems and launches the |
| 19 | // test executable at /tester, passes the exit code back out over the control socket to ktest and |
| 20 | // then terminates the VM kernel. |
| 21 | package main |
| 22 | |
| 23 | import ( |
| 24 | "errors" |
| 25 | "fmt" |
| 26 | "os" |
| 27 | "os/exec" |
| 28 | |
| 29 | "golang.org/x/sys/unix" |
| 30 | ) |
| 31 | |
| 32 | func mountInit() error { |
| 33 | for _, el := range []struct { |
| 34 | dir string |
| 35 | fs string |
| 36 | flags uintptr |
| 37 | }{ |
| 38 | {"/sys", "sysfs", unix.MS_NOEXEC | unix.MS_NOSUID | unix.MS_NODEV}, |
| 39 | {"/proc", "proc", unix.MS_NOEXEC | unix.MS_NOSUID | unix.MS_NODEV}, |
| 40 | {"/dev", "devtmpfs", unix.MS_NOEXEC | unix.MS_NOSUID}, |
| 41 | {"/dev/pts", "devpts", unix.MS_NOEXEC | unix.MS_NOSUID}, |
Lorenz Brun | 9956e72 | 2021-03-24 18:48:55 +0100 | [diff] [blame] | 42 | {"/tmp", "tmpfs", 0}, |
Lorenz Brun | 547b33f | 2020-04-23 15:27:06 +0200 | [diff] [blame] | 43 | } { |
| 44 | if err := os.Mkdir(el.dir, 0755); err != nil && !os.IsExist(err) { |
| 45 | return fmt.Errorf("could not make %s: %w", el.dir, err) |
| 46 | } |
| 47 | if err := unix.Mount(el.fs, el.dir, el.fs, el.flags, ""); err != nil { |
| 48 | return fmt.Errorf("could not mount %s on %s: %w", el.fs, el.dir, err) |
| 49 | } |
| 50 | } |
| 51 | return nil |
| 52 | } |
| 53 | |
| 54 | func main() { |
| 55 | if err := mountInit(); err != nil { |
| 56 | panic(err) |
| 57 | } |
| 58 | |
| 59 | // First virtual serial is always stdout, second is control |
| 60 | ioConn, err := os.OpenFile("/dev/vport1p1", os.O_RDWR, 0) |
| 61 | if err != nil { |
| 62 | fmt.Printf("Failed to open communication device: %v\n", err) |
| 63 | return |
| 64 | } |
| 65 | cmd := exec.Command("/tester", "-test.v") |
| 66 | cmd.Stderr = os.Stderr |
| 67 | cmd.Stdout = os.Stdout |
| 68 | cmd.Env = append(cmd.Env, "IN_KTEST=true") |
| 69 | if err := cmd.Run(); err != nil { |
| 70 | var exerr *exec.ExitError |
| 71 | if errors.As(err, &exerr) { |
| 72 | if _, err := ioConn.Write([]byte{uint8(exerr.ExitCode())}); err != nil { |
| 73 | panic(err) |
| 74 | } |
| 75 | } else if err != nil { |
| 76 | fmt.Printf("Failed to execute tests (tests didn't run): %v", err) |
| 77 | } |
Lorenz Brun | 3ff5af3 | 2020-06-24 16:34:11 +0200 | [diff] [blame] | 78 | } else { |
| 79 | ioConn.Write([]byte{0}) |
Lorenz Brun | 547b33f | 2020-04-23 15:27:06 +0200 | [diff] [blame] | 80 | } |
Lorenz Brun | 3ff5af3 | 2020-06-24 16:34:11 +0200 | [diff] [blame] | 81 | ioConn.Close() |
Lorenz Brun | 547b33f | 2020-04-23 15:27:06 +0200 | [diff] [blame] | 82 | |
| 83 | unix.Reboot(unix.LINUX_REBOOT_CMD_RESTART) |
| 84 | } |