Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 2 | # Copyright (C) 2013 Intel Corporation |
| 3 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 4 | # SPDX-License-Identifier: MIT |
| 5 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 6 | |
| 7 | # This module is used by testimage.bbclass for setting up and controlling a target machine. |
| 8 | |
| 9 | import os |
| 10 | import shutil |
| 11 | import subprocess |
| 12 | import bb |
| 13 | import traceback |
| 14 | import sys |
| 15 | import logging |
| 16 | from oeqa.utils.sshcontrol import SSHControl |
| 17 | from oeqa.utils.qemurunner import QemuRunner |
| 18 | from oeqa.utils.qemutinyrunner import QemuTinyRunner |
| 19 | from oeqa.utils.dump import TargetDumper |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 20 | from oeqa.utils.dump import MonitorDumper |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 21 | from oeqa.controllers.testtargetloader import TestTargetLoader |
| 22 | from abc import ABCMeta, abstractmethod |
| 23 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 24 | class BaseTarget(object, metaclass=ABCMeta): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 25 | |
| 26 | supported_image_fstypes = [] |
| 27 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 28 | def __init__(self, d, logger): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 29 | self.connection = None |
| 30 | self.ip = None |
| 31 | self.server_ip = None |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 32 | self.datetime = d.getVar('DATETIME') |
| 33 | self.testdir = d.getVar("TEST_LOG_DIR") |
| 34 | self.pn = d.getVar("PN") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 35 | self.logger = logger |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 36 | |
| 37 | @abstractmethod |
| 38 | def deploy(self): |
| 39 | |
| 40 | self.sshlog = os.path.join(self.testdir, "ssh_target_log.%s" % self.datetime) |
| 41 | sshloglink = os.path.join(self.testdir, "ssh_target_log") |
| 42 | if os.path.islink(sshloglink): |
| 43 | os.unlink(sshloglink) |
| 44 | os.symlink(self.sshlog, sshloglink) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 45 | self.logger.info("SSH log file: %s" % self.sshlog) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 46 | |
| 47 | @abstractmethod |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 48 | def start(self, params=None, ssh=True, extra_bootparams=None): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 49 | pass |
| 50 | |
| 51 | @abstractmethod |
| 52 | def stop(self): |
| 53 | pass |
| 54 | |
| 55 | @classmethod |
| 56 | def get_extra_files(self): |
| 57 | return None |
| 58 | |
| 59 | @classmethod |
| 60 | def match_image_fstype(self, d, image_fstypes=None): |
| 61 | if not image_fstypes: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 62 | image_fstypes = d.getVar('IMAGE_FSTYPES').split(' ') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 63 | possible_image_fstypes = [fstype for fstype in self.supported_image_fstypes if fstype in image_fstypes] |
| 64 | if possible_image_fstypes: |
| 65 | return possible_image_fstypes[0] |
| 66 | else: |
| 67 | return None |
| 68 | |
| 69 | def get_image_fstype(self, d): |
| 70 | image_fstype = self.match_image_fstype(d) |
| 71 | if image_fstype: |
| 72 | return image_fstype |
| 73 | else: |
| 74 | bb.fatal("IMAGE_FSTYPES should contain a Target Controller supported image fstype: %s " % ', '.join(map(str, self.supported_image_fstypes))) |
| 75 | |
| 76 | def restart(self, params=None): |
| 77 | self.stop() |
| 78 | self.start(params) |
| 79 | |
| 80 | def run(self, cmd, timeout=None): |
| 81 | return self.connection.run(cmd, timeout) |
| 82 | |
| 83 | def copy_to(self, localpath, remotepath): |
| 84 | return self.connection.copy_to(localpath, remotepath) |
| 85 | |
| 86 | def copy_from(self, remotepath, localpath): |
| 87 | return self.connection.copy_from(remotepath, localpath) |
| 88 | |
| 89 | |
| 90 | |
| 91 | class QemuTarget(BaseTarget): |
| 92 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 93 | supported_image_fstypes = ['ext3', 'ext4', 'cpio.gz', 'wic'] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 94 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 95 | def __init__(self, d, logger, image_fstype=None): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 96 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 97 | import oe.types |
| 98 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 99 | super(QemuTarget, self).__init__(d, logger) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 100 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 101 | self.rootfs = '' |
| 102 | self.kernel = '' |
| 103 | self.image_fstype = '' |
| 104 | |
| 105 | if d.getVar('FIND_ROOTFS') == '1': |
| 106 | self.image_fstype = image_fstype or self.get_image_fstype(d) |
| 107 | self.rootfs = os.path.join(d.getVar("DEPLOY_DIR_IMAGE"), d.getVar("IMAGE_LINK_NAME") + '.' + self.image_fstype) |
| 108 | self.kernel = os.path.join(d.getVar("DEPLOY_DIR_IMAGE"), d.getVar("KERNEL_IMAGETYPE", False) + '-' + d.getVar('MACHINE', False) + '.bin') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 109 | self.qemulog = os.path.join(self.testdir, "qemu_boot_log.%s" % self.datetime) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 110 | dump_target_cmds = d.getVar("testimage_dump_target") |
| 111 | dump_host_cmds = d.getVar("testimage_dump_host") |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 112 | dump_monitor_cmds = d.getVar("testimage_dump_monitor") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 113 | dump_dir = d.getVar("TESTIMAGE_DUMP_DIR") |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 114 | if not dump_dir: |
| 115 | dump_dir = os.path.join(d.getVar('LOG_DIR'), 'runtime-hostdump') |
| 116 | use_kvm = oe.types.qemu_use_kvm(d.getVar('QEMU_USE_KVM'), d.getVar('TARGET_ARCH')) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 117 | |
| 118 | # Log QemuRunner log output to a file |
| 119 | import oe.path |
| 120 | bb.utils.mkdirhier(self.testdir) |
| 121 | self.qemurunnerlog = os.path.join(self.testdir, 'qemurunner_log.%s' % self.datetime) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 122 | self.loggerhandler = logging.FileHandler(self.qemurunnerlog) |
| 123 | self.loggerhandler.setFormatter(logging.Formatter("%(levelname)s: %(message)s")) |
| 124 | self.logger.addHandler(self.loggerhandler) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 125 | oe.path.symlink(os.path.basename(self.qemurunnerlog), os.path.join(self.testdir, 'qemurunner_log'), force=True) |
| 126 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 127 | if d.getVar("DISTRO") == "poky-tiny": |
| 128 | self.runner = QemuTinyRunner(machine=d.getVar("MACHINE"), |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 129 | rootfs=self.rootfs, |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 130 | tmpdir = d.getVar("TMPDIR"), |
| 131 | deploy_dir_image = d.getVar("DEPLOY_DIR_IMAGE"), |
| 132 | display = d.getVar("BB_ORIGENV", False).getVar("DISPLAY"), |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 133 | logfile = self.qemulog, |
| 134 | kernel = self.kernel, |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 135 | boottime = int(d.getVar("TEST_QEMUBOOT_TIMEOUT")), |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 136 | tmpfsdir = d.getVar("RUNQEMU_TMPFS_DIR"), |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 137 | logger = logger) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 138 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 139 | self.runner = QemuRunner(machine=d.getVar("MACHINE"), |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 140 | rootfs=self.rootfs, |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 141 | tmpdir = d.getVar("TMPDIR"), |
| 142 | deploy_dir_image = d.getVar("DEPLOY_DIR_IMAGE"), |
| 143 | display = d.getVar("BB_ORIGENV", False).getVar("DISPLAY"), |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 144 | logfile = self.qemulog, |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 145 | boottime = int(d.getVar("TEST_QEMUBOOT_TIMEOUT")), |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 146 | use_kvm = use_kvm, |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 147 | dump_dir = dump_dir, |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 148 | dump_host_cmds = d.getVar("testimage_dump_host"), |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 149 | logger = logger, |
Andrew Geissler | 3b8a17c | 2021-04-15 15:55:55 -0500 | [diff] [blame] | 150 | tmpfsdir = d.getVar("RUNQEMU_TMPFS_DIR"), |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 151 | serial_ports = len(d.getVar("SERIAL_CONSOLES").split())) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 152 | |
| 153 | self.target_dumper = TargetDumper(dump_target_cmds, dump_dir, self.runner) |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 154 | self.monitor_dumper = MonitorDumper(dump_monitor_cmds, dump_dir, self.runner) |
Andrew Geissler | 5f35090 | 2021-07-23 13:09:54 -0400 | [diff] [blame^] | 155 | if (self.monitor_dumper): |
| 156 | self.monitor_dumper.create_dir("qmp") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 157 | |
| 158 | def deploy(self): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 159 | bb.utils.mkdirhier(self.testdir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 160 | |
| 161 | qemuloglink = os.path.join(self.testdir, "qemu_boot_log") |
| 162 | if os.path.islink(qemuloglink): |
| 163 | os.unlink(qemuloglink) |
| 164 | os.symlink(self.qemulog, qemuloglink) |
| 165 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 166 | self.logger.info("rootfs file: %s" % self.rootfs) |
| 167 | self.logger.info("Qemu log file: %s" % self.qemulog) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 168 | super(QemuTarget, self).deploy() |
| 169 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 170 | def start(self, params=None, ssh=True, extra_bootparams='', runqemuparams='', launch_cmd='', discard_writes=True): |
| 171 | if launch_cmd: |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 172 | start = self.runner.launch(get_ip=ssh, launch_cmd=launch_cmd, qemuparams=params) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 173 | else: |
| 174 | start = self.runner.start(params, get_ip=ssh, extra_bootparams=extra_bootparams, runqemuparams=runqemuparams, discard_writes=discard_writes) |
| 175 | |
| 176 | if start: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 177 | if ssh: |
| 178 | self.ip = self.runner.ip |
| 179 | self.server_ip = self.runner.server_ip |
| 180 | self.connection = SSHControl(ip=self.ip, logfile=self.sshlog) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 181 | else: |
| 182 | self.stop() |
| 183 | if os.path.exists(self.qemulog): |
| 184 | with open(self.qemulog, 'r') as f: |
| 185 | bb.error("Qemu log output from %s:\n%s" % (self.qemulog, f.read())) |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 186 | raise RuntimeError("%s - FAILED to start qemu - check the task log and the boot log" % self.pn) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 187 | |
| 188 | def check(self): |
| 189 | return self.runner.is_alive() |
| 190 | |
| 191 | def stop(self): |
Andrew Geissler | 4ed12e1 | 2020-06-05 18:00:41 -0500 | [diff] [blame] | 192 | try: |
| 193 | self.runner.stop() |
| 194 | except: |
| 195 | pass |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 196 | self.logger.removeHandler(self.loggerhandler) |
Andrew Geissler | 475cb72 | 2020-07-10 16:00:51 -0500 | [diff] [blame] | 197 | self.loggerhandler.close() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 198 | self.connection = None |
| 199 | self.ip = None |
| 200 | self.server_ip = None |
| 201 | |
| 202 | def restart(self, params=None): |
| 203 | if self.runner.restart(params): |
| 204 | self.ip = self.runner.ip |
| 205 | self.server_ip = self.runner.server_ip |
| 206 | self.connection = SSHControl(ip=self.ip, logfile=self.sshlog) |
| 207 | else: |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 208 | raise RuntimError("%s - FAILED to re-start qemu - check the task log and the boot log" % self.pn) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 209 | |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 210 | def run_serial(self, command, timeout=60): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 211 | return self.runner.run_serial(command, timeout=timeout) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 212 | |
| 213 | |
| 214 | class SimpleRemoteTarget(BaseTarget): |
| 215 | |
| 216 | def __init__(self, d): |
| 217 | super(SimpleRemoteTarget, self).__init__(d) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 218 | addr = d.getVar("TEST_TARGET_IP") or bb.fatal('Please set TEST_TARGET_IP with the IP address of the machine you want to run the tests on.') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 219 | self.ip = addr.split(":")[0] |
| 220 | try: |
| 221 | self.port = addr.split(":")[1] |
| 222 | except IndexError: |
| 223 | self.port = None |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 224 | self.logger.info("Target IP: %s" % self.ip) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 225 | self.server_ip = d.getVar("TEST_SERVER_IP") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 226 | if not self.server_ip: |
| 227 | try: |
| 228 | self.server_ip = subprocess.check_output(['ip', 'route', 'get', self.ip ]).split("\n")[0].split()[-1] |
| 229 | except Exception as e: |
| 230 | bb.fatal("Failed to determine the host IP address (alternatively you can set TEST_SERVER_IP with the IP address of this machine): %s" % e) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 231 | self.logger.info("Server IP: %s" % self.server_ip) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 232 | |
| 233 | def deploy(self): |
| 234 | super(SimpleRemoteTarget, self).deploy() |
| 235 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 236 | def start(self, params=None, ssh=True, extra_bootparams=None): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 237 | if ssh: |
| 238 | self.connection = SSHControl(self.ip, logfile=self.sshlog, port=self.port) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 239 | |
| 240 | def stop(self): |
| 241 | self.connection = None |
| 242 | self.ip = None |
| 243 | self.server_ip = None |