自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
Cuttlefish:快照和恢复
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Android 15 介绍了如何拍摄和恢复 Cuttlefish 虚拟设备的快照。拍摄 Cuttlefish 设备的快照后,您就可以将设备状态保存在磁盘上的映像中。然后,您可以恢复快照,将 Cuttlefish 设备恢复到之前保存的状态。
您可以在各种自动或手动工作流中使用快照。执行修改设备的程序时,您可以拍摄设备快照,以确保可以将设备恢复到指定状态。例如,在运行会修改设备状态并可能导致后续测试出现问题或不稳定性状况的测试套件时,您可以拍摄快照,以便在测试后将设备恢复到已保存的状态,从而确保后续测试能够顺利运行。
在测试应用的行为时,快照也非常有用。在根据一组操作测试应用的行为时,您可以在应用运行期间的各操作之间拍摄快照,以便您恢复到该快照,而无需从头开始。例如,如果启动游戏需要很长时间,您可以在显示主菜单后拍摄快照,以便将设备恢复到该状态,从而跳过启动时间。
拍摄 Cuttlefish 设备的快照
在拍摄设备快照时,必须暂停 Cuttlefish 设备,以确保 Cuttlefish 设备处于稳定状态。当设备暂停时,所有 vCPU 和设备都会暂停,所有缓冲区都会将其状态推送到虚拟机。然后,快照会将 vCPU 状态、内存和设备状态保存到磁盘的指定目标文件夹中。
VirtioFS 不受支持,因此必须在拍摄快照时将其停用。如需停用 VirtioFS,请在运行 cvd create
或 cvd start
时传递参数 --enable_virtiofs=false
。
快照仅支持 SwiftShader (guest_swiftshader
) GPU 模式。不支持其他加速图形模式。
以下步骤介绍了启动 Cuttlefish 设备和拍摄快照的过程。
启动设备,同时停用 VirtioFS。(然后,您即可使用该设备。)
cvd create --enable_virtiofs=false --gpu_mode=guest_swiftshader
通过运行 cvd snapshot_take
并使用以下标志来拍摄快照:
--force
:如果指定的快照路径中存在文件夹,此标志可确保删除现有文件夹,并在包含快照的快照路径中创建新文件夹
--auto_suspend
:在拍摄快照之前暂停设备,并在拍摄快照后恢复设备。
--snapshot_path
:使用快照创建的新文件夹的指定路径。
cvd snapshot_take --force --auto_suspend \
--snapshot_path=PATH
恢复 Cuttlefish 设备
恢复 Cuttlefish 设备快照时,必须停止拍摄快照的 Cuttlefish 实例。如果实例已停止,则无需执行进一步操作,并且可以恢复快照。
如需恢复 Cuttlefish 设备的快照,请使用 cvd create
启动设备并添加快照路径。如果在上面拍摄快照的 Cuttlefish 设备的基本实例编号与当前 Cuttlefish 实例的基本实例编号不同,请使用 --base_instance_num
标志传递该基本实例编号。
cvd create --snapshot_path=PATH \
--base_instance_num=ID
暂停 Cuttlefish 设备
您可以暂停 Cuttlefish 设备,而无需拍摄快照(系统不会使用磁盘空间来保存状态)。如需暂停 Cuttlefish 设备,请运行:
cvd suspend
恢复 Cuttlefish 设备
如需恢复暂停的 Cuttlefish 设备,请运行:
cvd resume
验证快照/恢复功能
您可以通过运行以下测试来验证快照拍摄/恢复功能:
atest SnapshotTest
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-03-26。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-03-26。"],[],[],null,["# Cuttlefish: Snapshot and restore\n\nAndroid 15 introduces how to take a snapshot and\nrestore a snapshot of a Cuttlefish virtual device. Taking a snapshot of a\nCuttlefish device lets you save the state of the device in an image on disk.\nYou can then restore the snapshot to bring up a Cuttlefish device to the\npreviously saved state.\n\nYou can use snapshots in various automated or manual workflows. When\nperforming a procedure that modifies the device, you can take a snapshot of\nthe device to ensure you can restore the device back to a specified state.\nFor example, when running test suites that modify the state of the device\nand can potentially cause issues or instabilities in the following tests,\nyou can take a snapshot so you can restore the device to a saved state after\na test, ensuring that subsequent tests run cleanly.\n\nAnother example workflow where snapshots are useful is when testing the\nbehavior of an app. When testing the behavior of an app based on a set of\nactions, you can take a snapshot in between actions as the app is running\nto let you restore to that snapshot without having to restart from the\nbeginning. For example, if launching a game has a long bringup time, you\ncan take a snapshot after reaching the main menu so you can restore the\ndevice to that state, skipping the bringup time.\n\nTake snapshot of a Cuttlefish device\n------------------------------------\n\n| **Note:** Snapshots are only supported on x86_64 platforms.\n\nWhen taking a snapshot of a device, the Cuttlefish device must be suspended\nto make sure the Cuttlefish device is in a stable state. When the device\nis suspended, all vCPUs and devices are stopped, and all buffers push\ntheir state to the VM. The snapshot then saves the vCPU state, memory,\nand device state to disk at a specified destination folder.\n\nVirtiosFS isn't supported and must be disabled when taking a snapshot. To\ndisable VirtioFS, pass the argument\n`--enable_virtiofs=false` when running `cvd create` or `cvd start`.\n\nOnly the SwiftShader (`guest_swiftshader`) GPU mode is supported for\nsnapshots. Other [accelerated graphics modes](/docs/devices/cuttlefish/gpu)\naren't supported.\n\nThe following steps describes the process of launching a Cuttlefish device\nand taking a snapshot.\n\n1. Launch a device, while disabling VirtioFS. (You can then use the device.)\n\n cvd create --enable_virtiofs=false --gpu_mode=guest_swiftshader\n\n2. Take a snapshot by running `cvd snapshot_take` with the following flags:\n\n - `--force`: If a folder exists at the specified snapshot path, this\n flag ensures that the existing folder is deleted, and a new folder\n is created at the snapshot path containing the snapshot\n\n - `--auto_suspend`: Suspends the device before the snapshot is taken and\n resumes the device after the snapshot is taken.\n\n - `--snapshot_path`: The specified path where a new folder is created\n with the snapshot.\n\n cvd snapshot_take --force --auto_suspend \\\n --snapshot_path=\u003cvar translate=\"no\"\u003ePATH\u003c/var\u003e\n\nRestore a Cuttlefish device\n---------------------------\n\nWhen restoring a Cuttlefish device snapshot, the Cuttlefish instance for\nwhich the snapshot was taken must be stopped. If the instance is already\nstopped, no further action is required and the snapshot can be restored.\n\nTo restore a snapshot of a Cuttlefish device, launch a device with `cvd create`\nand include the snapshot path. If the base instance number of the Cuttlefish\ndevice on which the snapshot was taken is different from the base instance\nnumber of the current Cuttlefish instance, pass that base instance\nnumber using the `--base_instance_num` flag. \n\n cvd create --snapshot_path=\u003cvar translate=\"no\"\u003ePATH\u003c/var\u003e \\\n --base_instance_num=\u003cvar translate=\"no\"\u003eID\u003c/var\u003e\n\nSuspend a Cuttlefish device\n---------------------------\n\nYou can suspend a Cuttlefish device without taking a snapshot (no disk space\nis used for saving the state). To suspend a Cuttlefish device, run: \n\n cvd suspend\n\nResume a Cuttlefish device\n--------------------------\n\nTo resume a suspended Cuttlefish device, run: \n\n cvd resume\n\nValidate the Snapshot/Restore feature\n-------------------------------------\n\nThe Snapshot/Restore feature can be validated by running the following test: \n\n atest SnapshotTest"]]