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 |
| 21 | import logging |
| 22 | import os |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 23 | import shutil |
John Snow | de6e08b | 2020-07-10 01:06:48 -0400 | [diff] [blame] | 24 | import signal |
John Snow | 932ca4b | 2020-10-06 19:57:58 -0400 | [diff] [blame] | 25 | import subprocess |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 26 | import tempfile |
John Snow | 1dda040 | 2020-05-14 01:53:44 -0400 | [diff] [blame] | 27 | from types import TracebackType |
John Snow | 652809d | 2020-10-06 19:58:01 -0400 | [diff] [blame^] | 28 | from typing import List, Optional, Type |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 29 | |
John Snow | 932ca4b | 2020-10-06 19:57:58 -0400 | [diff] [blame] | 30 | from . import console_socket, qmp |
John Snow | c4e6023 | 2020-10-06 19:57:59 -0400 | [diff] [blame] | 31 | from .qmp import SocketAddrT |
John Snow | 932ca4b | 2020-10-06 19:57:58 -0400 | [diff] [blame] | 32 | |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 33 | |
| 34 | LOG = logging.getLogger(__name__) |
| 35 | |
John Snow | 8dfac2e | 2020-05-28 18:21:29 -0400 | [diff] [blame] | 36 | |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 37 | class QEMUMachineError(Exception): |
| 38 | """ |
| 39 | Exception called when an error in QEMUMachine happens. |
| 40 | """ |
| 41 | |
| 42 | |
| 43 | class QEMUMachineAddDeviceError(QEMUMachineError): |
| 44 | """ |
| 45 | Exception raised when a request to add a device can not be fulfilled |
| 46 | |
| 47 | The failures are caused by limitations, lack of information or conflicting |
| 48 | requests on the QEMUMachine methods. This exception does not represent |
| 49 | failures reported by the QEMU binary itself. |
| 50 | """ |
| 51 | |
| 52 | |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 53 | class AbnormalShutdown(QEMUMachineError): |
| 54 | """ |
| 55 | Exception raised when a graceful shutdown was requested, but not performed. |
| 56 | """ |
| 57 | |
| 58 | |
John Snow | 9b8ccd6 | 2020-05-28 18:21:28 -0400 | [diff] [blame] | 59 | class QEMUMachine: |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 60 | """ |
| 61 | A QEMU VM |
| 62 | |
John Snow | 8dfac2e | 2020-05-28 18:21:29 -0400 | [diff] [blame] | 63 | Use this object as a context manager to ensure |
| 64 | the QEMU process terminates:: |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 65 | |
| 66 | with VM(binary) as vm: |
| 67 | ... |
| 68 | # vm is guaranteed to be shut down here |
| 69 | """ |
| 70 | |
| 71 | def __init__(self, binary, args=None, wrapper=None, name=None, |
John Snow | c4e6023 | 2020-10-06 19:57:59 -0400 | [diff] [blame] | 72 | test_dir="/var/tmp", |
| 73 | monitor_address: Optional[SocketAddrT] = None, |
Robert Foley | 0fc8f66 | 2020-07-01 14:56:24 +0100 | [diff] [blame] | 74 | socket_scm_helper=None, sock_dir=None, |
| 75 | drain_console=False, console_log=None): |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 76 | ''' |
| 77 | Initialize a QEMUMachine |
| 78 | |
| 79 | @param binary: path to the qemu binary |
| 80 | @param args: list of extra arguments |
| 81 | @param wrapper: list of arguments used as prefix to qemu binary |
| 82 | @param name: prefix for socket and log file names (default: qemu-PID) |
| 83 | @param test_dir: where to create socket and log file |
| 84 | @param monitor_address: address for QMP monitor |
| 85 | @param socket_scm_helper: helper program, required for send_fd_scm() |
Robert Foley | 0fc8f66 | 2020-07-01 14:56:24 +0100 | [diff] [blame] | 86 | @param sock_dir: where to create socket (overrides test_dir for sock) |
Robert Foley | 0fc8f66 | 2020-07-01 14:56:24 +0100 | [diff] [blame] | 87 | @param drain_console: (optional) True to drain console socket to buffer |
John Snow | c5e61a6 | 2020-10-06 19:58:00 -0400 | [diff] [blame] | 88 | @param console_log: (optional) path to console log file |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 89 | @note: Qemu process is not started until launch() is used. |
| 90 | ''' |
John Snow | c5e61a6 | 2020-10-06 19:58:00 -0400 | [diff] [blame] | 91 | # Direct user configuration |
| 92 | |
| 93 | self._binary = binary |
| 94 | |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 95 | if args is None: |
| 96 | args = [] |
John Snow | c5e61a6 | 2020-10-06 19:58:00 -0400 | [diff] [blame] | 97 | # Copy mutable input: we will be modifying our copy |
| 98 | self._args = list(args) |
| 99 | |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 100 | if wrapper is None: |
| 101 | wrapper = [] |
John Snow | c5e61a6 | 2020-10-06 19:58:00 -0400 | [diff] [blame] | 102 | self._wrapper = wrapper |
| 103 | |
| 104 | self._name = name or "qemu-%d" % os.getpid() |
| 105 | self._test_dir = test_dir |
| 106 | self._sock_dir = sock_dir or self._test_dir |
| 107 | self._socket_scm_helper = socket_scm_helper |
| 108 | |
John Snow | c4e6023 | 2020-10-06 19:57:59 -0400 | [diff] [blame] | 109 | if monitor_address is not None: |
| 110 | self._monitor_address = monitor_address |
| 111 | self._remove_monitor_sockfile = False |
| 112 | else: |
| 113 | self._monitor_address = os.path.join( |
John Snow | c5e61a6 | 2020-10-06 19:58:00 -0400 | [diff] [blame] | 114 | self._sock_dir, f"{self._name}-monitor.sock" |
John Snow | c4e6023 | 2020-10-06 19:57:59 -0400 | [diff] [blame] | 115 | ) |
| 116 | self._remove_monitor_sockfile = True |
John Snow | c5e61a6 | 2020-10-06 19:58:00 -0400 | [diff] [blame] | 117 | |
| 118 | self._console_log_path = console_log |
| 119 | if self._console_log_path: |
| 120 | # In order to log the console, buffering needs to be enabled. |
| 121 | self._drain_console = True |
| 122 | else: |
| 123 | self._drain_console = drain_console |
| 124 | |
| 125 | # Runstate |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 126 | self._qemu_log_path = None |
| 127 | self._qemu_log_file = None |
| 128 | self._popen = None |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 129 | self._events = [] |
| 130 | self._iolog = None |
Wainer dos Santos Moschetta | 74b56bb | 2019-12-11 13:55:35 -0500 | [diff] [blame] | 131 | self._qmp_set = True # Enable QMP monitor by default. |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 132 | self._qmp = None |
| 133 | self._qemu_full_args = None |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 134 | self._temp_dir = None |
| 135 | self._launched = False |
| 136 | self._machine = None |
Philippe Mathieu-Daudé | 746f244 | 2020-01-21 00:51:56 +0100 | [diff] [blame] | 137 | self._console_index = 0 |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 138 | self._console_set = False |
| 139 | self._console_device_type = None |
John Snow | 652809d | 2020-10-06 19:58:01 -0400 | [diff] [blame^] | 140 | self._console_address = os.path.join( |
| 141 | self._sock_dir, f"{self._name}-console.sock" |
| 142 | ) |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 143 | self._console_socket = None |
Max Reitz | 32558ce | 2019-10-17 15:31:34 +0200 | [diff] [blame] | 144 | self._remove_files = [] |
John Snow | de6e08b | 2020-07-10 01:06:48 -0400 | [diff] [blame] | 145 | self._user_killed = False |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 146 | |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 147 | def __enter__(self): |
| 148 | return self |
| 149 | |
John Snow | 1dda040 | 2020-05-14 01:53:44 -0400 | [diff] [blame] | 150 | def __exit__(self, |
| 151 | exc_type: Optional[Type[BaseException]], |
| 152 | exc_val: Optional[BaseException], |
| 153 | exc_tb: Optional[TracebackType]) -> None: |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 154 | self.shutdown() |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 155 | |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 156 | def add_monitor_null(self): |
John Snow | 306dfcd | 2019-06-27 17:28:15 -0400 | [diff] [blame] | 157 | """ |
| 158 | This can be used to add an unused monitor instance. |
| 159 | """ |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 160 | self._args.append('-monitor') |
| 161 | self._args.append('null') |
| 162 | |
| 163 | def add_fd(self, fd, fdset, opaque, opts=''): |
| 164 | """ |
| 165 | Pass a file descriptor to the VM |
| 166 | """ |
| 167 | options = ['fd=%d' % fd, |
| 168 | 'set=%d' % fdset, |
| 169 | 'opaque=%s' % opaque] |
| 170 | if opts: |
| 171 | options.append(opts) |
| 172 | |
| 173 | # This did not exist before 3.4, but since then it is |
| 174 | # mandatory for our purpose |
| 175 | if hasattr(os, 'set_inheritable'): |
| 176 | os.set_inheritable(fd, True) |
| 177 | |
| 178 | self._args.append('-add-fd') |
| 179 | self._args.append(','.join(options)) |
| 180 | return self |
| 181 | |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 182 | def send_fd_scm(self, fd=None, file_path=None): |
John Snow | 306dfcd | 2019-06-27 17:28:15 -0400 | [diff] [blame] | 183 | """ |
| 184 | Send an fd or file_path to socket_scm_helper. |
| 185 | |
| 186 | Exactly one of fd and file_path must be given. |
| 187 | If it is file_path, the helper will open that file and pass its own fd. |
| 188 | """ |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 189 | # In iotest.py, the qmp should always use unix socket. |
| 190 | assert self._qmp.is_scm_available() |
| 191 | if self._socket_scm_helper is None: |
| 192 | raise QEMUMachineError("No path to socket_scm_helper set") |
| 193 | if not os.path.exists(self._socket_scm_helper): |
| 194 | raise QEMUMachineError("%s does not exist" % |
| 195 | self._socket_scm_helper) |
| 196 | |
| 197 | # This did not exist before 3.4, but since then it is |
| 198 | # mandatory for our purpose |
| 199 | if hasattr(os, 'set_inheritable'): |
| 200 | os.set_inheritable(self._qmp.get_sock_fd(), True) |
| 201 | if fd is not None: |
| 202 | os.set_inheritable(fd, True) |
| 203 | |
| 204 | fd_param = ["%s" % self._socket_scm_helper, |
| 205 | "%d" % self._qmp.get_sock_fd()] |
| 206 | |
| 207 | if file_path is not None: |
| 208 | assert fd is None |
| 209 | fd_param.append(file_path) |
| 210 | else: |
| 211 | assert fd is not None |
| 212 | fd_param.append(str(fd)) |
| 213 | |
| 214 | devnull = open(os.path.devnull, 'rb') |
John Snow | 8dfac2e | 2020-05-28 18:21:29 -0400 | [diff] [blame] | 215 | proc = subprocess.Popen( |
| 216 | fd_param, stdin=devnull, stdout=subprocess.PIPE, |
| 217 | stderr=subprocess.STDOUT, close_fds=False |
| 218 | ) |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 219 | output = proc.communicate()[0] |
| 220 | if output: |
| 221 | LOG.debug(output) |
| 222 | |
| 223 | return proc.returncode |
| 224 | |
| 225 | @staticmethod |
| 226 | def _remove_if_exists(path): |
| 227 | """ |
| 228 | Remove file object at path if it exists |
| 229 | """ |
| 230 | try: |
| 231 | os.remove(path) |
| 232 | except OSError as exception: |
| 233 | if exception.errno == errno.ENOENT: |
| 234 | return |
| 235 | raise |
| 236 | |
| 237 | def is_running(self): |
John Snow | 306dfcd | 2019-06-27 17:28:15 -0400 | [diff] [blame] | 238 | """Returns true if the VM is running.""" |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 239 | return self._popen is not None and self._popen.poll() is None |
| 240 | |
| 241 | def exitcode(self): |
John Snow | 306dfcd | 2019-06-27 17:28:15 -0400 | [diff] [blame] | 242 | """Returns the exit code if possible, or None.""" |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 243 | if self._popen is None: |
| 244 | return None |
| 245 | return self._popen.poll() |
| 246 | |
| 247 | def get_pid(self): |
John Snow | 306dfcd | 2019-06-27 17:28:15 -0400 | [diff] [blame] | 248 | """Returns the PID of the running process, or None.""" |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 249 | if not self.is_running(): |
| 250 | return None |
| 251 | return self._popen.pid |
| 252 | |
| 253 | def _load_io_log(self): |
| 254 | if self._qemu_log_path is not None: |
| 255 | with open(self._qemu_log_path, "r") as iolog: |
| 256 | self._iolog = iolog.read() |
| 257 | |
John Snow | 652809d | 2020-10-06 19:58:01 -0400 | [diff] [blame^] | 258 | @property |
| 259 | def _base_args(self) -> List[str]: |
Wainer dos Santos Moschetta | 74b56bb | 2019-12-11 13:55:35 -0500 | [diff] [blame] | 260 | args = ['-display', 'none', '-vga', 'none'] |
John Snow | c4e6023 | 2020-10-06 19:57:59 -0400 | [diff] [blame] | 261 | |
Wainer dos Santos Moschetta | 74b56bb | 2019-12-11 13:55:35 -0500 | [diff] [blame] | 262 | if self._qmp_set: |
| 263 | if isinstance(self._monitor_address, tuple): |
John Snow | c4e6023 | 2020-10-06 19:57:59 -0400 | [diff] [blame] | 264 | moncdev = "socket,id=mon,host={},port={}".format( |
| 265 | *self._monitor_address |
| 266 | ) |
Wainer dos Santos Moschetta | 74b56bb | 2019-12-11 13:55:35 -0500 | [diff] [blame] | 267 | else: |
John Snow | c4e6023 | 2020-10-06 19:57:59 -0400 | [diff] [blame] | 268 | moncdev = f"socket,id=mon,path={self._monitor_address}" |
Wainer dos Santos Moschetta | 74b56bb | 2019-12-11 13:55:35 -0500 | [diff] [blame] | 269 | args.extend(['-chardev', moncdev, '-mon', |
| 270 | 'chardev=mon,mode=control']) |
John Snow | c4e6023 | 2020-10-06 19:57:59 -0400 | [diff] [blame] | 271 | |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 272 | if self._machine is not None: |
| 273 | args.extend(['-machine', self._machine]) |
John Snow | 9b8ccd6 | 2020-05-28 18:21:28 -0400 | [diff] [blame] | 274 | for _ in range(self._console_index): |
Philippe Mathieu-Daudé | 746f244 | 2020-01-21 00:51:56 +0100 | [diff] [blame] | 275 | args.extend(['-serial', 'null']) |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 276 | if self._console_set: |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 277 | chardev = ('socket,id=console,path=%s,server,nowait' % |
| 278 | self._console_address) |
| 279 | args.extend(['-chardev', chardev]) |
| 280 | if self._console_device_type is None: |
| 281 | args.extend(['-serial', 'chardev:console']) |
| 282 | else: |
| 283 | device = '%s,chardev=console' % self._console_device_type |
| 284 | args.extend(['-device', device]) |
| 285 | return args |
| 286 | |
| 287 | def _pre_launch(self): |
| 288 | self._temp_dir = tempfile.mkdtemp(dir=self._test_dir) |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 289 | self._qemu_log_path = os.path.join(self._temp_dir, self._name + ".log") |
| 290 | self._qemu_log_file = open(self._qemu_log_path, 'wb') |
| 291 | |
John Snow | 652809d | 2020-10-06 19:58:01 -0400 | [diff] [blame^] | 292 | if self._console_set: |
| 293 | self._remove_files.append(self._console_address) |
| 294 | |
Wainer dos Santos Moschetta | 74b56bb | 2019-12-11 13:55:35 -0500 | [diff] [blame] | 295 | if self._qmp_set: |
John Snow | c4e6023 | 2020-10-06 19:57:59 -0400 | [diff] [blame] | 296 | if self._remove_monitor_sockfile: |
| 297 | assert isinstance(self._monitor_address, str) |
| 298 | self._remove_files.append(self._monitor_address) |
| 299 | self._qmp = qmp.QEMUMonitorProtocol( |
| 300 | self._monitor_address, |
| 301 | server=True, |
| 302 | nickname=self._name |
| 303 | ) |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 304 | |
| 305 | def _post_launch(self): |
Wainer dos Santos Moschetta | 74b56bb | 2019-12-11 13:55:35 -0500 | [diff] [blame] | 306 | if self._qmp: |
| 307 | self._qmp.accept() |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 308 | |
| 309 | def _post_shutdown(self): |
John Snow | a3842cb | 2020-07-10 01:06:42 -0400 | [diff] [blame] | 310 | """ |
| 311 | Called to cleanup the VM instance after the process has exited. |
| 312 | May also be called after a failed launch. |
| 313 | """ |
| 314 | # Comprehensive reset for the failed launch case: |
| 315 | self._early_cleanup() |
| 316 | |
John Snow | 671940e | 2020-07-10 01:06:39 -0400 | [diff] [blame] | 317 | if self._qmp: |
| 318 | self._qmp.close() |
| 319 | self._qmp = None |
| 320 | |
John Snow | 14661d9 | 2020-07-10 01:06:38 -0400 | [diff] [blame] | 321 | self._load_io_log() |
| 322 | |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 323 | if self._qemu_log_file is not None: |
| 324 | self._qemu_log_file.close() |
| 325 | self._qemu_log_file = None |
| 326 | |
| 327 | self._qemu_log_path = None |
| 328 | |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 329 | if self._temp_dir is not None: |
| 330 | shutil.rmtree(self._temp_dir) |
| 331 | self._temp_dir = None |
| 332 | |
Max Reitz | 32558ce | 2019-10-17 15:31:34 +0200 | [diff] [blame] | 333 | while len(self._remove_files) > 0: |
| 334 | self._remove_if_exists(self._remove_files.pop()) |
| 335 | |
John Snow | 14661d9 | 2020-07-10 01:06:38 -0400 | [diff] [blame] | 336 | exitcode = self.exitcode() |
John Snow | de6e08b | 2020-07-10 01:06:48 -0400 | [diff] [blame] | 337 | if (exitcode is not None and exitcode < 0 |
| 338 | and not (self._user_killed and exitcode == -signal.SIGKILL)): |
John Snow | 14661d9 | 2020-07-10 01:06:38 -0400 | [diff] [blame] | 339 | msg = 'qemu received signal %i; command: "%s"' |
| 340 | if self._qemu_full_args: |
| 341 | command = ' '.join(self._qemu_full_args) |
| 342 | else: |
| 343 | command = '' |
| 344 | LOG.warning(msg, -int(exitcode), command) |
| 345 | |
John Snow | de6e08b | 2020-07-10 01:06:48 -0400 | [diff] [blame] | 346 | self._user_killed = False |
John Snow | 14661d9 | 2020-07-10 01:06:38 -0400 | [diff] [blame] | 347 | self._launched = False |
| 348 | |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 349 | def launch(self): |
| 350 | """ |
| 351 | Launch the VM and make sure we cleanup and expose the |
| 352 | command line/output in case of exception |
| 353 | """ |
| 354 | |
| 355 | if self._launched: |
| 356 | raise QEMUMachineError('VM already launched') |
| 357 | |
| 358 | self._iolog = None |
| 359 | self._qemu_full_args = None |
| 360 | try: |
| 361 | self._launch() |
| 362 | self._launched = True |
| 363 | except: |
John Snow | a3842cb | 2020-07-10 01:06:42 -0400 | [diff] [blame] | 364 | self._post_shutdown() |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 365 | |
| 366 | LOG.debug('Error launching VM') |
| 367 | if self._qemu_full_args: |
| 368 | LOG.debug('Command: %r', ' '.join(self._qemu_full_args)) |
| 369 | if self._iolog: |
| 370 | LOG.debug('Output: %r', self._iolog) |
| 371 | raise |
| 372 | |
| 373 | def _launch(self): |
| 374 | """ |
| 375 | Launch the VM and establish a QMP connection |
| 376 | """ |
| 377 | devnull = open(os.path.devnull, 'rb') |
| 378 | self._pre_launch() |
| 379 | self._qemu_full_args = (self._wrapper + [self._binary] + |
John Snow | 652809d | 2020-10-06 19:58:01 -0400 | [diff] [blame^] | 380 | self._base_args + self._args) |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 381 | LOG.debug('VM launch command: %r', ' '.join(self._qemu_full_args)) |
| 382 | self._popen = subprocess.Popen(self._qemu_full_args, |
| 383 | stdin=devnull, |
| 384 | stdout=self._qemu_log_file, |
| 385 | stderr=subprocess.STDOUT, |
| 386 | shell=False, |
| 387 | close_fds=False) |
| 388 | self._post_launch() |
| 389 | |
John Snow | e2c97f1 | 2020-07-10 01:06:40 -0400 | [diff] [blame] | 390 | def _early_cleanup(self) -> None: |
| 391 | """ |
| 392 | Perform any cleanup that needs to happen before the VM exits. |
John Snow | a3842cb | 2020-07-10 01:06:42 -0400 | [diff] [blame] | 393 | |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 394 | May be invoked by both soft and hard shutdown in failover scenarios. |
John Snow | a3842cb | 2020-07-10 01:06:42 -0400 | [diff] [blame] | 395 | Called additionally by _post_shutdown for comprehensive cleanup. |
John Snow | e2c97f1 | 2020-07-10 01:06:40 -0400 | [diff] [blame] | 396 | """ |
| 397 | # If we keep the console socket open, we may deadlock waiting |
| 398 | # for QEMU to exit, while QEMU is waiting for the socket to |
| 399 | # become writeable. |
| 400 | if self._console_socket is not None: |
| 401 | self._console_socket.close() |
| 402 | self._console_socket = None |
| 403 | |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 404 | def _hard_shutdown(self) -> None: |
| 405 | """ |
| 406 | Perform early cleanup, kill the VM, and wait for it to terminate. |
| 407 | |
| 408 | :raise subprocess.Timeout: When timeout is exceeds 60 seconds |
| 409 | waiting for the QEMU process to terminate. |
| 410 | """ |
| 411 | self._early_cleanup() |
| 412 | self._popen.kill() |
| 413 | self._popen.wait(timeout=60) |
| 414 | |
John Snow | 8226a4b | 2020-07-20 12:02:52 -0400 | [diff] [blame] | 415 | def _soft_shutdown(self, timeout: Optional[int], |
| 416 | has_quit: bool = False) -> None: |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 417 | """ |
| 418 | Perform early cleanup, attempt to gracefully shut down the VM, and wait |
| 419 | for it to terminate. |
| 420 | |
John Snow | 8226a4b | 2020-07-20 12:02:52 -0400 | [diff] [blame] | 421 | :param timeout: Timeout in seconds for graceful shutdown. |
| 422 | A value of None is an infinite wait. |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 423 | :param has_quit: When True, don't attempt to issue 'quit' QMP command |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 424 | |
| 425 | :raise ConnectionReset: On QMP communication errors |
| 426 | :raise subprocess.TimeoutExpired: When timeout is exceeded waiting for |
| 427 | the QEMU process to terminate. |
| 428 | """ |
| 429 | self._early_cleanup() |
| 430 | |
| 431 | if self._qmp is not None: |
| 432 | if not has_quit: |
| 433 | # Might raise ConnectionReset |
| 434 | self._qmp.cmd('quit') |
| 435 | |
| 436 | # May raise subprocess.TimeoutExpired |
| 437 | self._popen.wait(timeout=timeout) |
| 438 | |
John Snow | 8226a4b | 2020-07-20 12:02:52 -0400 | [diff] [blame] | 439 | def _do_shutdown(self, timeout: Optional[int], |
| 440 | has_quit: bool = False) -> None: |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 441 | """ |
| 442 | Attempt to shutdown the VM gracefully; fallback to a hard shutdown. |
| 443 | |
John Snow | 8226a4b | 2020-07-20 12:02:52 -0400 | [diff] [blame] | 444 | :param timeout: Timeout in seconds for graceful shutdown. |
| 445 | A value of None is an infinite wait. |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 446 | :param has_quit: When True, don't attempt to issue 'quit' QMP command |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 447 | |
| 448 | :raise AbnormalShutdown: When the VM could not be shut down gracefully. |
| 449 | The inner exception will likely be ConnectionReset or |
| 450 | subprocess.TimeoutExpired. In rare cases, non-graceful termination |
| 451 | may result in its own exceptions, likely subprocess.TimeoutExpired. |
| 452 | """ |
| 453 | try: |
John Snow | 8226a4b | 2020-07-20 12:02:52 -0400 | [diff] [blame] | 454 | self._soft_shutdown(timeout, has_quit) |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 455 | except Exception as exc: |
| 456 | self._hard_shutdown() |
| 457 | raise AbnormalShutdown("Could not perform graceful shutdown") \ |
| 458 | from exc |
| 459 | |
John Snow | c9b3045 | 2020-07-10 01:06:43 -0400 | [diff] [blame] | 460 | def shutdown(self, has_quit: bool = False, |
| 461 | hard: bool = False, |
John Snow | 8226a4b | 2020-07-20 12:02:52 -0400 | [diff] [blame] | 462 | timeout: Optional[int] = 30) -> None: |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 463 | """ |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 464 | Terminate the VM (gracefully if possible) and perform cleanup. |
| 465 | Cleanup will always be performed. |
| 466 | |
| 467 | If the VM has not yet been launched, or shutdown(), wait(), or kill() |
| 468 | have already been called, this method does nothing. |
| 469 | |
| 470 | :param has_quit: When true, do not attempt to issue 'quit' QMP command. |
| 471 | :param hard: When true, do not attempt graceful shutdown, and |
| 472 | suppress the SIGKILL warning log message. |
| 473 | :param timeout: Optional timeout in seconds for graceful shutdown. |
John Snow | 8226a4b | 2020-07-20 12:02:52 -0400 | [diff] [blame] | 474 | Default 30 seconds, A `None` value is an infinite wait. |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 475 | """ |
John Snow | a3842cb | 2020-07-10 01:06:42 -0400 | [diff] [blame] | 476 | if not self._launched: |
| 477 | return |
| 478 | |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 479 | try: |
Vladimir Sementsov-Ogievskiy | e0e925a | 2020-02-17 18:02:42 +0300 | [diff] [blame] | 480 | if hard: |
John Snow | de6e08b | 2020-07-10 01:06:48 -0400 | [diff] [blame] | 481 | self._user_killed = True |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 482 | self._hard_shutdown() |
| 483 | else: |
John Snow | 8226a4b | 2020-07-20 12:02:52 -0400 | [diff] [blame] | 484 | self._do_shutdown(timeout, has_quit) |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 485 | finally: |
| 486 | self._post_shutdown() |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 487 | |
Vladimir Sementsov-Ogievskiy | e0e925a | 2020-02-17 18:02:42 +0300 | [diff] [blame] | 488 | def kill(self): |
John Snow | 193bf1c | 2020-07-10 01:06:47 -0400 | [diff] [blame] | 489 | """ |
| 490 | Terminate the VM forcefully, wait for it to exit, and perform cleanup. |
| 491 | """ |
Vladimir Sementsov-Ogievskiy | e0e925a | 2020-02-17 18:02:42 +0300 | [diff] [blame] | 492 | self.shutdown(hard=True) |
| 493 | |
John Snow | 8226a4b | 2020-07-20 12:02:52 -0400 | [diff] [blame] | 494 | def wait(self, timeout: Optional[int] = 30) -> None: |
John Snow | 8952805 | 2020-07-10 01:06:44 -0400 | [diff] [blame] | 495 | """ |
| 496 | Wait for the VM to power off and perform post-shutdown cleanup. |
| 497 | |
John Snow | 8226a4b | 2020-07-20 12:02:52 -0400 | [diff] [blame] | 498 | :param timeout: Optional timeout in seconds. Default 30 seconds. |
| 499 | A value of `None` is an infinite wait. |
John Snow | 8952805 | 2020-07-10 01:06:44 -0400 | [diff] [blame] | 500 | """ |
| 501 | self.shutdown(has_quit=True, timeout=timeout) |
| 502 | |
Wainer dos Santos Moschetta | 74b56bb | 2019-12-11 13:55:35 -0500 | [diff] [blame] | 503 | def set_qmp_monitor(self, enabled=True): |
| 504 | """ |
| 505 | Set the QMP monitor. |
| 506 | |
| 507 | @param enabled: if False, qmp monitor options will be removed from |
| 508 | the base arguments of the resulting QEMU command |
| 509 | line. Default is True. |
| 510 | @note: call this function before launch(). |
| 511 | """ |
| 512 | if enabled: |
| 513 | self._qmp_set = True |
| 514 | else: |
| 515 | self._qmp_set = False |
| 516 | self._qmp = None |
| 517 | |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 518 | def qmp(self, cmd, conv_keys=True, **args): |
| 519 | """ |
| 520 | Invoke a QMP command and return the response dict |
| 521 | """ |
| 522 | qmp_args = dict() |
| 523 | for key, value in args.items(): |
| 524 | if conv_keys: |
| 525 | qmp_args[key.replace('_', '-')] = value |
| 526 | else: |
| 527 | qmp_args[key] = value |
| 528 | |
| 529 | return self._qmp.cmd(cmd, args=qmp_args) |
| 530 | |
| 531 | def command(self, cmd, conv_keys=True, **args): |
| 532 | """ |
| 533 | Invoke a QMP command. |
| 534 | On success return the response dict. |
| 535 | On failure raise an exception. |
| 536 | """ |
| 537 | reply = self.qmp(cmd, conv_keys, **args) |
| 538 | if reply is None: |
| 539 | raise qmp.QMPError("Monitor is closed") |
| 540 | if "error" in reply: |
John Snow | e3a23b4 | 2020-07-10 01:22:07 -0400 | [diff] [blame] | 541 | raise qmp.QMPResponseError(reply) |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 542 | return reply["return"] |
| 543 | |
| 544 | def get_qmp_event(self, wait=False): |
| 545 | """ |
| 546 | Poll for one queued QMP events and return it |
| 547 | """ |
John Snow | 306dfcd | 2019-06-27 17:28:15 -0400 | [diff] [blame] | 548 | if self._events: |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 549 | return self._events.pop(0) |
| 550 | return self._qmp.pull_event(wait=wait) |
| 551 | |
| 552 | def get_qmp_events(self, wait=False): |
| 553 | """ |
| 554 | Poll for queued QMP events and return a list of dicts |
| 555 | """ |
| 556 | events = self._qmp.get_events(wait=wait) |
| 557 | events.extend(self._events) |
| 558 | del self._events[:] |
| 559 | self._qmp.clear_events() |
| 560 | return events |
| 561 | |
| 562 | @staticmethod |
| 563 | def event_match(event, match=None): |
| 564 | """ |
| 565 | Check if an event matches optional match criteria. |
| 566 | |
| 567 | The match criteria takes the form of a matching subdict. The event is |
| 568 | checked to be a superset of the subdict, recursively, with matching |
| 569 | values whenever the subdict values are not None. |
| 570 | |
| 571 | This has a limitation that you cannot explicitly check for None values. |
| 572 | |
| 573 | Examples, with the subdict queries on the left: |
| 574 | - None matches any object. |
| 575 | - {"foo": None} matches {"foo": {"bar": 1}} |
| 576 | - {"foo": None} matches {"foo": 5} |
| 577 | - {"foo": {"abc": None}} does not match {"foo": {"bar": 1}} |
| 578 | - {"foo": {"rab": 2}} matches {"foo": {"bar": 1, "rab": 2}} |
| 579 | """ |
| 580 | if match is None: |
| 581 | return True |
| 582 | |
| 583 | try: |
| 584 | for key in match: |
| 585 | if key in event: |
| 586 | if not QEMUMachine.event_match(event[key], match[key]): |
| 587 | return False |
| 588 | else: |
| 589 | return False |
| 590 | return True |
| 591 | except TypeError: |
| 592 | # either match or event wasn't iterable (not a dict) |
| 593 | return match == event |
| 594 | |
| 595 | def event_wait(self, name, timeout=60.0, match=None): |
| 596 | """ |
| 597 | event_wait waits for and returns a named event from QMP with a timeout. |
| 598 | |
| 599 | name: The event to wait for. |
| 600 | timeout: QEMUMonitorProtocol.pull_event timeout parameter. |
| 601 | match: Optional match criteria. See event_match for details. |
| 602 | """ |
| 603 | return self.events_wait([(name, match)], timeout) |
| 604 | |
| 605 | def events_wait(self, events, timeout=60.0): |
| 606 | """ |
John Snow | 8dfac2e | 2020-05-28 18:21:29 -0400 | [diff] [blame] | 607 | events_wait waits for and returns a named event |
| 608 | from QMP with a timeout. |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 609 | |
| 610 | events: a sequence of (name, match_criteria) tuples. |
| 611 | The match criteria are optional and may be None. |
| 612 | See event_match for details. |
| 613 | timeout: QEMUMonitorProtocol.pull_event timeout parameter. |
| 614 | """ |
| 615 | def _match(event): |
| 616 | for name, match in events: |
John Snow | 306dfcd | 2019-06-27 17:28:15 -0400 | [diff] [blame] | 617 | if event['event'] == name and self.event_match(event, match): |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 618 | return True |
| 619 | return False |
| 620 | |
| 621 | # Search cached events |
| 622 | for event in self._events: |
| 623 | if _match(event): |
| 624 | self._events.remove(event) |
| 625 | return event |
| 626 | |
| 627 | # Poll for new events |
| 628 | while True: |
| 629 | event = self._qmp.pull_event(wait=timeout) |
| 630 | if _match(event): |
| 631 | return event |
| 632 | self._events.append(event) |
| 633 | |
| 634 | return None |
| 635 | |
| 636 | def get_log(self): |
| 637 | """ |
| 638 | After self.shutdown or failed qemu execution, this returns the output |
| 639 | of the qemu process. |
| 640 | """ |
| 641 | return self._iolog |
| 642 | |
| 643 | def add_args(self, *args): |
| 644 | """ |
| 645 | Adds to the list of extra arguments to be given to the QEMU binary |
| 646 | """ |
| 647 | self._args.extend(args) |
| 648 | |
| 649 | def set_machine(self, machine_type): |
| 650 | """ |
| 651 | Sets the machine type |
| 652 | |
| 653 | If set, the machine type will be added to the base arguments |
| 654 | of the resulting QEMU command line. |
| 655 | """ |
| 656 | self._machine = machine_type |
| 657 | |
Philippe Mathieu-Daudé | 746f244 | 2020-01-21 00:51:56 +0100 | [diff] [blame] | 658 | def set_console(self, device_type=None, console_index=0): |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 659 | """ |
| 660 | Sets the device type for a console device |
| 661 | |
| 662 | If set, the console device and a backing character device will |
| 663 | be added to the base arguments of the resulting QEMU command |
| 664 | line. |
| 665 | |
| 666 | This is a convenience method that will either use the provided |
| 667 | device type, or default to a "-serial chardev:console" command |
| 668 | line argument. |
| 669 | |
| 670 | The actual setting of command line arguments will be be done at |
| 671 | machine launch time, as it depends on the temporary directory |
| 672 | to be created. |
| 673 | |
| 674 | @param device_type: the device type, such as "isa-serial". If |
| 675 | None is given (the default value) a "-serial |
| 676 | chardev:console" command line argument will |
| 677 | be used instead, resorting to the machine's |
| 678 | default device type. |
Philippe Mathieu-Daudé | 746f244 | 2020-01-21 00:51:56 +0100 | [diff] [blame] | 679 | @param console_index: the index of the console device to use. |
| 680 | If not zero, the command line will create |
| 681 | 'index - 1' consoles and connect them to |
| 682 | the 'null' backing character device. |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 683 | """ |
| 684 | self._console_set = True |
| 685 | self._console_device_type = device_type |
Philippe Mathieu-Daudé | 746f244 | 2020-01-21 00:51:56 +0100 | [diff] [blame] | 686 | self._console_index = console_index |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 687 | |
| 688 | @property |
| 689 | def console_socket(self): |
| 690 | """ |
| 691 | Returns a socket connected to the console |
| 692 | """ |
| 693 | if self._console_socket is None: |
Robert Foley | 80ded8e | 2020-07-24 07:45:08 +0100 | [diff] [blame] | 694 | self._console_socket = console_socket.ConsoleSocket( |
| 695 | self._console_address, |
| 696 | file=self._console_log_path, |
| 697 | drain=self._drain_console) |
John Snow | abf0bf9 | 2019-06-27 17:28:14 -0400 | [diff] [blame] | 698 | return self._console_socket |