blob: 41be025ac7be7e32aec5531096cacb70a92f0fb1 [file] [log] [blame]
John Snow306dfcd2019-06-27 17:28:15 -04001"""
2QEMU machine module:
3
4The machine module primarily provides the QEMUMachine class,
5which provides facilities for managing the lifetime of a QEMU VM.
6"""
7
John Snowabf0bf92019-06-27 17:28:14 -04008# 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
20import errno
John Snowaad3f3b2020-10-06 19:58:06 -040021from itertools import chain
John Snow5690b432021-09-16 14:22:47 -040022import locale
John Snowabf0bf92019-06-27 17:28:14 -040023import logging
24import os
John Snowabf0bf92019-06-27 17:28:14 -040025import shutil
John Snowde6e08b2020-07-10 01:06:48 -040026import signal
John Snowf12a2822020-10-06 19:58:08 -040027import socket
John Snow932ca4b2020-10-06 19:57:58 -040028import subprocess
John Snowabf0bf92019-06-27 17:28:14 -040029import tempfile
John Snow1dda0402020-05-14 01:53:44 -040030from types import TracebackType
John Snowaaa81ec2020-10-06 19:58:03 -040031from typing import (
32 Any,
John Snowf12a2822020-10-06 19:58:08 -040033 BinaryIO,
John Snowaaa81ec2020-10-06 19:58:03 -040034 Dict,
35 List,
36 Optional,
John Snowaad3f3b2020-10-06 19:58:06 -040037 Sequence,
38 Tuple,
John Snowaaa81ec2020-10-06 19:58:03 -040039 Type,
Vladimir Sementsov-Ogievskiy15c3b862021-08-24 11:38:47 +030040 TypeVar,
John Snowaaa81ec2020-10-06 19:58:03 -040041)
John Snowabf0bf92019-06-27 17:28:14 -040042
John Snowa4225302022-03-21 16:33:12 -040043from qemu.aqmp import SocketAddrT
44from qemu.aqmp.legacy import (
45 QEMUMonitorProtocol,
John Snowbeb6b572021-05-27 17:16:53 -040046 QMPMessage,
47 QMPReturnValue,
John Snowbeb6b572021-05-27 17:16:53 -040048)
49
50from . import console_socket
John Snow932ca4b2020-10-06 19:57:58 -040051
John Snowabf0bf92019-06-27 17:28:14 -040052
53LOG = logging.getLogger(__name__)
54
John Snow8dfac2e2020-05-28 18:21:29 -040055
John Snowabf0bf92019-06-27 17:28:14 -040056class QEMUMachineError(Exception):
57 """
58 Exception called when an error in QEMUMachine happens.
59 """
60
61
62class 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 Snow50465f92022-01-31 23:11:32 -050072class 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 Snow193bf1c2020-07-10 01:06:47 -0400101class AbnormalShutdown(QEMUMachineError):
102 """
103 Exception raised when a graceful shutdown was requested, but not performed.
104 """
105
106
Vladimir Sementsov-Ogievskiy15c3b862021-08-24 11:38:47 +0300107_T = TypeVar('_T', bound='QEMUMachine')
108
109
John Snow9b8ccd62020-05-28 18:21:28 -0400110class QEMUMachine:
John Snowabf0bf92019-06-27 17:28:14 -0400111 """
John Snowf12a2822020-10-06 19:58:08 -0400112 A QEMU VM.
John Snowabf0bf92019-06-27 17:28:14 -0400113
John Snow8dfac2e2020-05-28 18:21:29 -0400114 Use this object as a context manager to ensure
115 the QEMU process terminates::
John Snowabf0bf92019-06-27 17:28:14 -0400116
117 with VM(binary) as vm:
118 ...
119 # vm is guaranteed to be shut down here
120 """
John Snow82e65172021-06-29 17:43:11 -0400121 # pylint: disable=too-many-instance-attributes, too-many-public-methods
John Snowabf0bf92019-06-27 17:28:14 -0400122
John Snowaad3f3b2020-10-06 19:58:06 -0400123 def __init__(self,
124 binary: str,
125 args: Sequence[str] = (),
126 wrapper: Sequence[str] = (),
127 name: Optional[str] = None,
Cleber Rosa2ca6e262021-02-11 17:01:42 -0500128 base_temp_dir: str = "/var/tmp",
John Snowc4e60232020-10-06 19:57:59 -0400129 monitor_address: Optional[SocketAddrT] = None,
John Snowf12a2822020-10-06 19:58:08 -0400130 sock_dir: Optional[str] = None,
131 drain_console: bool = False,
Cleber Rosab306e262021-02-11 16:55:05 -0500132 console_log: Optional[str] = None,
Emanuele Giuseppe Espositoe2f948a2021-08-09 11:00:59 +0200133 log_dir: Optional[str] = None,
134 qmp_timer: Optional[float] = None):
John Snowabf0bf92019-06-27 17:28:14 -0400135 '''
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 Snow859aeb62021-05-27 17:16:51 -0400142 @param base_temp_dir: default location where temp files are created
John Snowabf0bf92019-06-27 17:28:14 -0400143 @param monitor_address: address for QMP monitor
Cleber Rosa2ca6e262021-02-11 17:01:42 -0500144 @param sock_dir: where to create socket (defaults to base_temp_dir)
Robert Foley0fc8f662020-07-01 14:56:24 +0100145 @param drain_console: (optional) True to drain console socket to buffer
John Snowc5e61a62020-10-06 19:58:00 -0400146 @param console_log: (optional) path to console log file
Cleber Rosab306e262021-02-11 16:55:05 -0500147 @param log_dir: where to create and keep log files
Emanuele Giuseppe Espositoe2f948a2021-08-09 11:00:59 +0200148 @param qmp_timer: (optional) default QMP socket timeout
John Snowabf0bf92019-06-27 17:28:14 -0400149 @note: Qemu process is not started until launch() is used.
150 '''
John Snow82e65172021-06-29 17:43:11 -0400151 # pylint: disable=too-many-arguments
152
John Snowc5e61a62020-10-06 19:58:00 -0400153 # Direct user configuration
154
155 self._binary = binary
John Snowc5e61a62020-10-06 19:58:00 -0400156 self._args = list(args)
John Snowc5e61a62020-10-06 19:58:00 -0400157 self._wrapper = wrapper
Emanuele Giuseppe Espositoe2f948a2021-08-09 11:00:59 +0200158 self._qmp_timer = qmp_timer
John Snowc5e61a62020-10-06 19:58:00 -0400159
John Snow72b17fe2021-11-18 15:46:16 -0500160 self._name = name or f"qemu-{os.getpid()}-{id(self):02x}"
John Snow87bf1fe2021-11-18 15:46:14 -0500161 self._temp_dir: Optional[str] = None
Cleber Rosa2ca6e262021-02-11 17:01:42 -0500162 self._base_temp_dir = base_temp_dir
John Snow87bf1fe2021-11-18 15:46:14 -0500163 self._sock_dir = sock_dir
Cleber Rosab306e262021-02-11 16:55:05 -0500164 self._log_dir = log_dir
John Snowc5e61a62020-10-06 19:58:00 -0400165
John Snowc4e60232020-10-06 19:57:59 -0400166 if monitor_address is not None:
167 self._monitor_address = monitor_address
John Snowc4e60232020-10-06 19:57:59 -0400168 else:
169 self._monitor_address = os.path.join(
John Snow87bf1fe2021-11-18 15:46:14 -0500170 self.sock_dir, f"{self._name}-monitor.sock"
John Snowc4e60232020-10-06 19:57:59 -0400171 )
John Snowc5e61a62020-10-06 19:58:00 -0400172
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 Snowf12a2822020-10-06 19:58:08 -0400181 self._qemu_log_path: Optional[str] = None
182 self._qemu_log_file: Optional[BinaryIO] = None
John Snow9223fda2020-10-06 19:58:05 -0400183 self._popen: Optional['subprocess.Popen[bytes]'] = None
John Snowf12a2822020-10-06 19:58:08 -0400184 self._events: List[QMPMessage] = []
185 self._iolog: Optional[str] = None
Wainer dos Santos Moschetta74b56bb2019-12-11 13:55:35 -0500186 self._qmp_set = True # Enable QMP monitor by default.
John Snowbeb6b572021-05-27 17:16:53 -0400187 self._qmp_connection: Optional[QEMUMonitorProtocol] = None
John Snowaad3f3b2020-10-06 19:58:06 -0400188 self._qemu_full_args: Tuple[str, ...] = ()
John Snowabf0bf92019-06-27 17:28:14 -0400189 self._launched = False
John Snowf12a2822020-10-06 19:58:08 -0400190 self._machine: Optional[str] = None
Philippe Mathieu-Daudé746f2442020-01-21 00:51:56 +0100191 self._console_index = 0
John Snowabf0bf92019-06-27 17:28:14 -0400192 self._console_set = False
John Snowf12a2822020-10-06 19:58:08 -0400193 self._console_device_type: Optional[str] = None
John Snow652809d2020-10-06 19:58:01 -0400194 self._console_address = os.path.join(
John Snow87bf1fe2021-11-18 15:46:14 -0500195 self.sock_dir, f"{self._name}-console.sock"
John Snow652809d2020-10-06 19:58:01 -0400196 )
John Snowf12a2822020-10-06 19:58:08 -0400197 self._console_socket: Optional[socket.socket] = None
198 self._remove_files: List[str] = []
John Snowde6e08b2020-07-10 01:06:48 -0400199 self._user_killed = False
John Snowb9420e42021-10-26 13:56:05 -0400200 self._quit_issued = False
John Snowabf0bf92019-06-27 17:28:14 -0400201
Vladimir Sementsov-Ogievskiy15c3b862021-08-24 11:38:47 +0300202 def __enter__(self: _T) -> _T:
John Snowabf0bf92019-06-27 17:28:14 -0400203 return self
204
John Snow1dda0402020-05-14 01:53:44 -0400205 def __exit__(self,
206 exc_type: Optional[Type[BaseException]],
207 exc_val: Optional[BaseException],
208 exc_tb: Optional[TracebackType]) -> None:
John Snowabf0bf92019-06-27 17:28:14 -0400209 self.shutdown()
John Snowabf0bf92019-06-27 17:28:14 -0400210
John Snowf12a2822020-10-06 19:58:08 -0400211 def add_monitor_null(self) -> None:
John Snow306dfcd2019-06-27 17:28:15 -0400212 """
213 This can be used to add an unused monitor instance.
214 """
John Snowabf0bf92019-06-27 17:28:14 -0400215 self._args.append('-monitor')
216 self._args.append('null')
217
Vladimir Sementsov-Ogievskiy15c3b862021-08-24 11:38:47 +0300218 def add_fd(self: _T, fd: int, fdset: int,
219 opaque: str, opts: str = '') -> _T:
John Snowabf0bf92019-06-27 17:28:14 -0400220 """
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 Snowf12a2822020-10-06 19:58:08 -0400238 def send_fd_scm(self, fd: Optional[int] = None,
239 file_path: Optional[str] = None) -> int:
John Snow306dfcd2019-06-27 17:28:15 -0400240 """
John Snow514d00d2021-09-22 20:49:30 -0400241 Send an fd or file_path to the remote via SCM_RIGHTS.
John Snow306dfcd2019-06-27 17:28:15 -0400242
John Snow514d00d2021-09-22 20:49:30 -0400243 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 Snow306dfcd2019-06-27 17:28:15 -0400246 """
John Snowabf0bf92019-06-27 17:28:14 -0400247 if file_path is not None:
248 assert fd is None
John Snow514d00d2021-09-22 20:49:30 -0400249 with open(file_path, "rb") as passfile:
250 fd = passfile.fileno()
251 self._qmp.send_fd_scm(fd)
John Snowabf0bf92019-06-27 17:28:14 -0400252 else:
253 assert fd is not None
John Snow514d00d2021-09-22 20:49:30 -0400254 self._qmp.send_fd_scm(fd)
John Snowabf0bf92019-06-27 17:28:14 -0400255
John Snow514d00d2021-09-22 20:49:30 -0400256 return 0
John Snowabf0bf92019-06-27 17:28:14 -0400257
258 @staticmethod
John Snowf12a2822020-10-06 19:58:08 -0400259 def _remove_if_exists(path: str) -> None:
John Snowabf0bf92019-06-27 17:28:14 -0400260 """
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 Snowf12a2822020-10-06 19:58:08 -0400270 def is_running(self) -> bool:
John Snow306dfcd2019-06-27 17:28:15 -0400271 """Returns true if the VM is running."""
John Snowabf0bf92019-06-27 17:28:14 -0400272 return self._popen is not None and self._popen.poll() is None
273
John Snow9223fda2020-10-06 19:58:05 -0400274 @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 Snowf12a2822020-10-06 19:58:08 -0400280 def exitcode(self) -> Optional[int]:
John Snow306dfcd2019-06-27 17:28:15 -0400281 """Returns the exit code if possible, or None."""
John Snowabf0bf92019-06-27 17:28:14 -0400282 if self._popen is None:
283 return None
284 return self._popen.poll()
285
John Snowf12a2822020-10-06 19:58:08 -0400286 def get_pid(self) -> Optional[int]:
John Snow306dfcd2019-06-27 17:28:15 -0400287 """Returns the PID of the running process, or None."""
John Snowabf0bf92019-06-27 17:28:14 -0400288 if not self.is_running():
289 return None
John Snow9223fda2020-10-06 19:58:05 -0400290 return self._subp.pid
John Snowabf0bf92019-06-27 17:28:14 -0400291
John Snowf12a2822020-10-06 19:58:08 -0400292 def _load_io_log(self) -> None:
John Snow5690b432021-09-16 14:22:47 -0400293 # 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 Snowabf0bf92019-06-27 17:28:14 -0400297 if self._qemu_log_path is not None:
John Snow5690b432021-09-16 14:22:47 -0400298 with open(self._qemu_log_path, "r", encoding=encoding) as iolog:
John Snowabf0bf92019-06-27 17:28:14 -0400299 self._iolog = iolog.read()
300
John Snow652809d2020-10-06 19:58:01 -0400301 @property
302 def _base_args(self) -> List[str]:
Wainer dos Santos Moschetta74b56bb2019-12-11 13:55:35 -0500303 args = ['-display', 'none', '-vga', 'none']
John Snowc4e60232020-10-06 19:57:59 -0400304
Wainer dos Santos Moschetta74b56bb2019-12-11 13:55:35 -0500305 if self._qmp_set:
306 if isinstance(self._monitor_address, tuple):
John Snowc4e60232020-10-06 19:57:59 -0400307 moncdev = "socket,id=mon,host={},port={}".format(
308 *self._monitor_address
309 )
Wainer dos Santos Moschetta74b56bb2019-12-11 13:55:35 -0500310 else:
John Snowc4e60232020-10-06 19:57:59 -0400311 moncdev = f"socket,id=mon,path={self._monitor_address}"
Wainer dos Santos Moschetta74b56bb2019-12-11 13:55:35 -0500312 args.extend(['-chardev', moncdev, '-mon',
313 'chardev=mon,mode=control'])
John Snowc4e60232020-10-06 19:57:59 -0400314
John Snowabf0bf92019-06-27 17:28:14 -0400315 if self._machine is not None:
316 args.extend(['-machine', self._machine])
John Snow9b8ccd62020-05-28 18:21:28 -0400317 for _ in range(self._console_index):
Philippe Mathieu-Daudé746f2442020-01-21 00:51:56 +0100318 args.extend(['-serial', 'null'])
John Snowabf0bf92019-06-27 17:28:14 -0400319 if self._console_set:
Paolo Bonzini991c1802020-11-13 03:10:52 -0500320 chardev = ('socket,id=console,path=%s,server=on,wait=off' %
John Snowabf0bf92019-06-27 17:28:14 -0400321 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 Moschetta555fe0c2021-04-30 10:34:12 -0300330 @property
331 def args(self) -> List[str]:
332 """Returns the list of arguments given to the QEMU binary."""
333 return self._args
334
John Snowf12a2822020-10-06 19:58:08 -0400335 def _pre_launch(self) -> None:
John Snow652809d2020-10-06 19:58:01 -0400336 if self._console_set:
337 self._remove_files.append(self._console_address)
338
Wainer dos Santos Moschetta74b56bb2019-12-11 13:55:35 -0500339 if self._qmp_set:
John Snow6eeb3de2021-11-18 15:46:15 -0500340 if isinstance(self._monitor_address, str):
John Snowc4e60232020-10-06 19:57:59 -0400341 self._remove_files.append(self._monitor_address)
John Snowbeb6b572021-05-27 17:16:53 -0400342 self._qmp_connection = QEMUMonitorProtocol(
John Snowc4e60232020-10-06 19:57:59 -0400343 self._monitor_address,
344 server=True,
345 nickname=self._name
346 )
John Snowabf0bf92019-06-27 17:28:14 -0400347
John Snow63c33f32021-05-27 17:16:49 -0400348 # NOTE: Make sure any opened resources are *definitely* freed in
349 # _post_shutdown()!
350 # pylint: disable=consider-using-with
Cleber Rosab306e262021-02-11 16:55:05 -0500351 self._qemu_log_path = os.path.join(self.log_dir, self._name + ".log")
John Snow63c33f32021-05-27 17:16:49 -0400352 self._qemu_log_file = open(self._qemu_log_path, 'wb')
353
John Snowb1ca9912021-11-18 15:46:17 -0500354 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 Snowf12a2822020-10-06 19:58:08 -0400362 def _post_launch(self) -> None:
John Snowbe1183e2020-10-06 19:58:04 -0400363 if self._qmp_connection:
Emanuele Giuseppe Espositoe2f948a2021-08-09 11:00:59 +0200364 self._qmp.accept(self._qmp_timer)
John Snowabf0bf92019-06-27 17:28:14 -0400365
Emanuele Giuseppe Espositoeb7a91d2021-08-09 11:01:13 +0200366 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 Snowf12a2822020-10-06 19:58:08 -0400371 def _post_shutdown(self) -> None:
John Snowa3842cb2020-07-10 01:06:42 -0400372 """
373 Called to cleanup the VM instance after the process has exited.
374 May also be called after a failed launch.
375 """
John Snow49a608b2021-10-26 13:56:06 -0400376 try:
377 self._close_qmp_connection()
378 except Exception as err: # pylint: disable=broad-except
379 LOG.warning(
380 "Exception closing QMP connection: %s",
381 str(err) if str(err) else type(err).__name__
382 )
383 finally:
384 assert self._qmp_connection is None
John Snow671940e2020-07-10 01:06:39 -0400385
Emanuele Giuseppe Espositoeb7a91d2021-08-09 11:01:13 +0200386 self._close_qemu_log_file()
John Snowabf0bf92019-06-27 17:28:14 -0400387
Cleber Rosa3c1e16c2021-02-11 17:01:41 -0500388 self._load_io_log()
389
John Snowabf0bf92019-06-27 17:28:14 -0400390 self._qemu_log_path = None
391
John Snowabf0bf92019-06-27 17:28:14 -0400392 if self._temp_dir is not None:
393 shutil.rmtree(self._temp_dir)
394 self._temp_dir = None
395
Max Reitz32558ce2019-10-17 15:31:34 +0200396 while len(self._remove_files) > 0:
397 self._remove_if_exists(self._remove_files.pop())
398
John Snow14661d92020-07-10 01:06:38 -0400399 exitcode = self.exitcode()
John Snowde6e08b2020-07-10 01:06:48 -0400400 if (exitcode is not None and exitcode < 0
401 and not (self._user_killed and exitcode == -signal.SIGKILL)):
John Snow14661d92020-07-10 01:06:38 -0400402 msg = 'qemu received signal %i; command: "%s"'
403 if self._qemu_full_args:
404 command = ' '.join(self._qemu_full_args)
405 else:
406 command = ''
407 LOG.warning(msg, -int(exitcode), command)
408
John Snowb9420e42021-10-26 13:56:05 -0400409 self._quit_issued = False
John Snowde6e08b2020-07-10 01:06:48 -0400410 self._user_killed = False
John Snow14661d92020-07-10 01:06:38 -0400411 self._launched = False
412
John Snowf12a2822020-10-06 19:58:08 -0400413 def launch(self) -> None:
John Snowabf0bf92019-06-27 17:28:14 -0400414 """
415 Launch the VM and make sure we cleanup and expose the
416 command line/output in case of exception
417 """
418
419 if self._launched:
420 raise QEMUMachineError('VM already launched')
421
John Snowabf0bf92019-06-27 17:28:14 -0400422 try:
423 self._launch()
John Snow50465f92022-01-31 23:11:32 -0500424 except BaseException as exc:
John Snow1611e6c2021-11-18 15:46:18 -0500425 # We may have launched the process but it may
426 # have exited before we could connect via QMP.
427 # Assume the VM didn't launch or is exiting.
428 # If we don't wait for the process, exitcode() may still be
429 # 'None' by the time control is ceded back to the caller.
430 if self._launched:
431 self.wait()
432 else:
433 self._post_shutdown()
John Snowabf0bf92019-06-27 17:28:14 -0400434
John Snow50465f92022-01-31 23:11:32 -0500435 if isinstance(exc, Exception):
436 raise VMLaunchFailure(
437 exitcode=self.exitcode(),
438 command=' '.join(self._qemu_full_args),
439 output=self._iolog
440 ) from exc
441
442 # Don't wrap 'BaseException'; doing so would downgrade
443 # that exception. However, we still want to clean up.
John Snowabf0bf92019-06-27 17:28:14 -0400444 raise
445
John Snowf12a2822020-10-06 19:58:08 -0400446 def _launch(self) -> None:
John Snowabf0bf92019-06-27 17:28:14 -0400447 """
448 Launch the VM and establish a QMP connection
449 """
John Snowabf0bf92019-06-27 17:28:14 -0400450 self._pre_launch()
John Snowabf0bf92019-06-27 17:28:14 -0400451 LOG.debug('VM launch command: %r', ' '.join(self._qemu_full_args))
John Snowa0eae172021-05-27 17:16:50 -0400452
453 # Cleaning up of this subprocess is guaranteed by _do_shutdown.
454 # pylint: disable=consider-using-with
John Snowabf0bf92019-06-27 17:28:14 -0400455 self._popen = subprocess.Popen(self._qemu_full_args,
John Snow07b71232021-05-27 17:16:46 -0400456 stdin=subprocess.DEVNULL,
John Snowabf0bf92019-06-27 17:28:14 -0400457 stdout=self._qemu_log_file,
458 stderr=subprocess.STDOUT,
459 shell=False,
460 close_fds=False)
John Snow1611e6c2021-11-18 15:46:18 -0500461 self._launched = True
John Snowabf0bf92019-06-27 17:28:14 -0400462 self._post_launch()
463
John Snow49a608b2021-10-26 13:56:06 -0400464 def _close_qmp_connection(self) -> None:
465 """
466 Close the underlying QMP connection, if any.
467
468 Dutifully report errors that occurred while closing, but assume
469 that any error encountered indicates an abnormal termination
470 process and not a failure to close.
471 """
472 if self._qmp_connection is None:
473 return
474
475 try:
476 self._qmp.close()
477 except EOFError:
478 # EOF can occur as an Exception here when using the Async
479 # QMP backend. It indicates that the server closed the
480 # stream. If we successfully issued 'quit' at any point,
481 # then this was expected. If the remote went away without
482 # our permission, it's worth reporting that as an abnormal
483 # shutdown case.
484 if not (self._user_killed or self._quit_issued):
485 raise
486 finally:
487 self._qmp_connection = None
488
John Snowe2c97f12020-07-10 01:06:40 -0400489 def _early_cleanup(self) -> None:
490 """
491 Perform any cleanup that needs to happen before the VM exits.
John Snowa3842cb2020-07-10 01:06:42 -0400492
John Snow1611e6c2021-11-18 15:46:18 -0500493 This method may be called twice upon shutdown, once each by soft
494 and hard shutdown in failover scenarios.
John Snowe2c97f12020-07-10 01:06:40 -0400495 """
496 # If we keep the console socket open, we may deadlock waiting
497 # for QEMU to exit, while QEMU is waiting for the socket to
498 # become writeable.
499 if self._console_socket is not None:
500 self._console_socket.close()
501 self._console_socket = None
502
John Snow193bf1c2020-07-10 01:06:47 -0400503 def _hard_shutdown(self) -> None:
504 """
505 Perform early cleanup, kill the VM, and wait for it to terminate.
506
507 :raise subprocess.Timeout: When timeout is exceeds 60 seconds
508 waiting for the QEMU process to terminate.
509 """
510 self._early_cleanup()
John Snow9223fda2020-10-06 19:58:05 -0400511 self._subp.kill()
512 self._subp.wait(timeout=60)
John Snow193bf1c2020-07-10 01:06:47 -0400513
John Snowb9420e42021-10-26 13:56:05 -0400514 def _soft_shutdown(self, timeout: Optional[int]) -> None:
John Snow193bf1c2020-07-10 01:06:47 -0400515 """
516 Perform early cleanup, attempt to gracefully shut down the VM, and wait
517 for it to terminate.
518
John Snow8226a4b2020-07-20 12:02:52 -0400519 :param timeout: Timeout in seconds for graceful shutdown.
520 A value of None is an infinite wait.
John Snow193bf1c2020-07-10 01:06:47 -0400521
522 :raise ConnectionReset: On QMP communication errors
523 :raise subprocess.TimeoutExpired: When timeout is exceeded waiting for
524 the QEMU process to terminate.
525 """
526 self._early_cleanup()
527
John Snowbe1183e2020-10-06 19:58:04 -0400528 if self._qmp_connection:
John Snow49a608b2021-10-26 13:56:06 -0400529 try:
530 if not self._quit_issued:
531 # May raise ExecInterruptedError or StateError if the
532 # connection dies or has *already* died.
533 self.qmp('quit')
534 finally:
535 # Regardless, we want to quiesce the connection.
536 self._close_qmp_connection()
John Snow193bf1c2020-07-10 01:06:47 -0400537
538 # May raise subprocess.TimeoutExpired
John Snow9223fda2020-10-06 19:58:05 -0400539 self._subp.wait(timeout=timeout)
John Snow193bf1c2020-07-10 01:06:47 -0400540
John Snowb9420e42021-10-26 13:56:05 -0400541 def _do_shutdown(self, timeout: Optional[int]) -> None:
John Snow193bf1c2020-07-10 01:06:47 -0400542 """
543 Attempt to shutdown the VM gracefully; fallback to a hard shutdown.
544
John Snow8226a4b2020-07-20 12:02:52 -0400545 :param timeout: Timeout in seconds for graceful shutdown.
546 A value of None is an infinite wait.
John Snow193bf1c2020-07-10 01:06:47 -0400547
548 :raise AbnormalShutdown: When the VM could not be shut down gracefully.
549 The inner exception will likely be ConnectionReset or
550 subprocess.TimeoutExpired. In rare cases, non-graceful termination
551 may result in its own exceptions, likely subprocess.TimeoutExpired.
552 """
553 try:
John Snowb9420e42021-10-26 13:56:05 -0400554 self._soft_shutdown(timeout)
John Snow193bf1c2020-07-10 01:06:47 -0400555 except Exception as exc:
556 self._hard_shutdown()
557 raise AbnormalShutdown("Could not perform graceful shutdown") \
558 from exc
559
John Snowb9420e42021-10-26 13:56:05 -0400560 def shutdown(self,
John Snowc9b30452020-07-10 01:06:43 -0400561 hard: bool = False,
John Snow8226a4b2020-07-20 12:02:52 -0400562 timeout: Optional[int] = 30) -> None:
John Snowabf0bf92019-06-27 17:28:14 -0400563 """
John Snow193bf1c2020-07-10 01:06:47 -0400564 Terminate the VM (gracefully if possible) and perform cleanup.
565 Cleanup will always be performed.
566
567 If the VM has not yet been launched, or shutdown(), wait(), or kill()
568 have already been called, this method does nothing.
569
John Snow193bf1c2020-07-10 01:06:47 -0400570 :param hard: When true, do not attempt graceful shutdown, and
571 suppress the SIGKILL warning log message.
572 :param timeout: Optional timeout in seconds for graceful shutdown.
John Snow8226a4b2020-07-20 12:02:52 -0400573 Default 30 seconds, A `None` value is an infinite wait.
John Snowabf0bf92019-06-27 17:28:14 -0400574 """
John Snowa3842cb2020-07-10 01:06:42 -0400575 if not self._launched:
576 return
577
John Snow193bf1c2020-07-10 01:06:47 -0400578 try:
Vladimir Sementsov-Ogievskiye0e925a2020-02-17 18:02:42 +0300579 if hard:
John Snowde6e08b2020-07-10 01:06:48 -0400580 self._user_killed = True
John Snow193bf1c2020-07-10 01:06:47 -0400581 self._hard_shutdown()
582 else:
John Snowb9420e42021-10-26 13:56:05 -0400583 self._do_shutdown(timeout)
John Snow193bf1c2020-07-10 01:06:47 -0400584 finally:
585 self._post_shutdown()
John Snowabf0bf92019-06-27 17:28:14 -0400586
John Snowf12a2822020-10-06 19:58:08 -0400587 def kill(self) -> None:
John Snow193bf1c2020-07-10 01:06:47 -0400588 """
589 Terminate the VM forcefully, wait for it to exit, and perform cleanup.
590 """
Vladimir Sementsov-Ogievskiye0e925a2020-02-17 18:02:42 +0300591 self.shutdown(hard=True)
592
John Snow8226a4b2020-07-20 12:02:52 -0400593 def wait(self, timeout: Optional[int] = 30) -> None:
John Snow89528052020-07-10 01:06:44 -0400594 """
595 Wait for the VM to power off and perform post-shutdown cleanup.
596
John Snow8226a4b2020-07-20 12:02:52 -0400597 :param timeout: Optional timeout in seconds. Default 30 seconds.
598 A value of `None` is an infinite wait.
John Snow89528052020-07-10 01:06:44 -0400599 """
John Snowb9420e42021-10-26 13:56:05 -0400600 self._quit_issued = True
601 self.shutdown(timeout=timeout)
John Snow89528052020-07-10 01:06:44 -0400602
John Snowf12a2822020-10-06 19:58:08 -0400603 def set_qmp_monitor(self, enabled: bool = True) -> None:
Wainer dos Santos Moschetta74b56bb2019-12-11 13:55:35 -0500604 """
605 Set the QMP monitor.
606
607 @param enabled: if False, qmp monitor options will be removed from
608 the base arguments of the resulting QEMU command
609 line. Default is True.
John Snow5c02c862021-06-29 17:43:23 -0400610
611 .. note:: Call this function before launch().
Wainer dos Santos Moschetta74b56bb2019-12-11 13:55:35 -0500612 """
John Snowbe1183e2020-10-06 19:58:04 -0400613 self._qmp_set = enabled
614
615 @property
John Snowbeb6b572021-05-27 17:16:53 -0400616 def _qmp(self) -> QEMUMonitorProtocol:
John Snowbe1183e2020-10-06 19:58:04 -0400617 if self._qmp_connection is None:
618 raise QEMUMachineError("Attempt to access QMP with no connection")
619 return self._qmp_connection
Wainer dos Santos Moschetta74b56bb2019-12-11 13:55:35 -0500620
John Snowaaa81ec2020-10-06 19:58:03 -0400621 @classmethod
Vladimir Sementsov-Ogievskiyc7daa572021-08-24 11:38:45 +0300622 def _qmp_args(cls, conv_keys: bool,
623 args: Dict[str, Any]) -> Dict[str, object]:
624 if conv_keys:
625 return {k.replace('_', '-'): v for k, v in args.items()}
626
627 return args
John Snowabf0bf92019-06-27 17:28:14 -0400628
John Snowaaa81ec2020-10-06 19:58:03 -0400629 def qmp(self, cmd: str,
Vladimir Sementsov-Ogievskiy3f3c9b42021-08-24 11:38:46 +0300630 args_dict: Optional[Dict[str, object]] = None,
631 conv_keys: Optional[bool] = None,
John Snowaaa81ec2020-10-06 19:58:03 -0400632 **args: Any) -> QMPMessage:
633 """
634 Invoke a QMP command and return the response dict
635 """
Vladimir Sementsov-Ogievskiy3f3c9b42021-08-24 11:38:46 +0300636 if args_dict is not None:
637 assert not args
638 assert conv_keys is None
639 args = args_dict
640 conv_keys = False
641
642 if conv_keys is None:
643 conv_keys = True
644
Vladimir Sementsov-Ogievskiyc7daa572021-08-24 11:38:45 +0300645 qmp_args = self._qmp_args(conv_keys, args)
John Snowb9420e42021-10-26 13:56:05 -0400646 ret = self._qmp.cmd(cmd, args=qmp_args)
647 if cmd == 'quit' and 'error' not in ret and 'return' in ret:
648 self._quit_issued = True
649 return ret
John Snowabf0bf92019-06-27 17:28:14 -0400650
John Snowf12a2822020-10-06 19:58:08 -0400651 def command(self, cmd: str,
652 conv_keys: bool = True,
653 **args: Any) -> QMPReturnValue:
John Snowabf0bf92019-06-27 17:28:14 -0400654 """
655 Invoke a QMP command.
656 On success return the response dict.
657 On failure raise an exception.
658 """
Vladimir Sementsov-Ogievskiyc7daa572021-08-24 11:38:45 +0300659 qmp_args = self._qmp_args(conv_keys, args)
John Snowb9420e42021-10-26 13:56:05 -0400660 ret = self._qmp.command(cmd, **qmp_args)
661 if cmd == 'quit':
662 self._quit_issued = True
663 return ret
John Snowabf0bf92019-06-27 17:28:14 -0400664
John Snowf12a2822020-10-06 19:58:08 -0400665 def get_qmp_event(self, wait: bool = False) -> Optional[QMPMessage]:
John Snowabf0bf92019-06-27 17:28:14 -0400666 """
667 Poll for one queued QMP events and return it
668 """
John Snow306dfcd2019-06-27 17:28:15 -0400669 if self._events:
John Snowabf0bf92019-06-27 17:28:14 -0400670 return self._events.pop(0)
671 return self._qmp.pull_event(wait=wait)
672
John Snowf12a2822020-10-06 19:58:08 -0400673 def get_qmp_events(self, wait: bool = False) -> List[QMPMessage]:
John Snowabf0bf92019-06-27 17:28:14 -0400674 """
675 Poll for queued QMP events and return a list of dicts
676 """
677 events = self._qmp.get_events(wait=wait)
678 events.extend(self._events)
679 del self._events[:]
John Snowabf0bf92019-06-27 17:28:14 -0400680 return events
681
682 @staticmethod
John Snowf12a2822020-10-06 19:58:08 -0400683 def event_match(event: Any, match: Optional[Any]) -> bool:
John Snowabf0bf92019-06-27 17:28:14 -0400684 """
685 Check if an event matches optional match criteria.
686
687 The match criteria takes the form of a matching subdict. The event is
688 checked to be a superset of the subdict, recursively, with matching
689 values whenever the subdict values are not None.
690
691 This has a limitation that you cannot explicitly check for None values.
692
693 Examples, with the subdict queries on the left:
694 - None matches any object.
695 - {"foo": None} matches {"foo": {"bar": 1}}
696 - {"foo": None} matches {"foo": 5}
697 - {"foo": {"abc": None}} does not match {"foo": {"bar": 1}}
698 - {"foo": {"rab": 2}} matches {"foo": {"bar": 1, "rab": 2}}
699 """
700 if match is None:
701 return True
702
703 try:
704 for key in match:
705 if key in event:
706 if not QEMUMachine.event_match(event[key], match[key]):
707 return False
708 else:
709 return False
710 return True
711 except TypeError:
712 # either match or event wasn't iterable (not a dict)
John Snowf12a2822020-10-06 19:58:08 -0400713 return bool(match == event)
John Snowabf0bf92019-06-27 17:28:14 -0400714
John Snowf12a2822020-10-06 19:58:08 -0400715 def event_wait(self, name: str,
716 timeout: float = 60.0,
717 match: Optional[QMPMessage] = None) -> Optional[QMPMessage]:
John Snowabf0bf92019-06-27 17:28:14 -0400718 """
719 event_wait waits for and returns a named event from QMP with a timeout.
720
721 name: The event to wait for.
722 timeout: QEMUMonitorProtocol.pull_event timeout parameter.
723 match: Optional match criteria. See event_match for details.
724 """
725 return self.events_wait([(name, match)], timeout)
726
John Snowf12a2822020-10-06 19:58:08 -0400727 def events_wait(self,
728 events: Sequence[Tuple[str, Any]],
729 timeout: float = 60.0) -> Optional[QMPMessage]:
John Snowabf0bf92019-06-27 17:28:14 -0400730 """
John Snow1847a4a2020-10-06 19:58:02 -0400731 events_wait waits for and returns a single named event from QMP.
732 In the case of multiple qualifying events, this function returns the
733 first one.
John Snowabf0bf92019-06-27 17:28:14 -0400734
John Snow1847a4a2020-10-06 19:58:02 -0400735 :param events: A sequence of (name, match_criteria) tuples.
736 The match criteria are optional and may be None.
737 See event_match for details.
738 :param timeout: Optional timeout, in seconds.
739 See QEMUMonitorProtocol.pull_event.
740
John Snowa4225302022-03-21 16:33:12 -0400741 :raise asyncio.TimeoutError:
742 If timeout was non-zero and no matching events were found.
743
John Snow1847a4a2020-10-06 19:58:02 -0400744 :return: A QMP event matching the filter criteria.
745 If timeout was 0 and no event matched, None.
John Snowabf0bf92019-06-27 17:28:14 -0400746 """
John Snowf12a2822020-10-06 19:58:08 -0400747 def _match(event: QMPMessage) -> bool:
John Snowabf0bf92019-06-27 17:28:14 -0400748 for name, match in events:
John Snow306dfcd2019-06-27 17:28:15 -0400749 if event['event'] == name and self.event_match(event, match):
John Snowabf0bf92019-06-27 17:28:14 -0400750 return True
751 return False
752
John Snow1847a4a2020-10-06 19:58:02 -0400753 event: Optional[QMPMessage]
754
John Snowabf0bf92019-06-27 17:28:14 -0400755 # Search cached events
756 for event in self._events:
757 if _match(event):
758 self._events.remove(event)
759 return event
760
761 # Poll for new events
762 while True:
763 event = self._qmp.pull_event(wait=timeout)
John Snow1847a4a2020-10-06 19:58:02 -0400764 if event is None:
765 # NB: None is only returned when timeout is false-ish.
John Snowa4225302022-03-21 16:33:12 -0400766 # Timeouts raise asyncio.TimeoutError instead!
John Snow1847a4a2020-10-06 19:58:02 -0400767 break
John Snowabf0bf92019-06-27 17:28:14 -0400768 if _match(event):
769 return event
770 self._events.append(event)
771
772 return None
773
John Snowf12a2822020-10-06 19:58:08 -0400774 def get_log(self) -> Optional[str]:
John Snowabf0bf92019-06-27 17:28:14 -0400775 """
776 After self.shutdown or failed qemu execution, this returns the output
777 of the qemu process.
778 """
779 return self._iolog
780
John Snowf12a2822020-10-06 19:58:08 -0400781 def add_args(self, *args: str) -> None:
John Snowabf0bf92019-06-27 17:28:14 -0400782 """
783 Adds to the list of extra arguments to be given to the QEMU binary
784 """
785 self._args.extend(args)
786
John Snowf12a2822020-10-06 19:58:08 -0400787 def set_machine(self, machine_type: str) -> None:
John Snowabf0bf92019-06-27 17:28:14 -0400788 """
789 Sets the machine type
790
791 If set, the machine type will be added to the base arguments
792 of the resulting QEMU command line.
793 """
794 self._machine = machine_type
795
John Snowf12a2822020-10-06 19:58:08 -0400796 def set_console(self,
797 device_type: Optional[str] = None,
798 console_index: int = 0) -> None:
John Snowabf0bf92019-06-27 17:28:14 -0400799 """
800 Sets the device type for a console device
801
802 If set, the console device and a backing character device will
803 be added to the base arguments of the resulting QEMU command
804 line.
805
806 This is a convenience method that will either use the provided
807 device type, or default to a "-serial chardev:console" command
808 line argument.
809
810 The actual setting of command line arguments will be be done at
811 machine launch time, as it depends on the temporary directory
812 to be created.
813
814 @param device_type: the device type, such as "isa-serial". If
815 None is given (the default value) a "-serial
816 chardev:console" command line argument will
817 be used instead, resorting to the machine's
818 default device type.
Philippe Mathieu-Daudé746f2442020-01-21 00:51:56 +0100819 @param console_index: the index of the console device to use.
820 If not zero, the command line will create
821 'index - 1' consoles and connect them to
822 the 'null' backing character device.
John Snowabf0bf92019-06-27 17:28:14 -0400823 """
824 self._console_set = True
825 self._console_device_type = device_type
Philippe Mathieu-Daudé746f2442020-01-21 00:51:56 +0100826 self._console_index = console_index
John Snowabf0bf92019-06-27 17:28:14 -0400827
828 @property
John Snowf12a2822020-10-06 19:58:08 -0400829 def console_socket(self) -> socket.socket:
John Snowabf0bf92019-06-27 17:28:14 -0400830 """
831 Returns a socket connected to the console
832 """
833 if self._console_socket is None:
Robert Foley80ded8e2020-07-24 07:45:08 +0100834 self._console_socket = console_socket.ConsoleSocket(
835 self._console_address,
836 file=self._console_log_path,
837 drain=self._drain_console)
John Snowabf0bf92019-06-27 17:28:14 -0400838 return self._console_socket
Cleber Rosa2ca6e262021-02-11 17:01:42 -0500839
840 @property
841 def temp_dir(self) -> str:
842 """
843 Returns a temporary directory to be used for this machine
844 """
845 if self._temp_dir is None:
846 self._temp_dir = tempfile.mkdtemp(prefix="qemu-machine-",
847 dir=self._base_temp_dir)
848 return self._temp_dir
Cleber Rosab306e262021-02-11 16:55:05 -0500849
850 @property
John Snow87bf1fe2021-11-18 15:46:14 -0500851 def sock_dir(self) -> str:
852 """
853 Returns the directory used for sockfiles by this machine.
854 """
855 if self._sock_dir:
856 return self._sock_dir
857 return self.temp_dir
858
859 @property
Cleber Rosab306e262021-02-11 16:55:05 -0500860 def log_dir(self) -> str:
861 """
862 Returns a directory to be used for writing logs
863 """
864 if self._log_dir is None:
865 return self.temp_dir
866 return self._log_dir