blob: 79fd724f7d4f983628b88c6371c1a758c3fb2324 [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
Brad Bishop6e60e8b2018-02-01 10:27:11 -05002# Copyright (C) 2016 Intel Corporation
Brad Bishopc342db32019-05-15 21:57:59 -04003#
4# SPDX-License-Identifier: MIT
5#
Brad Bishop6e60e8b2018-02-01 10:27:11 -05006
7import os
8import sys
9import signal
10import time
Andrew Geissler09036742021-06-25 14:25:14 -050011import glob
12import subprocess
Andrew Geissler82c905d2020-04-13 13:39:40 -050013from collections import defaultdict
Brad Bishop6e60e8b2018-02-01 10:27:11 -050014
15from .ssh import OESSHTarget
16from oeqa.utils.qemurunner import QemuRunner
Andrew Geisslerc926e172021-05-07 16:11:35 -050017from oeqa.utils.dump import MonitorDumper
Andrew Geisslerc3d88e42020-10-02 09:45:00 -050018from oeqa.utils.dump import TargetDumper
Brad Bishop6e60e8b2018-02-01 10:27:11 -050019
Brad Bishop316dfdd2018-06-25 12:45:53 -040020supported_fstypes = ['ext3', 'ext4', 'cpio.gz', 'wic']
Brad Bishop6e60e8b2018-02-01 10:27:11 -050021
22class OEQemuTarget(OESSHTarget):
Brad Bishop977dc1a2019-02-06 16:01:43 -050023 def __init__(self, logger, server_ip, timeout=300, user='root',
Brad Bishop19323692019-04-05 15:28:33 -040024 port=None, machine='', rootfs='', kernel='', kvm=False, slirp=False,
Brad Bishop6e60e8b2018-02-01 10:27:11 -050025 dump_dir='', dump_host_cmds='', display='', bootlog='',
Andrew Geissler82c905d2020-04-13 13:39:40 -050026 tmpdir='', dir_image='', boottime=60, serial_ports=2,
Andrew Geissler3b8a17c2021-04-15 15:55:55 -050027 boot_patterns = defaultdict(str), ovmf=False, tmpfsdir=None, **kwargs):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050028
Brad Bishop977dc1a2019-02-06 16:01:43 -050029 super(OEQemuTarget, self).__init__(logger, None, server_ip, timeout,
Brad Bishop6e60e8b2018-02-01 10:27:11 -050030 user, port)
31
Brad Bishop6e60e8b2018-02-01 10:27:11 -050032 self.server_ip = server_ip
Andrew Geissler82c905d2020-04-13 13:39:40 -050033 self.server_port = 0
Brad Bishop6e60e8b2018-02-01 10:27:11 -050034 self.machine = machine
35 self.rootfs = rootfs
36 self.kernel = kernel
37 self.kvm = kvm
Andrew Geissler82c905d2020-04-13 13:39:40 -050038 self.ovmf = ovmf
Brad Bishop19323692019-04-05 15:28:33 -040039 self.use_slirp = slirp
Andrew Geissler82c905d2020-04-13 13:39:40 -050040 self.boot_patterns = boot_patterns
Andrew Geissler09036742021-06-25 14:25:14 -050041 self.dump_dir = dump_dir
42 self.bootlog = bootlog
Brad Bishop6e60e8b2018-02-01 10:27:11 -050043
44 self.runner = QemuRunner(machine=machine, rootfs=rootfs, tmpdir=tmpdir,
45 deploy_dir_image=dir_image, display=display,
46 logfile=bootlog, boottime=boottime,
Brad Bishop19323692019-04-05 15:28:33 -040047 use_kvm=kvm, use_slirp=slirp, dump_dir=dump_dir,
Andrew Geissler82c905d2020-04-13 13:39:40 -050048 dump_host_cmds=dump_host_cmds, logger=logger,
49 serial_ports=serial_ports, boot_patterns = boot_patterns,
Andrew Geissler3b8a17c2021-04-15 15:55:55 -050050 use_ovmf=ovmf, tmpfsdir=tmpfsdir)
Andrew Geisslerc926e172021-05-07 16:11:35 -050051 dump_monitor_cmds = kwargs.get("testimage_dump_monitor")
52 self.monitor_dumper = MonitorDumper(dump_monitor_cmds, dump_dir, self.runner)
53 if self.monitor_dumper:
54 self.monitor_dumper.create_dir("qmp")
55
Andrew Geisslerc3d88e42020-10-02 09:45:00 -050056 dump_target_cmds = kwargs.get("testimage_dump_target")
57 self.target_dumper = TargetDumper(dump_target_cmds, dump_dir, self.runner)
58 self.target_dumper.create_dir("qemu")
Brad Bishop6e60e8b2018-02-01 10:27:11 -050059
Brad Bishop19323692019-04-05 15:28:33 -040060 def start(self, params=None, extra_bootparams=None, runqemuparams=''):
61 if self.use_slirp and not self.server_ip:
62 self.logger.error("Could not start qemu with slirp without server ip - provide 'TEST_SERVER_IP'")
63 raise RuntimeError("FAILED to start qemu - check the task log and the boot log")
64 if self.runner.start(params, extra_bootparams=extra_bootparams, runqemuparams=runqemuparams):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050065 self.ip = self.runner.ip
Brad Bishop19323692019-04-05 15:28:33 -040066 if self.use_slirp:
67 target_ip_port = self.runner.ip.split(':')
68 if len(target_ip_port) == 2:
69 target_ip = target_ip_port[0]
70 port = target_ip_port[1]
71 self.ip = target_ip
72 self.ssh = self.ssh + ['-p', port]
73 self.scp = self.scp + ['-P', port]
74 else:
75 self.logger.error("Could not get host machine port to connect qemu with slirp, ssh will not be "
76 "able to connect to qemu with slirp")
77 if self.runner.server_ip:
78 self.server_ip = self.runner.server_ip
Brad Bishop6e60e8b2018-02-01 10:27:11 -050079 else:
80 self.stop()
Andrew Geissler09036742021-06-25 14:25:14 -050081 # Display the first 20 lines of top and
82 # last 20 lines of the bootlog when the
83 # target is not being booted up.
84 topfile = glob.glob(self.dump_dir + "/*_qemu/host_*_top")
85 msg = "\n\n===== start: snippet =====\n\n"
86 for f in topfile:
87 msg += "file: %s\n\n" % f
88 with open(f) as tf:
89 for x in range(20):
90 msg += next(tf)
91 msg += "\n\n===== end: snippet =====\n\n"
92 blcmd = ["tail", "-20", self.bootlog]
93 msg += "===== start: snippet =====\n\n"
94 try:
95 out = subprocess.check_output(blcmd, stderr=subprocess.STDOUT, timeout=1).decode('utf-8')
96 msg += "file: %s\n\n" % self.bootlog
97 msg += out
98 except (subprocess.CalledProcessError, subprocess.TimeoutExpired, FileNotFoundError) as err:
99 msg += "Error running command: %s\n%s\n" % (blcmd, err)
100 msg += "\n\n===== end: snippet =====\n"
101
102 raise RuntimeError("FAILED to start qemu - check the task log and the boot log %s" % (msg))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500103
104 def stop(self):
105 self.runner.stop()