2015-04-09 08:08:11 +00:00
|
|
|
# Copyright (C) 2015 Intel Corporation
|
|
|
|
#
|
|
|
|
# Released under the MIT license (see COPYING.MIT)
|
|
|
|
|
|
|
|
# This module provides a class for starting qemu images of poky tiny.
|
|
|
|
# It's used by testimage.bbclass.
|
|
|
|
|
|
|
|
import subprocess
|
|
|
|
import os
|
|
|
|
import time
|
|
|
|
import signal
|
|
|
|
import re
|
|
|
|
import socket
|
|
|
|
import select
|
|
|
|
import bb
|
2016-05-20 10:57:44 +00:00
|
|
|
from .qemurunner import QemuRunner
|
2015-04-09 08:08:11 +00:00
|
|
|
|
|
|
|
class QemuTinyRunner(QemuRunner):
|
|
|
|
|
2017-11-08 18:17:17 +00:00
|
|
|
def __init__(self, machine, rootfs, display, tmpdir, deploy_dir_image, logfile, kernel, boottime, logger):
|
2015-04-09 08:08:11 +00:00
|
|
|
|
|
|
|
# Popen object for runqemu
|
|
|
|
self.runqemu = None
|
|
|
|
# pid of the qemu process that runqemu will start
|
|
|
|
self.qemupid = None
|
|
|
|
# target ip - from the command line
|
|
|
|
self.ip = None
|
|
|
|
# host ip - where qemu is running
|
|
|
|
self.server_ip = None
|
|
|
|
|
|
|
|
self.machine = machine
|
|
|
|
self.rootfs = rootfs
|
|
|
|
self.display = display
|
|
|
|
self.tmpdir = tmpdir
|
|
|
|
self.deploy_dir_image = deploy_dir_image
|
|
|
|
self.logfile = logfile
|
|
|
|
self.boottime = boottime
|
|
|
|
|
|
|
|
self.runqemutime = 60
|
|
|
|
self.socketfile = "console.sock"
|
|
|
|
self.server_socket = None
|
|
|
|
self.kernel = kernel
|
2017-11-08 18:17:17 +00:00
|
|
|
self.logger = logger
|
2015-04-09 08:08:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def create_socket(self):
|
|
|
|
tries = 3
|
|
|
|
while tries > 0:
|
|
|
|
try:
|
|
|
|
self.server_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
|
|
self.server_socket.connect(self.socketfile)
|
|
|
|
bb.note("Created listening socket for qemu serial console.")
|
|
|
|
tries = 0
|
2016-05-21 11:29:16 +00:00
|
|
|
except socket.error as msg:
|
2015-04-09 08:08:11 +00:00
|
|
|
self.server_socket.close()
|
|
|
|
bb.fatal("Failed to create listening socket.")
|
|
|
|
tries -= 1
|
|
|
|
|
|
|
|
def log(self, msg):
|
|
|
|
if self.logfile:
|
|
|
|
with open(self.logfile, "a") as f:
|
|
|
|
f.write("%s" % msg)
|
|
|
|
|
2017-03-28 08:02:19 +00:00
|
|
|
def start(self, qemuparams = None, ssh=True, extra_bootparams=None, runqemuparams='', discard_writes=True):
|
2015-04-09 08:08:11 +00:00
|
|
|
|
|
|
|
if self.display:
|
|
|
|
os.environ["DISPLAY"] = self.display
|
|
|
|
else:
|
|
|
|
bb.error("To start qemu I need a X desktop, please set DISPLAY correctly (e.g. DISPLAY=:1)")
|
|
|
|
return False
|
|
|
|
if not os.path.exists(self.rootfs):
|
|
|
|
bb.error("Invalid rootfs %s" % self.rootfs)
|
|
|
|
return False
|
|
|
|
if not os.path.exists(self.tmpdir):
|
|
|
|
bb.error("Invalid TMPDIR path %s" % self.tmpdir)
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
os.environ["OE_TMPDIR"] = self.tmpdir
|
|
|
|
if not os.path.exists(self.deploy_dir_image):
|
|
|
|
bb.error("Invalid DEPLOY_DIR_IMAGE path %s" % self.deploy_dir_image)
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
os.environ["DEPLOY_DIR_IMAGE"] = self.deploy_dir_image
|
|
|
|
|
|
|
|
# Set this flag so that Qemu doesn't do any grabs as SDL grabs interact
|
|
|
|
# badly with screensavers.
|
|
|
|
os.environ["QEMU_DONT_GRAB"] = "1"
|
|
|
|
self.qemuparams = '--append "root=/dev/ram0 console=ttyS0" -nographic -serial unix:%s,server,nowait' % self.socketfile
|
|
|
|
|
|
|
|
launch_cmd = 'qemu-system-i386 -kernel %s -initrd %s %s' % (self.kernel, self.rootfs, self.qemuparams)
|
|
|
|
self.runqemu = subprocess.Popen(launch_cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT,preexec_fn=os.setpgrp)
|
|
|
|
|
|
|
|
bb.note("runqemu started, pid is %s" % self.runqemu.pid)
|
|
|
|
bb.note("waiting at most %s seconds for qemu pid" % self.runqemutime)
|
|
|
|
endtime = time.time() + self.runqemutime
|
|
|
|
while not self.is_alive() and time.time() < endtime:
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
if self.is_alive():
|
|
|
|
bb.note("qemu started - qemu procces pid is %s" % self.qemupid)
|
|
|
|
self.create_socket()
|
|
|
|
else:
|
|
|
|
bb.note("Qemu pid didn't appeared in %s seconds" % self.runqemutime)
|
|
|
|
output = self.runqemu.stdout
|
|
|
|
self.stop()
|
2016-05-20 10:17:05 +00:00
|
|
|
bb.note("Output from runqemu:\n%s" % output.read().decode("utf-8"))
|
2015-04-09 08:08:11 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
return self.is_alive()
|
|
|
|
|
2017-03-24 21:46:16 +00:00
|
|
|
def run_serial(self, command, timeout=5):
|
2015-04-09 08:08:11 +00:00
|
|
|
self.server_socket.sendall(command+'\n')
|
|
|
|
data = ''
|
|
|
|
status = 0
|
|
|
|
stopread = False
|
2017-03-24 21:46:16 +00:00
|
|
|
endtime = time.time()+timeout
|
2015-04-09 08:08:11 +00:00
|
|
|
while time.time()<endtime and not stopread:
|
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],[],[],1)
|
|
|
|
except InterruptedError:
|
|
|
|
continue
|
2015-04-09 08:08:11 +00:00
|
|
|
for sock in sread:
|
|
|
|
answer = sock.recv(1024)
|
|
|
|
if answer:
|
|
|
|
data += answer
|
|
|
|
else:
|
|
|
|
sock.close()
|
|
|
|
stopread = True
|
|
|
|
if not data:
|
|
|
|
status = 1
|
2017-03-24 21:46:16 +00:00
|
|
|
if not stopread:
|
|
|
|
data += "<<< run_serial(): command timed out after %d seconds without output >>>\r\n\r\n" % timeout
|
2015-04-09 08:08:11 +00:00
|
|
|
return (status, str(data))
|
|
|
|
|
|
|
|
def find_child(self,parent_pid):
|
|
|
|
#
|
|
|
|
# Walk the process tree from the process specified looking for a qemu-system. Return its [pid'cmd]
|
|
|
|
#
|
|
|
|
ps = subprocess.Popen(['ps', 'axww', '-o', 'pid,ppid,command'], stdout=subprocess.PIPE).communicate()[0]
|
2016-05-20 10:17:05 +00:00
|
|
|
processes = ps.decode("utf-8").split('\n')
|
2015-04-09 08:08:11 +00:00
|
|
|
nfields = len(processes[0].split()) - 1
|
|
|
|
pids = {}
|
|
|
|
commands = {}
|
|
|
|
for row in processes[1:]:
|
|
|
|
data = row.split(None, nfields)
|
|
|
|
if len(data) != 3:
|
|
|
|
continue
|
|
|
|
if data[1] not in pids:
|
|
|
|
pids[data[1]] = []
|
|
|
|
|
|
|
|
pids[data[1]].append(data[0])
|
|
|
|
commands[data[0]] = data[2]
|
|
|
|
|
|
|
|
if parent_pid not in pids:
|
|
|
|
return []
|
|
|
|
|
|
|
|
parents = []
|
|
|
|
newparents = pids[parent_pid]
|
|
|
|
while newparents:
|
|
|
|
next = []
|
|
|
|
for p in newparents:
|
|
|
|
if p in pids:
|
|
|
|
for n in pids[p]:
|
|
|
|
if n not in parents and n not in next:
|
|
|
|
next.append(n)
|
|
|
|
if p not in parents:
|
|
|
|
parents.append(p)
|
|
|
|
newparents = next
|
2016-05-16 07:29:52 +00:00
|
|
|
#print("Children matching %s:" % str(parents))
|
2015-04-09 08:08:11 +00:00
|
|
|
for p in parents:
|
2016-08-25 14:41:07 +00:00
|
|
|
# Need to be careful here since runqemu runs "ldd qemu-system-xxxx"
|
2015-04-09 08:08:11 +00:00
|
|
|
# Also, old versions of ldd (2.11) run "LD_XXXX qemu-system-xxxx"
|
|
|
|
basecmd = commands[p].split()[0]
|
|
|
|
basecmd = os.path.basename(basecmd)
|
|
|
|
if "qemu-system" in basecmd and "-serial unix" in commands[p]:
|
2016-08-08 20:41:28 +00:00
|
|
|
return [int(p),commands[p]]
|