自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
Cuttlefish:快照和還原
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Android 15 介紹了如何擷取 Cuttlefish 虛擬裝置的快照,以及還原快照。擷取 Cuttlefish 裝置的快照,即可將裝置狀態儲存在磁碟上的圖片中。接著,您可以還原快照,讓 Cuttlefish 裝置恢復先前儲存的狀態。
您可以在各種自動或手動工作流程中使用快照。執行修改裝置的程序時,您可以為裝置拍攝快照,確保裝置可還原至指定狀態。舉例來說,如果執行的測試套件會修改裝置狀態,且可能導致後續測試出現問題或不穩定,您可以拍攝快照,以便在測試後將裝置還原至已儲存的狀態,確保後續測試能順利執行。
另一個快照實用的例子是測試應用程式行為。在根據一組動作測試應用程式行為時,您可以在應用程式執行期間在動作之間擷取快照,以便還原該快照,而無須從頭重新啟動。舉例來說,如果啟動遊戲需要很長的時間,您可以在進入主選單後拍攝快照,以便將裝置還原至該狀態,跳過啟動時間。
拍攝 Cuttlefish 裝置的快照
擷取裝置快照時,必須暫停 Cuttlefish 裝置,確保 Cuttlefish 裝置處於穩定狀態。裝置暫停時,所有 vCPU 和裝置都會停止,所有緩衝區也會將狀態推送至 VM。然後快照會將 vCPU 狀態、記憶體和裝置狀態儲存至指定目標資料夾的磁碟。
系統不支援 VirtiosFS,因此在擷取快照時必須停用此功能。如要停用 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 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","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"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],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"]]