targetcontrol.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #
  2. # Copyright (C) 2013 Intel Corporation
  3. #
  4. # SPDX-License-Identifier: MIT
  5. #
  6. # This module is used by testimage.bbclass for setting up and controlling a target machine.
  7. import os
  8. import subprocess
  9. import bb
  10. import logging
  11. from oeqa.utils.sshcontrol import SSHControl
  12. from oeqa.utils.qemurunner import QemuRunner
  13. from oeqa.utils.qemutinyrunner import QemuTinyRunner
  14. from oeqa.utils.dump import TargetDumper
  15. from oeqa.utils.dump import MonitorDumper
  16. from abc import ABCMeta, abstractmethod
  17. class BaseTarget(object, metaclass=ABCMeta):
  18. supported_image_fstypes = []
  19. def __init__(self, d, logger):
  20. self.connection = None
  21. self.ip = None
  22. self.server_ip = None
  23. self.datetime = d.getVar('DATETIME')
  24. self.testdir = d.getVar("TEST_LOG_DIR")
  25. self.pn = d.getVar("PN")
  26. self.logger = logger
  27. @abstractmethod
  28. def deploy(self):
  29. self.sshlog = os.path.join(self.testdir, "ssh_target_log.%s" % self.datetime)
  30. sshloglink = os.path.join(self.testdir, "ssh_target_log")
  31. if os.path.islink(sshloglink):
  32. os.unlink(sshloglink)
  33. os.symlink(self.sshlog, sshloglink)
  34. self.logger.info("SSH log file: %s" % self.sshlog)
  35. @abstractmethod
  36. def start(self, params=None, ssh=True, extra_bootparams=None):
  37. pass
  38. @abstractmethod
  39. def stop(self):
  40. pass
  41. @classmethod
  42. def get_extra_files(self):
  43. return None
  44. @classmethod
  45. def match_image_fstype(self, d, image_fstypes=None):
  46. if not image_fstypes:
  47. image_fstypes = d.getVar('IMAGE_FSTYPES').split(' ')
  48. possible_image_fstypes = [fstype for fstype in self.supported_image_fstypes if fstype in image_fstypes]
  49. if possible_image_fstypes:
  50. return possible_image_fstypes[0]
  51. else:
  52. return None
  53. def get_image_fstype(self, d):
  54. image_fstype = self.match_image_fstype(d)
  55. if image_fstype:
  56. return image_fstype
  57. else:
  58. bb.fatal("IMAGE_FSTYPES should contain a Target Controller supported image fstype: %s " % ', '.join(map(str, self.supported_image_fstypes)))
  59. def restart(self, params=None):
  60. self.stop()
  61. self.start(params)
  62. def run(self, cmd, timeout=None):
  63. return self.connection.run(cmd, timeout)
  64. def copy_to(self, localpath, remotepath):
  65. return self.connection.copy_to(localpath, remotepath)
  66. def copy_from(self, remotepath, localpath):
  67. return self.connection.copy_from(remotepath, localpath)
  68. class QemuTarget(BaseTarget):
  69. supported_image_fstypes = ['ext3', 'ext4', 'cpio.gz', 'wic', 'ext3.zst', 'ext4.zst', 'wic.zst']
  70. def __init__(self, d, logger, image_fstype=None, boot_patterns=None):
  71. import oe.types
  72. super(QemuTarget, self).__init__(d, logger)
  73. self.rootfs = ''
  74. self.kernel = ''
  75. self.image_fstype = ''
  76. if d.getVar('FIND_ROOTFS') == '1':
  77. self.image_fstype = image_fstype or self.get_image_fstype(d)
  78. self.rootfs = os.path.join(d.getVar("DEPLOY_DIR_IMAGE"), d.getVar("IMAGE_LINK_NAME") + '.' + self.image_fstype)
  79. self.kernel = os.path.join(d.getVar("DEPLOY_DIR_IMAGE"), d.getVar("KERNEL_IMAGETYPE", False) + '-' + d.getVar('MACHINE', False) + '.bin')
  80. self.qemulog = os.path.join(self.testdir, "qemu_boot_log.%s" % self.datetime)
  81. dump_monitor_cmds = d.getVar("testimage_dump_monitor")
  82. dump_dir = d.getVar("TESTIMAGE_DUMP_DIR")
  83. if not dump_dir:
  84. dump_dir = os.path.join(d.getVar('LOG_DIR'), 'runtime-hostdump')
  85. use_kvm = oe.types.qemu_use_kvm(d.getVar('QEMU_USE_KVM'), d.getVar('TARGET_ARCH'))
  86. # Log QemuRunner log output to a file
  87. import oe.path
  88. bb.utils.mkdirhier(self.testdir)
  89. self.qemurunnerlog = os.path.join(self.testdir, 'qemurunner_log.%s' % self.datetime)
  90. self.loggerhandler = logging.FileHandler(self.qemurunnerlog)
  91. self.loggerhandler.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))
  92. self.logger.addHandler(self.loggerhandler)
  93. oe.path.symlink(os.path.basename(self.qemurunnerlog), os.path.join(self.testdir, 'qemurunner_log'), force=True)
  94. if d.getVar("DISTRO") == "poky-tiny":
  95. self.runner = QemuTinyRunner(machine=d.getVar("MACHINE"),
  96. rootfs=self.rootfs,
  97. tmpdir = d.getVar("TMPDIR"),
  98. deploy_dir_image = d.getVar("DEPLOY_DIR_IMAGE"),
  99. display = d.getVar("BB_ORIGENV", False).getVar("DISPLAY"),
  100. logfile = self.qemulog,
  101. kernel = self.kernel,
  102. boottime = int(d.getVar("TEST_QEMUBOOT_TIMEOUT")),
  103. tmpfsdir = d.getVar("RUNQEMU_TMPFS_DIR"),
  104. logger = logger)
  105. else:
  106. self.runner = QemuRunner(machine=d.getVar("MACHINE"),
  107. rootfs=self.rootfs,
  108. tmpdir = d.getVar("TMPDIR"),
  109. deploy_dir_image = d.getVar("DEPLOY_DIR_IMAGE"),
  110. display = d.getVar("BB_ORIGENV", False).getVar("DISPLAY"),
  111. logfile = self.qemulog,
  112. boottime = int(d.getVar("TEST_QEMUBOOT_TIMEOUT")),
  113. use_kvm = use_kvm,
  114. dump_dir = dump_dir,
  115. logger = logger,
  116. tmpfsdir = d.getVar("RUNQEMU_TMPFS_DIR"),
  117. serial_ports = len(d.getVar("SERIAL_CONSOLES").split()),
  118. boot_patterns = boot_patterns)
  119. self.monitor_dumper = MonitorDumper(dump_monitor_cmds, dump_dir, self.runner)
  120. if (self.monitor_dumper):
  121. self.monitor_dumper.create_dir("qmp")
  122. def deploy(self):
  123. bb.utils.mkdirhier(self.testdir)
  124. qemuloglink = os.path.join(self.testdir, "qemu_boot_log")
  125. if os.path.islink(qemuloglink):
  126. os.unlink(qemuloglink)
  127. os.symlink(self.qemulog, qemuloglink)
  128. self.logger.info("rootfs file: %s" % self.rootfs)
  129. self.logger.info("Qemu log file: %s" % self.qemulog)
  130. super(QemuTarget, self).deploy()
  131. def start(self, params=None, ssh=True, extra_bootparams='', runqemuparams='', launch_cmd='', discard_writes=True):
  132. if launch_cmd:
  133. start = self.runner.launch(get_ip=ssh, launch_cmd=launch_cmd, qemuparams=params)
  134. else:
  135. start = self.runner.start(params, get_ip=ssh, extra_bootparams=extra_bootparams, runqemuparams=runqemuparams, discard_writes=discard_writes)
  136. if start:
  137. if ssh:
  138. self.ip = self.runner.ip
  139. self.server_ip = self.runner.server_ip
  140. self.connection = SSHControl(ip=self.ip, logfile=self.sshlog)
  141. else:
  142. self.stop()
  143. if os.path.exists(self.qemulog):
  144. with open(self.qemulog, 'r') as f:
  145. bb.error("Qemu log output from %s:\n%s" % (self.qemulog, f.read()))
  146. raise RuntimeError("%s - FAILED to start qemu - check the task log and the boot log" % self.pn)
  147. def check(self):
  148. return self.runner.is_alive()
  149. def stop(self):
  150. try:
  151. self.runner.stop()
  152. except:
  153. pass
  154. self.logger.removeHandler(self.loggerhandler)
  155. self.loggerhandler.close()
  156. self.connection = None
  157. self.ip = None
  158. self.server_ip = None
  159. def restart(self, params=None):
  160. if self.runner.restart(params):
  161. self.ip = self.runner.ip
  162. self.server_ip = self.runner.server_ip
  163. self.connection = SSHControl(ip=self.ip, logfile=self.sshlog)
  164. else:
  165. raise RuntimeError("%s - FAILED to re-start qemu - check the task log and the boot log" % self.pn)
  166. def run_serial(self, command, timeout=60):
  167. return self.runner.run_serial(command, timeout=timeout)
  168. class SimpleRemoteTarget(BaseTarget):
  169. def __init__(self, d):
  170. super(SimpleRemoteTarget, self).__init__(d)
  171. 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.')
  172. self.ip = addr.split(":")[0]
  173. try:
  174. self.port = addr.split(":")[1]
  175. except IndexError:
  176. self.port = None
  177. self.logger.info("Target IP: %s" % self.ip)
  178. self.server_ip = d.getVar("TEST_SERVER_IP")
  179. if not self.server_ip:
  180. try:
  181. self.server_ip = subprocess.check_output(['ip', 'route', 'get', self.ip ]).split("\n")[0].split()[-1]
  182. except Exception as e:
  183. 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)
  184. self.logger.info("Server IP: %s" % self.server_ip)
  185. def deploy(self):
  186. super(SimpleRemoteTarget, self).deploy()
  187. def start(self, params=None, ssh=True, extra_bootparams=None):
  188. if ssh:
  189. self.connection = SSHControl(self.ip, logfile=self.sshlog, port=self.port)
  190. def stop(self):
  191. self.connection = None
  192. self.ip = None
  193. self.server_ip = None