forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qemu.go
138 lines (115 loc) · 3.81 KB
/
qemu.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright 2018 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package qemu is suitable for running QEMU-based integration tests.
//
// The environment variable `UROOT_QEMU` overrides the path to QEMU and the
// first few arguments (defaults to "qemu"). For example, I use:
//
// UROOT_QEMU='qemu-system-x86_64 -L . -m 4096 -enable-kvm'
//
// For CI, this environment variable is set in `.circleci/images/integration/Dockerfile`.
package qemu
import (
"errors"
"os"
"regexp"
"strings"
"time"
"github.com/google/goexpect"
)
// DefaultTimeout for `Expect` and `ExpectRE` functions.
var DefaultTimeout = 7 * time.Second
// TimeoutMultiplier increases all timeouts proportionally. Useful when running
// QEMU on a slow machine.
var TimeoutMultiplier = 2.0
// QEMU is filled and pass to `Start()`.
type QEMU struct {
// Path to the bzImage kernel
Kernel string
// Path to the initramfs
InitRAMFS string
// Extra kernel arguments
KernelArgs string
// Extra QEMU arguments
ExtraArgs []string
gExpect *expect.GExpect
}
// CmdLine returns the command line arguments used to start QEMU. These
// arguments are derived from the given QEMU struct.
func (q *QEMU) CmdLine() []string {
// Read first few arguments for env.
env := os.Getenv("UROOT_QEMU")
if env == "" {
env = "qemu" // default
}
args := strings.Fields(env)
// Disable graphics because we are using serial.
args = append(args, "-nographic")
// Arguments passed to the kernel:
//
// - earlyprintk=ttyS0: print very early debug messages to the serial
// - console=ttyS0: /dev/console points to /dev/ttyS0 (the serial port)
// - q.KernelArgs: extra, optional kernel arguments
args = append(args, "-append", "console=ttyS0 earlyprintk=ttyS0")
if q.KernelArgs != "" {
args[len(args)-1] += " " + q.KernelArgs
}
// Kernel and initramfs
args = append(args, "-kernel", q.Kernel)
if q.InitRAMFS != "" {
args = append(args, "-initrd", q.InitRAMFS)
}
return append(args, q.ExtraArgs...)
}
// CmdLineQuoted quotes any of QEMU's command line arguments containing a space
// so it is easy to copy-n-paste into a shell for debugging.
func (q *QEMU) CmdLineQuoted() (cmdline string) {
args := q.CmdLine()
for i, v := range q.CmdLine() {
if strings.ContainsAny(v, " \t\n") {
args[i] = "'" + v + "'"
}
}
return strings.Join(args, " ")
}
// Start QEMU.
func (q *QEMU) Start() error {
if q.gExpect != nil {
return errors.New("QEMU already started")
}
var err error
q.gExpect, _, err = expect.SpawnWithArgs(q.CmdLine(), -1)
return err
}
// Close stops QEMU.
func (q *QEMU) Close() {
q.gExpect.Close()
q.gExpect = nil
}
// Send sends a string to QEMU's serial.
func (q *QEMU) Send(in string) {
q.gExpect.Send(in)
}
// Expect returns an error if the given string is not found in QEMU's serial
// output within `DefaultTimeout`.
func (q *QEMU) Expect(search string) error {
return q.ExpectTimeout(search, DefaultTimeout)
}
// ExpectTimeout returns an error if the given string is not found in QEMU's serial
// output within the given timeout.
func (q *QEMU) ExpectTimeout(search string, timeout time.Duration) error {
return q.ExpectRETimeout(regexp.MustCompile(regexp.QuoteMeta(search)), timeout)
}
// ExpectRE returns an error if the given regular expression is not found in
// QEMU's serial output within `DefaultTimeout`.
func (q *QEMU) ExpectRE(pattern *regexp.Regexp) error {
return q.ExpectRETimeout(pattern, DefaultTimeout)
}
// ExpectRETimeout returns an error if the given regular expression is not
// found in QEMU's serial output within the given timeout.
func (q *QEMU) ExpectRETimeout(pattern *regexp.Regexp, timeout time.Duration) error {
scaled := time.Duration(float64(timeout) * TimeoutMultiplier)
_, _, err := q.gExpect.Expect(pattern, scaled)
return err
}