1. 主页
  2. 文档
  3. Fuchsia OS 简体中文文档 2022...
  4. Fuchsia 入门
  5. 启动 Fuchsia 模拟器

启动 Fuchsia 模拟器

本指南提供关于在您的设备上安装和启动 Fuchsia 模拟器(FEMU)的方法说明。

步骤如下:

  1. 前提条件
  2. 为 FEMU 构建 Fuchsia
  3. 启用 VM 加速(可选)
  4. 启动 FEMU
  5. 发现 FEMU

1. 前提条件 {#prerequisites}

运行 FEMU 需要您完成以下指南:

2. 为 FEMU 构建 Fuchsia {#build-fuchsia-for-femu}

<!–
To run FEMU, you first need to build a Fuchsia system image that supports
the emulator environment. This guide uses qemu-x64 for the board
and workstation_eng for the product as an example.
–>

要运行 FEMU,您首先要构建一个支持模拟器环境的 Fuchsia 系统镜像。本指南使用 qemu-x64 板型和 workstation_eng 产品作为示例。

要构建 FEMU 的 Fuchsia 镜像,请执行以下操作:

  1. 设置 Fuchsia 构建配置:

    fx set workstation_eng.qemu-x64 --release

  1. 构建 Fuchsia:

    fx build

要获取关于支持的板型和产品的更多信息,请参阅 Fuchsia 模拟器(FEMU)概述页面。

3. 启用 VM 加速(可选) {#enable-vm-acceleration}

仅限Linux)大部分 Linux 设备支持通过 KVM 进行 VM 加速,这大大提高了模拟器的性能和可用性。

如果您的设备可以使用 KVM,请更新您的组权限来启用 KVM。

  • {Linux}

要在您的设备上启用 KVM,请执行以下操作:

注意:您只需要在每台设备上执行一次。

<!–
1. Add yourself to the kvm group on your machine:
–>

  1. 在您的设备上添加您自己到 kvm 组:

      sudo usermod -a -G kvm ${USER}

  1. 注销与您设备的所有桌面会话,然后重新登录。

  1. 要验证 KVM 配置正确,请运行以下命令:

      if [[ -r /dev/kvm ]] && grep '^flags' /proc/cpuinfo | grep -qE 'vmx|svm'; then echo 'KVM is working'; else echo 'KVM not working'; fi

  验证此命令输出以下行:

  ```none {:.devsite-disable-click-to-copy}
  KVM is working
  ```

<!–
If you see KVM not working, you may need to reboot your machine for
the permission change to take effect.
–>

  如果您看到 KVM not working,那么您可能需要重启您的设备,以使权限修改生效。
  • {macOS}

macOS 无需额外的设置。

在 macOS 上 Fuchsia 模拟器使用的是 Hypervisor 框架{: .external},而不是 KVM。

4. 启动 FEMU {#start-femu}

启动包服务器

在启动模拟器之前,请启动包服务器。

要启动包服务器,请运行以下命令:

  fx serve

<!–
Note: Alternatively you can background the fx serve process.
–>

注意:或者,您可以将 fx serve 进程置于后台。

启动模拟器

要在您的 Linux 设备启动模拟器,请执行以下操作:

  • {Linux}

  1. 通过运行以下命令配置 upscript:

  注意:如果您的设备使用了防火墙,您可能需要应用一些额外的配置来允许模拟器访问网络。这通常通过运行“upscript”来完成,该脚本为当前进程设置接口和防火墙访问规则。如果您位于企业网络,请联系您的内部网络团队来查看他们是否有现行的 upscript 供您使用。

<!–
If you're not behind a firewall, there's still some configuration needed to
enable tun/tap networking. The example upscript
at {{ '' }}FUCHSIA_ROOT{{ '' }}/scripts/start-unsecure-internet.sh
should work for the majority of non-corporate users.
–>
如果您没有使用防火墙,那么还需要进行一些配置才能启用 tun/tap 网络。位于 {{ '' }}FUCHSIA_ROOT{{ '' }}/scripts/start-unsecure-internet.sh 的 upscript 示例应该适用于大多数非企业网络。

  ```posix-terminal
  ffx config set emu.upscript {{ '' }}FUCHSIA_ROOT{{ '' }}/scripts/start-unsecure-internet.sh
  ```

<!–
* start-unsecure-internet.sh is an example upscript.
* FUCHSIA_ROOT is the path to your Fuchsia directory.
–>

  * start-unsecure-internet.sh 是一个 upscript 示例。
  * FUCHSIA_ROOT 是您 Fuchsia 目录的路径。

  1. 启动 FEMU

  1. 要启动可以访问外部网络的模拟器,请运行以下命令:

      ```posix-terminal
      ffx emu start --net tap
      ```

<!–
* -net specifies the networking mode for the emulator. -net tap
attaches to a Tun/Tap interface.
–>

      * --net 指定模拟器的网络模式。--net tap 附加到 Tun/Tap 接口。

  1. 要启动不可访问外部网络的模拟器,请运行以下命令:

      ```posix-terminal
      ffx emu start --net none
      ```

启动模拟器会打开一个标题为 **Fuchsia Emulator** 的新窗口,当模拟器完成启动后,您会返回到命令提示符,模拟器会在后台运行。
  • {macOS}

要在 macOS 上启动 FEMU,请执行以下操作:

  1. 启动 FEMU:

     ffx emu start

<!–
If you launch FEMU for the first time on your macOS (including after a reboot),
a window pops up asking if you want to allow the process aemu to run on your
machine. Click **Allow**.
–>

 如果您在 macOS 上第一次(包括在重启后的第一次)启动 FEMU,则会有窗口弹出,询问您是否要允许 aemu 进程在您的设备上运行。请点击 **允许**(Allow)。

 该命令打开一个标题为 **Fuchsia Emulator** 的新窗口。当模拟器启动完成后,您会返回到命令提示符,模拟器会在后台运行。

<!–
2. (Optional) If you need to specify the launched Fuchsia emulator, you can
run the fx set-device command in the same terminal:
–>

  1. (可选)如果您需要指定已启动的 Fuchsia 模拟器,您可以在同一终端运行 fx set-device 命令。

     fx set-device {{ '' }}NAME{{ '' }}

 请替换以下内容:

<!–
* NAME: Use the desired value from the ffx emu list or ffx target list
command’s output. fuchsia-emulator is the default value.
–>

 * NAME:请从 ffx emu listffx target list 命令的输出中选择希望使用的值。fuchsia-emulator 为默认值。

5. 发现 FEMU {#discover-femu}

要将 Fuchsia 模拟器发现为正在运行的 Fuchsia 设备,请运行以下命令:

ffx target list

该命令的输出类似于以下内容:

$ ffx target list
NAME                      SERIAL       TYPE                    STATE      ADDRS/IP                            RCS
fuchsia-emulator    <unknown>    workstation_eng.qemu-x64    Product    [fe80::866a:a5ea:cd9e:69f6%qemu]    N

<!–
fuchsia-emulator is the default node name of the Fuchsia emulator.
–>

fuchsia-emulator 是 Fuchsia 模拟器的默认节点名称。

<!–
The output of ffx target list is influenced by the -net option in the
following ways:
–>

ffx target list 的输出受 --net 选项影响,具体如下:

<!–
* -net none disables networking, which causes the device to not be
discoverable when running ffx target list.
–>

  • --net none 禁用网络,这导致设备在运行 ffx target list 时无法被发现。
    <!–
  • -net tap and -net user allow the device to be discoverable
    when running ffx target list.
    –>
  • --net tap--net user 允许设备在运行 ffx target list 时能被发现。

后续步骤

要了解关于 Fuchsia 设备命令和 Fuchsia 工作流的更多内容,请参阅探索 Fuchsia

附录

本章节提供额外的 FEMU 选项。

查看所有可用的标记(flag)

要查看模拟器所支持标记的完整列表,请运行以下命令:

ffx emu start --help

在没有 GUI 支持的情况下运行 FEMU

如果您不需要图形界面或者在远程工作流下工作,您可以在无头模式(headless mode)下运行 FEMU:

ffx emu start --headless

指定 FEMU 使用的 GPU

<!–
By default, the FEMU launcher attempts to detect if the host has a GPU that can be used for
graphics rendering. If you need to explicitly set the rendering type, you can use
the -gpu flag with the following options:
–>

默认情况下,FEMU 启动器尝试检测主机是否有可用于图形渲染的 GPU。如果需要显式设置渲染类型,那么您可以使用带有以下选项的 --gpu 标记:

<!–

GPU Emulation method Explanation Flag
Hardware (host GPU) Uses the host machine’s GPU directly to perform GPU processing. ffx emu start --gpu host
Software (host CPU) Uses the host machine’s CPU to simulate GPU processing. ffx emu start --gpu guest
SwiftShader Uses SwiftShader libraries to simulate GPU processing. ffx emu start --gpu swiftshader_indirect
Auto Resolves to host if there is a hardware GPU available or
swiftshader_indirect if there isn’t a hardware GPU available.
auto is the current default.
ffx emu start --gpu auto

–>

GPU 仿真方式 说明 标记
硬件(主机 GPU) 直接使用主机设备的 GPU 进行 GPU 处理。 ffx emu start --gpu host
软件(主机 CPU) 使用主机设备的 CPU 来模拟 GPU 处理。 ffx emu start --gpu guest
SwiftShader 使用 SwiftShader 库来模拟 GPU 处理。 ffx emu start --gpu swiftshader_indirect
自动 如果有可用的 GPU 硬件,则解析为 host,如果没有可用的 GPU 硬件,则解析为 swiftshader_indirectauto 是当前默认值。 ffx emu start --gpu auto

重启 FEMU {#reboot-femu}

<!–
To reboot FEMU, run the following ffx command:
–>

要重启 FEMU,请运行以下 ffx 命令:

ffx target reboot

停止 FEMU {#stop-femu}

<!–
To stop FEMU, run the following ffx command:
–>

要停止 FEMU,请运行以下 ffx 命令:

ffx emu stop

配置 IPv6 网络 {#configure-ipv6-network}

本节提供关于在 Linux 设备上使用 TUN/TAP{: .external} 为 FEMU 配置 IPv6 网络的方法说明。

  • {Linux}

注意:这必须在每台设备上完成一次。

要使用 TAP 网络{: .external} 在 FEMU 中启用网络,请执行以下操作:

<!–
1. Set up tuntap:
–>

  1. 设置 tuntap

     sudo ip tuntap add dev qemu mode tap user $USER

<!–
1. Enable the network for qemu:
–>

  1. qemu 启用网络:

     sudo ip link set qemu up
  • {macOS}

macOS 不需要额外的 IPv6 网络设置。

<!–
[User Networking (SLIRP)][slirp]{: .external} is the default network setup
for FEMU on macOS – while this setup does not support Fuchsia device
discovery, you can still use fx tools (for example,fx ssh) to
interact with your FEMU instance.
–>

用户网络(SLIRP){: .external}是 macOS 上 FEMU 的默认网络设置——虽然此设置不支持 Fuchsia 设备发现,但您仍然可以使用 fx 工具(例如,fx ssh)与您的 FEMU 实例进行交互。

我们要如何帮助您?

Comments

Leave a reply

您的电子邮箱地址不会被公开。 必填项已用*标注