blob: bdc6e0a8f849ea58417556514fa1d3da7457a32e [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# Copyright (C) 2013 Intel Corporation
2#
3# Released under the MIT license (see COPYING.MIT)
4
5# This module provides a class for starting qemu images using runqemu.
6# It's used by testimage.bbclass.
7
8import subprocess
9import os
10import time
11import signal
12import re
13import socket
14import select
15import errno
Patrick Williamsf1e5d692016-03-30 15:21:19 -050016import string
Patrick Williamsc124f4f2015-09-15 14:41:29 -050017import threading
Patrick Williamsf1e5d692016-03-30 15:21:19 -050018import codecs
Patrick Williamsc124f4f2015-09-15 14:41:29 -050019from oeqa.utils.dump import HostDumper
20
21import logging
22logger = logging.getLogger("BitBake.QemuRunner")
23
Patrick Williamsf1e5d692016-03-30 15:21:19 -050024# Get Unicode non printable control chars
25control_range = range(0,32)+range(127,160)
26control_chars = [unichr(x) for x in control_range
27 if unichr(x) not in string.printable]
28re_control_char = re.compile('[%s]' % re.escape("".join(control_chars)))
29
Patrick Williamsc124f4f2015-09-15 14:41:29 -050030class QemuRunner:
31
32 def __init__(self, machine, rootfs, display, tmpdir, deploy_dir_image, logfile, boottime, dump_dir, dump_host_cmds):
33
34 # Popen object for runqemu
35 self.runqemu = None
36 # pid of the qemu process that runqemu will start
37 self.qemupid = None
38 # target ip - from the command line
39 self.ip = None
40 # host ip - where qemu is running
41 self.server_ip = None
42
43 self.machine = machine
44 self.rootfs = rootfs
45 self.display = display
46 self.tmpdir = tmpdir
47 self.deploy_dir_image = deploy_dir_image
48 self.logfile = logfile
49 self.boottime = boottime
50 self.logged = False
51 self.thread = None
52
53 self.runqemutime = 60
54 self.host_dumper = HostDumper(dump_host_cmds, dump_dir)
55
56 def create_socket(self):
57 try:
58 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
59 sock.setblocking(0)
60 sock.bind(("127.0.0.1",0))
61 sock.listen(2)
62 port = sock.getsockname()[1]
63 logger.info("Created listening socket for qemu serial console on: 127.0.0.1:%s" % port)
64 return (sock, port)
65
66 except socket.error:
67 sock.close()
68 raise
69
70 def log(self, msg):
71 if self.logfile:
Patrick Williamsf1e5d692016-03-30 15:21:19 -050072 # It is needed to sanitize the data received from qemu
73 # because is possible to have control characters
74 msg = re_control_char.sub('', unicode(msg, 'utf-8'))
75 with codecs.open(self.logfile, "a", encoding="utf-8") as f:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050076 f.write("%s" % msg)
77
78 def getOutput(self, o):
79 import fcntl
80 fl = fcntl.fcntl(o, fcntl.F_GETFL)
81 fcntl.fcntl(o, fcntl.F_SETFL, fl | os.O_NONBLOCK)
82 return os.read(o.fileno(), 1000000)
83
84
85 def handleSIGCHLD(self, signum, frame):
86 if self.runqemu and self.runqemu.poll():
87 if self.runqemu.returncode:
88 logger.info('runqemu exited with code %d' % self.runqemu.returncode)
89 logger.info("Output from runqemu:\n%s" % self.getOutput(self.runqemu.stdout))
90 self.stop()
91 self._dump_host()
92 raise SystemExit
93
94 def start(self, qemuparams = None):
95 if self.display:
96 os.environ["DISPLAY"] = self.display
97 else:
98 logger.error("To start qemu I need a X desktop, please set DISPLAY correctly (e.g. DISPLAY=:1)")
99 return False
100 if not os.path.exists(self.rootfs):
101 logger.error("Invalid rootfs %s" % self.rootfs)
102 return False
103 if not os.path.exists(self.tmpdir):
104 logger.error("Invalid TMPDIR path %s" % self.tmpdir)
105 return False
106 else:
107 os.environ["OE_TMPDIR"] = self.tmpdir
108 if not os.path.exists(self.deploy_dir_image):
109 logger.error("Invalid DEPLOY_DIR_IMAGE path %s" % self.deploy_dir_image)
110 return False
111 else:
112 os.environ["DEPLOY_DIR_IMAGE"] = self.deploy_dir_image
113
114 try:
115 threadsock, threadport = self.create_socket()
116 self.server_socket, self.serverport = self.create_socket()
117 except socket.error, msg:
118 logger.error("Failed to create listening socket: %s" % msg[1])
119 return False
120
121 # Set this flag so that Qemu doesn't do any grabs as SDL grabs interact
122 # badly with screensavers.
123 os.environ["QEMU_DONT_GRAB"] = "1"
124 self.qemuparams = 'bootparams="console=tty1 console=ttyS0,115200n8" qemuparams="-serial tcp:127.0.0.1:{}"'.format(threadport)
125 if qemuparams:
126 self.qemuparams = self.qemuparams[:-1] + " " + qemuparams + " " + '\"'
127
128 self.origchldhandler = signal.getsignal(signal.SIGCHLD)
129 signal.signal(signal.SIGCHLD, self.handleSIGCHLD)
130
131 launch_cmd = 'runqemu tcpserial=%s %s %s %s' % (self.serverport, self.machine, self.rootfs, self.qemuparams)
132 # FIXME: We pass in stdin=subprocess.PIPE here to work around stty
133 # blocking at the end of the runqemu script when using this within
134 # oe-selftest (this makes stty error out immediately). There ought
135 # to be a proper fix but this will suffice for now.
136 self.runqemu = subprocess.Popen(launch_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE, preexec_fn=os.setpgrp)
137 output = self.runqemu.stdout
138
139 #
140 # We need the preexec_fn above so that all runqemu processes can easily be killed
141 # (by killing their process group). This presents a problem if this controlling
142 # process itself is killed however since those processes don't notice the death
143 # of the parent and merrily continue on.
144 #
145 # Rather than hack runqemu to deal with this, we add something here instead.
146 # Basically we fork off another process which holds an open pipe to the parent
147 # and also is setpgrp. If/when the pipe sees EOF from the parent dieing, it kills
148 # the process group. This is like pctrl's PDEATHSIG but for a process group
149 # rather than a single process.
150 #
151 r, w = os.pipe()
152 self.monitorpid = os.fork()
153 if self.monitorpid:
154 os.close(r)
155 self.monitorpipe = os.fdopen(w, "w")
156 else:
157 # child process
158 os.setpgrp()
159 os.close(w)
160 r = os.fdopen(r)
161 x = r.read()
162 os.killpg(os.getpgid(self.runqemu.pid), signal.SIGTERM)
163 sys.exit(0)
164
165 logger.info("runqemu started, pid is %s" % self.runqemu.pid)
166 logger.info("waiting at most %s seconds for qemu pid" % self.runqemutime)
167 endtime = time.time() + self.runqemutime
168 while not self.is_alive() and time.time() < endtime:
169 if self.runqemu.poll():
170 if self.runqemu.returncode:
171 # No point waiting any longer
172 logger.info('runqemu exited with code %d' % self.runqemu.returncode)
173 self._dump_host()
174 self.stop()
175 logger.info("Output from runqemu:\n%s" % self.getOutput(output))
176 return False
177 time.sleep(1)
178
179 if self.is_alive():
180 logger.info("qemu started - qemu procces pid is %s" % self.qemupid)
181 cmdline = ''
182 with open('/proc/%s/cmdline' % self.qemupid) as p:
183 cmdline = p.read()
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500184 # It is needed to sanitize the data received
185 # because is possible to have control characters
186 cmdline = re_control_char.sub('', cmdline)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500187 try:
188 ips = re.findall("((?:[0-9]{1,3}\.){3}[0-9]{1,3})", cmdline.split("ip=")[1])
189 if not ips or len(ips) != 3:
190 raise ValueError
191 else:
192 self.ip = ips[0]
193 self.server_ip = ips[1]
194 except IndexError, ValueError:
195 logger.info("Couldn't get ip from qemu process arguments! Here is the qemu command line used:\n%s\nand output from runqemu:\n%s" % (cmdline, self.getOutput(output)))
196 self._dump_host()
197 self.stop()
198 return False
199 logger.info("qemu cmdline used:\n{}".format(cmdline))
200 logger.info("Target IP: %s" % self.ip)
201 logger.info("Server IP: %s" % self.server_ip)
202
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500203 self.thread = LoggingThread(self.log, threadsock, logger)
204 self.thread.start()
205 if not self.thread.connection_established.wait(self.boottime):
206 logger.error("Didn't receive a console connection from qemu. "
207 "Here is the qemu command line used:\n%s\nand "
208 "output from runqemu:\n%s" % (cmdline,
209 self.getOutput(output)))
210 self.stop_thread()
211 return False
212
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500213 logger.info("Output from runqemu:\n%s", self.getOutput(output))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500214 logger.info("Waiting at most %d seconds for login banner" % self.boottime)
215 endtime = time.time() + self.boottime
216 socklist = [self.server_socket]
217 reachedlogin = False
218 stopread = False
219 qemusock = None
220 bootlog = ''
221 while time.time() < endtime and not stopread:
222 sread, swrite, serror = select.select(socklist, [], [], 5)
223 for sock in sread:
224 if sock is self.server_socket:
225 qemusock, addr = self.server_socket.accept()
226 qemusock.setblocking(0)
227 socklist.append(qemusock)
228 socklist.remove(self.server_socket)
229 logger.info("Connection from %s:%s" % addr)
230 else:
231 data = sock.recv(1024)
232 if data:
233 bootlog += data
234 if re.search(".* login:", bootlog):
235 self.server_socket = qemusock
236 stopread = True
237 reachedlogin = True
238 logger.info("Reached login banner")
239 else:
240 socklist.remove(sock)
241 sock.close()
242 stopread = True
243
244 if not reachedlogin:
245 logger.info("Target didn't reached login boot in %d seconds" % self.boottime)
246 lines = "\n".join(bootlog.splitlines()[-25:])
247 logger.info("Last 25 lines of text:\n%s" % lines)
248 logger.info("Check full boot log: %s" % self.logfile)
249 self._dump_host()
250 self.stop()
251 return False
252
253 # If we are not able to login the tests can continue
254 try:
255 (status, output) = self.run_serial("root\n", raw=True)
256 if re.search("root@[a-zA-Z0-9\-]+:~#", output):
257 self.logged = True
258 logger.info("Logged as root in serial console")
259 else:
260 logger.info("Couldn't login into serial console"
261 " as root using blank password")
262 except:
263 logger.info("Serial console failed while trying to login")
264
265 else:
266 logger.info("Qemu pid didn't appeared in %s seconds" % self.runqemutime)
267 self._dump_host()
268 self.stop()
269 logger.info("Output from runqemu:\n%s" % self.getOutput(output))
270 return False
271
272 return self.is_alive()
273
274 def stop(self):
275 self.stop_thread()
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500276 if hasattr(self, "origchldhandler"):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500277 signal.signal(signal.SIGCHLD, self.origchldhandler)
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500278 if self.runqemu:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500279 os.kill(self.monitorpid, signal.SIGKILL)
280 logger.info("Sending SIGTERM to runqemu")
281 try:
282 os.killpg(os.getpgid(self.runqemu.pid), signal.SIGTERM)
283 except OSError as e:
284 if e.errno != errno.ESRCH:
285 raise
286 endtime = time.time() + self.runqemutime
287 while self.runqemu.poll() is None and time.time() < endtime:
288 time.sleep(1)
289 if self.runqemu.poll() is None:
290 logger.info("Sending SIGKILL to runqemu")
291 os.killpg(os.getpgid(self.runqemu.pid), signal.SIGKILL)
292 self.runqemu = None
293 if hasattr(self, 'server_socket') and self.server_socket:
294 self.server_socket.close()
295 self.server_socket = None
296 self.qemupid = None
297 self.ip = None
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500298
299 def stop_thread(self):
300 if self.thread and self.thread.is_alive():
301 self.thread.stop()
302 self.thread.join()
303
304 def restart(self, qemuparams = None):
305 logger.info("Restarting qemu process")
306 if self.runqemu.poll() is None:
307 self.stop()
308 if self.start(qemuparams):
309 return True
310 return False
311
312 def is_alive(self):
313 if not self.runqemu:
314 return False
315 qemu_child = self.find_child(str(self.runqemu.pid))
316 if qemu_child:
317 self.qemupid = qemu_child[0]
318 if os.path.exists("/proc/" + str(self.qemupid)):
319 return True
320 return False
321
322 def find_child(self,parent_pid):
323 #
324 # Walk the process tree from the process specified looking for a qemu-system. Return its [pid'cmd]
325 #
326 ps = subprocess.Popen(['ps', 'axww', '-o', 'pid,ppid,command'], stdout=subprocess.PIPE).communicate()[0]
327 processes = ps.split('\n')
328 nfields = len(processes[0].split()) - 1
329 pids = {}
330 commands = {}
331 for row in processes[1:]:
332 data = row.split(None, nfields)
333 if len(data) != 3:
334 continue
335 if data[1] not in pids:
336 pids[data[1]] = []
337
338 pids[data[1]].append(data[0])
339 commands[data[0]] = data[2]
340
341 if parent_pid not in pids:
342 return []
343
344 parents = []
345 newparents = pids[parent_pid]
346 while newparents:
347 next = []
348 for p in newparents:
349 if p in pids:
350 for n in pids[p]:
351 if n not in parents and n not in next:
352 next.append(n)
353 if p not in parents:
354 parents.append(p)
355 newparents = next
356 #print "Children matching %s:" % str(parents)
357 for p in parents:
358 # Need to be careful here since runqemu-internal runs "ldd qemu-system-xxxx"
359 # Also, old versions of ldd (2.11) run "LD_XXXX qemu-system-xxxx"
360 basecmd = commands[p].split()[0]
361 basecmd = os.path.basename(basecmd)
362 if "qemu-system" in basecmd and "-serial tcp" in commands[p]:
363 return [int(p),commands[p]]
364
365 def run_serial(self, command, raw=False):
366 # We assume target system have echo to get command status
367 if not raw:
368 command = "%s; echo $?\n" % command
369 self.server_socket.sendall(command)
370 data = ''
371 status = 0
372 stopread = False
373 endtime = time.time()+5
374 while time.time()<endtime and not stopread:
375 sread, _, _ = select.select([self.server_socket],[],[],5)
376 for sock in sread:
377 answer = sock.recv(1024)
378 if answer:
379 data += answer
380 # Search the prompt to stop
381 if re.search("[a-zA-Z0-9]+@[a-zA-Z0-9\-]+:~#", data):
382 stopread = True
383 break
384 else:
385 raise Exception("No data on serial console socket")
386 if data:
387 if raw:
388 status = 1
389 else:
390 # Remove first line (command line) and last line (prompt)
391 data = data[data.find('$?\r\n')+4:data.rfind('\r\n')]
392 index = data.rfind('\r\n')
393 if index == -1:
394 status_cmd = data
395 data = ""
396 else:
397 status_cmd = data[index+2:]
398 data = data[:index]
399 if (status_cmd == "0"):
400 status = 1
401 return (status, str(data))
402
403
404 def _dump_host(self):
405 self.host_dumper.create_dir("qemu")
406 logger.warn("Qemu ended unexpectedly, dump data from host"
407 " is in %s" % self.host_dumper.dump_dir)
408 self.host_dumper.dump_host()
409
410# This class is for reading data from a socket and passing it to logfunc
411# to be processed. It's completely event driven and has a straightforward
412# event loop. The mechanism for stopping the thread is a simple pipe which
413# will wake up the poll and allow for tearing everything down.
414class LoggingThread(threading.Thread):
415 def __init__(self, logfunc, sock, logger):
416 self.connection_established = threading.Event()
417 self.serversock = sock
418 self.logfunc = logfunc
419 self.logger = logger
420 self.readsock = None
421 self.running = False
422
423 self.errorevents = select.POLLERR | select.POLLHUP | select.POLLNVAL
424 self.readevents = select.POLLIN | select.POLLPRI
425
426 threading.Thread.__init__(self, target=self.threadtarget)
427
428 def threadtarget(self):
429 try:
430 self.eventloop()
431 finally:
432 self.teardown()
433
434 def run(self):
435 self.logger.info("Starting logging thread")
436 self.readpipe, self.writepipe = os.pipe()
437 threading.Thread.run(self)
438
439 def stop(self):
440 self.logger.info("Stopping logging thread")
441 if self.running:
442 os.write(self.writepipe, "stop")
443
444 def teardown(self):
445 self.logger.info("Tearing down logging thread")
446 self.close_socket(self.serversock)
447
448 if self.readsock is not None:
449 self.close_socket(self.readsock)
450
451 self.close_ignore_error(self.readpipe)
452 self.close_ignore_error(self.writepipe)
453 self.running = False
454
455 def eventloop(self):
456 poll = select.poll()
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500457 event_read_mask = self.errorevents | self.readevents
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500458 poll.register(self.serversock.fileno())
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500459 poll.register(self.readpipe, event_read_mask)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500460
461 breakout = False
462 self.running = True
463 self.logger.info("Starting thread event loop")
464 while not breakout:
465 events = poll.poll()
466 for event in events:
467 # An error occurred, bail out
468 if event[1] & self.errorevents:
469 raise Exception(self.stringify_event(event[1]))
470
471 # Event to stop the thread
472 if self.readpipe == event[0]:
473 self.logger.info("Stop event received")
474 breakout = True
475 break
476
477 # A connection request was received
478 elif self.serversock.fileno() == event[0]:
479 self.logger.info("Connection request received")
480 self.readsock, _ = self.serversock.accept()
481 self.readsock.setblocking(0)
482 poll.unregister(self.serversock.fileno())
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500483 poll.register(self.readsock.fileno(), event_read_mask)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500484
485 self.logger.info("Setting connection established event")
486 self.connection_established.set()
487
488 # Actual data to be logged
489 elif self.readsock.fileno() == event[0]:
490 data = self.recv(1024)
491 self.logfunc(data)
492
493 # Since the socket is non-blocking make sure to honor EAGAIN
494 # and EWOULDBLOCK.
495 def recv(self, count):
496 try:
497 data = self.readsock.recv(count)
498 except socket.error as e:
499 if e.errno == errno.EAGAIN or e.errno == errno.EWOULDBLOCK:
500 return ''
501 else:
502 raise
503
504 if data is None:
505 raise Exception("No data on read ready socket")
506 elif not data:
507 # This actually means an orderly shutdown
508 # happened. But for this code it counts as an
509 # error since the connection shouldn't go away
510 # until qemu exits.
511 raise Exception("Console connection closed unexpectedly")
512
513 return data
514
515 def stringify_event(self, event):
516 val = ''
517 if select.POLLERR == event:
518 val = 'POLLER'
519 elif select.POLLHUP == event:
520 val = 'POLLHUP'
521 elif select.POLLNVAL == event:
522 val = 'POLLNVAL'
523 return val
524
525 def close_socket(self, sock):
526 sock.shutdown(socket.SHUT_RDWR)
527 sock.close()
528
529 def close_ignore_error(self, fd):
530 try:
531 os.close(fd)
532 except OSError:
533 pass