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) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 155 | |
| 156 | def deploy(self): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 157 | bb.utils.mkdirhier(self.testdir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 158 | |
| 159 | qemuloglink = os.path.join(self.testdir, "qemu_boot_log") |
| 160 | if os.path.islink(qemuloglink): |
| 161 | os.unlink(qemuloglink) |
| 162 | os.symlink(self.qemulog, qemuloglink) |
| 163 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 164 | self.logger.info("rootfs file: %s" % self.rootfs) |
| 165 | self.logger.info("Qemu log file: %s" % self.qemulog) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 166 | super(QemuTarget, self).deploy() |
| 167 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 168 | def start(self, params=None, ssh=True, extra_bootparams='', runqemuparams='', launch_cmd='', discard_writes=True): |
| 169 | if launch_cmd: |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 170 | 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] | 171 | else: |
| 172 | start = self.runner.start(params, get_ip=ssh, extra_bootparams=extra_bootparams, runqemuparams=runqemuparams, discard_writes=discard_writes) |
| 173 | |
| 174 | if start: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 175 | if ssh: |
| 176 | self.ip = self.runner.ip |
| 177 | self.server_ip = self.runner.server_ip |
| 178 | self.connection = SSHControl(ip=self.ip, logfile=self.sshlog) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 179 | else: |
| 180 | self.stop() |
| 181 | if os.path.exists(self.qemulog): |
| 182 | with open(self.qemulog, 'r') as f: |
| 183 | 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] | 184 | 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] | 185 | |
| 186 | def check(self): |
| 187 | return self.runner.is_alive() |
| 188 | |
| 189 | def stop(self): |
Andrew Geissler | 4ed12e1 | 2020-06-05 18:00:41 -0500 | [diff] [blame] | 190 | try: |
| 191 | self.runner.stop() |
| 192 | except: |
| 193 | pass |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 194 | self.logger.removeHandler(self.loggerhandler) |
Andrew Geissler | 475cb72 | 2020-07-10 16:00:51 -0500 | [diff] [blame] | 195 | self.loggerhandler.close() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 196 | self.connection = None |
| 197 | self.ip = None |
| 198 | self.server_ip = None |
| 199 | |
| 200 | def restart(self, params=None): |
| 201 | if self.runner.restart(params): |
| 202 | self.ip = self.runner.ip |
| 203 | self.server_ip = self.runner.server_ip |
| 204 | self.connection = SSHControl(ip=self.ip, logfile=self.sshlog) |
| 205 | else: |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 206 | 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] | 207 | |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 208 | def run_serial(self, command, timeout=60): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 209 | return self.runner.run_serial(command, timeout=timeout) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 210 | |
| 211 | |
| 212 | class SimpleRemoteTarget(BaseTarget): |
| 213 | |
| 214 | def __init__(self, d): |
| 215 | super(SimpleRemoteTarget, self).__init__(d) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 216 | 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] | 217 | self.ip = addr.split(":")[0] |
| 218 | try: |
| 219 | self.port = addr.split(":")[1] |
| 220 | except IndexError: |
| 221 | self.port = None |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 222 | self.logger.info("Target IP: %s" % self.ip) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 223 | self.server_ip = d.getVar("TEST_SERVER_IP") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 224 | if not self.server_ip: |
| 225 | try: |
| 226 | self.server_ip = subprocess.check_output(['ip', 'route', 'get', self.ip ]).split("\n")[0].split()[-1] |
| 227 | except Exception as e: |
| 228 | 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] | 229 | self.logger.info("Server IP: %s" % self.server_ip) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 230 | |
| 231 | def deploy(self): |
| 232 | super(SimpleRemoteTarget, self).deploy() |
| 233 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 234 | def start(self, params=None, ssh=True, extra_bootparams=None): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 235 | if ssh: |
| 236 | self.connection = SSHControl(self.ip, logfile=self.sshlog, port=self.port) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 237 | |
| 238 | def stop(self): |
| 239 | self.connection = None |
| 240 | self.ip = None |
| 241 | self.server_ip = None |