John Snow | 306dfcd | 2019-06-27 17:28:15 -0400 | [diff] [blame] | 1 | """ |
| 2 | QEMU machine module: |
| 3 | |
| 4 | The machine module primarily provides the QEMUMachine class, |
| 5 | which provides facilities for managing the lifetime of a QEMU VM. |
| 6 | """ |
| 7 | |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 8 | # Copyright (C) 2015-2016 Red Hat Inc. |
| 9 | # Copyright (C) 2012 IBM Corp. |
| 10 | # |
| 11 | # Authors: |
| 12 | # Fam Zheng <famz@redhat.com> |
| 13 | # |
| 14 | # This work is licensed under the terms of the GNU GPL, version 2. See |
| 15 | # the COPYING file in the top-level directory. |
| 16 | # |
| 17 | # Based on qmp.py. |
| 18 | # |
| 19 | |
| 20 | import errno |
John Snow | aad3f3b | 2020-10-06 19:58:06 -0400 | [diff] [blame] | 21 | from itertools import chain |
John Snow | 5690b43 | 2021-09-16 14:22:47 -0400 | [diff] [blame] | 22 | import locale |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 23 | import logging |
| 24 | import os |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 25 | import shutil |
John Snow | de6e08b | 2020-07-10 01:06:48 -0400 | [diff] [blame] | 26 | import signal |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 27 | import socket |
John Snow | 932ca4b | 2020-10-06 19:57:58 -0400 | [diff] [blame] | 28 | import subprocess |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 29 | import tempfile |
John Snow | 1dda040 | 2020-05-14 01:53:44 -0400 | [diff] [blame] | 30 | from types import TracebackType |
John Snow | aaa81ec | 2020-10-06 19:58:03 -0400 | [diff] [blame] | 31 | from typing import ( |
| 32 | Any, |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 33 | BinaryIO, |
John Snow | aaa81ec | 2020-10-06 19:58:03 -0400 | [diff] [blame] | 34 | Dict, |
| 35 | List, |
| 36 | Optional, |
John Snow | aad3f3b | 2020-10-06 19:58:06 -0400 | [diff] [blame] | 37 | Sequence, |
| 38 | Tuple, |
John Snow | aaa81ec | 2020-10-06 19:58:03 -0400 | [diff] [blame] | 39 | Type, |
Vladimir Sementsov-Ogievskiy | 15c3b86 | 2021-08-24 11:38:47 +0300 | [diff] [blame] | 40 | TypeVar, |
John Snow | aaa81ec | 2020-10-06 19:58:03 -0400 | [diff] [blame] | 41 | ) |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 42 | |
John Snow | 37094b6 | 2022-03-30 13:28:10 -0400 | [diff] [blame] | 43 | from qemu.qmp import SocketAddrT |
| 44 | from qemu.qmp.legacy import ( |
John Snow | a422530 | 2022-03-21 16:33:12 -0400 | [diff] [blame] | 45 | QEMUMonitorProtocol, |
John Snow | beb6b57 | 2021-05-27 17:16:53 -0400 | [diff] [blame] | 46 | QMPMessage, |
| 47 | QMPReturnValue, |
John Snow | beb6b57 | 2021-05-27 17:16:53 -0400 | [diff] [blame] | 48 | ) |
| 49 | |
| 50 | from . import console_socket |
John Snow | 932ca4b | 2020-10-06 19:57:58 -0400 | [diff] [blame] | 51 | |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 52 | |
| 53 | LOG = logging.getLogger(__name__) |
| 54 | |
John Snow | 8dfac2e | 2020-05-28 18:21:29 -0400 | [diff] [blame] | 55 | |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 56 | class QEMUMachineError(Exception): |
| 57 | """ |
| 58 | Exception called when an error in QEMUMachine happens. |
| 59 | """ |
| 60 | |
| 61 | |
| 62 | class QEMUMachineAddDeviceError(QEMUMachineError): |
| 63 | """ |
| 64 | Exception raised when a request to add a device can not be fulfilled |
| 65 | |
| 66 | The failures are caused by limitations, lack of information or conflicting |
| 67 | requests on the QEMUMachine methods. This exception does not represent |
| 68 | failures reported by the QEMU binary itself. |
| 69 | """ |
| 70 | |
| 71 | |
John Snow | 50465f9 | 2022-01-31 23:11:32 -0500 | [diff] [blame] | 72 | class VMLaunchFailure(QEMUMachineError): |
| 73 | """ |
| 74 | Exception raised when a VM launch was attempted, but failed. |
| 75 | """ |
| 76 | def __init__(self, exitcode: Optional[int], |
| 77 | command: str, output: Optional[str]): |
| 78 | super().__init__(exitcode, command, output) |
| 79 | self.exitcode = exitcode |
| 80 | self.command = command |
| 81 | self.output = output |
| 82 | |
| 83 | def __str__(self) -> str: |
| 84 | ret = '' |
| 85 | if self.__cause__ is not None: |
| 86 | name = type(self.__cause__).__name__ |
| 87 | reason = str(self.__cause__) |
| 88 | if reason: |
| 89 | ret += f"{name}: {reason}" |
| 90 | else: |
| 91 | ret += f"{name}" |
| 92 | ret += '\n' |
| 93 | |
| 94 | if self.exitcode is not None: |
| 95 | ret += f"\tExit code: {self.exitcode}\n" |
| 96 | ret += f"\tCommand: {self.command}\n" |
| 97 | ret += f"\tOutput: {self.output}\n" |
| 98 | return ret |
| 99 | |
| 100 | |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 101 | class AbnormalShutdown(QEMUMachineError): |
| 102 | """ |
| 103 | Exception raised when a graceful shutdown was requested, but not performed. |
| 104 | """ |
| 105 | |
| 106 | |
Vladimir Sementsov-Ogievskiy | 15c3b86 | 2021-08-24 11:38:47 +0300 | [diff] [blame] | 107 | _T = TypeVar('_T', bound='QEMUMachine') |
| 108 | |
| 109 | |
John Snow | 9b8ccd6 | 2020-05-28 18:21:28 -0400 | [diff] [blame] | 110 | class QEMUMachine: |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 111 | """ |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 112 | A QEMU VM. |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 113 | |
John Snow | 8dfac2e | 2020-05-28 18:21:29 -0400 | [diff] [blame] | 114 | Use this object as a context manager to ensure |
| 115 | the QEMU process terminates:: |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 116 | |
| 117 | with VM(binary) as vm: |
| 118 | ... |
| 119 | # vm is guaranteed to be shut down here |
| 120 | """ |
John Snow | 82e6517 | 2021-06-29 17:43:11 -0400 | [diff] [blame] | 121 | # pylint: disable=too-many-instance-attributes, too-many-public-methods |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 122 | |
John Snow | aad3f3b | 2020-10-06 19:58:06 -0400 | [diff] [blame] | 123 | def __init__(self, |
| 124 | binary: str, |
| 125 | args: Sequence[str] = (), |
| 126 | wrapper: Sequence[str] = (), |
| 127 | name: Optional[str] = None, |
Cleber Rosa | 2ca6e26 | 2021-02-11 17:01:42 -0500 | [diff] [blame] | 128 | base_temp_dir: str = "/var/tmp", |
John Snow | c4e6023 | 2020-10-06 19:57:59 -0400 | [diff] [blame] | 129 | monitor_address: Optional[SocketAddrT] = None, |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 130 | sock_dir: Optional[str] = None, |
| 131 | drain_console: bool = False, |
Cleber Rosa | b306e26 | 2021-02-11 16:55:05 -0500 | [diff] [blame] | 132 | console_log: Optional[str] = None, |
Emanuele Giuseppe Esposito | e2f948a | 2021-08-09 11:00:59 +0200 | [diff] [blame] | 133 | log_dir: Optional[str] = None, |
| 134 | qmp_timer: Optional[float] = None): |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 135 | ''' |
| 136 | Initialize a QEMUMachine |
| 137 | |
| 138 | @param binary: path to the qemu binary |
| 139 | @param args: list of extra arguments |
| 140 | @param wrapper: list of arguments used as prefix to qemu binary |
| 141 | @param name: prefix for socket and log file names (default: qemu-PID) |
John Snow | 859aeb6 | 2021-05-27 17:16:51 -0400 | [diff] [blame] | 142 | @param base_temp_dir: default location where temp files are created |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 143 | @param monitor_address: address for QMP monitor |
Cleber Rosa | 2ca6e26 | 2021-02-11 17:01:42 -0500 | [diff] [blame] | 144 | @param sock_dir: where to create socket (defaults to base_temp_dir) |
Robert Foley | 0fc8f66 | 2020-07-01 14:56:24 +0100 | [diff] [blame] | 145 | @param drain_console: (optional) True to drain console socket to buffer |
John Snow | c5e61a6 | 2020-10-06 19:58:00 -0400 | [diff] [blame] | 146 | @param console_log: (optional) path to console log file |
Cleber Rosa | b306e26 | 2021-02-11 16:55:05 -0500 | [diff] [blame] | 147 | @param log_dir: where to create and keep log files |
Emanuele Giuseppe Esposito | e2f948a | 2021-08-09 11:00:59 +0200 | [diff] [blame] | 148 | @param qmp_timer: (optional) default QMP socket timeout |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 149 | @note: Qemu process is not started until launch() is used. |
| 150 | ''' |
John Snow | 82e6517 | 2021-06-29 17:43:11 -0400 | [diff] [blame] | 151 | # pylint: disable=too-many-arguments |
| 152 | |
John Snow | c5e61a6 | 2020-10-06 19:58:00 -0400 | [diff] [blame] | 153 | # Direct user configuration |
| 154 | |
| 155 | self._binary = binary |
John Snow | c5e61a6 | 2020-10-06 19:58:00 -0400 | [diff] [blame] | 156 | self._args = list(args) |
John Snow | c5e61a6 | 2020-10-06 19:58:00 -0400 | [diff] [blame] | 157 | self._wrapper = wrapper |
Emanuele Giuseppe Esposito | e2f948a | 2021-08-09 11:00:59 +0200 | [diff] [blame] | 158 | self._qmp_timer = qmp_timer |
John Snow | c5e61a6 | 2020-10-06 19:58:00 -0400 | [diff] [blame] | 159 | |
John Snow | 72b17fe | 2021-11-18 15:46:16 -0500 | [diff] [blame] | 160 | self._name = name or f"qemu-{os.getpid()}-{id(self):02x}" |
John Snow | 87bf1fe | 2021-11-18 15:46:14 -0500 | [diff] [blame] | 161 | self._temp_dir: Optional[str] = None |
Cleber Rosa | 2ca6e26 | 2021-02-11 17:01:42 -0500 | [diff] [blame] | 162 | self._base_temp_dir = base_temp_dir |
John Snow | 87bf1fe | 2021-11-18 15:46:14 -0500 | [diff] [blame] | 163 | self._sock_dir = sock_dir |
Cleber Rosa | b306e26 | 2021-02-11 16:55:05 -0500 | [diff] [blame] | 164 | self._log_dir = log_dir |
John Snow | c5e61a6 | 2020-10-06 19:58:00 -0400 | [diff] [blame] | 165 | |
John Snow | c4e6023 | 2020-10-06 19:57:59 -0400 | [diff] [blame] | 166 | if monitor_address is not None: |
| 167 | self._monitor_address = monitor_address |
John Snow | c4e6023 | 2020-10-06 19:57:59 -0400 | [diff] [blame] | 168 | else: |
| 169 | self._monitor_address = os.path.join( |
John Snow | 87bf1fe | 2021-11-18 15:46:14 -0500 | [diff] [blame] | 170 | self.sock_dir, f"{self._name}-monitor.sock" |
John Snow | c4e6023 | 2020-10-06 19:57:59 -0400 | [diff] [blame] | 171 | ) |
John Snow | c5e61a6 | 2020-10-06 19:58:00 -0400 | [diff] [blame] | 172 | |
| 173 | self._console_log_path = console_log |
| 174 | if self._console_log_path: |
| 175 | # In order to log the console, buffering needs to be enabled. |
| 176 | self._drain_console = True |
| 177 | else: |
| 178 | self._drain_console = drain_console |
| 179 | |
| 180 | # Runstate |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 181 | self._qemu_log_path: Optional[str] = None |
| 182 | self._qemu_log_file: Optional[BinaryIO] = None |
John Snow | 9223fda | 2020-10-06 19:58:05 -0400 | [diff] [blame] | 183 | self._popen: Optional['subprocess.Popen[bytes]'] = None |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 184 | self._events: List[QMPMessage] = [] |
| 185 | self._iolog: Optional[str] = None |
Wainer dos Santos Moschetta | 74b56bb | 2019-12-11 13:55:35 -0500 | [diff] [blame] | 186 | self._qmp_set = True # Enable QMP monitor by default. |
John Snow | beb6b57 | 2021-05-27 17:16:53 -0400 | [diff] [blame] | 187 | self._qmp_connection: Optional[QEMUMonitorProtocol] = None |
John Snow | aad3f3b | 2020-10-06 19:58:06 -0400 | [diff] [blame] | 188 | self._qemu_full_args: Tuple[str, ...] = () |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 189 | self._launched = False |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 190 | self._machine: Optional[str] = None |
Philippe Mathieu-Daudé | 746f244 | 2020-01-21 00:51:56 +0100 | [diff] [blame] | 191 | self._console_index = 0 |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 192 | self._console_set = False |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 193 | self._console_device_type: Optional[str] = None |
John Snow | 652809d | 2020-10-06 19:58:01 -0400 | [diff] [blame] | 194 | self._console_address = os.path.join( |
John Snow | 87bf1fe | 2021-11-18 15:46:14 -0500 | [diff] [blame] | 195 | self.sock_dir, f"{self._name}-console.sock" |
John Snow | 652809d | 2020-10-06 19:58:01 -0400 | [diff] [blame] | 196 | ) |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 197 | self._console_socket: Optional[socket.socket] = None |
| 198 | self._remove_files: List[str] = [] |
John Snow | de6e08b | 2020-07-10 01:06:48 -0400 | [diff] [blame] | 199 | self._user_killed = False |
John Snow | b9420e4 | 2021-10-26 13:56:05 -0400 | [diff] [blame] | 200 | self._quit_issued = False |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 201 | |
Vladimir Sementsov-Ogievskiy | 15c3b86 | 2021-08-24 11:38:47 +0300 | [diff] [blame] | 202 | def __enter__(self: _T) -> _T: |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 203 | return self |
| 204 | |
John Snow | 1dda040 | 2020-05-14 01:53:44 -0400 | [diff] [blame] | 205 | def __exit__(self, |
| 206 | exc_type: Optional[Type[BaseException]], |
| 207 | exc_val: Optional[BaseException], |
| 208 | exc_tb: Optional[TracebackType]) -> None: |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 209 | self.shutdown() |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 210 | |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 211 | def add_monitor_null(self) -> None: |
John Snow | 306dfcd | 2019-06-27 17:28:15 -0400 | [diff] [blame] | 212 | """ |
| 213 | This can be used to add an unused monitor instance. |
| 214 | """ |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 215 | self._args.append('-monitor') |
| 216 | self._args.append('null') |
| 217 | |
Vladimir Sementsov-Ogievskiy | 15c3b86 | 2021-08-24 11:38:47 +0300 | [diff] [blame] | 218 | def add_fd(self: _T, fd: int, fdset: int, |
| 219 | opaque: str, opts: str = '') -> _T: |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 220 | """ |
| 221 | Pass a file descriptor to the VM |
| 222 | """ |
| 223 | options = ['fd=%d' % fd, |
| 224 | 'set=%d' % fdset, |
| 225 | 'opaque=%s' % opaque] |
| 226 | if opts: |
| 227 | options.append(opts) |
| 228 | |
| 229 | # This did not exist before 3.4, but since then it is |
| 230 | # mandatory for our purpose |
| 231 | if hasattr(os, 'set_inheritable'): |
| 232 | os.set_inheritable(fd, True) |
| 233 | |
| 234 | self._args.append('-add-fd') |
| 235 | self._args.append(','.join(options)) |
| 236 | return self |
| 237 | |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 238 | def send_fd_scm(self, fd: Optional[int] = None, |
| 239 | file_path: Optional[str] = None) -> int: |
John Snow | 306dfcd | 2019-06-27 17:28:15 -0400 | [diff] [blame] | 240 | """ |
John Snow | 514d00d | 2021-09-22 20:49:30 -0400 | [diff] [blame] | 241 | Send an fd or file_path to the remote via SCM_RIGHTS. |
John Snow | 306dfcd | 2019-06-27 17:28:15 -0400 | [diff] [blame] | 242 | |
John Snow | 514d00d | 2021-09-22 20:49:30 -0400 | [diff] [blame] | 243 | Exactly one of fd and file_path must be given. If it is |
| 244 | file_path, the file will be opened read-only and the new file |
| 245 | descriptor will be sent to the remote. |
John Snow | 306dfcd | 2019-06-27 17:28:15 -0400 | [diff] [blame] | 246 | """ |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 247 | if file_path is not None: |
| 248 | assert fd is None |
John Snow | 514d00d | 2021-09-22 20:49:30 -0400 | [diff] [blame] | 249 | with open(file_path, "rb") as passfile: |
| 250 | fd = passfile.fileno() |
| 251 | self._qmp.send_fd_scm(fd) |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 252 | else: |
| 253 | assert fd is not None |
John Snow | 514d00d | 2021-09-22 20:49:30 -0400 | [diff] [blame] | 254 | self._qmp.send_fd_scm(fd) |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 255 | |
John Snow | 514d00d | 2021-09-22 20:49:30 -0400 | [diff] [blame] | 256 | return 0 |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 257 | |
| 258 | @staticmethod |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 259 | def _remove_if_exists(path: str) -> None: |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 260 | """ |
| 261 | Remove file object at path if it exists |
| 262 | """ |
| 263 | try: |
| 264 | os.remove(path) |
| 265 | except OSError as exception: |
| 266 | if exception.errno == errno.ENOENT: |
| 267 | return |
| 268 | raise |
| 269 | |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 270 | def is_running(self) -> bool: |
John Snow | 306dfcd | 2019-06-27 17:28:15 -0400 | [diff] [blame] | 271 | """Returns true if the VM is running.""" |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 272 | return self._popen is not None and self._popen.poll() is None |
| 273 | |
John Snow | 9223fda | 2020-10-06 19:58:05 -0400 | [diff] [blame] | 274 | @property |
| 275 | def _subp(self) -> 'subprocess.Popen[bytes]': |
| 276 | if self._popen is None: |
| 277 | raise QEMUMachineError('Subprocess pipe not present') |
| 278 | return self._popen |
| 279 | |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 280 | def exitcode(self) -> Optional[int]: |
John Snow | 306dfcd | 2019-06-27 17:28:15 -0400 | [diff] [blame] | 281 | """Returns the exit code if possible, or None.""" |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 282 | if self._popen is None: |
| 283 | return None |
| 284 | return self._popen.poll() |
| 285 | |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 286 | def get_pid(self) -> Optional[int]: |
John Snow | 306dfcd | 2019-06-27 17:28:15 -0400 | [diff] [blame] | 287 | """Returns the PID of the running process, or None.""" |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 288 | if not self.is_running(): |
| 289 | return None |
John Snow | 9223fda | 2020-10-06 19:58:05 -0400 | [diff] [blame] | 290 | return self._subp.pid |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 291 | |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 292 | def _load_io_log(self) -> None: |
John Snow | 5690b43 | 2021-09-16 14:22:47 -0400 | [diff] [blame] | 293 | # Assume that the output encoding of QEMU's terminal output is |
| 294 | # defined by our locale. If indeterminate, allow open() to fall |
| 295 | # back to the platform default. |
| 296 | _, encoding = locale.getlocale() |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 297 | if self._qemu_log_path is not None: |
John Snow | 5690b43 | 2021-09-16 14:22:47 -0400 | [diff] [blame] | 298 | with open(self._qemu_log_path, "r", encoding=encoding) as iolog: |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 299 | self._iolog = iolog.read() |
| 300 | |
John Snow | 652809d | 2020-10-06 19:58:01 -0400 | [diff] [blame] | 301 | @property |
| 302 | def _base_args(self) -> List[str]: |
Wainer dos Santos Moschetta | 74b56bb | 2019-12-11 13:55:35 -0500 | [diff] [blame] | 303 | args = ['-display', 'none', '-vga', 'none'] |
John Snow | c4e6023 | 2020-10-06 19:57:59 -0400 | [diff] [blame] | 304 | |
Wainer dos Santos Moschetta | 74b56bb | 2019-12-11 13:55:35 -0500 | [diff] [blame] | 305 | if self._qmp_set: |
| 306 | if isinstance(self._monitor_address, tuple): |
John Snow | c4e6023 | 2020-10-06 19:57:59 -0400 | [diff] [blame] | 307 | moncdev = "socket,id=mon,host={},port={}".format( |
| 308 | *self._monitor_address |
| 309 | ) |
Wainer dos Santos Moschetta | 74b56bb | 2019-12-11 13:55:35 -0500 | [diff] [blame] | 310 | else: |
John Snow | c4e6023 | 2020-10-06 19:57:59 -0400 | [diff] [blame] | 311 | moncdev = f"socket,id=mon,path={self._monitor_address}" |
Wainer dos Santos Moschetta | 74b56bb | 2019-12-11 13:55:35 -0500 | [diff] [blame] | 312 | args.extend(['-chardev', moncdev, '-mon', |
| 313 | 'chardev=mon,mode=control']) |
John Snow | c4e6023 | 2020-10-06 19:57:59 -0400 | [diff] [blame] | 314 | |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 315 | if self._machine is not None: |
| 316 | args.extend(['-machine', self._machine]) |
John Snow | 9b8ccd6 | 2020-05-28 18:21:28 -0400 | [diff] [blame] | 317 | for _ in range(self._console_index): |
Philippe Mathieu-Daudé | 746f244 | 2020-01-21 00:51:56 +0100 | [diff] [blame] | 318 | args.extend(['-serial', 'null']) |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 319 | if self._console_set: |
Paolo Bonzini | 991c180 | 2020-11-13 03:10:52 -0500 | [diff] [blame] | 320 | chardev = ('socket,id=console,path=%s,server=on,wait=off' % |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 321 | self._console_address) |
| 322 | args.extend(['-chardev', chardev]) |
| 323 | if self._console_device_type is None: |
| 324 | args.extend(['-serial', 'chardev:console']) |
| 325 | else: |
| 326 | device = '%s,chardev=console' % self._console_device_type |
| 327 | args.extend(['-device', device]) |
| 328 | return args |
| 329 | |
Wainer dos Santos Moschetta | 555fe0c | 2021-04-30 10:34:12 -0300 | [diff] [blame] | 330 | @property |
| 331 | def args(self) -> List[str]: |
| 332 | """Returns the list of arguments given to the QEMU binary.""" |
| 333 | return self._args |
| 334 | |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 335 | def _pre_launch(self) -> None: |
John Snow | 652809d | 2020-10-06 19:58:01 -0400 | [diff] [blame] | 336 | if self._console_set: |
| 337 | self._remove_files.append(self._console_address) |
| 338 | |
Wainer dos Santos Moschetta | 74b56bb | 2019-12-11 13:55:35 -0500 | [diff] [blame] | 339 | if self._qmp_set: |
John Snow | 6eeb3de | 2021-11-18 15:46:15 -0500 | [diff] [blame] | 340 | if isinstance(self._monitor_address, str): |
John Snow | c4e6023 | 2020-10-06 19:57:59 -0400 | [diff] [blame] | 341 | self._remove_files.append(self._monitor_address) |
John Snow | beb6b57 | 2021-05-27 17:16:53 -0400 | [diff] [blame] | 342 | self._qmp_connection = QEMUMonitorProtocol( |
John Snow | c4e6023 | 2020-10-06 19:57:59 -0400 | [diff] [blame] | 343 | self._monitor_address, |
| 344 | server=True, |
| 345 | nickname=self._name |
| 346 | ) |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 347 | |
John Snow | 63c33f3 | 2021-05-27 17:16:49 -0400 | [diff] [blame] | 348 | # NOTE: Make sure any opened resources are *definitely* freed in |
| 349 | # _post_shutdown()! |
| 350 | # pylint: disable=consider-using-with |
Cleber Rosa | b306e26 | 2021-02-11 16:55:05 -0500 | [diff] [blame] | 351 | self._qemu_log_path = os.path.join(self.log_dir, self._name + ".log") |
John Snow | 63c33f3 | 2021-05-27 17:16:49 -0400 | [diff] [blame] | 352 | self._qemu_log_file = open(self._qemu_log_path, 'wb') |
| 353 | |
John Snow | b1ca991 | 2021-11-18 15:46:17 -0500 | [diff] [blame] | 354 | self._iolog = None |
| 355 | self._qemu_full_args = tuple(chain( |
| 356 | self._wrapper, |
| 357 | [self._binary], |
| 358 | self._base_args, |
| 359 | self._args |
| 360 | )) |
| 361 | |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 362 | def _post_launch(self) -> None: |
John Snow | be1183e | 2020-10-06 19:58:04 -0400 | [diff] [blame] | 363 | if self._qmp_connection: |
Emanuele Giuseppe Esposito | e2f948a | 2021-08-09 11:00:59 +0200 | [diff] [blame] | 364 | self._qmp.accept(self._qmp_timer) |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 365 | |
Emanuele Giuseppe Esposito | eb7a91d | 2021-08-09 11:01:13 +0200 | [diff] [blame] | 366 | def _close_qemu_log_file(self) -> None: |
| 367 | if self._qemu_log_file is not None: |
| 368 | self._qemu_log_file.close() |
| 369 | self._qemu_log_file = None |
| 370 | |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 371 | def _post_shutdown(self) -> None: |
John Snow | a3842cb | 2020-07-10 01:06:42 -0400 | [diff] [blame] | 372 | """ |
| 373 | Called to cleanup the VM instance after the process has exited. |
| 374 | May also be called after a failed launch. |
| 375 | """ |
John Snow | 9cccb33 | 2022-10-27 14:58:35 -0400 | [diff] [blame^] | 376 | LOG.debug("Cleaning up after VM process") |
John Snow | 49a608b | 2021-10-26 13:56:06 -0400 | [diff] [blame] | 377 | try: |
| 378 | self._close_qmp_connection() |
| 379 | except Exception as err: # pylint: disable=broad-except |
| 380 | LOG.warning( |
| 381 | "Exception closing QMP connection: %s", |
| 382 | str(err) if str(err) else type(err).__name__ |
| 383 | ) |
| 384 | finally: |
| 385 | assert self._qmp_connection is None |
John Snow | 671940e | 2020-07-10 01:06:39 -0400 | [diff] [blame] | 386 | |
Emanuele Giuseppe Esposito | eb7a91d | 2021-08-09 11:01:13 +0200 | [diff] [blame] | 387 | self._close_qemu_log_file() |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 388 | |
Cleber Rosa | 3c1e16c | 2021-02-11 17:01:41 -0500 | [diff] [blame] | 389 | self._load_io_log() |
| 390 | |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 391 | self._qemu_log_path = None |
| 392 | |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 393 | if self._temp_dir is not None: |
| 394 | shutil.rmtree(self._temp_dir) |
| 395 | self._temp_dir = None |
| 396 | |
Max Reitz | 32558ce | 2019-10-17 15:31:34 +0200 | [diff] [blame] | 397 | while len(self._remove_files) > 0: |
| 398 | self._remove_if_exists(self._remove_files.pop()) |
| 399 | |
John Snow | 14661d9 | 2020-07-10 01:06:38 -0400 | [diff] [blame] | 400 | exitcode = self.exitcode() |
John Snow | de6e08b | 2020-07-10 01:06:48 -0400 | [diff] [blame] | 401 | if (exitcode is not None and exitcode < 0 |
| 402 | and not (self._user_killed and exitcode == -signal.SIGKILL)): |
John Snow | 14661d9 | 2020-07-10 01:06:38 -0400 | [diff] [blame] | 403 | msg = 'qemu received signal %i; command: "%s"' |
| 404 | if self._qemu_full_args: |
| 405 | command = ' '.join(self._qemu_full_args) |
| 406 | else: |
| 407 | command = '' |
| 408 | LOG.warning(msg, -int(exitcode), command) |
| 409 | |
John Snow | b9420e4 | 2021-10-26 13:56:05 -0400 | [diff] [blame] | 410 | self._quit_issued = False |
John Snow | de6e08b | 2020-07-10 01:06:48 -0400 | [diff] [blame] | 411 | self._user_killed = False |
John Snow | 14661d9 | 2020-07-10 01:06:38 -0400 | [diff] [blame] | 412 | self._launched = False |
| 413 | |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 414 | def launch(self) -> None: |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 415 | """ |
| 416 | Launch the VM and make sure we cleanup and expose the |
| 417 | command line/output in case of exception |
| 418 | """ |
| 419 | |
| 420 | if self._launched: |
| 421 | raise QEMUMachineError('VM already launched') |
| 422 | |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 423 | try: |
| 424 | self._launch() |
John Snow | 50465f9 | 2022-01-31 23:11:32 -0500 | [diff] [blame] | 425 | except BaseException as exc: |
John Snow | 1611e6c | 2021-11-18 15:46:18 -0500 | [diff] [blame] | 426 | # We may have launched the process but it may |
| 427 | # have exited before we could connect via QMP. |
| 428 | # Assume the VM didn't launch or is exiting. |
| 429 | # If we don't wait for the process, exitcode() may still be |
| 430 | # 'None' by the time control is ceded back to the caller. |
| 431 | if self._launched: |
| 432 | self.wait() |
| 433 | else: |
| 434 | self._post_shutdown() |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 435 | |
John Snow | 50465f9 | 2022-01-31 23:11:32 -0500 | [diff] [blame] | 436 | if isinstance(exc, Exception): |
| 437 | raise VMLaunchFailure( |
| 438 | exitcode=self.exitcode(), |
| 439 | command=' '.join(self._qemu_full_args), |
| 440 | output=self._iolog |
| 441 | ) from exc |
| 442 | |
| 443 | # Don't wrap 'BaseException'; doing so would downgrade |
| 444 | # that exception. However, we still want to clean up. |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 445 | raise |
| 446 | |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 447 | def _launch(self) -> None: |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 448 | """ |
| 449 | Launch the VM and establish a QMP connection |
| 450 | """ |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 451 | self._pre_launch() |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 452 | LOG.debug('VM launch command: %r', ' '.join(self._qemu_full_args)) |
John Snow | a0eae17 | 2021-05-27 17:16:50 -0400 | [diff] [blame] | 453 | |
| 454 | # Cleaning up of this subprocess is guaranteed by _do_shutdown. |
| 455 | # pylint: disable=consider-using-with |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 456 | self._popen = subprocess.Popen(self._qemu_full_args, |
John Snow | 07b7123 | 2021-05-27 17:16:46 -0400 | [diff] [blame] | 457 | stdin=subprocess.DEVNULL, |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 458 | stdout=self._qemu_log_file, |
| 459 | stderr=subprocess.STDOUT, |
| 460 | shell=False, |
| 461 | close_fds=False) |
John Snow | 1611e6c | 2021-11-18 15:46:18 -0500 | [diff] [blame] | 462 | self._launched = True |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 463 | self._post_launch() |
| 464 | |
John Snow | 49a608b | 2021-10-26 13:56:06 -0400 | [diff] [blame] | 465 | def _close_qmp_connection(self) -> None: |
| 466 | """ |
| 467 | Close the underlying QMP connection, if any. |
| 468 | |
| 469 | Dutifully report errors that occurred while closing, but assume |
| 470 | that any error encountered indicates an abnormal termination |
| 471 | process and not a failure to close. |
| 472 | """ |
| 473 | if self._qmp_connection is None: |
| 474 | return |
| 475 | |
| 476 | try: |
| 477 | self._qmp.close() |
| 478 | except EOFError: |
| 479 | # EOF can occur as an Exception here when using the Async |
| 480 | # QMP backend. It indicates that the server closed the |
| 481 | # stream. If we successfully issued 'quit' at any point, |
| 482 | # then this was expected. If the remote went away without |
| 483 | # our permission, it's worth reporting that as an abnormal |
| 484 | # shutdown case. |
| 485 | if not (self._user_killed or self._quit_issued): |
| 486 | raise |
| 487 | finally: |
| 488 | self._qmp_connection = None |
| 489 | |
John Snow | e2c97f1 | 2020-07-10 01:06:40 -0400 | [diff] [blame] | 490 | def _early_cleanup(self) -> None: |
| 491 | """ |
| 492 | Perform any cleanup that needs to happen before the VM exits. |
John Snow | a3842cb | 2020-07-10 01:06:42 -0400 | [diff] [blame] | 493 | |
John Snow | 1611e6c | 2021-11-18 15:46:18 -0500 | [diff] [blame] | 494 | This method may be called twice upon shutdown, once each by soft |
| 495 | and hard shutdown in failover scenarios. |
John Snow | e2c97f1 | 2020-07-10 01:06:40 -0400 | [diff] [blame] | 496 | """ |
| 497 | # If we keep the console socket open, we may deadlock waiting |
| 498 | # for QEMU to exit, while QEMU is waiting for the socket to |
Peter Maydell | 9323e79 | 2022-06-08 19:38:47 +0100 | [diff] [blame] | 499 | # become writable. |
John Snow | e2c97f1 | 2020-07-10 01:06:40 -0400 | [diff] [blame] | 500 | if self._console_socket is not None: |
John Snow | 9cccb33 | 2022-10-27 14:58:35 -0400 | [diff] [blame^] | 501 | LOG.debug("Closing console socket") |
John Snow | e2c97f1 | 2020-07-10 01:06:40 -0400 | [diff] [blame] | 502 | self._console_socket.close() |
| 503 | self._console_socket = None |
| 504 | |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 505 | def _hard_shutdown(self) -> None: |
| 506 | """ |
| 507 | Perform early cleanup, kill the VM, and wait for it to terminate. |
| 508 | |
| 509 | :raise subprocess.Timeout: When timeout is exceeds 60 seconds |
| 510 | waiting for the QEMU process to terminate. |
| 511 | """ |
John Snow | 9cccb33 | 2022-10-27 14:58:35 -0400 | [diff] [blame^] | 512 | LOG.debug("Performing hard shutdown") |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 513 | self._early_cleanup() |
John Snow | 9223fda | 2020-10-06 19:58:05 -0400 | [diff] [blame] | 514 | self._subp.kill() |
| 515 | self._subp.wait(timeout=60) |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 516 | |
John Snow | b9420e4 | 2021-10-26 13:56:05 -0400 | [diff] [blame] | 517 | def _soft_shutdown(self, timeout: Optional[int]) -> None: |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 518 | """ |
| 519 | Perform early cleanup, attempt to gracefully shut down the VM, and wait |
| 520 | for it to terminate. |
| 521 | |
John Snow | 8226a4b | 2020-07-20 12:02:52 -0400 | [diff] [blame] | 522 | :param timeout: Timeout in seconds for graceful shutdown. |
| 523 | A value of None is an infinite wait. |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 524 | |
| 525 | :raise ConnectionReset: On QMP communication errors |
| 526 | :raise subprocess.TimeoutExpired: When timeout is exceeded waiting for |
| 527 | the QEMU process to terminate. |
| 528 | """ |
John Snow | 9cccb33 | 2022-10-27 14:58:35 -0400 | [diff] [blame^] | 529 | LOG.debug("Attempting graceful termination") |
| 530 | |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 531 | self._early_cleanup() |
| 532 | |
John Snow | 9cccb33 | 2022-10-27 14:58:35 -0400 | [diff] [blame^] | 533 | if self._quit_issued: |
| 534 | LOG.debug( |
| 535 | "Anticipating QEMU termination due to prior 'quit' command, " |
| 536 | "or explicit call to wait()" |
| 537 | ) |
| 538 | else: |
| 539 | LOG.debug("Politely asking QEMU to terminate") |
| 540 | |
John Snow | be1183e | 2020-10-06 19:58:04 -0400 | [diff] [blame] | 541 | if self._qmp_connection: |
John Snow | 49a608b | 2021-10-26 13:56:06 -0400 | [diff] [blame] | 542 | try: |
| 543 | if not self._quit_issued: |
| 544 | # May raise ExecInterruptedError or StateError if the |
| 545 | # connection dies or has *already* died. |
| 546 | self.qmp('quit') |
| 547 | finally: |
| 548 | # Regardless, we want to quiesce the connection. |
| 549 | self._close_qmp_connection() |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 550 | |
| 551 | # May raise subprocess.TimeoutExpired |
John Snow | 9cccb33 | 2022-10-27 14:58:35 -0400 | [diff] [blame^] | 552 | LOG.debug( |
| 553 | "Waiting (timeout=%s) for QEMU process (pid=%s) to terminate", |
| 554 | timeout, self._subp.pid |
| 555 | ) |
John Snow | 9223fda | 2020-10-06 19:58:05 -0400 | [diff] [blame] | 556 | self._subp.wait(timeout=timeout) |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 557 | |
John Snow | b9420e4 | 2021-10-26 13:56:05 -0400 | [diff] [blame] | 558 | def _do_shutdown(self, timeout: Optional[int]) -> None: |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 559 | """ |
| 560 | Attempt to shutdown the VM gracefully; fallback to a hard shutdown. |
| 561 | |
John Snow | 8226a4b | 2020-07-20 12:02:52 -0400 | [diff] [blame] | 562 | :param timeout: Timeout in seconds for graceful shutdown. |
| 563 | A value of None is an infinite wait. |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 564 | |
| 565 | :raise AbnormalShutdown: When the VM could not be shut down gracefully. |
| 566 | The inner exception will likely be ConnectionReset or |
| 567 | subprocess.TimeoutExpired. In rare cases, non-graceful termination |
| 568 | may result in its own exceptions, likely subprocess.TimeoutExpired. |
| 569 | """ |
| 570 | try: |
John Snow | b9420e4 | 2021-10-26 13:56:05 -0400 | [diff] [blame] | 571 | self._soft_shutdown(timeout) |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 572 | except Exception as exc: |
John Snow | 9cccb33 | 2022-10-27 14:58:35 -0400 | [diff] [blame^] | 573 | if isinstance(exc, subprocess.TimeoutExpired): |
| 574 | LOG.debug("Timed out waiting for QEMU process to exit") |
| 575 | LOG.debug("Graceful shutdown failed", exc_info=True) |
| 576 | LOG.debug("Falling back to hard shutdown") |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 577 | self._hard_shutdown() |
| 578 | raise AbnormalShutdown("Could not perform graceful shutdown") \ |
| 579 | from exc |
| 580 | |
John Snow | b9420e4 | 2021-10-26 13:56:05 -0400 | [diff] [blame] | 581 | def shutdown(self, |
John Snow | c9b3045 | 2020-07-10 01:06:43 -0400 | [diff] [blame] | 582 | hard: bool = False, |
John Snow | 8226a4b | 2020-07-20 12:02:52 -0400 | [diff] [blame] | 583 | timeout: Optional[int] = 30) -> None: |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 584 | """ |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 585 | Terminate the VM (gracefully if possible) and perform cleanup. |
| 586 | Cleanup will always be performed. |
| 587 | |
| 588 | If the VM has not yet been launched, or shutdown(), wait(), or kill() |
| 589 | have already been called, this method does nothing. |
| 590 | |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 591 | :param hard: When true, do not attempt graceful shutdown, and |
| 592 | suppress the SIGKILL warning log message. |
| 593 | :param timeout: Optional timeout in seconds for graceful shutdown. |
John Snow | 8226a4b | 2020-07-20 12:02:52 -0400 | [diff] [blame] | 594 | Default 30 seconds, A `None` value is an infinite wait. |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 595 | """ |
John Snow | a3842cb | 2020-07-10 01:06:42 -0400 | [diff] [blame] | 596 | if not self._launched: |
| 597 | return |
| 598 | |
John Snow | 9cccb33 | 2022-10-27 14:58:35 -0400 | [diff] [blame^] | 599 | LOG.debug("Shutting down VM appliance; timeout=%s", timeout) |
| 600 | if hard: |
| 601 | LOG.debug("Caller requests immediate termination of QEMU process.") |
| 602 | |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 603 | try: |
Vladimir Sementsov-Ogievskiy | e0e925a | 2020-02-17 18:02:42 +0300 | [diff] [blame] | 604 | if hard: |
John Snow | de6e08b | 2020-07-10 01:06:48 -0400 | [diff] [blame] | 605 | self._user_killed = True |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 606 | self._hard_shutdown() |
| 607 | else: |
John Snow | b9420e4 | 2021-10-26 13:56:05 -0400 | [diff] [blame] | 608 | self._do_shutdown(timeout) |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 609 | finally: |
| 610 | self._post_shutdown() |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 611 | |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 612 | def kill(self) -> None: |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 613 | """ |
| 614 | Terminate the VM forcefully, wait for it to exit, and perform cleanup. |
| 615 | """ |
Vladimir Sementsov-Ogievskiy | e0e925a | 2020-02-17 18:02:42 +0300 | [diff] [blame] | 616 | self.shutdown(hard=True) |
| 617 | |
John Snow | 8226a4b | 2020-07-20 12:02:52 -0400 | [diff] [blame] | 618 | def wait(self, timeout: Optional[int] = 30) -> None: |
John Snow | 8952805 | 2020-07-10 01:06:44 -0400 | [diff] [blame] | 619 | """ |
| 620 | Wait for the VM to power off and perform post-shutdown cleanup. |
| 621 | |
John Snow | 8226a4b | 2020-07-20 12:02:52 -0400 | [diff] [blame] | 622 | :param timeout: Optional timeout in seconds. Default 30 seconds. |
| 623 | A value of `None` is an infinite wait. |
John Snow | 8952805 | 2020-07-10 01:06:44 -0400 | [diff] [blame] | 624 | """ |
John Snow | b9420e4 | 2021-10-26 13:56:05 -0400 | [diff] [blame] | 625 | self._quit_issued = True |
| 626 | self.shutdown(timeout=timeout) |
John Snow | 8952805 | 2020-07-10 01:06:44 -0400 | [diff] [blame] | 627 | |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 628 | def set_qmp_monitor(self, enabled: bool = True) -> None: |
Wainer dos Santos Moschetta | 74b56bb | 2019-12-11 13:55:35 -0500 | [diff] [blame] | 629 | """ |
| 630 | Set the QMP monitor. |
| 631 | |
| 632 | @param enabled: if False, qmp monitor options will be removed from |
| 633 | the base arguments of the resulting QEMU command |
| 634 | line. Default is True. |
John Snow | 5c02c86 | 2021-06-29 17:43:23 -0400 | [diff] [blame] | 635 | |
| 636 | .. note:: Call this function before launch(). |
Wainer dos Santos Moschetta | 74b56bb | 2019-12-11 13:55:35 -0500 | [diff] [blame] | 637 | """ |
John Snow | be1183e | 2020-10-06 19:58:04 -0400 | [diff] [blame] | 638 | self._qmp_set = enabled |
| 639 | |
| 640 | @property |
John Snow | beb6b57 | 2021-05-27 17:16:53 -0400 | [diff] [blame] | 641 | def _qmp(self) -> QEMUMonitorProtocol: |
John Snow | be1183e | 2020-10-06 19:58:04 -0400 | [diff] [blame] | 642 | if self._qmp_connection is None: |
| 643 | raise QEMUMachineError("Attempt to access QMP with no connection") |
| 644 | return self._qmp_connection |
Wainer dos Santos Moschetta | 74b56bb | 2019-12-11 13:55:35 -0500 | [diff] [blame] | 645 | |
John Snow | aaa81ec | 2020-10-06 19:58:03 -0400 | [diff] [blame] | 646 | @classmethod |
Vladimir Sementsov-Ogievskiy | c7daa57 | 2021-08-24 11:38:45 +0300 | [diff] [blame] | 647 | def _qmp_args(cls, conv_keys: bool, |
| 648 | args: Dict[str, Any]) -> Dict[str, object]: |
| 649 | if conv_keys: |
| 650 | return {k.replace('_', '-'): v for k, v in args.items()} |
| 651 | |
| 652 | return args |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 653 | |
John Snow | aaa81ec | 2020-10-06 19:58:03 -0400 | [diff] [blame] | 654 | def qmp(self, cmd: str, |
Vladimir Sementsov-Ogievskiy | 3f3c9b4 | 2021-08-24 11:38:46 +0300 | [diff] [blame] | 655 | args_dict: Optional[Dict[str, object]] = None, |
| 656 | conv_keys: Optional[bool] = None, |
John Snow | aaa81ec | 2020-10-06 19:58:03 -0400 | [diff] [blame] | 657 | **args: Any) -> QMPMessage: |
| 658 | """ |
| 659 | Invoke a QMP command and return the response dict |
| 660 | """ |
Vladimir Sementsov-Ogievskiy | 3f3c9b4 | 2021-08-24 11:38:46 +0300 | [diff] [blame] | 661 | if args_dict is not None: |
| 662 | assert not args |
| 663 | assert conv_keys is None |
| 664 | args = args_dict |
| 665 | conv_keys = False |
| 666 | |
| 667 | if conv_keys is None: |
| 668 | conv_keys = True |
| 669 | |
Vladimir Sementsov-Ogievskiy | c7daa57 | 2021-08-24 11:38:45 +0300 | [diff] [blame] | 670 | qmp_args = self._qmp_args(conv_keys, args) |
John Snow | b9420e4 | 2021-10-26 13:56:05 -0400 | [diff] [blame] | 671 | ret = self._qmp.cmd(cmd, args=qmp_args) |
| 672 | if cmd == 'quit' and 'error' not in ret and 'return' in ret: |
| 673 | self._quit_issued = True |
| 674 | return ret |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 675 | |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 676 | def command(self, cmd: str, |
| 677 | conv_keys: bool = True, |
| 678 | **args: Any) -> QMPReturnValue: |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 679 | """ |
| 680 | Invoke a QMP command. |
| 681 | On success return the response dict. |
| 682 | On failure raise an exception. |
| 683 | """ |
Vladimir Sementsov-Ogievskiy | c7daa57 | 2021-08-24 11:38:45 +0300 | [diff] [blame] | 684 | qmp_args = self._qmp_args(conv_keys, args) |
John Snow | b9420e4 | 2021-10-26 13:56:05 -0400 | [diff] [blame] | 685 | ret = self._qmp.command(cmd, **qmp_args) |
| 686 | if cmd == 'quit': |
| 687 | self._quit_issued = True |
| 688 | return ret |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 689 | |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 690 | def get_qmp_event(self, wait: bool = False) -> Optional[QMPMessage]: |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 691 | """ |
| 692 | Poll for one queued QMP events and return it |
| 693 | """ |
John Snow | 306dfcd | 2019-06-27 17:28:15 -0400 | [diff] [blame] | 694 | if self._events: |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 695 | return self._events.pop(0) |
| 696 | return self._qmp.pull_event(wait=wait) |
| 697 | |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 698 | def get_qmp_events(self, wait: bool = False) -> List[QMPMessage]: |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 699 | """ |
| 700 | Poll for queued QMP events and return a list of dicts |
| 701 | """ |
| 702 | events = self._qmp.get_events(wait=wait) |
| 703 | events.extend(self._events) |
| 704 | del self._events[:] |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 705 | return events |
| 706 | |
| 707 | @staticmethod |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 708 | def event_match(event: Any, match: Optional[Any]) -> bool: |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 709 | """ |
| 710 | Check if an event matches optional match criteria. |
| 711 | |
| 712 | The match criteria takes the form of a matching subdict. The event is |
| 713 | checked to be a superset of the subdict, recursively, with matching |
| 714 | values whenever the subdict values are not None. |
| 715 | |
| 716 | This has a limitation that you cannot explicitly check for None values. |
| 717 | |
| 718 | Examples, with the subdict queries on the left: |
| 719 | - None matches any object. |
| 720 | - {"foo": None} matches {"foo": {"bar": 1}} |
| 721 | - {"foo": None} matches {"foo": 5} |
| 722 | - {"foo": {"abc": None}} does not match {"foo": {"bar": 1}} |
| 723 | - {"foo": {"rab": 2}} matches {"foo": {"bar": 1, "rab": 2}} |
| 724 | """ |
| 725 | if match is None: |
| 726 | return True |
| 727 | |
| 728 | try: |
| 729 | for key in match: |
| 730 | if key in event: |
| 731 | if not QEMUMachine.event_match(event[key], match[key]): |
| 732 | return False |
| 733 | else: |
| 734 | return False |
| 735 | return True |
| 736 | except TypeError: |
| 737 | # either match or event wasn't iterable (not a dict) |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 738 | return bool(match == event) |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 739 | |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 740 | def event_wait(self, name: str, |
| 741 | timeout: float = 60.0, |
| 742 | match: Optional[QMPMessage] = None) -> Optional[QMPMessage]: |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 743 | """ |
| 744 | event_wait waits for and returns a named event from QMP with a timeout. |
| 745 | |
| 746 | name: The event to wait for. |
| 747 | timeout: QEMUMonitorProtocol.pull_event timeout parameter. |
| 748 | match: Optional match criteria. See event_match for details. |
| 749 | """ |
| 750 | return self.events_wait([(name, match)], timeout) |
| 751 | |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 752 | def events_wait(self, |
| 753 | events: Sequence[Tuple[str, Any]], |
| 754 | timeout: float = 60.0) -> Optional[QMPMessage]: |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 755 | """ |
John Snow | 1847a4a | 2020-10-06 19:58:02 -0400 | [diff] [blame] | 756 | events_wait waits for and returns a single named event from QMP. |
| 757 | In the case of multiple qualifying events, this function returns the |
| 758 | first one. |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 759 | |
John Snow | 1847a4a | 2020-10-06 19:58:02 -0400 | [diff] [blame] | 760 | :param events: A sequence of (name, match_criteria) tuples. |
| 761 | The match criteria are optional and may be None. |
| 762 | See event_match for details. |
| 763 | :param timeout: Optional timeout, in seconds. |
| 764 | See QEMUMonitorProtocol.pull_event. |
| 765 | |
John Snow | a422530 | 2022-03-21 16:33:12 -0400 | [diff] [blame] | 766 | :raise asyncio.TimeoutError: |
| 767 | If timeout was non-zero and no matching events were found. |
| 768 | |
John Snow | 1847a4a | 2020-10-06 19:58:02 -0400 | [diff] [blame] | 769 | :return: A QMP event matching the filter criteria. |
| 770 | If timeout was 0 and no event matched, None. |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 771 | """ |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 772 | def _match(event: QMPMessage) -> bool: |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 773 | for name, match in events: |
John Snow | 306dfcd | 2019-06-27 17:28:15 -0400 | [diff] [blame] | 774 | if event['event'] == name and self.event_match(event, match): |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 775 | return True |
| 776 | return False |
| 777 | |
John Snow | 1847a4a | 2020-10-06 19:58:02 -0400 | [diff] [blame] | 778 | event: Optional[QMPMessage] |
| 779 | |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 780 | # Search cached events |
| 781 | for event in self._events: |
| 782 | if _match(event): |
| 783 | self._events.remove(event) |
| 784 | return event |
| 785 | |
| 786 | # Poll for new events |
| 787 | while True: |
| 788 | event = self._qmp.pull_event(wait=timeout) |
John Snow | 1847a4a | 2020-10-06 19:58:02 -0400 | [diff] [blame] | 789 | if event is None: |
| 790 | # NB: None is only returned when timeout is false-ish. |
John Snow | a422530 | 2022-03-21 16:33:12 -0400 | [diff] [blame] | 791 | # Timeouts raise asyncio.TimeoutError instead! |
John Snow | 1847a4a | 2020-10-06 19:58:02 -0400 | [diff] [blame] | 792 | break |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 793 | if _match(event): |
| 794 | return event |
| 795 | self._events.append(event) |
| 796 | |
| 797 | return None |
| 798 | |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 799 | def get_log(self) -> Optional[str]: |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 800 | """ |
| 801 | After self.shutdown or failed qemu execution, this returns the output |
| 802 | of the qemu process. |
| 803 | """ |
| 804 | return self._iolog |
| 805 | |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 806 | def add_args(self, *args: str) -> None: |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 807 | """ |
| 808 | Adds to the list of extra arguments to be given to the QEMU binary |
| 809 | """ |
| 810 | self._args.extend(args) |
| 811 | |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 812 | def set_machine(self, machine_type: str) -> None: |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 813 | """ |
| 814 | Sets the machine type |
| 815 | |
| 816 | If set, the machine type will be added to the base arguments |
| 817 | of the resulting QEMU command line. |
| 818 | """ |
| 819 | self._machine = machine_type |
| 820 | |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 821 | def set_console(self, |
| 822 | device_type: Optional[str] = None, |
| 823 | console_index: int = 0) -> None: |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 824 | """ |
| 825 | Sets the device type for a console device |
| 826 | |
| 827 | If set, the console device and a backing character device will |
| 828 | be added to the base arguments of the resulting QEMU command |
| 829 | line. |
| 830 | |
| 831 | This is a convenience method that will either use the provided |
| 832 | device type, or default to a "-serial chardev:console" command |
| 833 | line argument. |
| 834 | |
| 835 | The actual setting of command line arguments will be be done at |
| 836 | machine launch time, as it depends on the temporary directory |
| 837 | to be created. |
| 838 | |
| 839 | @param device_type: the device type, such as "isa-serial". If |
| 840 | None is given (the default value) a "-serial |
| 841 | chardev:console" command line argument will |
| 842 | be used instead, resorting to the machine's |
| 843 | default device type. |
Philippe Mathieu-Daudé | 746f244 | 2020-01-21 00:51:56 +0100 | [diff] [blame] | 844 | @param console_index: the index of the console device to use. |
| 845 | If not zero, the command line will create |
| 846 | 'index - 1' consoles and connect them to |
| 847 | the 'null' backing character device. |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 848 | """ |
| 849 | self._console_set = True |
| 850 | self._console_device_type = device_type |
Philippe Mathieu-Daudé | 746f244 | 2020-01-21 00:51:56 +0100 | [diff] [blame] | 851 | self._console_index = console_index |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 852 | |
| 853 | @property |
John Snow | f12a282 | 2020-10-06 19:58:08 -0400 | [diff] [blame] | 854 | def console_socket(self) -> socket.socket: |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 855 | """ |
| 856 | Returns a socket connected to the console |
| 857 | """ |
| 858 | if self._console_socket is None: |
Robert Foley | 80ded8e | 2020-07-24 07:45:08 +0100 | [diff] [blame] | 859 | self._console_socket = console_socket.ConsoleSocket( |
| 860 | self._console_address, |
| 861 | file=self._console_log_path, |
| 862 | drain=self._drain_console) |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 863 | return self._console_socket |
Cleber Rosa | 2ca6e26 | 2021-02-11 17:01:42 -0500 | [diff] [blame] | 864 | |
| 865 | @property |
| 866 | def temp_dir(self) -> str: |
| 867 | """ |
| 868 | Returns a temporary directory to be used for this machine |
| 869 | """ |
| 870 | if self._temp_dir is None: |
| 871 | self._temp_dir = tempfile.mkdtemp(prefix="qemu-machine-", |
| 872 | dir=self._base_temp_dir) |
| 873 | return self._temp_dir |
Cleber Rosa | b306e26 | 2021-02-11 16:55:05 -0500 | [diff] [blame] | 874 | |
| 875 | @property |
John Snow | 87bf1fe | 2021-11-18 15:46:14 -0500 | [diff] [blame] | 876 | def sock_dir(self) -> str: |
| 877 | """ |
| 878 | Returns the directory used for sockfiles by this machine. |
| 879 | """ |
| 880 | if self._sock_dir: |
| 881 | return self._sock_dir |
| 882 | return self.temp_dir |
| 883 | |
| 884 | @property |
Cleber Rosa | b306e26 | 2021-02-11 16:55:05 -0500 | [diff] [blame] | 885 | def log_dir(self) -> str: |
| 886 | """ |
| 887 | Returns a directory to be used for writing logs |
| 888 | """ |
| 889 | if self._log_dir is None: |
| 890 | return self.temp_dir |
| 891 | return self._log_dir |