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