Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +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 | |
Serge Bazanski | 66e5895 | 2021-10-05 17:06:56 +0200 | [diff] [blame] | 17 | // launch implements test harnesses for running qemu VMs from tests. |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 18 | package launch |
| 19 | |
| 20 | import ( |
Lorenz Brun | 3ff5af3 | 2020-06-24 16:34:11 +0200 | [diff] [blame] | 21 | "bytes" |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 22 | "context" |
Lorenz Brun | 3ff5af3 | 2020-06-24 16:34:11 +0200 | [diff] [blame] | 23 | "errors" |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 24 | "fmt" |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 25 | "net" |
| 26 | "os" |
| 27 | "os/exec" |
Lorenz Brun | 3ff5af3 | 2020-06-24 16:34:11 +0200 | [diff] [blame] | 28 | "strconv" |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 29 | "strings" |
Lorenz Brun | 3ff5af3 | 2020-06-24 16:34:11 +0200 | [diff] [blame] | 30 | "syscall" |
Lorenz Brun | ed0503c | 2020-07-28 17:21:25 +0200 | [diff] [blame] | 31 | |
Lorenz Brun | 3ff5af3 | 2020-06-24 16:34:11 +0200 | [diff] [blame] | 32 | "golang.org/x/sys/unix" |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 33 | "google.golang.org/grpc" |
| 34 | |
Serge Bazanski | 52304a8 | 2021-10-29 16:56:18 +0200 | [diff] [blame^] | 35 | "source.monogon.dev/metropolis/node" |
Serge Bazanski | 31370b0 | 2021-01-07 16:31:14 +0100 | [diff] [blame] | 36 | "source.monogon.dev/metropolis/pkg/freeport" |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 37 | ) |
| 38 | |
Serge Bazanski | 66e5895 | 2021-10-05 17:06:56 +0200 | [diff] [blame] | 39 | type QemuValue map[string][]string |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 40 | |
Serge Bazanski | 66e5895 | 2021-10-05 17:06:56 +0200 | [diff] [blame] | 41 | // ToOption encodes structured data into a QEMU option. Example: "test", {"key1": |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 42 | // {"val1"}, "key2": {"val2", "val3"}} returns "test,key1=val1,key2=val2,key2=val3" |
Serge Bazanski | 66e5895 | 2021-10-05 17:06:56 +0200 | [diff] [blame] | 43 | func (value QemuValue) ToOption(name string) string { |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 44 | var optionValues []string |
Lorenz Brun | 3ff5af3 | 2020-06-24 16:34:11 +0200 | [diff] [blame] | 45 | if name != "" { |
| 46 | optionValues = append(optionValues, name) |
| 47 | } |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 48 | for name, values := range value { |
| 49 | if len(values) == 0 { |
| 50 | optionValues = append(optionValues, name) |
| 51 | } |
| 52 | for _, val := range values { |
| 53 | optionValues = append(optionValues, fmt.Sprintf("%v=%v", name, val)) |
| 54 | } |
| 55 | } |
| 56 | return strings.Join(optionValues, ",") |
| 57 | } |
| 58 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 59 | // PortMap represents where VM ports are mapped to on the host. It maps from the VM |
| 60 | // port number to the host port number. |
Serge Bazanski | 52304a8 | 2021-10-29 16:56:18 +0200 | [diff] [blame^] | 61 | type PortMap map[node.Port]uint16 |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 62 | |
Serge Bazanski | 66e5895 | 2021-10-05 17:06:56 +0200 | [diff] [blame] | 63 | // ToQemuForwards generates QEMU hostfwd values (https://qemu.weilnetz.de/doc/qemu- |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 64 | // doc.html#:~:text=hostfwd=) for all mapped ports. |
Serge Bazanski | 66e5895 | 2021-10-05 17:06:56 +0200 | [diff] [blame] | 65 | func (p PortMap) ToQemuForwards() []string { |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 66 | var hostfwdOptions []string |
| 67 | for vmPort, hostPort := range p { |
Serge Bazanski | 52304a8 | 2021-10-29 16:56:18 +0200 | [diff] [blame^] | 68 | hostfwdOptions = append(hostfwdOptions, fmt.Sprintf("tcp::%d-:%d", hostPort, vmPort)) |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 69 | } |
| 70 | return hostfwdOptions |
| 71 | } |
| 72 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 73 | // DialGRPC creates a gRPC client for a VM port that's forwarded/mapped to the |
| 74 | // host. The given port is automatically resolved to the host-mapped port. |
Serge Bazanski | 52304a8 | 2021-10-29 16:56:18 +0200 | [diff] [blame^] | 75 | func (p PortMap) DialGRPC(port node.Port, opts ...grpc.DialOption) (*grpc.ClientConn, error) { |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 76 | mappedPort, ok := p[port] |
| 77 | if !ok { |
Serge Bazanski | 52304a8 | 2021-10-29 16:56:18 +0200 | [diff] [blame^] | 78 | return nil, fmt.Errorf("cannot dial port: port %d is not mapped/forwarded", port) |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 79 | } |
Serge Bazanski | 52304a8 | 2021-10-29 16:56:18 +0200 | [diff] [blame^] | 80 | grpcClient, err := grpc.Dial(fmt.Sprintf("localhost:%d", mappedPort), opts...) |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 81 | if err != nil { |
Serge Bazanski | 52304a8 | 2021-10-29 16:56:18 +0200 | [diff] [blame^] | 82 | return nil, fmt.Errorf("failed to dial port %d: %w", port, err) |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 83 | } |
| 84 | return grpcClient, nil |
| 85 | } |
| 86 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 87 | // IdentityPortMap returns a port map where each given port is mapped onto itself |
| 88 | // on the host. This is mainly useful for development against Metropolis. The dbg |
| 89 | // command requires this mapping. |
Serge Bazanski | 52304a8 | 2021-10-29 16:56:18 +0200 | [diff] [blame^] | 90 | func IdentityPortMap(ports []node.Port) PortMap { |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 91 | portMap := make(PortMap) |
Lorenz Brun | ed0503c | 2020-07-28 17:21:25 +0200 | [diff] [blame] | 92 | for _, port := range ports { |
Serge Bazanski | 52304a8 | 2021-10-29 16:56:18 +0200 | [diff] [blame^] | 93 | portMap[port] = uint16(port) |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 94 | } |
| 95 | return portMap |
| 96 | } |
| 97 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 98 | // ConflictFreePortMap returns a port map where each given port is mapped onto a |
| 99 | // random free port on the host. This is intended for automated testing where |
| 100 | // multiple instances of Metropolis nodes might be running. Please call this |
| 101 | // function for each Launch command separately and as close to it as possible since |
| 102 | // it cannot guarantee that the ports will remain free. |
Serge Bazanski | 52304a8 | 2021-10-29 16:56:18 +0200 | [diff] [blame^] | 103 | func ConflictFreePortMap(ports []node.Port) (PortMap, error) { |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 104 | portMap := make(PortMap) |
Lorenz Brun | ed0503c | 2020-07-28 17:21:25 +0200 | [diff] [blame] | 105 | for _, port := range ports { |
Serge Bazanski | cb883e2 | 2020-07-06 17:47:55 +0200 | [diff] [blame] | 106 | mappedPort, listenCloser, err := freeport.AllocateTCPPort() |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 107 | if err != nil { |
| 108 | return portMap, fmt.Errorf("failed to get free host port: %w", err) |
| 109 | } |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 110 | // Defer closing of the listening port until the function is done and all ports are |
| 111 | // allocated |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 112 | defer listenCloser.Close() |
| 113 | portMap[port] = mappedPort |
| 114 | } |
| 115 | return portMap, nil |
| 116 | } |
| 117 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 118 | // NewSocketPair creates a new socket pair. By connecting both ends to different |
| 119 | // instances you can connect them with a virtual "network cable". The ends can be |
| 120 | // passed into the ConnectToSocket option. |
Lorenz Brun | 3ff5af3 | 2020-06-24 16:34:11 +0200 | [diff] [blame] | 121 | func NewSocketPair() (*os.File, *os.File, error) { |
| 122 | fds, err := unix.Socketpair(unix.AF_UNIX, syscall.SOCK_STREAM, 0) |
| 123 | if err != nil { |
| 124 | return nil, nil, fmt.Errorf("failed to call socketpair: %w", err) |
| 125 | } |
| 126 | |
| 127 | fd1 := os.NewFile(uintptr(fds[0]), "network0") |
| 128 | fd2 := os.NewFile(uintptr(fds[1]), "network1") |
| 129 | return fd1, fd2, nil |
| 130 | } |
| 131 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 132 | // HostInterfaceMAC is the MAC address the host SLIRP network interface has if it |
| 133 | // is not disabled (see DisableHostNetworkInterface in MicroVMOptions) |
Lorenz Brun | 3ff5af3 | 2020-06-24 16:34:11 +0200 | [diff] [blame] | 134 | var HostInterfaceMAC = net.HardwareAddr{0x02, 0x72, 0x82, 0xbf, 0xc3, 0x56} |
| 135 | |
| 136 | // MicroVMOptions contains all options to start a MicroVM |
| 137 | type MicroVMOptions struct { |
| 138 | // Path to the ELF kernel binary |
| 139 | KernelPath string |
| 140 | |
| 141 | // Path to the Initramfs |
| 142 | InitramfsPath string |
| 143 | |
| 144 | // Cmdline contains additional kernel commandline options |
| 145 | Cmdline string |
| 146 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 147 | // SerialPort is a File(descriptor) over which you can communicate with the serial |
| 148 | // port of the machine It can be set to an existing file descriptor (like |
| 149 | // os.Stdout/os.Stderr) or you can use NewSocketPair() to get one end to talk to |
| 150 | // from Go. |
Lorenz Brun | 3ff5af3 | 2020-06-24 16:34:11 +0200 | [diff] [blame] | 151 | SerialPort *os.File |
| 152 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 153 | // ExtraChardevs can be used similar to SerialPort, but can contain an arbitrary |
| 154 | // number of additional serial ports |
Lorenz Brun | 3ff5af3 | 2020-06-24 16:34:11 +0200 | [diff] [blame] | 155 | ExtraChardevs []*os.File |
| 156 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 157 | // ExtraNetworkInterfaces can contain an arbitrary number of file descriptors which |
| 158 | // are mapped into the VM as virtio network interfaces. The first interface is |
| 159 | // always a SLIRP-backed interface for communicating with the host. |
Lorenz Brun | 3ff5af3 | 2020-06-24 16:34:11 +0200 | [diff] [blame] | 160 | ExtraNetworkInterfaces []*os.File |
| 161 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 162 | // PortMap contains ports that are mapped to the host through the built-in SLIRP |
| 163 | // network interface. |
Lorenz Brun | 3ff5af3 | 2020-06-24 16:34:11 +0200 | [diff] [blame] | 164 | PortMap PortMap |
| 165 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 166 | // DisableHostNetworkInterface disables the SLIRP-backed host network interface |
| 167 | // that is normally the first network interface. If this is set PortMap is ignored. |
| 168 | // Mostly useful for speeding up QEMU's startup time for tests. |
Lorenz Brun | 3ff5af3 | 2020-06-24 16:34:11 +0200 | [diff] [blame] | 169 | DisableHostNetworkInterface bool |
| 170 | } |
| 171 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 172 | // RunMicroVM launches a tiny VM mostly intended for testing. Very quick to boot |
| 173 | // (<40ms). |
Lorenz Brun | 3ff5af3 | 2020-06-24 16:34:11 +0200 | [diff] [blame] | 174 | func RunMicroVM(ctx context.Context, opts *MicroVMOptions) error { |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 175 | // Generate options for all the file descriptors we'll be passing as virtio "serial |
| 176 | // ports" |
Lorenz Brun | 3ff5af3 | 2020-06-24 16:34:11 +0200 | [diff] [blame] | 177 | var extraArgs []string |
| 178 | for idx, _ := range opts.ExtraChardevs { |
| 179 | idxStr := strconv.Itoa(idx) |
| 180 | id := "extra" + idxStr |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 181 | // That this works is pretty much a hack, but upstream QEMU doesn't have a |
| 182 | // bidirectional chardev backend not based around files/sockets on the disk which |
| 183 | // are a giant pain to work with. We're using QEMU's fdset functionality to make |
| 184 | // FDs available as pseudo-files and then "ab"using the pipe backend's fallback |
| 185 | // functionality to get a single bidirectional chardev backend backed by a passed- |
| 186 | // down RDWR fd. Ref https://lists.gnu.org/archive/html/qemu-devel/2015- |
| 187 | // 12/msg01256.html |
Serge Bazanski | 66e5895 | 2021-10-05 17:06:56 +0200 | [diff] [blame] | 188 | addFdConf := QemuValue{ |
Lorenz Brun | 3ff5af3 | 2020-06-24 16:34:11 +0200 | [diff] [blame] | 189 | "set": {idxStr}, |
| 190 | "fd": {strconv.Itoa(idx + 3)}, |
| 191 | } |
Serge Bazanski | 66e5895 | 2021-10-05 17:06:56 +0200 | [diff] [blame] | 192 | chardevConf := QemuValue{ |
Lorenz Brun | 3ff5af3 | 2020-06-24 16:34:11 +0200 | [diff] [blame] | 193 | "id": {id}, |
| 194 | "path": {"/dev/fdset/" + idxStr}, |
| 195 | } |
Serge Bazanski | 66e5895 | 2021-10-05 17:06:56 +0200 | [diff] [blame] | 196 | deviceConf := QemuValue{ |
Lorenz Brun | 3ff5af3 | 2020-06-24 16:34:11 +0200 | [diff] [blame] | 197 | "chardev": {id}, |
| 198 | } |
Serge Bazanski | 66e5895 | 2021-10-05 17:06:56 +0200 | [diff] [blame] | 199 | extraArgs = append(extraArgs, "-add-fd", addFdConf.ToOption(""), |
| 200 | "-chardev", chardevConf.ToOption("pipe"), "-device", deviceConf.ToOption("virtserialport")) |
Lorenz Brun | 3ff5af3 | 2020-06-24 16:34:11 +0200 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | for idx, _ := range opts.ExtraNetworkInterfaces { |
| 204 | id := fmt.Sprintf("net%v", idx) |
Serge Bazanski | 66e5895 | 2021-10-05 17:06:56 +0200 | [diff] [blame] | 205 | netdevConf := QemuValue{ |
Lorenz Brun | 3ff5af3 | 2020-06-24 16:34:11 +0200 | [diff] [blame] | 206 | "id": {id}, |
| 207 | "fd": {strconv.Itoa(idx + 3 + len(opts.ExtraChardevs))}, |
| 208 | } |
Serge Bazanski | 66e5895 | 2021-10-05 17:06:56 +0200 | [diff] [blame] | 209 | extraArgs = append(extraArgs, "-netdev", netdevConf.ToOption("socket"), "-device", "virtio-net-device,netdev="+id) |
Lorenz Brun | 3ff5af3 | 2020-06-24 16:34:11 +0200 | [diff] [blame] | 210 | } |
| 211 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 212 | // This sets up a minimum viable environment for our Linux kernel. It clears all |
| 213 | // standard QEMU configuration and sets up a MicroVM machine |
| 214 | // (https://github.com/qemu/qemu/blob/master/docs/microvm.rst) with all legacy |
| 215 | // emulation turned off. This means the only "hardware" the Linux kernel inside can |
| 216 | // communicate with is a single virtio-mmio region. Over that MMIO interface we run |
| 217 | // a paravirtualized RNG (since the kernel in there has nothing to gather that from |
| 218 | // and it delays booting), a single paravirtualized console and an arbitrary number |
| 219 | // of extra serial ports for talking to various things that might run inside. The |
| 220 | // kernel, initramfs and command line are mapped into VM memory at boot time and |
| 221 | // not loaded from any sort of disk. Booting and shutting off one of these VMs |
| 222 | // takes <100ms. |
Lorenz Brun | 3ff5af3 | 2020-06-24 16:34:11 +0200 | [diff] [blame] | 223 | baseArgs := []string{"-nodefaults", "-no-user-config", "-nographic", "-no-reboot", |
| 224 | "-accel", "kvm", "-cpu", "host", |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 225 | // Needed until QEMU updates their bundled qboot version (needs |
| 226 | // https://github.com/bonzini/qboot/pull/28) |
Lorenz Brun | 3ff5af3 | 2020-06-24 16:34:11 +0200 | [diff] [blame] | 227 | "-bios", "external/com_github_bonzini_qboot/bios.bin", |
| 228 | "-M", "microvm,x-option-roms=off,pic=off,pit=off,rtc=off,isa-serial=off", |
| 229 | "-kernel", opts.KernelPath, |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 230 | // We force using a triple-fault reboot strategy since otherwise the kernel first |
| 231 | // tries others (like ACPI) which are not available in this very restricted |
| 232 | // environment. Similarly we need to override the boot console since there's |
| 233 | // nothing on the ISA bus that the kernel could talk to. We also force quiet for |
| 234 | // performance reasons. |
Lorenz Brun | 3ff5af3 | 2020-06-24 16:34:11 +0200 | [diff] [blame] | 235 | "-append", "reboot=t console=hvc0 quiet " + opts.Cmdline, |
| 236 | "-initrd", opts.InitramfsPath, |
| 237 | "-device", "virtio-rng-device,max-bytes=1024,period=1000", |
| 238 | "-device", "virtio-serial-device,max_ports=16", |
| 239 | "-chardev", "stdio,id=con0", "-device", "virtconsole,chardev=con0", |
| 240 | } |
| 241 | |
| 242 | if !opts.DisableHostNetworkInterface { |
| 243 | qemuNetType := "user" |
Serge Bazanski | 66e5895 | 2021-10-05 17:06:56 +0200 | [diff] [blame] | 244 | qemuNetConfig := QemuValue{ |
Lorenz Brun | 3ff5af3 | 2020-06-24 16:34:11 +0200 | [diff] [blame] | 245 | "id": {"usernet0"}, |
| 246 | "net": {"10.42.0.0/24"}, |
| 247 | "dhcpstart": {"10.42.0.10"}, |
| 248 | } |
| 249 | if opts.PortMap != nil { |
Serge Bazanski | 66e5895 | 2021-10-05 17:06:56 +0200 | [diff] [blame] | 250 | qemuNetConfig["hostfwd"] = opts.PortMap.ToQemuForwards() |
Lorenz Brun | 3ff5af3 | 2020-06-24 16:34:11 +0200 | [diff] [blame] | 251 | } |
| 252 | |
Serge Bazanski | 66e5895 | 2021-10-05 17:06:56 +0200 | [diff] [blame] | 253 | baseArgs = append(baseArgs, "-netdev", qemuNetConfig.ToOption(qemuNetType), |
Lorenz Brun | 3ff5af3 | 2020-06-24 16:34:11 +0200 | [diff] [blame] | 254 | "-device", "virtio-net-device,netdev=usernet0,mac="+HostInterfaceMAC.String()) |
| 255 | } |
| 256 | |
| 257 | var stdErrBuf bytes.Buffer |
| 258 | cmd := exec.CommandContext(ctx, "qemu-system-x86_64", append(baseArgs, extraArgs...)...) |
| 259 | cmd.Stdout = opts.SerialPort |
| 260 | cmd.Stderr = &stdErrBuf |
| 261 | |
| 262 | cmd.ExtraFiles = append(cmd.ExtraFiles, opts.ExtraChardevs...) |
| 263 | cmd.ExtraFiles = append(cmd.ExtraFiles, opts.ExtraNetworkInterfaces...) |
| 264 | |
| 265 | err := cmd.Run() |
Serge Bazanski | 66e5895 | 2021-10-05 17:06:56 +0200 | [diff] [blame] | 266 | // If it's a context error, just quit. There's no way to tell a |
| 267 | // killed-due-to-context vs killed-due-to-external-reason error returned by Run, |
| 268 | // so we approximate by looking at the context's status. |
| 269 | if err != nil && ctx.Err() != nil { |
| 270 | return ctx.Err() |
| 271 | } |
| 272 | |
Lorenz Brun | 3ff5af3 | 2020-06-24 16:34:11 +0200 | [diff] [blame] | 273 | var exerr *exec.ExitError |
| 274 | if err != nil && errors.As(err, &exerr) { |
| 275 | exerr.Stderr = stdErrBuf.Bytes() |
| 276 | newErr := QEMUError(*exerr) |
| 277 | return &newErr |
| 278 | } |
| 279 | return err |
| 280 | } |
| 281 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 282 | // QEMUError is a special type of ExitError used when QEMU fails. In addition to |
| 283 | // normal ExitError features it prints stderr for debugging. |
Lorenz Brun | 3ff5af3 | 2020-06-24 16:34:11 +0200 | [diff] [blame] | 284 | type QEMUError exec.ExitError |
| 285 | |
| 286 | func (e *QEMUError) Error() string { |
| 287 | return fmt.Sprintf("%v: %v", e.String(), string(e.Stderr)) |
Lorenz Brun | fc5dbc6 | 2020-05-28 12:18:07 +0200 | [diff] [blame] | 288 | } |