masterimage.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. # Copyright (C) 2014 Intel Corporation
  2. #
  3. # SPDX-License-Identifier: MIT
  4. #
  5. # This module adds support to testimage.bbclass to deploy images and run
  6. # tests using a "master image" - this is a "known good" image that is
  7. # installed onto the device as part of initial setup and will be booted into
  8. # with no interaction; we can then use it to deploy the image to be tested
  9. # to a second partition before running the tests.
  10. #
  11. # For an example master image, see core-image-testmaster
  12. # (meta/recipes-extended/images/core-image-testmaster.bb)
  13. import os
  14. import bb
  15. import traceback
  16. import time
  17. import subprocess
  18. import oeqa.targetcontrol
  19. import oeqa.utils.sshcontrol as sshcontrol
  20. import oeqa.utils.commands as commands
  21. from oeqa.utils import CommandError
  22. from abc import ABCMeta, abstractmethod
  23. class MasterImageHardwareTarget(oeqa.targetcontrol.BaseTarget, metaclass=ABCMeta):
  24. supported_image_fstypes = ['tar.gz', 'tar.bz2']
  25. def __init__(self, d):
  26. super(MasterImageHardwareTarget, self).__init__(d)
  27. # target ip
  28. 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.')
  29. self.ip = addr.split(":")[0]
  30. try:
  31. self.port = addr.split(":")[1]
  32. except IndexError:
  33. self.port = None
  34. bb.note("Target IP: %s" % self.ip)
  35. self.server_ip = d.getVar("TEST_SERVER_IP")
  36. if not self.server_ip:
  37. try:
  38. self.server_ip = subprocess.check_output(['ip', 'route', 'get', self.ip ]).split("\n")[0].split()[-1]
  39. except Exception as e:
  40. 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)
  41. bb.note("Server IP: %s" % self.server_ip)
  42. # test rootfs + kernel
  43. self.image_fstype = self.get_image_fstype(d)
  44. self.rootfs = os.path.join(d.getVar("DEPLOY_DIR_IMAGE"), d.getVar("IMAGE_LINK_NAME") + '.' + self.image_fstype)
  45. self.kernel = os.path.join(d.getVar("DEPLOY_DIR_IMAGE"), d.getVar("KERNEL_IMAGETYPE", False) + '-' + d.getVar('MACHINE', False) + '.bin')
  46. if not os.path.isfile(self.rootfs):
  47. # we could've checked that IMAGE_FSTYPES contains tar.gz but the config for running testimage might not be
  48. # the same as the config with which the image was build, ie
  49. # you bitbake core-image-sato with IMAGE_FSTYPES += "tar.gz"
  50. # and your autobuilder overwrites the config, adds the test bits and runs bitbake core-image-sato -c testimage
  51. bb.fatal("No rootfs found. Did you build the image ?\nIf yes, did you build it with IMAGE_FSTYPES += \"tar.gz\" ? \
  52. \nExpected path: %s" % self.rootfs)
  53. if not os.path.isfile(self.kernel):
  54. bb.fatal("No kernel found. Expected path: %s" % self.kernel)
  55. # master ssh connection
  56. self.master = None
  57. # if the user knows what they are doing, then by all means...
  58. self.user_cmds = d.getVar("TEST_DEPLOY_CMDS")
  59. self.deploy_cmds = None
  60. # this is the name of the command that controls the power for a board
  61. # e.g: TEST_POWERCONTROL_CMD = "/home/user/myscripts/powercontrol.py ${MACHINE} what-ever-other-args-the-script-wants"
  62. # the command should take as the last argument "off" and "on" and "cycle" (off, on)
  63. self.powercontrol_cmd = d.getVar("TEST_POWERCONTROL_CMD") or None
  64. self.powercontrol_args = d.getVar("TEST_POWERCONTROL_EXTRA_ARGS", False) or ""
  65. self.serialcontrol_cmd = d.getVar("TEST_SERIALCONTROL_CMD") or None
  66. self.serialcontrol_args = d.getVar("TEST_SERIALCONTROL_EXTRA_ARGS", False) or ""
  67. self.origenv = os.environ
  68. if self.powercontrol_cmd or self.serialcontrol_cmd:
  69. # the external script for controlling power might use ssh
  70. # ssh + keys means we need the original user env
  71. bborigenv = d.getVar("BB_ORIGENV", False) or {}
  72. for key in bborigenv:
  73. val = bborigenv.getVar(key)
  74. if val is not None:
  75. self.origenv[key] = str(val)
  76. if self.powercontrol_cmd:
  77. if self.powercontrol_args:
  78. self.powercontrol_cmd = "%s %s" % (self.powercontrol_cmd, self.powercontrol_args)
  79. if self.serialcontrol_cmd:
  80. if self.serialcontrol_args:
  81. self.serialcontrol_cmd = "%s %s" % (self.serialcontrol_cmd, self.serialcontrol_args)
  82. def power_ctl(self, msg):
  83. if self.powercontrol_cmd:
  84. cmd = "%s %s" % (self.powercontrol_cmd, msg)
  85. try:
  86. commands.runCmd(cmd, assert_error=False, preexec_fn=os.setsid, env=self.origenv)
  87. except CommandError as e:
  88. bb.fatal(str(e))
  89. def power_cycle(self, conn):
  90. if self.powercontrol_cmd:
  91. # be nice, don't just cut power
  92. conn.run("shutdown -h now")
  93. time.sleep(10)
  94. self.power_ctl("cycle")
  95. else:
  96. status, output = conn.run("sync; { sleep 1; reboot; } > /dev/null &")
  97. if status != 0:
  98. bb.error("Failed rebooting target and no power control command defined. You need to manually reset the device.\n%s" % output)
  99. def _wait_until_booted(self):
  100. ''' Waits until the target device has booted (if we have just power cycled it) '''
  101. # Subclasses with better methods of determining boot can override this
  102. time.sleep(120)
  103. def deploy(self):
  104. # base class just sets the ssh log file for us
  105. super(MasterImageHardwareTarget, self).deploy()
  106. self.master = sshcontrol.SSHControl(ip=self.ip, logfile=self.sshlog, timeout=600, port=self.port)
  107. status, output = self.master.run("cat /etc/masterimage")
  108. if status != 0:
  109. # We're not booted into the master image, so try rebooting
  110. bb.plain("%s - booting into the master image" % self.pn)
  111. self.power_ctl("cycle")
  112. self._wait_until_booted()
  113. bb.plain("%s - deploying image on target" % self.pn)
  114. status, output = self.master.run("cat /etc/masterimage")
  115. if status != 0:
  116. bb.fatal("No ssh connectivity or target isn't running a master image.\n%s" % output)
  117. if self.user_cmds:
  118. self.deploy_cmds = self.user_cmds.split("\n")
  119. try:
  120. self._deploy()
  121. except Exception as e:
  122. bb.fatal("Failed deploying test image: %s" % e)
  123. @abstractmethod
  124. def _deploy(self):
  125. pass
  126. def start(self, extra_bootparams=None):
  127. bb.plain("%s - boot test image on target" % self.pn)
  128. self._start()
  129. # set the ssh object for the target/test image
  130. self.connection = sshcontrol.SSHControl(self.ip, logfile=self.sshlog, port=self.port)
  131. bb.plain("%s - start running tests" % self.pn)
  132. @abstractmethod
  133. def _start(self):
  134. pass
  135. def stop(self):
  136. bb.plain("%s - reboot/powercycle target" % self.pn)
  137. self.power_cycle(self.master)
  138. class SystemdbootTarget(MasterImageHardwareTarget):
  139. def __init__(self, d):
  140. super(SystemdbootTarget, self).__init__(d)
  141. # this the value we need to set in the LoaderEntryOneShot EFI variable
  142. # so the system boots the 'test' bootloader label and not the default
  143. # The first four bytes are EFI bits, and the rest is an utf-16le string
  144. # (EFI vars values need to be utf-16)
  145. # $ echo -en "test\0" | iconv -f ascii -t utf-16le | hexdump -C
  146. # 00000000 74 00 65 00 73 00 74 00 00 00 |t.e.s.t...|
  147. self.efivarvalue = r'\x07\x00\x00\x00\x74\x00\x65\x00\x73\x00\x74\x00\x00\x00'
  148. self.deploy_cmds = [
  149. 'mount -L boot /boot',
  150. 'mkdir -p /mnt/testrootfs',
  151. 'mount -L testrootfs /mnt/testrootfs',
  152. 'modprobe efivarfs',
  153. 'mount -t efivarfs efivarfs /sys/firmware/efi/efivars',
  154. 'cp ~/test-kernel /boot',
  155. 'rm -rf /mnt/testrootfs/*',
  156. 'tar xvf ~/test-rootfs.%s -C /mnt/testrootfs' % self.image_fstype,
  157. 'printf "%s" > /sys/firmware/efi/efivars/LoaderEntryOneShot-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f' % self.efivarvalue
  158. ]
  159. def _deploy(self):
  160. # make sure these aren't mounted
  161. self.master.run("umount /boot; umount /mnt/testrootfs; umount /sys/firmware/efi/efivars;")
  162. # from now on, every deploy cmd should return 0
  163. # else an exception will be thrown by sshcontrol
  164. self.master.ignore_status = False
  165. self.master.copy_to(self.rootfs, "~/test-rootfs." + self.image_fstype)
  166. self.master.copy_to(self.kernel, "~/test-kernel")
  167. for cmd in self.deploy_cmds:
  168. self.master.run(cmd)
  169. def _start(self, params=None):
  170. self.power_cycle(self.master)
  171. # there are better ways than a timeout but this should work for now
  172. time.sleep(120)