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