Cuttlefish on Mac: Running Android’s Virtual Device Framework Natively on Apple Silicon

本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。

Cuttlefish on Mac: Running Android’s Virtual Device Framework Natively on Apple Silicon

By Hadrien Zalek, Joe Knox, Alex Chong, Marcel Apfelbaum. How Meta’s Reality Labs brought Cuttlefish and its QEMU backend to macOS and Apple silicon. Meta’s Reality Labs builds products such as AI glasses. Emulators provide us with a way to scale development and testing without relying on scarce physical hardware.

作者:Hadrien Zalek、Joe Knox、Alex Chong、Marcel Apfelbaum。本文介绍了 Meta 的 Reality Labs 如何将 Cuttlefish 及其 QEMU 后端引入 macOS 和 Apple Silicon。Meta 的 Reality Labs 致力于开发 AI 眼镜等产品。模拟器为我们提供了一种扩展开发和测试的方法,而无需依赖稀缺的物理硬件。

Testing software for non-phone Android devices depends on how closely a virtual device reproduces the software behavior of the physical product. While a lightweight emulator like Goldfish gets you part of the way, differences from the real device can hide bugs until later in development. This lack of fidelity also erodes trust in device emulation, increasing reliance on scarce physical hardware.

针对非手机类 Android 设备的软件测试,取决于虚拟设备在多大程度上还原了物理产品的软件行为。虽然像 Goldfish 这样的轻量级模拟器可以实现部分功能,但与真实设备之间的差异可能会导致 Bug 在开发后期才被发现。这种保真度的缺失也会削弱对设备模拟的信任,从而增加对稀缺物理硬件的依赖。

Cuttlefish is the Android Open Source Project’s (AOSP’s) virtual-device framework for testing Android software against virtual hardware. It boots the same system images as real hardware and runs a realistic software stack against virtual hardware. This enables Cuttlefish to more closely track a physical device’s behavior than a lightweight emulator does. Unfortunately, Cuttlefish is only supported on Linux, and our application and library developers work on Macs with Apple silicon. This forced us to support Goldfish and Cuttlefish side by side.

Cuttlefish 是 Android 开源项目 (AOSP) 的虚拟设备框架,用于在虚拟硬件上测试 Android 软件。它引导与真实硬件相同的系统镜像,并在虚拟硬件上运行真实的软件栈。这使得 Cuttlefish 比轻量级模拟器能更紧密地追踪物理设备的行为。遗憾的是,Cuttlefish 仅支持 Linux,而我们的应用和库开发人员使用的是搭载 Apple Silicon 的 Mac。这迫使我们不得不同时维护 Goldfish 和 Cuttlefish。

We knew if we could consolidate onto a single Cuttlefish-based platform, we could give all of our developers the benefit of the same high-fidelity virtual device. The challenge was porting the stack to make Cuttlefish run natively on macOS.

我们深知,如果能整合到一个基于 Cuttlefish 的单一平台上,就能让所有开发人员享受到同样高保真虚拟设备带来的益处。挑战在于如何移植软件栈,使 Cuttlefish 能够在 macOS 上原生运行。

The Impact of Fragmentation

碎片化的影响

Maintaining dual emulator platforms imposed a heavy operational burden: It drained computing resources, created confusion for developers, and slowed down the emulator team. Every tool required support for two separate platforms, while infrastructure and engineering capacity were divided between them. Furthermore, developers had to choose an emulator target based on how they worked: Goldfish ran everywhere but had less fidelity while Cuttlefish needed Linux.

维护双模拟器平台带来了沉重的运营负担:它消耗了计算资源,给开发人员造成了困惑,并拖慢了模拟器团队的进度。每个工具都需要支持两个独立的平台,而基础设施和工程能力也被分散在两者之间。此外,开发人员必须根据工作方式选择模拟器目标:Goldfish 虽然可以在任何地方运行,但保真度较低,而 Cuttlefish 则需要 Linux 环境。

The AOSP mirrors this exact divide: Goldfish targets application developers, whereas Cuttlefish serves Android platform developers. So long as we maintained that division, we would suffer the effects of fragmentation.

AOSP 正好反映了这种分歧:Goldfish 面向应用开发人员,而 Cuttlefish 则服务于 Android 平台开发人员。只要我们维持这种分工,就必然会承受碎片化带来的后果。

The Stack and the macOS Gaps

软件栈与 macOS 的差距

Cuttlefish is the orchestration layer, while the component that actually runs the guest OS is a virtual machine monitor (VMM). On Linux, Cuttlefish normally uses crosvm but previously included support for QEMU as an alternative VMM. That integration had become inactive over time. To enable native Apple silicon support on macOS, we revived QEMU support.

Cuttlefish 是编排层,而实际运行客户机操作系统的组件是虚拟机监视器 (VMM)。在 Linux 上,Cuttlefish 通常使用 crosvm,但此前也曾包含对 QEMU 作为替代 VMM 的支持。随着时间的推移,该集成已处于非活动状态。为了在 macOS 上实现对 Apple Silicon 的原生支持,我们恢复了对 QEMU 的支持。

Many of Cuttlefish’s virtual devices use vhost-user backends, separate processes that provide virtio devices to QEMU over a socket. The architecture is clean, but large parts of it were written with a hard Linux assumption. Three of those assumptions break on macOS. The vhost-user machinery is gated to Linux and won’t build or run. The Apple hypervisor imposes resource limits QEMU didn’t expect. Finally, the native display, build, and signing paths didn’t exist. We tackled all three.

许多 Cuttlefish 虚拟设备使用 vhost-user 后端,即通过套接字向 QEMU 提供 virtio 设备的独立进程。该架构设计简洁,但其中很大一部分代码是基于 Linux 的硬性假设编写的。其中三个假设在 macOS 上无法成立:vhost-user 机制被限制在 Linux 上,无法构建或运行;Apple 的 Hypervisor 施加了 QEMU 未预料到的资源限制;最后,原生的显示、构建和签名路径尚不存在。我们解决了这三个问题。

The Cuttlefish-on-Mac stack. Cuttlefish orchestrates virtual devices backed by QEMU. The highlighted orange blocks are the Mac-specific work: adding macOS support to vhost-user, increasing memory-region tracking slots in QEMU’s HVF backend, and using the native Cocoa display and code-signing paths.

Cuttlefish-on-Mac 软件栈。Cuttlefish 编排由 QEMU 支持的虚拟设备。橙色高亮部分是针对 Mac 的工作:为 vhost-user 添加 macOS 支持,增加 QEMU 的 HVF 后端中的内存区域跟踪槽位,以及使用原生的 Cocoa 显示和代码签名路径。

Making vhost-user Portable Beyond Linux

让 vhost-user 跨越 Linux 实现可移植性

The most structurally important changes were in QEMU’s vhost-user path. The frontends, libvhost-user, and the contrib backends such as vhost-user-blk were all gated to Linux. We relaxed those build restrictions so the vhost-user frontends and libraries can build on macOS as well as Linux. This lets Cuttlefish’s device backends hand off to QEMU on a Mac the same way they do on Linux. Without this, the higher-fidelity device model that makes Cuttlefish worth using isn’t available outside Linux.

结构上最重要的变化发生在 QEMU 的 vhost-user 路径中。前端、libvhost-user 以及诸如 vhost-user-blk 之类的贡献后端此前都仅限于 Linux。我们放宽了这些构建限制,使得 vhost-user 前端和库既能在 macOS 上构建,也能在 Linux 上构建。这使得 Cuttlefish 的设备后端能够在 Mac 上以与 Linux 相同的方式与 QEMU 进行交互。如果没有这一点,Cuttlefish 所具备的高保真设备模型将无法在 Linux 之外使用。

Living Within Hypervisor.framework

在 Hypervisor.framework 中运行

On Apple silicon, QEMU accelerates through HVF (Hypervisor.framework) instead of KVM. QEMU’s HVF backend did not track enough memory regions for larger Cuttlefish configurations. We increased its tracking-slot count so those configurations could run correctly.

在 Apple Silicon 上,QEMU 通过 HVF (Hypervisor.framework) 而非 KVM 进行加速。QEMU 的 HVF 后端对于较大的 Cuttlefish 配置而言,无法跟踪足够的内存区域。我们增加了其跟踪槽位的数量,以便这些配置能够正确运行。

A Usable Native Display

可用的原生显示

Local application development requires an interactive display allowing developers to view and engage directly with the virtual device. To enable direct interaction and bypass reliance on the WebRTC interface, we transitioned to QEMU’s native Cocoa display. The changes include:

本地应用开发需要一个交互式显示界面,允许开发人员直接查看并操作虚拟设备。为了实现直接交互并绕过对 WebRTC 接口的依赖,我们转向了 QEMU 的原生 Cocoa 显示。所做的更改包括:

  • BGRA Pixel Format Support: Enables accurate display rendering to eliminate visual artifacts on Apple silicon.

  • Mouse-Click-as-Touch: Translates pointer actions into touch input for interacting with touchscreen devices.

  • Custom Geometry Options: Adds the -g flag to graphic_console_init for display configuration.

  • BGRA 像素格式支持:实现精确的显示渲染,消除 Apple Silicon 上的视觉伪影。

  • 鼠标点击模拟触摸:将指针操作转换为触摸输入,以便与触摸屏设备进行交互。

  • 自定义几何选项:为 graphic_console_init 添加了 -g 标志,用于显示配置。

We also enabled the unstable Rutabaga structs needed to match the Rust-side graphics ABI in gfxstream/rutabaga configurations.

我们还启用了在 gfxstream/rutabaga 配置中匹配 Rust 端图形 ABI 所需的不稳定 Rutabaga 结构。

Building and Signing on Apple Silicon

在 Apple Silicon 上进行构建与签名

Finally, there was the build and packaging work: initial Darwin build support, building QEMU for arm64 on macOS, and toolchain fixes. The macOS builds also needed the JIT entitlement in addition to support for code signing and notarization. On the Cuttlefish side, we added macOS host support so the framework’s orchestration builds and runs on a Mac instead of assuming a Linux host.

最后是构建和打包工作:初步的 Darwin 构建支持、在 macOS 上为 arm64 构建 QEMU 以及工具链修复。macOS 构建除了需要支持代码签名和公证外,还需要 JIT 授权。在 Cuttlefish 方面,我们添加了 macOS 主机支持,使框架的编排能够在 Mac 上构建和运行,而不再假设必须使用 Linux 主机。

The Benefits

收益

By moving to a unified platform, we cut maintenance requirements, reduced infrastructure needs, eliminated redundant tooling, enhanced fidelity, and ensured developers run the exact same Cuttlefish virtual devices used across CI. A single platform also made it easier to support capabilities on a shared stack, including:

通过转向统一平台,我们减少了维护需求,降低了基础设施需求,消除了冗余工具,提高了保真度,并确保开发人员运行的 Cuttlefish 虚拟设备与 CI(持续集成)中使用的完全一致。单一平台也使得在共享软件栈上支持各项功能变得更加容易,包括:

  • Bluetooth pairing and connectivity.

  • Wi-Fi.

  • Cellular.

  • 3D graphics.

  • Over-the-air (OTA) update testing to exercise over-the-air update flows against a virtual device.

  • Fastboot.

  • Snapshotting to save and restore device state in CI and development flows.

  • 蓝牙配对与连接。

  • Wi-Fi。

  • 蜂窝网络。

  • 3D 图形。

  • OTA 更新测试,用于在虚拟设备上执行空中下载更新流程。

  • Fastboot。

  • 快照功能,用于在 CI 和开发流程中保存及恢复设备状态。

Beyond supporting Reality Labs…

除了支持 Reality Labs……