blob: 16ffa290b376aebbc7d80495b1423c954e3c3efc [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},
Lorenz Brun8bc82862024-04-30 11:47:09 +000040 {"/sys/kernel/debug", "debugfs", unix.MS_NOEXEC | unix.MS_NOSUID | unix.MS_NODEV},
Lorenz Brun547b33f2020-04-23 15:27:06 +020041 {"/proc", "proc", unix.MS_NOEXEC | unix.MS_NOSUID | unix.MS_NODEV},
42 {"/dev", "devtmpfs", unix.MS_NOEXEC | unix.MS_NOSUID},
43 {"/dev/pts", "devpts", unix.MS_NOEXEC | unix.MS_NOSUID},
Lorenz Brun9956e722021-03-24 18:48:55 +010044 {"/tmp", "tmpfs", 0},
Lorenz Brun547b33f2020-04-23 15:27:06 +020045 } {
46 if err := os.Mkdir(el.dir, 0755); err != nil && !os.IsExist(err) {
47 return fmt.Errorf("could not make %s: %w", el.dir, err)
48 }
49 if err := unix.Mount(el.fs, el.dir, el.fs, el.flags, ""); err != nil {
50 return fmt.Errorf("could not mount %s on %s: %w", el.fs, el.dir, err)
51 }
52 }
53 return nil
54}
55
56func main() {
57 if err := mountInit(); err != nil {
58 panic(err)
59 }
60
61 // First virtual serial is always stdout, second is control
62 ioConn, err := os.OpenFile("/dev/vport1p1", os.O_RDWR, 0)
63 if err != nil {
64 fmt.Printf("Failed to open communication device: %v\n", err)
65 return
66 }
67 cmd := exec.Command("/tester", "-test.v")
68 cmd.Stderr = os.Stderr
69 cmd.Stdout = os.Stdout
70 cmd.Env = append(cmd.Env, "IN_KTEST=true")
71 if err := cmd.Run(); err != nil {
72 var exerr *exec.ExitError
73 if errors.As(err, &exerr) {
74 if _, err := ioConn.Write([]byte{uint8(exerr.ExitCode())}); err != nil {
75 panic(err)
76 }
Lorenz Brun547b33f2020-04-23 15:27:06 +020077 }
Tim Windelschmidta27272c2024-04-11 22:54:29 +020078 fmt.Printf("Failed to execute tests (tests didn't run): %v", err)
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}