2013-07-29 21:45:53 +00:00
|
|
|
# Copyright (C) 2013 Intel Corporation
|
|
|
|
#
|
|
|
|
# Released under the MIT license (see COPYING.MIT)
|
|
|
|
|
|
|
|
# This module provides a class for starting qemu images using runqemu.
|
|
|
|
# It's used by testimage.bbclass.
|
|
|
|
|
2013-06-28 08:27:56 +00:00
|
|
|
import subprocess
|
|
|
|
import os
|
2017-01-13 14:33:54 +00:00
|
|
|
import sys
|
2013-06-28 08:27:56 +00:00
|
|
|
import time
|
|
|
|
import signal
|
|
|
|
import re
|
2013-08-13 14:47:12 +00:00
|
|
|
import socket
|
|
|
|
import select
|
2015-07-27 13:04:00 +00:00
|
|
|
import errno
|
2015-09-28 12:49:47 +00:00
|
|
|
import string
|
2015-08-20 20:01:25 +00:00
|
|
|
import threading
|
2015-10-01 10:40:47 +00:00
|
|
|
import codecs
|
2015-07-22 14:20:48 +00:00
|
|
|
import logging
|
2017-09-07 09:49:19 +00:00
|
|
|
from oeqa.utils.dump import HostDumper
|
2013-06-28 08:27:56 +00:00
|
|
|
|
2015-10-01 10:40:47 +00:00
|
|
|
# Get Unicode non printable control chars
|
2016-05-20 10:53:11 +00:00
|
|
|
control_range = list(range(0,32))+list(range(127,160))
|
2016-05-20 10:57:44 +00:00
|
|
|
control_chars = [chr(x) for x in control_range
|
|
|
|
if chr(x) not in string.printable]
|
2015-10-01 10:40:47 +00:00
|
|
|
re_control_char = re.compile('[%s]' % re.escape("".join(control_chars)))
|
|
|
|
|
2013-06-28 08:27:56 +00:00
|
|
|
class QemuRunner:
|
|
|
|
|
2016-08-16 23:08:11 +00:00
|
|
|
def __init__(self, machine, rootfs, display, tmpdir, deploy_dir_image, logfile, boottime, dump_dir, dump_host_cmds, use_kvm):
|
2013-06-28 08:27:56 +00:00
|
|
|
|
2013-11-26 09:18:22 +00:00
|
|
|
# Popen object for runqemu
|
|
|
|
self.runqemu = None
|
|
|
|
# pid of the qemu process that runqemu will start
|
2013-06-28 08:27:56 +00:00
|
|
|
self.qemupid = None
|
2017-03-17 13:18:33 +00:00
|
|
|
# target ip - from the command line or runqemu output
|
2013-06-28 08:27:56 +00:00
|
|
|
self.ip = None
|
2013-11-26 09:18:22 +00:00
|
|
|
# host ip - where qemu is running
|
|
|
|
self.server_ip = None
|
2017-03-17 13:18:33 +00:00
|
|
|
# target ip netmask
|
|
|
|
self.netmask = None
|
2013-06-28 08:27:56 +00:00
|
|
|
|
2013-11-26 09:18:22 +00:00
|
|
|
self.machine = machine
|
|
|
|
self.rootfs = rootfs
|
2013-06-28 08:27:56 +00:00
|
|
|
self.display = display
|
|
|
|
self.tmpdir = tmpdir
|
2013-09-12 11:04:52 +00:00
|
|
|
self.deploy_dir_image = deploy_dir_image
|
2013-06-28 08:27:56 +00:00
|
|
|
self.logfile = logfile
|
2013-07-10 11:33:32 +00:00
|
|
|
self.boottime = boottime
|
2015-08-11 13:41:04 +00:00
|
|
|
self.logged = False
|
2015-08-20 20:01:25 +00:00
|
|
|
self.thread = None
|
2016-08-16 23:08:11 +00:00
|
|
|
self.use_kvm = use_kvm
|
2017-09-22 23:05:41 +00:00
|
|
|
self.msg = ''
|
2013-11-26 09:18:22 +00:00
|
|
|
|
2017-08-29 15:51:27 +00:00
|
|
|
self.runqemutime = 120
|
2017-09-07 00:33:58 +00:00
|
|
|
self.qemu_pidfile = 'pidfile_'+str(os.getpid())
|
2015-09-01 07:36:29 +00:00
|
|
|
self.host_dumper = HostDumper(dump_host_cmds, dump_dir)
|
2013-08-13 14:47:12 +00:00
|
|
|
|
2017-09-07 09:49:19 +00:00
|
|
|
self.logger = logging.getLogger("BitBake.QemuRunner")
|
|
|
|
self.logger.addHandler(logging.StreamHandler(sys.stdout))
|
|
|
|
|
2013-08-22 11:47:21 +00:00
|
|
|
def create_socket(self):
|
2013-08-13 14:47:12 +00:00
|
|
|
try:
|
2015-08-20 17:55:17 +00:00
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
sock.setblocking(0)
|
|
|
|
sock.bind(("127.0.0.1",0))
|
|
|
|
sock.listen(2)
|
|
|
|
port = sock.getsockname()[1]
|
2017-09-07 09:49:19 +00:00
|
|
|
self.logger.info("Created listening socket for qemu serial console on: 127.0.0.1:%s" % port)
|
2015-08-20 17:55:17 +00:00
|
|
|
return (sock, port)
|
2013-08-13 14:47:12 +00:00
|
|
|
|
2015-08-20 17:55:17 +00:00
|
|
|
except socket.error:
|
|
|
|
sock.close()
|
|
|
|
raise
|
2013-08-13 14:47:12 +00:00
|
|
|
|
|
|
|
def log(self, msg):
|
|
|
|
if self.logfile:
|
2015-09-28 12:49:47 +00:00
|
|
|
# It is needed to sanitize the data received from qemu
|
2015-10-01 10:40:47 +00:00
|
|
|
# because is possible to have control characters
|
qemurunner.py: ignore decode errors
qemu output can contain control characters. This cause qemurunner
API to crash when decoding the output to utf-8:
Traceback (most recent call last):
File "/usr/lib64/python3.4/threading.py", line 911, in _bootstrap_inner
self.run()
File "meta/lib/oeqa/utils/qemurunner.py", line 472, in run
threading.Thread.run(self)
File "/usr/lib64/python3.4/threading.py", line 859, in run
self._target(*self._args, **self._kwargs)
File "meta/lib/oeqa/utils/qemurunner.py", line 465, in threadtarget
self.eventloop()
File "meta/lib/oeqa/utils/qemurunner.py", line 526, in eventloop
self.logfunc(data)
File "meta/lib/oeqa/utils/qemurunner.py", line 77, in log
msg = msg.decode("utf-8")
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xda in position 0:
unexpected end of data
Added errors='ignore' to decode call to fix this.
(From OE-Core rev: 4a46dd5190d97fdcb6297a0c1d8c824d425c4c51)
Signed-off-by: Ed Bartosh <ed.bartosh@linux.intel.com>
Signed-off-by: Ross Burton <ross.burton@intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
2017-03-06 15:10:12 +00:00
|
|
|
msg = msg.decode("utf-8", errors='ignore')
|
2016-05-20 10:17:05 +00:00
|
|
|
msg = re_control_char.sub('', msg)
|
2017-09-22 23:05:41 +00:00
|
|
|
self.msg += msg
|
2015-10-01 10:40:47 +00:00
|
|
|
with codecs.open(self.logfile, "a", encoding="utf-8") as f:
|
2013-08-13 14:47:12 +00:00
|
|
|
f.write("%s" % msg)
|
2013-06-28 08:27:56 +00:00
|
|
|
|
2015-09-04 15:59:38 +00:00
|
|
|
def getOutput(self, o):
|
|
|
|
import fcntl
|
|
|
|
fl = fcntl.fcntl(o, fcntl.F_GETFL)
|
|
|
|
fcntl.fcntl(o, fcntl.F_SETFL, fl | os.O_NONBLOCK)
|
2016-05-20 10:17:05 +00:00
|
|
|
return os.read(o.fileno(), 1000000).decode("utf-8")
|
2015-09-04 15:59:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def handleSIGCHLD(self, signum, frame):
|
|
|
|
if self.runqemu and self.runqemu.poll():
|
|
|
|
if self.runqemu.returncode:
|
2017-09-07 09:49:19 +00:00
|
|
|
self.logger.info('runqemu exited with code %d' % self.runqemu.returncode)
|
|
|
|
self.logger.info("Output from runqemu:\n%s" % self.getOutput(self.runqemu.stdout))
|
2015-09-04 15:59:38 +00:00
|
|
|
self.stop()
|
|
|
|
self._dump_host()
|
|
|
|
raise SystemExit
|
|
|
|
|
2017-03-28 08:02:19 +00:00
|
|
|
def start(self, qemuparams = None, get_ip = True, extra_bootparams = None, runqemuparams='', launch_cmd=None, discard_writes=True):
|
2013-06-28 08:27:56 +00:00
|
|
|
if self.display:
|
|
|
|
os.environ["DISPLAY"] = self.display
|
2015-08-27 14:14:21 +00:00
|
|
|
# Set this flag so that Qemu doesn't do any grabs as SDL grabs
|
|
|
|
# interact badly with screensavers.
|
|
|
|
os.environ["QEMU_DONT_GRAB"] = "1"
|
2013-06-28 08:27:56 +00:00
|
|
|
if not os.path.exists(self.rootfs):
|
2017-09-07 09:49:19 +00:00
|
|
|
self.logger.error("Invalid rootfs %s" % self.rootfs)
|
2013-06-28 08:27:56 +00:00
|
|
|
return False
|
|
|
|
if not os.path.exists(self.tmpdir):
|
2017-09-07 09:49:19 +00:00
|
|
|
self.logger.error("Invalid TMPDIR path %s" % self.tmpdir)
|
2013-06-28 08:27:56 +00:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
os.environ["OE_TMPDIR"] = self.tmpdir
|
2013-09-12 11:04:52 +00:00
|
|
|
if not os.path.exists(self.deploy_dir_image):
|
2017-09-07 09:49:19 +00:00
|
|
|
self.logger.error("Invalid DEPLOY_DIR_IMAGE path %s" % self.deploy_dir_image)
|
2013-09-12 11:04:52 +00:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
os.environ["DEPLOY_DIR_IMAGE"] = self.deploy_dir_image
|
2013-06-28 08:27:56 +00:00
|
|
|
|
2017-03-24 08:44:59 +00:00
|
|
|
if not launch_cmd:
|
2017-03-28 08:02:19 +00:00
|
|
|
launch_cmd = 'runqemu %s %s ' % ('snapshot' if discard_writes else '', runqemuparams)
|
2017-03-24 08:44:59 +00:00
|
|
|
if self.use_kvm:
|
2017-09-07 09:49:19 +00:00
|
|
|
self.logger.info('Using kvm for runqemu')
|
2017-03-24 08:44:59 +00:00
|
|
|
launch_cmd += ' kvm'
|
|
|
|
else:
|
2017-09-07 09:49:19 +00:00
|
|
|
self.logger.info('Not using kvm for runqemu')
|
2017-03-24 08:44:59 +00:00
|
|
|
if not self.display:
|
|
|
|
launch_cmd += ' nographic'
|
|
|
|
launch_cmd += ' %s %s' % (self.machine, self.rootfs)
|
|
|
|
|
2017-03-24 08:45:00 +00:00
|
|
|
return self.launch(launch_cmd, qemuparams=qemuparams, get_ip=get_ip, extra_bootparams=extra_bootparams)
|
2017-03-24 08:44:59 +00:00
|
|
|
|
2017-03-24 08:45:00 +00:00
|
|
|
def launch(self, launch_cmd, get_ip = True, qemuparams = None, extra_bootparams = None):
|
2015-08-20 17:55:17 +00:00
|
|
|
try:
|
2015-08-20 20:01:25 +00:00
|
|
|
threadsock, threadport = self.create_socket()
|
2015-08-20 17:55:17 +00:00
|
|
|
self.server_socket, self.serverport = self.create_socket()
|
2016-05-21 11:29:16 +00:00
|
|
|
except socket.error as msg:
|
2017-09-07 09:49:19 +00:00
|
|
|
self.logger.error("Failed to create listening socket: %s" % msg[1])
|
2015-07-22 14:20:48 +00:00
|
|
|
return False
|
|
|
|
|
2016-07-12 21:29:49 +00:00
|
|
|
bootparams = 'console=tty1 console=ttyS0,115200n8 printk.time=1'
|
|
|
|
if extra_bootparams:
|
|
|
|
bootparams = bootparams + ' ' + extra_bootparams
|
|
|
|
|
2017-09-07 00:33:58 +00:00
|
|
|
# Ask QEMU to store the QEMU process PID in file, this way we don't have to parse running processes
|
|
|
|
# and analyze descendents in order to determine it.
|
|
|
|
if os.path.exists(self.qemu_pidfile):
|
|
|
|
os.remove(self.qemu_pidfile)
|
|
|
|
self.qemuparams = 'bootparams="{0}" qemuparams="-serial tcp:127.0.0.1:{1} -pidfile {2}"'.format(bootparams, threadport, self.qemu_pidfile)
|
2013-08-13 14:47:12 +00:00
|
|
|
if qemuparams:
|
|
|
|
self.qemuparams = self.qemuparams[:-1] + " " + qemuparams + " " + '\"'
|
|
|
|
|
2017-03-24 08:44:59 +00:00
|
|
|
launch_cmd += ' tcpserial=%s %s' % (self.serverport, self.qemuparams)
|
|
|
|
|
2015-09-04 15:59:38 +00:00
|
|
|
self.origchldhandler = signal.getsignal(signal.SIGCHLD)
|
|
|
|
signal.signal(signal.SIGCHLD, self.handleSIGCHLD)
|
2015-08-09 07:07:05 +00:00
|
|
|
|
2017-09-07 09:49:19 +00:00
|
|
|
self.logger.info('launchcmd=%s'%(launch_cmd))
|
2016-09-09 16:38:54 +00:00
|
|
|
|
2015-07-27 13:03:59 +00:00
|
|
|
# FIXME: We pass in stdin=subprocess.PIPE here to work around stty
|
|
|
|
# blocking at the end of the runqemu script when using this within
|
|
|
|
# oe-selftest (this makes stty error out immediately). There ought
|
|
|
|
# to be a proper fix but this will suffice for now.
|
|
|
|
self.runqemu = subprocess.Popen(launch_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE, preexec_fn=os.setpgrp)
|
2015-08-09 07:07:05 +00:00
|
|
|
output = self.runqemu.stdout
|
2013-06-28 08:27:56 +00:00
|
|
|
|
2015-09-08 22:35:04 +00:00
|
|
|
#
|
2016-09-09 16:38:54 +00:00
|
|
|
# We need the preexec_fn above so that all runqemu processes can easily be killed
|
2015-09-08 22:35:04 +00:00
|
|
|
# (by killing their process group). This presents a problem if this controlling
|
2016-09-09 16:38:54 +00:00
|
|
|
# process itself is killed however since those processes don't notice the death
|
2015-09-08 22:35:04 +00:00
|
|
|
# of the parent and merrily continue on.
|
|
|
|
#
|
2016-09-09 16:38:54 +00:00
|
|
|
# Rather than hack runqemu to deal with this, we add something here instead.
|
2015-09-08 22:35:04 +00:00
|
|
|
# Basically we fork off another process which holds an open pipe to the parent
|
|
|
|
# and also is setpgrp. If/when the pipe sees EOF from the parent dieing, it kills
|
|
|
|
# the process group. This is like pctrl's PDEATHSIG but for a process group
|
|
|
|
# rather than a single process.
|
|
|
|
#
|
|
|
|
r, w = os.pipe()
|
|
|
|
self.monitorpid = os.fork()
|
|
|
|
if self.monitorpid:
|
|
|
|
os.close(r)
|
|
|
|
self.monitorpipe = os.fdopen(w, "w")
|
|
|
|
else:
|
|
|
|
# child process
|
|
|
|
os.setpgrp()
|
|
|
|
os.close(w)
|
|
|
|
r = os.fdopen(r)
|
|
|
|
x = r.read()
|
|
|
|
os.killpg(os.getpgid(self.runqemu.pid), signal.SIGTERM)
|
|
|
|
sys.exit(0)
|
|
|
|
|
2017-09-07 09:49:19 +00:00
|
|
|
self.logger.info("runqemu started, pid is %s" % self.runqemu.pid)
|
|
|
|
self.logger.info("waiting at most %s seconds for qemu pid" % self.runqemutime)
|
2013-08-13 14:47:12 +00:00
|
|
|
endtime = time.time() + self.runqemutime
|
2013-06-28 08:27:56 +00:00
|
|
|
while not self.is_alive() and time.time() < endtime:
|
2015-07-27 13:04:00 +00:00
|
|
|
if self.runqemu.poll():
|
|
|
|
if self.runqemu.returncode:
|
|
|
|
# No point waiting any longer
|
2017-09-07 09:49:19 +00:00
|
|
|
self.logger.info('runqemu exited with code %d' % self.runqemu.returncode)
|
2015-09-01 07:36:29 +00:00
|
|
|
self._dump_host()
|
2015-07-27 13:04:00 +00:00
|
|
|
self.stop()
|
2017-09-07 09:49:19 +00:00
|
|
|
self.logger.info("Output from runqemu:\n%s" % self.getOutput(output))
|
2015-07-27 13:04:00 +00:00
|
|
|
return False
|
2017-09-07 00:33:58 +00:00
|
|
|
time.sleep(0.5)
|
2013-06-28 08:27:56 +00:00
|
|
|
|
2017-09-07 00:33:58 +00:00
|
|
|
if not self.is_alive():
|
2017-09-07 09:49:19 +00:00
|
|
|
self.logger.error("Qemu pid didn't appear in %s seconds" % self.runqemutime)
|
2017-09-07 00:33:58 +00:00
|
|
|
# Dump all processes to help us to figure out what is going on...
|
|
|
|
ps = subprocess.Popen(['ps', 'axww', '-o', 'pid,ppid,command '], stdout=subprocess.PIPE).communicate()[0]
|
|
|
|
processes = ps.decode("utf-8")
|
|
|
|
self.logger.info("Running processes:\n%s" % processes)
|
2015-09-01 07:36:29 +00:00
|
|
|
self._dump_host()
|
2013-11-26 09:18:22 +00:00
|
|
|
self.stop()
|
2017-08-29 15:51:27 +00:00
|
|
|
op = self.getOutput(output)
|
|
|
|
if op:
|
2017-09-07 09:49:19 +00:00
|
|
|
self.logger.error("Output from runqemu:\n%s" % op)
|
2017-08-29 15:51:27 +00:00
|
|
|
else:
|
2017-09-07 09:49:19 +00:00
|
|
|
self.logger.error("No output from runqemu.\n")
|
2013-06-28 08:27:56 +00:00
|
|
|
return False
|
|
|
|
|
2017-09-07 00:33:58 +00:00
|
|
|
# We are alive: qemu is running
|
|
|
|
out = self.getOutput(output)
|
|
|
|
netconf = False # network configuration is not required by default
|
|
|
|
self.logger.info("qemu started in %s seconds - qemu procces pid is %s" % (time.time() - (endtime - self.runqemutime), self.qemupid))
|
|
|
|
if get_ip:
|
|
|
|
cmdline = ''
|
|
|
|
with open('/proc/%s/cmdline' % self.qemupid) as p:
|
|
|
|
cmdline = p.read()
|
|
|
|
# It is needed to sanitize the data received
|
|
|
|
# because is possible to have control characters
|
|
|
|
cmdline = re_control_char.sub(' ', cmdline)
|
|
|
|
try:
|
|
|
|
ips = re.findall("((?:[0-9]{1,3}\.){3}[0-9]{1,3})", cmdline.split("ip=")[1])
|
|
|
|
self.ip = ips[0]
|
|
|
|
self.server_ip = ips[1]
|
|
|
|
self.logger.info("qemu cmdline used:\n{}".format(cmdline))
|
|
|
|
except (IndexError, ValueError):
|
|
|
|
# Try to get network configuration from runqemu output
|
|
|
|
match = re.match('.*Network configuration: ([0-9.]+)::([0-9.]+):([0-9.]+)$.*',
|
|
|
|
out, re.MULTILINE|re.DOTALL)
|
|
|
|
if match:
|
|
|
|
self.ip, self.server_ip, self.netmask = match.groups()
|
|
|
|
# network configuration is required as we couldn't get it
|
|
|
|
# from the runqemu command line, so qemu doesn't run kernel
|
|
|
|
# and guest networking is not configured
|
|
|
|
netconf = True
|
|
|
|
else:
|
|
|
|
self.logger.error("Couldn't get ip from qemu command line and runqemu output! "
|
|
|
|
"Here is the qemu command line used:\n%s\n"
|
|
|
|
"and output from runqemu:\n%s" % (cmdline, out))
|
|
|
|
self._dump_host()
|
|
|
|
self.stop()
|
|
|
|
return False
|
|
|
|
|
|
|
|
self.logger.info("Target IP: %s" % self.ip)
|
|
|
|
self.logger.info("Server IP: %s" % self.server_ip)
|
|
|
|
|
|
|
|
self.thread = LoggingThread(self.log, threadsock, self.logger)
|
|
|
|
self.thread.start()
|
|
|
|
if not self.thread.connection_established.wait(self.boottime):
|
|
|
|
self.logger.error("Didn't receive a console connection from qemu. "
|
|
|
|
"Here is the qemu command line used:\n%s\nand "
|
|
|
|
"output from runqemu:\n%s" % (cmdline, out))
|
|
|
|
self.stop_thread()
|
|
|
|
return False
|
|
|
|
|
|
|
|
self.logger.info("Output from runqemu:\n%s", out)
|
|
|
|
self.logger.info("Waiting at most %d seconds for login banner" % self.boottime)
|
|
|
|
endtime = time.time() + self.boottime
|
|
|
|
socklist = [self.server_socket]
|
|
|
|
reachedlogin = False
|
|
|
|
stopread = False
|
|
|
|
qemusock = None
|
|
|
|
bootlog = ''
|
|
|
|
data = b''
|
|
|
|
while time.time() < endtime and not stopread:
|
|
|
|
try:
|
|
|
|
sread, swrite, serror = select.select(socklist, [], [], 5)
|
|
|
|
except InterruptedError:
|
|
|
|
continue
|
|
|
|
for sock in sread:
|
|
|
|
if sock is self.server_socket:
|
|
|
|
qemusock, addr = self.server_socket.accept()
|
|
|
|
qemusock.setblocking(0)
|
|
|
|
socklist.append(qemusock)
|
|
|
|
socklist.remove(self.server_socket)
|
|
|
|
self.logger.info("Connection from %s:%s" % addr)
|
|
|
|
else:
|
|
|
|
data = data + sock.recv(1024)
|
|
|
|
if data:
|
|
|
|
try:
|
|
|
|
data = data.decode("utf-8", errors="surrogateescape")
|
|
|
|
bootlog += data
|
|
|
|
data = b''
|
|
|
|
if re.search(".* login:", bootlog):
|
|
|
|
self.server_socket = qemusock
|
|
|
|
stopread = True
|
|
|
|
reachedlogin = True
|
|
|
|
self.logger.info("Reached login banner")
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
socklist.remove(sock)
|
|
|
|
sock.close()
|
|
|
|
stopread = True
|
|
|
|
|
2017-09-22 23:05:41 +00:00
|
|
|
|
2017-09-07 00:33:58 +00:00
|
|
|
if not reachedlogin:
|
|
|
|
self.logger.info("Target didn't reached login boot in %d seconds" % self.boottime)
|
2017-09-22 23:05:41 +00:00
|
|
|
tail = lambda l: "\n".join(l.splitlines()[-25:])
|
|
|
|
# in case bootlog is empty, use tail qemu log store at self.msg
|
|
|
|
lines = tail(bootlog if bootlog else self.msg)
|
2017-09-07 00:33:58 +00:00
|
|
|
self.logger.info("Last 25 lines of text:\n%s" % lines)
|
|
|
|
self.logger.info("Check full boot log: %s" % self.logfile)
|
|
|
|
self._dump_host()
|
|
|
|
self.stop()
|
|
|
|
return False
|
|
|
|
|
|
|
|
# If we are not able to login the tests can continue
|
|
|
|
try:
|
|
|
|
(status, output) = self.run_serial("root\n", raw=True)
|
|
|
|
if re.search("root@[a-zA-Z0-9\-]+:~#", output):
|
|
|
|
self.logged = True
|
|
|
|
self.logger.info("Logged as root in serial console")
|
|
|
|
if netconf:
|
|
|
|
# configure guest networking
|
|
|
|
cmd = "ifconfig eth0 %s netmask %s up\n" % (self.ip, self.netmask)
|
|
|
|
output = self.run_serial(cmd, raw=True)[1]
|
|
|
|
if re.search("root@[a-zA-Z0-9\-]+:~#", output):
|
|
|
|
self.logger.info("configured ip address %s", self.ip)
|
|
|
|
else:
|
|
|
|
self.logger.info("Couldn't configure guest networking")
|
2017-10-20 16:24:04 +00:00
|
|
|
else:
|
|
|
|
self.logger.info("Couldn't login into serial console"
|
|
|
|
" as root using blank password")
|
2017-09-07 00:33:58 +00:00
|
|
|
except:
|
|
|
|
self.logger.info("Serial console failed while trying to login")
|
|
|
|
return True
|
2013-06-28 08:27:56 +00:00
|
|
|
|
2013-11-26 09:18:22 +00:00
|
|
|
def stop(self):
|
2015-08-25 20:58:47 +00:00
|
|
|
self.stop_thread()
|
2017-01-13 14:33:55 +00:00
|
|
|
self.stop_qemu_system()
|
2015-09-26 16:49:25 +00:00
|
|
|
if hasattr(self, "origchldhandler"):
|
2015-09-04 15:59:38 +00:00
|
|
|
signal.signal(signal.SIGCHLD, self.origchldhandler)
|
2015-09-26 16:49:25 +00:00
|
|
|
if self.runqemu:
|
2016-05-20 10:57:44 +00:00
|
|
|
if hasattr(self, "monitorpid"):
|
|
|
|
os.kill(self.monitorpid, signal.SIGKILL)
|
2017-09-07 09:49:19 +00:00
|
|
|
self.logger.info("Sending SIGTERM to runqemu")
|
2016-05-20 10:57:44 +00:00
|
|
|
try:
|
|
|
|
os.killpg(os.getpgid(self.runqemu.pid), signal.SIGTERM)
|
|
|
|
except OSError as e:
|
|
|
|
if e.errno != errno.ESRCH:
|
|
|
|
raise
|
2013-08-22 11:47:21 +00:00
|
|
|
endtime = time.time() + self.runqemutime
|
|
|
|
while self.runqemu.poll() is None and time.time() < endtime:
|
|
|
|
time.sleep(1)
|
|
|
|
if self.runqemu.poll() is None:
|
2017-09-07 09:49:19 +00:00
|
|
|
self.logger.info("Sending SIGKILL to runqemu")
|
2015-09-08 22:36:04 +00:00
|
|
|
os.killpg(os.getpgid(self.runqemu.pid), signal.SIGKILL)
|
2013-08-22 11:47:21 +00:00
|
|
|
self.runqemu = None
|
2015-07-27 21:15:57 +00:00
|
|
|
if hasattr(self, 'server_socket') and self.server_socket:
|
2013-08-13 14:47:12 +00:00
|
|
|
self.server_socket.close()
|
|
|
|
self.server_socket = None
|
2013-06-28 08:27:56 +00:00
|
|
|
self.qemupid = None
|
|
|
|
self.ip = None
|
2017-09-07 00:33:58 +00:00
|
|
|
if os.path.exists(self.qemu_pidfile):
|
|
|
|
os.remove(self.qemu_pidfile)
|
2015-08-20 20:01:25 +00:00
|
|
|
|
2017-01-13 14:33:55 +00:00
|
|
|
def stop_qemu_system(self):
|
|
|
|
if self.qemupid:
|
|
|
|
try:
|
|
|
|
# qemu-system behaves well and a SIGTERM is enough
|
|
|
|
os.kill(self.qemupid, signal.SIGTERM)
|
|
|
|
except ProcessLookupError as e:
|
2017-09-07 09:49:19 +00:00
|
|
|
self.logger.warn('qemu-system ended unexpectedly')
|
2017-01-13 14:33:55 +00:00
|
|
|
|
2015-08-20 20:01:25 +00:00
|
|
|
def stop_thread(self):
|
|
|
|
if self.thread and self.thread.is_alive():
|
|
|
|
self.thread.stop()
|
|
|
|
self.thread.join()
|
2013-06-28 08:27:56 +00:00
|
|
|
|
|
|
|
def restart(self, qemuparams = None):
|
2017-09-07 09:49:19 +00:00
|
|
|
self.logger.info("Restarting qemu process")
|
2013-08-22 11:47:21 +00:00
|
|
|
if self.runqemu.poll() is None:
|
2013-11-26 09:18:22 +00:00
|
|
|
self.stop()
|
|
|
|
if self.start(qemuparams):
|
2013-08-22 11:47:21 +00:00
|
|
|
return True
|
|
|
|
return False
|
2013-06-28 08:27:56 +00:00
|
|
|
|
|
|
|
def is_alive(self):
|
2015-09-04 15:59:38 +00:00
|
|
|
if not self.runqemu:
|
|
|
|
return False
|
2017-09-07 00:33:58 +00:00
|
|
|
if os.path.isfile(self.qemu_pidfile):
|
|
|
|
f = open(self.qemu_pidfile, 'r')
|
|
|
|
qemu_pid = f.read()
|
|
|
|
f.close()
|
2017-09-12 19:23:27 +00:00
|
|
|
qemupid = int(qemu_pid)
|
|
|
|
if os.path.exists("/proc/" + str(qemupid)):
|
|
|
|
self.qemupid = qemupid
|
|
|
|
return True
|
2013-06-28 08:27:56 +00:00
|
|
|
return False
|
|
|
|
|
2017-03-24 21:46:16 +00:00
|
|
|
def run_serial(self, command, raw=False, timeout=5):
|
2015-08-11 13:24:44 +00:00
|
|
|
# We assume target system have echo to get command status
|
2015-08-12 07:02:21 +00:00
|
|
|
if not raw:
|
|
|
|
command = "%s; echo $?\n" % command
|
2015-11-05 09:08:58 +00:00
|
|
|
|
2015-08-11 13:24:44 +00:00
|
|
|
data = ''
|
|
|
|
status = 0
|
2016-05-16 15:40:39 +00:00
|
|
|
self.server_socket.sendall(command.encode('utf-8'))
|
2017-03-24 21:46:16 +00:00
|
|
|
start = time.time()
|
|
|
|
end = start + timeout
|
|
|
|
while True:
|
|
|
|
now = time.time()
|
|
|
|
if now >= end:
|
|
|
|
data += "<<< run_serial(): command timed out after %d seconds without output >>>\r\n\r\n" % timeout
|
|
|
|
break
|
oeqa: tolerate interrupted select() while waiting for qemu
Sometimes, the OEQA utility code aborts with:
...
File ".../meta/lib/oeqa/utils/qemurunner.py", line 131, in start
return self.launch(launch_cmd, qemuparams=qemuparams, get_ip=get_ip, extra_bootparams=extra_bootparams)
File ".../meta/lib/oeqa/utils/qemurunner.py", line 259, in launch
sread, swrite, serror = select.select(socklist, [], [], 5)
InterruptedError: [Errno 4] Interrupted system call
strace shows that this is because of a SIGWINCH:
Connection from 127.0.0.1:52668
select(21, [20], [], [], {5, 0}) = ? ERESTARTNOHAND (To be restarted if no handler)
--- SIGWINCH {si_signo=SIGWINCH, si_code=SI_KERNEL} ---
This is related to some special conditions:
* whether qemu opens a graphical console window (enabled in Poky by default)
* where that window gets opened
* whether the window manager changes the size of the shell window (mine
is a tiling window manager and reorders and resizes windows automatically)
Ignoring the interrupted system calls avoids the problem. Code elsewhere (for example,
run() in ssh.py) already does the same thing.
(From OE-Core rev: 44fe106baf5fd5aebe26c5f28004e2b18d839b7c)
Signed-off-by: Patrick Ohly <patrick.ohly@intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
2017-03-27 13:03:22 +00:00
|
|
|
try:
|
|
|
|
sread, _, _ = select.select([self.server_socket],[],[], end - now)
|
|
|
|
except InterruptedError:
|
|
|
|
continue
|
2015-11-05 09:08:58 +00:00
|
|
|
if sread:
|
|
|
|
answer = self.server_socket.recv(1024)
|
2015-08-12 08:37:28 +00:00
|
|
|
if answer:
|
2016-05-16 15:40:39 +00:00
|
|
|
data += answer.decode('utf-8')
|
2015-08-12 08:37:28 +00:00
|
|
|
# Search the prompt to stop
|
|
|
|
if re.search("[a-zA-Z0-9]+@[a-zA-Z0-9\-]+:~#", data):
|
2017-03-24 21:46:16 +00:00
|
|
|
break
|
2015-08-12 08:37:28 +00:00
|
|
|
else:
|
2015-09-02 13:44:42 +00:00
|
|
|
raise Exception("No data on serial console socket")
|
2015-11-05 09:08:58 +00:00
|
|
|
|
2015-08-11 13:24:44 +00:00
|
|
|
if data:
|
2015-08-12 07:02:21 +00:00
|
|
|
if raw:
|
2015-08-11 13:24:44 +00:00
|
|
|
status = 1
|
2015-08-12 07:02:21 +00:00
|
|
|
else:
|
|
|
|
# Remove first line (command line) and last line (prompt)
|
|
|
|
data = data[data.find('$?\r\n')+4:data.rfind('\r\n')]
|
|
|
|
index = data.rfind('\r\n')
|
|
|
|
if index == -1:
|
|
|
|
status_cmd = data
|
|
|
|
data = ""
|
|
|
|
else:
|
|
|
|
status_cmd = data[index+2:]
|
|
|
|
data = data[:index]
|
|
|
|
if (status_cmd == "0"):
|
|
|
|
status = 1
|
2015-08-11 13:24:44 +00:00
|
|
|
return (status, str(data))
|
2015-08-20 20:01:25 +00:00
|
|
|
|
2015-09-01 07:36:29 +00:00
|
|
|
|
|
|
|
def _dump_host(self):
|
|
|
|
self.host_dumper.create_dir("qemu")
|
2017-09-07 09:49:19 +00:00
|
|
|
self.logger.warn("Qemu ended unexpectedly, dump data from host"
|
2015-09-01 07:36:29 +00:00
|
|
|
" is in %s" % self.host_dumper.dump_dir)
|
|
|
|
self.host_dumper.dump_host()
|
|
|
|
|
2015-08-20 20:01:25 +00:00
|
|
|
# This class is for reading data from a socket and passing it to logfunc
|
|
|
|
# to be processed. It's completely event driven and has a straightforward
|
|
|
|
# event loop. The mechanism for stopping the thread is a simple pipe which
|
|
|
|
# will wake up the poll and allow for tearing everything down.
|
|
|
|
class LoggingThread(threading.Thread):
|
|
|
|
def __init__(self, logfunc, sock, logger):
|
|
|
|
self.connection_established = threading.Event()
|
|
|
|
self.serversock = sock
|
|
|
|
self.logfunc = logfunc
|
|
|
|
self.logger = logger
|
|
|
|
self.readsock = None
|
|
|
|
self.running = False
|
|
|
|
|
|
|
|
self.errorevents = select.POLLERR | select.POLLHUP | select.POLLNVAL
|
|
|
|
self.readevents = select.POLLIN | select.POLLPRI
|
|
|
|
|
|
|
|
threading.Thread.__init__(self, target=self.threadtarget)
|
|
|
|
|
|
|
|
def threadtarget(self):
|
|
|
|
try:
|
|
|
|
self.eventloop()
|
|
|
|
finally:
|
|
|
|
self.teardown()
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
self.logger.info("Starting logging thread")
|
|
|
|
self.readpipe, self.writepipe = os.pipe()
|
|
|
|
threading.Thread.run(self)
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self.logger.info("Stopping logging thread")
|
|
|
|
if self.running:
|
2016-05-20 10:57:44 +00:00
|
|
|
os.write(self.writepipe, bytes("stop", "utf-8"))
|
2015-08-20 20:01:25 +00:00
|
|
|
|
|
|
|
def teardown(self):
|
2015-08-27 09:04:10 +00:00
|
|
|
self.logger.info("Tearing down logging thread")
|
2015-08-20 20:01:25 +00:00
|
|
|
self.close_socket(self.serversock)
|
|
|
|
|
|
|
|
if self.readsock is not None:
|
|
|
|
self.close_socket(self.readsock)
|
|
|
|
|
|
|
|
self.close_ignore_error(self.readpipe)
|
|
|
|
self.close_ignore_error(self.writepipe)
|
|
|
|
self.running = False
|
|
|
|
|
|
|
|
def eventloop(self):
|
|
|
|
poll = select.poll()
|
2015-09-22 16:56:22 +00:00
|
|
|
event_read_mask = self.errorevents | self.readevents
|
2015-08-20 20:01:25 +00:00
|
|
|
poll.register(self.serversock.fileno())
|
2015-09-22 16:56:22 +00:00
|
|
|
poll.register(self.readpipe, event_read_mask)
|
2015-08-20 20:01:25 +00:00
|
|
|
|
|
|
|
breakout = False
|
|
|
|
self.running = True
|
|
|
|
self.logger.info("Starting thread event loop")
|
|
|
|
while not breakout:
|
|
|
|
events = poll.poll()
|
|
|
|
for event in events:
|
|
|
|
# An error occurred, bail out
|
|
|
|
if event[1] & self.errorevents:
|
|
|
|
raise Exception(self.stringify_event(event[1]))
|
|
|
|
|
|
|
|
# Event to stop the thread
|
|
|
|
if self.readpipe == event[0]:
|
|
|
|
self.logger.info("Stop event received")
|
|
|
|
breakout = True
|
|
|
|
break
|
|
|
|
|
|
|
|
# A connection request was received
|
|
|
|
elif self.serversock.fileno() == event[0]:
|
|
|
|
self.logger.info("Connection request received")
|
|
|
|
self.readsock, _ = self.serversock.accept()
|
2015-08-25 20:58:47 +00:00
|
|
|
self.readsock.setblocking(0)
|
2015-08-20 20:01:25 +00:00
|
|
|
poll.unregister(self.serversock.fileno())
|
2015-09-22 16:56:22 +00:00
|
|
|
poll.register(self.readsock.fileno(), event_read_mask)
|
2015-08-20 20:01:25 +00:00
|
|
|
|
|
|
|
self.logger.info("Setting connection established event")
|
|
|
|
self.connection_established.set()
|
|
|
|
|
|
|
|
# Actual data to be logged
|
|
|
|
elif self.readsock.fileno() == event[0]:
|
2015-08-27 09:04:10 +00:00
|
|
|
data = self.recv(1024)
|
2015-08-20 20:01:25 +00:00
|
|
|
self.logfunc(data)
|
|
|
|
|
2015-08-26 07:15:15 +00:00
|
|
|
# Since the socket is non-blocking make sure to honor EAGAIN
|
2015-08-27 09:04:10 +00:00
|
|
|
# and EWOULDBLOCK.
|
|
|
|
def recv(self, count):
|
|
|
|
try:
|
|
|
|
data = self.readsock.recv(count)
|
|
|
|
except socket.error as e:
|
|
|
|
if e.errno == errno.EAGAIN or e.errno == errno.EWOULDBLOCK:
|
|
|
|
return ''
|
|
|
|
else:
|
|
|
|
raise
|
2015-08-26 07:15:15 +00:00
|
|
|
|
2015-08-27 09:04:10 +00:00
|
|
|
if data is None:
|
2015-08-26 07:15:15 +00:00
|
|
|
raise Exception("No data on read ready socket")
|
2015-08-27 09:04:10 +00:00
|
|
|
elif not data:
|
2015-08-26 07:15:15 +00:00
|
|
|
# This actually means an orderly shutdown
|
|
|
|
# happened. But for this code it counts as an
|
|
|
|
# error since the connection shouldn't go away
|
|
|
|
# until qemu exits.
|
|
|
|
raise Exception("Console connection closed unexpectedly")
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
2015-08-20 20:01:25 +00:00
|
|
|
def stringify_event(self, event):
|
|
|
|
val = ''
|
|
|
|
if select.POLLERR == event:
|
|
|
|
val = 'POLLER'
|
|
|
|
elif select.POLLHUP == event:
|
|
|
|
val = 'POLLHUP'
|
|
|
|
elif select.POLLNVAL == event:
|
|
|
|
val = 'POLLNVAL'
|
|
|
|
return val
|
|
|
|
|
|
|
|
def close_socket(self, sock):
|
|
|
|
sock.shutdown(socket.SHUT_RDWR)
|
|
|
|
sock.close()
|
|
|
|
|
|
|
|
def close_ignore_error(self, fd):
|
|
|
|
try:
|
|
|
|
os.close(fd)
|
|
|
|
except OSError:
|
|
|
|
pass
|