|
| 1 | +// Copyright 2019 The Flutter 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 | + |
| 5 | +import 'dart:io'; |
| 6 | + |
| 7 | +import 'package:file/file.dart'; |
| 8 | +import 'package:file/local.dart'; |
| 9 | +import 'package:meta/meta.dart'; |
| 10 | +import 'package:path/path.dart' as path; |
| 11 | + |
| 12 | +import 'command_line.dart'; |
| 13 | +import 'operation_result.dart'; |
| 14 | +import 'ssh_key_manager.dart'; |
| 15 | + |
| 16 | +/// A wrapper for running Fuchsia images on the Android Emulator (AEMU). |
| 17 | +class Emulator { |
| 18 | + /// Creates a new wrapper for the `emu` tool. |
| 19 | + Emulator({ |
| 20 | + @required this.aemuPath, |
| 21 | + @required this.fuchsiaImagePath, |
| 22 | + @required this.fuchsiaSdkPath, |
| 23 | + this.fs = const LocalFileSystem(), |
| 24 | + this.cli = const CommandLine(), |
| 25 | + @required this.qemuKernelPath, |
| 26 | + @required this.sshKeyManager, |
| 27 | + @required this.zbiPath, |
| 28 | + }) : assert(cli != null), |
| 29 | + assert(fs != null); |
| 30 | + |
| 31 | + /// The path to the AEMU executable on disk. |
| 32 | + final String aemuPath; |
| 33 | + |
| 34 | + /// Fuchsia image to load into the emulator. |
| 35 | + final String fuchsiaImagePath; |
| 36 | + |
| 37 | + /// The path to the Fuchsia SDK that contains the tools `fvm` and `zbi`. |
| 38 | + final String fuchsiaSdkPath; |
| 39 | + |
| 40 | + /// The QEMU kernel image to use. This is only bundled in Fuchsia QEMU images. |
| 41 | + final String qemuKernelPath; |
| 42 | + |
| 43 | + /// The Fuchsia bootloader image. |
| 44 | + final String zbiPath; |
| 45 | + |
| 46 | + /// Location of `fvm` in [fuchsiaSdkPath]. |
| 47 | + @visibleForTesting |
| 48 | + final String fvmToolPath = 'sdk/tools/fvm'; |
| 49 | + |
| 50 | + /// Location of `zbi` in [fuchsiaSdkPath]. |
| 51 | + @visibleForTesting |
| 52 | + final String zbiToolPath = 'sdk/tools/zbi'; |
| 53 | + |
| 54 | + /// Default AEMU window size to be launched. |
| 55 | + @visibleForTesting |
| 56 | + final String defaultWindowSize = '1280x800'; |
| 57 | + |
| 58 | + /// Flag to pass to AEMU to run in headless mode. |
| 59 | + @visibleForTesting |
| 60 | + final String aemuHeadlessFlag = '-no-window'; |
| 61 | + |
| 62 | + /// [SshKeyManager] for creating `authorized_keys` to access emulator. |
| 63 | + final SshKeyManager sshKeyManager; |
| 64 | + |
| 65 | + /// The [FileSystem] to use when running the `emu` tool. |
| 66 | + final FileSystem fs; |
| 67 | + |
| 68 | + /// The [CommandLine] wrapper for interacting with the current shell. |
| 69 | + final CommandLine cli; |
| 70 | + |
| 71 | + /// FVM extended image of [fuchsiaImagePath] for running on FEMU. |
| 72 | + @visibleForTesting |
| 73 | + String fvmImagePath; |
| 74 | + |
| 75 | + /// [zbiPath] that is accessible with SSH using [sshPath] keys. |
| 76 | + @visibleForTesting |
| 77 | + String signedZbiPath; |
| 78 | + |
| 79 | + /// Update given Fuchsia assets to make them compatible with FEMU. |
| 80 | + /// |
| 81 | + /// 1. Ensure required assets exist. |
| 82 | + /// 2. Create FVM image for running with FEMU. |
| 83 | + /// 3. Sign boot image for host access to the guest FEMU instance. |
| 84 | + Future<void> prepareEnvironment() async { |
| 85 | + assert(fs.isFileSync(fuchsiaImagePath)); |
| 86 | + assert(fs.isFileSync(zbiPath)); |
| 87 | + assert(fs.isFileSync(qemuKernelPath)); |
| 88 | + |
| 89 | + final String tmpPath = fs.systemTempDirectory.createTempSync().path; |
| 90 | + fvmImagePath = '$tmpPath/fvm.blk'; |
| 91 | + signedZbiPath = '$tmpPath/fuchsia-ssh.zbi'; |
| 92 | + |
| 93 | + await _prepareFvmImage(fuchsiaImagePath, fvmImagePath); |
| 94 | + await _signBootImage(zbiPath, signedZbiPath); |
| 95 | + } |
| 96 | + |
| 97 | + /// Double the size of [fuchsiaImagePath] to make space for the emulator |
| 98 | + /// to write back to it. |
| 99 | + Future<void> _prepareFvmImage(String fuchsiaImagePath, String fvmPath, |
| 100 | + {String fvmExecutable}) async { |
| 101 | + fvmExecutable ??= path.join(fuchsiaSdkPath, fvmToolPath); |
| 102 | + |
| 103 | + await cli.run(<String>['cp', fuchsiaImagePath, fvmPath]); |
| 104 | + |
| 105 | + /// [fvmTool] and FEMU need write access to [fvmPath]. |
| 106 | + await cli.run(<String>['chmod', 'u+w', fvmPath]); |
| 107 | + |
| 108 | + // Calculate new size by doubling the current size |
| 109 | + final File fvmFile = fs.file(fvmPath)..createSync(); |
| 110 | + final int newSize = fvmFile.lengthSync() * 2; |
| 111 | + |
| 112 | + await cli.run( |
| 113 | + <String>[fvmExecutable, fvmPath, 'extend', '--length', '$newSize']); |
| 114 | + } |
| 115 | + |
| 116 | + /// Signed [zbiPath] using [zbiExecutable] with [publicKeyPath] to |
| 117 | + /// create a bootloader image that is accessible from the host. |
| 118 | + Future<void> _signBootImage(String zbiPath, String signedZbiPath, |
| 119 | + {String zbiExecutable}) async { |
| 120 | + zbiExecutable ??= path.join(fuchsiaSdkPath, zbiToolPath); |
| 121 | + |
| 122 | + await sshKeyManager.createKeys(); |
| 123 | + |
| 124 | + /// Ensure `zbi` is able to find the ssh keys by giving the full path. |
| 125 | + final File authorizedKeysAbsolute = |
| 126 | + fs.file('.ssh/authorized_keys').absolute; |
| 127 | + |
| 128 | + final List<String> zbiCommand = <String>[ |
| 129 | + zbiExecutable, |
| 130 | + '--compressed=zstd', |
| 131 | + '-o', |
| 132 | + signedZbiPath, |
| 133 | + zbiPath, |
| 134 | + '-e', |
| 135 | + 'data/ssh/authorized_keys=${authorizedKeysAbsolute.path}' |
| 136 | + ]; |
| 137 | + await cli.run(zbiCommand); |
| 138 | + } |
| 139 | + |
| 140 | + /// Launch AEMU with [fvmImagePath], [signedZbiPath], and [qemuKernelPath]. |
| 141 | + /// |
| 142 | + /// [prepareEnvironment] must have been called before starting the emulator. |
| 143 | + /// |
| 144 | + /// If [headless] is true, AEMU will run without a graphical window. Infra |
| 145 | + /// will run AEMU in headless mode. |
| 146 | + /// |
| 147 | + /// [windowSize] is what AEMU will set its window size to. Defaults to |
| 148 | + /// [defaultWindowSize]. Expected to be in the format of "WIDTHxHEIGHT". |
| 149 | + Future<OperationResult> start( |
| 150 | + {bool headless = false, String windowSize}) async { |
| 151 | + assert(fvmImagePath != null && fs.isFileSync(fvmImagePath)); |
| 152 | + assert(signedZbiPath != null && fs.isFileSync(signedZbiPath)); |
| 153 | + |
| 154 | + final List<String> aemuCommand = <String>[ |
| 155 | + aemuPath, |
| 156 | + '-feature', |
| 157 | + 'VirtioInput,RefCountPipe,KVM,GLDirectMem,Vulkan', |
| 158 | + '-window-size', |
| 159 | + windowSize ?? defaultWindowSize, |
| 160 | + '-gpu', |
| 161 | + 'swiftshader_indirect', |
| 162 | + if (headless) aemuHeadlessFlag, |
| 163 | + |
| 164 | + /// Anything after -fuchsia flag will be passed to QEMU |
| 165 | + '-fuchsia', |
| 166 | + '-kernel', qemuKernelPath, |
| 167 | + '-initrd', signedZbiPath, |
| 168 | + '-m', '2048', |
| 169 | + '-serial', 'stdio', |
| 170 | + '-vga', 'none', |
| 171 | + '-device', 'virtio-keyboard-pci', |
| 172 | + '-device', 'virtio_input_multi_touch_pci_1', |
| 173 | + '-smp', '4,threads=2', |
| 174 | + '-machine', 'q35', |
| 175 | + '-device', 'isa-debug-exit,iobase=0xf4,iosize=0x04', |
| 176 | + // TODO(chillers): Add hardware acceleration option to configure this. |
| 177 | + '-enable-kvm', |
| 178 | + '-cpu', 'host,migratable=no,+invtsc', |
| 179 | + '-netdev', 'type=tap,ifname=qemu,script=no,downscript=no,id=net0', |
| 180 | + '-device', 'e1000,netdev=net0,mac=52:54:00:63:5e:7a', |
| 181 | + '-drive', 'file=$fvmImagePath,format=raw,if=none,id=vdisk', |
| 182 | + '-device', 'virtio-blk-pci,drive=vdisk', |
| 183 | + '-append', |
| 184 | + // TODO(chillers): Generate entropy mixin. |
| 185 | + '\'TERM=xterm-256color kernel.serial=legacy kernel.entropy-mixin=660486b6b20b4ace3fb5c81b0002abf5271289185c6a5620707606c55b377562 kernel.halt-on-panic=true\'', |
| 186 | + ]; |
| 187 | + |
| 188 | + await cli.start(aemuCommand); |
| 189 | + |
| 190 | + return OperationResult.success(); |
| 191 | + } |
| 192 | +} |
0 commit comments