]> git.proxmox.com Git - mirror_qemu.git/blame - scripts/qemu.py
Merge remote-tracking branch 'remotes/dgibson/tags/ppc-for-3.0-20180703' into staging
[mirror_qemu.git] / scripts / qemu.py
CommitLineData
66613974
DB
1# QEMU library
2#
3# Copyright (C) 2015-2016 Red Hat Inc.
4# Copyright (C) 2012 IBM Corp.
5#
6# Authors:
7# Fam Zheng <famz@redhat.com>
8#
9# This work is licensed under the terms of the GNU GPL, version 2. See
10# the COPYING file in the top-level directory.
11#
12# Based on qmp.py.
13#
14
15import errno
4738b0a8 16import logging
66613974 17import os
66613974
DB
18import subprocess
19import qmp.qmp
22dea9db 20import re
af99fa9f 21import shutil
22dea9db 22import socket
af99fa9f 23import tempfile
66613974
DB
24
25
4738b0a8
AP
26LOG = logging.getLogger(__name__)
27
28
22dea9db
CR
29#: Maps machine types to the preferred console device types
30CONSOLE_DEV_TYPES = {
31 r'^clipper$': 'isa-serial',
32 r'^malta': 'isa-serial',
33 r'^(pc.*|q35.*|isapc)$': 'isa-serial',
34 r'^(40p|powernv|prep)$': 'isa-serial',
35 r'^pseries.*': 'spapr-vty',
36 r'^s390-ccw-virtio.*': 'sclpconsole',
37 }
38
39
4738b0a8
AP
40class QEMUMachineError(Exception):
41 """
42 Exception called when an error in QEMUMachine happens.
43 """
44
45
22dea9db
CR
46class QEMUMachineAddDeviceError(QEMUMachineError):
47 """
48 Exception raised when a request to add a device can not be fulfilled
49
50 The failures are caused by limitations, lack of information or conflicting
51 requests on the QEMUMachine methods. This exception does not represent
52 failures reported by the QEMU binary itself.
53 """
54
a004e249
LD
55class MonitorResponseError(qmp.qmp.QMPError):
56 '''
57 Represents erroneous QMP monitor reply
58 '''
59 def __init__(self, reply):
60 try:
61 desc = reply["error"]["desc"]
62 except KeyError:
63 desc = reply
64 super(MonitorResponseError, self).__init__(desc)
65 self.reply = reply
66
67
66613974 68class QEMUMachine(object):
d792bc38
SH
69 '''A QEMU VM
70
71 Use this object as a context manager to ensure the QEMU process terminates::
72
73 with VM(binary) as vm:
74 ...
75 # vm is guaranteed to be shut down here
76 '''
66613974 77
2782fc51 78 def __init__(self, binary, args=None, wrapper=None, name=None,
2d853c70 79 test_dir="/var/tmp", monitor_address=None,
1a6d3757 80 socket_scm_helper=None):
2d853c70
LD
81 '''
82 Initialize a QEMUMachine
83
84 @param binary: path to the qemu binary
85 @param args: list of extra arguments
86 @param wrapper: list of arguments used as prefix to qemu binary
87 @param name: prefix for socket and log file names (default: qemu-PID)
88 @param test_dir: where to create socket and log file
89 @param monitor_address: address for QMP monitor
90 @param socket_scm_helper: helper program, required for send_fd_scm()"
2d853c70
LD
91 @note: Qemu process is not started until launch() is used.
92 '''
2782fc51
LD
93 if args is None:
94 args = []
95 if wrapper is None:
96 wrapper = []
66613974
DB
97 if name is None:
98 name = "qemu-%d" % os.getpid()
af99fa9f 99 self._name = name
66613974 100 self._monitor_address = monitor_address
af99fa9f
AP
101 self._vm_monitor = None
102 self._qemu_log_path = None
103 self._qemu_log_file = None
66613974
DB
104 self._popen = None
105 self._binary = binary
2d853c70 106 self._args = list(args) # Force copy args in case we modify them
66613974
DB
107 self._wrapper = wrapper
108 self._events = []
109 self._iolog = None
4c44b4a4 110 self._socket_scm_helper = socket_scm_helper
2d853c70 111 self._qmp = None
dab91d9a 112 self._qemu_full_args = None
af99fa9f
AP
113 self._test_dir = test_dir
114 self._temp_dir = None
156dc7b1 115 self._launched = False
22dea9db
CR
116 self._machine = None
117 self._console_device_type = None
118 self._console_address = None
119 self._console_socket = None
66613974 120
5810314e 121 # just in case logging wasn't configured by the main script:
1a6d3757 122 logging.basicConfig()
5810314e 123
d792bc38
SH
124 def __enter__(self):
125 return self
126
127 def __exit__(self, exc_type, exc_val, exc_tb):
128 self.shutdown()
129 return False
130
66613974
DB
131 # This can be used to add an unused monitor instance.
132 def add_monitor_telnet(self, ip, port):
133 args = 'tcp:%s:%d,server,nowait,telnet' % (ip, port)
134 self._args.append('-monitor')
135 self._args.append(args)
136
137 def add_fd(self, fd, fdset, opaque, opts=''):
138 '''Pass a file descriptor to the VM'''
139 options = ['fd=%d' % fd,
140 'set=%d' % fdset,
141 'opaque=%s' % opaque]
142 if opts:
143 options.append(opts)
144
145 self._args.append('-add-fd')
146 self._args.append(','.join(options))
147 return self
148
149 def send_fd_scm(self, fd_file_path):
150 # In iotest.py, the qmp should always use unix socket.
151 assert self._qmp.is_scm_available()
4c44b4a4 152 if self._socket_scm_helper is None:
4738b0a8 153 raise QEMUMachineError("No path to socket_scm_helper set")
2d853c70 154 if not os.path.exists(self._socket_scm_helper):
4738b0a8
AP
155 raise QEMUMachineError("%s does not exist" %
156 self._socket_scm_helper)
4c44b4a4 157 fd_param = ["%s" % self._socket_scm_helper,
66613974
DB
158 "%d" % self._qmp.get_sock_fd(),
159 "%s" % fd_file_path]
63e0ba55 160 devnull = open(os.path.devnull, 'rb')
4738b0a8
AP
161 proc = subprocess.Popen(fd_param, stdin=devnull, stdout=subprocess.PIPE,
162 stderr=subprocess.STDOUT)
163 output = proc.communicate()[0]
164 if output:
165 LOG.debug(output)
166
167 return proc.returncode
66613974
DB
168
169 @staticmethod
170 def _remove_if_exists(path):
171 '''Remove file object at path if it exists'''
172 try:
173 os.remove(path)
174 except OSError as exception:
175 if exception.errno == errno.ENOENT:
176 return
177 raise
178
37bbcd57 179 def is_running(self):
17589cae 180 return self._popen is not None and self._popen.poll() is None
37bbcd57 181
b2b8d986
EH
182 def exitcode(self):
183 if self._popen is None:
184 return None
17589cae 185 return self._popen.poll()
b2b8d986 186
66613974 187 def get_pid(self):
37bbcd57 188 if not self.is_running():
66613974
DB
189 return None
190 return self._popen.pid
191
192 def _load_io_log(self):
04a963b4
AP
193 if self._qemu_log_path is not None:
194 with open(self._qemu_log_path, "r") as iolog:
195 self._iolog = iolog.read()
66613974
DB
196
197 def _base_args(self):
198 if isinstance(self._monitor_address, tuple):
199 moncdev = "socket,id=mon,host=%s,port=%s" % (
200 self._monitor_address[0],
201 self._monitor_address[1])
202 else:
af99fa9f 203 moncdev = 'socket,id=mon,path=%s' % self._vm_monitor
22dea9db 204 args = ['-chardev', moncdev,
66613974
DB
205 '-mon', 'chardev=mon,mode=control',
206 '-display', 'none', '-vga', 'none']
22dea9db
CR
207 if self._machine is not None:
208 args.extend(['-machine', self._machine])
209 if self._console_device_type is not None:
210 self._console_address = os.path.join(self._temp_dir,
211 self._name + "-console.sock")
212 chardev = ('socket,id=console,path=%s,server,nowait' %
213 self._console_address)
214 device = '%s,chardev=console' % self._console_device_type
215 args.extend(['-chardev', chardev, '-device', device])
216 return args
66613974
DB
217
218 def _pre_launch(self):
af99fa9f
AP
219 self._temp_dir = tempfile.mkdtemp(dir=self._test_dir)
220 if self._monitor_address is not None:
221 self._vm_monitor = self._monitor_address
222 else:
223 self._vm_monitor = os.path.join(self._temp_dir,
224 self._name + "-monitor.sock")
225 self._qemu_log_path = os.path.join(self._temp_dir, self._name + ".log")
226 self._qemu_log_file = open(self._qemu_log_path, 'wb')
227
228 self._qmp = qmp.qmp.QEMUMonitorProtocol(self._vm_monitor,
09177654 229 server=True)
66613974
DB
230
231 def _post_launch(self):
232 self._qmp.accept()
233
234 def _post_shutdown(self):
af99fa9f
AP
235 if self._qemu_log_file is not None:
236 self._qemu_log_file.close()
237 self._qemu_log_file = None
238
239 self._qemu_log_path = None
240
22dea9db
CR
241 if self._console_socket is not None:
242 self._console_socket.close()
243 self._console_socket = None
244
af99fa9f
AP
245 if self._temp_dir is not None:
246 shutil.rmtree(self._temp_dir)
247 self._temp_dir = None
66613974
DB
248
249 def launch(self):
d301bccf
AP
250 """
251 Launch the VM and make sure we cleanup and expose the
252 command line/output in case of exception
253 """
156dc7b1
AP
254
255 if self._launched:
256 raise QEMUMachineError('VM already launched')
257
b92a0011 258 self._iolog = None
dab91d9a 259 self._qemu_full_args = None
66613974 260 try:
d301bccf 261 self._launch()
156dc7b1 262 self._launched = True
66613974 263 except:
c58b535f 264 self.shutdown()
b92a0011
AP
265
266 LOG.debug('Error launching VM')
267 if self._qemu_full_args:
268 LOG.debug('Command: %r', ' '.join(self._qemu_full_args))
269 if self._iolog:
270 LOG.debug('Output: %r', self._iolog)
66613974
DB
271 raise
272
d301bccf
AP
273 def _launch(self):
274 '''Launch the VM and establish a QMP connection'''
275 devnull = open(os.path.devnull, 'rb')
276 self._pre_launch()
277 self._qemu_full_args = (self._wrapper + [self._binary] +
278 self._base_args() + self._args)
279 self._popen = subprocess.Popen(self._qemu_full_args,
280 stdin=devnull,
281 stdout=self._qemu_log_file,
282 stderr=subprocess.STDOUT,
283 shell=False)
284 self._post_launch()
285
22491a2f
FZ
286 def wait(self):
287 '''Wait for the VM to power off'''
288 self._popen.wait()
289 self._qmp.close()
290 self._load_io_log()
291 self._post_shutdown()
292
66613974
DB
293 def shutdown(self):
294 '''Terminate the VM and clean up'''
37bbcd57 295 if self.is_running():
66613974
DB
296 try:
297 self._qmp.cmd('quit')
298 self._qmp.close()
299 except:
300 self._popen.kill()
dab91d9a 301 self._popen.wait()
66613974 302
04a963b4
AP
303 self._load_io_log()
304 self._post_shutdown()
66613974 305
dab91d9a
AP
306 exitcode = self.exitcode()
307 if exitcode is not None and exitcode < 0:
308 msg = 'qemu received signal %i: %s'
309 if self._qemu_full_args:
310 command = ' '.join(self._qemu_full_args)
311 else:
312 command = ''
313 LOG.warn(msg, exitcode, command)
314
156dc7b1
AP
315 self._launched = False
316
66613974 317 def qmp(self, cmd, conv_keys=True, **args):
2d853c70 318 '''Invoke a QMP command and return the response dict'''
66613974 319 qmp_args = dict()
fb2e1cc6 320 for key, value in args.items():
66613974 321 if conv_keys:
41f714b1 322 qmp_args[key.replace('_', '-')] = value
66613974 323 else:
7f33ca78 324 qmp_args[key] = value
66613974
DB
325
326 return self._qmp.cmd(cmd, args=qmp_args)
327
328 def command(self, cmd, conv_keys=True, **args):
2d853c70
LD
329 '''
330 Invoke a QMP command.
331 On success return the response dict.
332 On failure raise an exception.
333 '''
66613974
DB
334 reply = self.qmp(cmd, conv_keys, **args)
335 if reply is None:
a004e249 336 raise qmp.qmp.QMPError("Monitor is closed")
66613974 337 if "error" in reply:
a004e249 338 raise MonitorResponseError(reply)
66613974
DB
339 return reply["return"]
340
341 def get_qmp_event(self, wait=False):
342 '''Poll for one queued QMP events and return it'''
343 if len(self._events) > 0:
344 return self._events.pop(0)
345 return self._qmp.pull_event(wait=wait)
346
347 def get_qmp_events(self, wait=False):
348 '''Poll for queued QMP events and return a list of dicts'''
349 events = self._qmp.get_events(wait=wait)
350 events.extend(self._events)
351 del self._events[:]
352 self._qmp.clear_events()
353 return events
354
355 def event_wait(self, name, timeout=60.0, match=None):
2d853c70
LD
356 '''
357 Wait for specified timeout on named event in QMP; optionally filter
358 results by match.
359
360 The 'match' is checked to be a recursive subset of the 'event'; skips
361 branch processing on match's value None
362 {"foo": {"bar": 1}} matches {"foo": None}
363 {"foo": {"bar": 1}} does not matches {"foo": {"baz": None}}
364 '''
4c44b4a4
DB
365 def event_match(event, match=None):
366 if match is None:
367 return True
368
369 for key in match:
370 if key in event:
371 if isinstance(event[key], dict):
372 if not event_match(event[key], match[key]):
373 return False
374 elif event[key] != match[key]:
375 return False
376 else:
377 return False
378
379 return True
380
66613974
DB
381 # Search cached events
382 for event in self._events:
383 if (event['event'] == name) and event_match(event, match):
384 self._events.remove(event)
385 return event
386
387 # Poll for new events
388 while True:
389 event = self._qmp.pull_event(wait=timeout)
390 if (event['event'] == name) and event_match(event, match):
391 return event
392 self._events.append(event)
393
394 return None
395
396 def get_log(self):
2d853c70
LD
397 '''
398 After self.shutdown or failed qemu execution, this returns the output
399 of the qemu process.
400 '''
66613974 401 return self._iolog
572a8243
CR
402
403 def add_args(self, *args):
404 '''
405 Adds to the list of extra arguments to be given to the QEMU binary
406 '''
407 self._args.extend(args)
22dea9db
CR
408
409 def set_machine(self, machine_type):
410 '''
411 Sets the machine type
412
413 If set, the machine type will be added to the base arguments
414 of the resulting QEMU command line.
415 '''
416 self._machine = machine_type
417
418 def set_console(self, device_type=None):
419 '''
420 Sets the device type for a console device
421
422 If set, the console device and a backing character device will
423 be added to the base arguments of the resulting QEMU command
424 line.
425
426 This is a convenience method that will either use the provided
427 device type, of if not given, it will used the device type set
428 on CONSOLE_DEV_TYPES.
429
430 The actual setting of command line arguments will be be done at
431 machine launch time, as it depends on the temporary directory
432 to be created.
433
434 @param device_type: the device type, such as "isa-serial"
435 @raises: QEMUMachineAddDeviceError if the device type is not given
436 and can not be determined.
437 '''
438 if device_type is None:
439 if self._machine is None:
440 raise QEMUMachineAddDeviceError("Can not add a console device:"
441 " QEMU instance without a "
442 "defined machine type")
443 for regex, device in CONSOLE_DEV_TYPES.items():
444 if re.match(regex, self._machine):
445 device_type = device
446 break
447 if device_type is None:
448 raise QEMUMachineAddDeviceError("Can not add a console device:"
449 " no matching console device "
450 "type definition")
451 self._console_device_type = device_type
452
453 @property
454 def console_socket(self):
455 """
456 Returns a socket connected to the console
457 """
458 if self._console_socket is None:
459 self._console_socket = socket.socket(socket.AF_UNIX,
460 socket.SOCK_STREAM)
461 self._console_socket.connect(self._console_address)
462 return self._console_socket