boot_data.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # Copyright 2018 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. """Functions used to provision Fuchsia boot images."""
  5. import common
  6. import logging
  7. import os
  8. import subprocess
  9. import tempfile
  10. import time
  11. import uuid
  12. _SSH_CONFIG_TEMPLATE = """
  13. Host *
  14. CheckHostIP no
  15. StrictHostKeyChecking no
  16. ForwardAgent no
  17. ForwardX11 no
  18. User fuchsia
  19. IdentitiesOnly yes
  20. IdentityFile {identity}
  21. ServerAliveInterval 2
  22. ServerAliveCountMax 5
  23. ControlMaster auto
  24. ControlPersist 1m
  25. ControlPath /tmp/ssh-%r@%h:%p
  26. ConnectTimeout 5
  27. """
  28. # Specifies boot files intended for use by an emulator.
  29. TARGET_TYPE_QEMU = 'qemu'
  30. # Specifies boot files intended for use by anything (incl. physical devices).
  31. TARGET_TYPE_GENERIC = 'generic'
  32. # Defaults used by Fuchsia SDK
  33. _SSH_DIR = os.path.expanduser('~/.ssh')
  34. _SSH_CONFIG_DIR = os.path.expanduser('~/.fuchsia')
  35. def _GetAuthorizedKeysPath():
  36. """Returns a path to the authorized keys which get copied to your Fuchsia
  37. device during paving"""
  38. return os.path.join(_SSH_DIR, 'fuchsia_authorized_keys')
  39. def ProvisionSSH():
  40. """Generates a key pair and config file for SSH using the GN SDK."""
  41. returncode, out, err = common.RunGnSdkFunction('fuchsia-common.sh',
  42. 'check-fuchsia-ssh-config')
  43. if returncode != 0:
  44. logging.error('Command exited with error code %d' % (returncode))
  45. logging.error('Stdout: %s' % out)
  46. logging.error('Stderr: %s' % err)
  47. raise Exception('Failed to provision ssh keys')
  48. def GetTargetFile(filename, target_arch, target_type):
  49. """Computes a path to |filename| in the Fuchsia boot image directory specific
  50. to |target_type| and |target_arch|."""
  51. return os.path.join(common.IMAGES_ROOT, target_arch, target_type, filename)
  52. def GetSSHConfigPath():
  53. return os.path.join(_SSH_CONFIG_DIR, 'sshconfig')
  54. def GetBootImage(output_dir, target_arch, target_type):
  55. """"Gets a path to the Zircon boot image, with the SSH client public key
  56. added."""
  57. ProvisionSSH()
  58. authkeys_path = _GetAuthorizedKeysPath()
  59. zbi_tool = common.GetHostToolPathFromPlatform('zbi')
  60. image_source_path = GetTargetFile('zircon-a.zbi', target_arch, target_type)
  61. image_dest_path = os.path.join(output_dir, 'gen', 'fuchsia-with-keys.zbi')
  62. cmd = [
  63. zbi_tool, '-o', image_dest_path, image_source_path, '-e',
  64. 'data/ssh/authorized_keys=' + authkeys_path
  65. ]
  66. subprocess.check_call(cmd)
  67. return image_dest_path
  68. def GetKernelArgs():
  69. """Returns a list of Zircon commandline arguments to use when booting a
  70. system."""
  71. return [
  72. 'devmgr.epoch=%d' % time.time(),
  73. 'blobfs.write-compression-algorithm=UNCOMPRESSED'
  74. ]
  75. def AssertBootImagesExist(arch, platform):
  76. assert os.path.exists(GetTargetFile('zircon-a.zbi', arch, platform)), \
  77. 'This checkout is missing the files necessary for\n' \
  78. 'booting this configuration of Fuchsia.\n' \
  79. 'To check out the files, add this entry to the "custom_vars"\n' \
  80. 'section of your .gclient file:\n\n' \
  81. ' "checkout_fuchsia_boot_images": "%s.%s"\n\n' % \
  82. (platform, arch)