自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
VirtualizationService
透過集合功能整理內容 你可以依據偏好儲存及分類內容。
VirtualizationService
會管理在 Android 系統上執行的多個訪客 VM (無論是否受保護),主要透過管理 crosvm 的例項。VirtualizationService
會公開 AIDL API,系統服務或應用程式可使用該 API 啟動、監控及停止 VM。如要使用 VirtualizationService
,請直接執行 virtmgr
,或匯入 javalib 或 rustlib,以便執行 virtmgr
做為子程序。
VM 生命週期
IVirtualMachine
物件會追蹤 VM 的存取情況。只要至少有一個 IVirtualMachine
物件參照,VM 就會繼續執行 (除非 VM 自行當機或關機)。如果在 VM 關閉前,所有對 IVirtualMachine
物件的參照都已捨棄,VirtualizationService
就會自動關閉 VM。這個程序表示,如果啟動 VM 的用戶端遭到低記憶體殺手關閉,VM 也會一併關閉,藉此防止資源耗盡。
每個 VM 都由專屬的 crosvm 執行個體管理,而 VirtualizationService
會代表用戶端進行管理。virtmgr
中的 VirtualizationService
會視需要使用已分配的全球資源啟動這些 crosvm 子程序,包括 virtualizationservice
中 VirtualizationServiceInternal
授予的 CID,並將 VM 所需的映像檔檔案描述項傳遞給這些子程序。VirtualizationService
接著監控子程序的結束時間,以便通知任何剩餘的用戶端。
VM 封裝
crosvm 支援兩種不同的 VM 啟動方式:提供核心和 initrd,或提供 bootloader。無論是哪種情況,您都可以提供任意數量的磁碟映像檔,這些映像檔可能是原始映像檔,也可能是多個分區的合成映像檔。用戶端會提供各種圖片做為檔案描述符。
VirtualizationService
會視需求建構複合磁碟映像檔。這項程序是必要的,因為複合磁碟檔案會在內部參照組成磁碟的各種分區映像檔,這些檔案是由用戶端傳遞,可能無法直接由 crosvm 存取。為解決這個問題,VirtualizationService
會確保 crosvm 繼承的檔案描述元資料號碼與 VirtualizationService
用於建立合成圖片的檔案描述元資料號碼相同。複合磁碟映像檔會使用 /proc/self/fd/N
格式的檔案名稱來代表每個分區檔案。
針對 Microdroid pVM,AVF 包含一個引導程式,可按照標準 Android 驗證啟動流程,從複合磁碟映像檔的分區載入核心。
VM Sockets (vsock)
pVM 之間通訊的主要介面是 vsock,這是標準的 virtio 網路介面。每個 VM 都會透過 32 位元內容 ID (CID) 進行識別,這類似於 IP 位址,VirtualizationServiceInternal
會在 VirtualizationService
建立 VM 時將其指派給 VM,並可在 VM 選擇的任何通訊埠編號上公開服務。在 VM 執行期間,CID 是唯一的,但在終止 VM 並放棄所有 VM 的 IVirtualMachine
繫結器句柄時,可以回收 CID 值。
偵錯介面
vm
指令可用於偵錯。這個指令可讓開發人員從 Shell 啟動 VM、查看記錄,以及終止 VM。使用 vm
指令或 AVF 提供的其他介面,VM 可在可偵錯 (FULL) 或不可偵錯 (NONE) 模式下啟動。透過可偵錯的 VM,您可以查看 OS 層級記錄、存取 ADB shell,以及擷取當機傾印或應用程式酬載。建議您在正式環境中使用無法偵錯的 VM。如要進一步瞭解 AVF 提供的指令列工具和其他偵錯介面,請參閱 debug/README.md。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[null,null,["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# VirtualizationService\n\n`VirtualizationService` manages multiple guest VMs, protected or otherwise,\nrunning on an Android system, primarily by managing instances of crosvm.\n`VirtualizationService` exposes an AIDL API, which system services or apps can\nuse to start, monitor, and stop VMs. To use `VirtualizationService`, execute\n`virtmgr` directly or import [javalib](https://cs.android.com/android/platform/superproject/+/main:packages/modules/Virtualization/javalib/ \"javalib\") or [rustlib](https://cs.android.com/android/platform/superproject/+/main:packages/modules/Virtualization/vmclient/ \"rustlib\") which executes `virtmgr` as\na child process.\n\nVM lifecycle\n------------\n\nAccess to a VM is tracked by the `IVirtualMachine` object. As long as there's\nat least one reference to `IVirtualMachine` object then the VM continues to\nrun (unless it crashes or shuts down of its own accord). If all references to\nthe `IVirtualMachine` object are dropped before the VM shuts down, then\n`VirtualizationService` automatically shuts down the VM. This process implies\nthat if the client that started the VM is shut down by the low memory killer,\nthen the VM is also shut down, thus preventing resource leaks.\n\nEach VM is managed by its own instance of crosvm, which `VirtualizationService`\nin turn manages on behalf of the client. `VirtualizationService` in `virtmgr`\nstarts these crosvm child processes as required with allocated global resources\nincluding the CID granted by `VirtualizationServiceInternal` in\n`virtualizationservice`, and passes them the file descriptors for the images the\nVM needs. `VirtualizationService` then monitors the child process for when they\ndie, so it can notify any remaining clients accordingly.\n\nVM packaging\n------------\n\ncrosvm supports two different ways of booting a VM: either a kernel and initrd\nare provided or a bootloader is provided. In either case, an\narbitrary number of disk images can also be provided, which might be either\na raw image or a composite of several partitions. The various images are\nprovided by the client as file descriptors.\n\n`VirtualizationService` builds composite disk images on demand. This process is\nnecessary because the composite disk file refers internally to the various\npartition image files composing the disk, which are passed by the client and\nmight not be directly accessible by crosvm. To get around this issue,\n`VirtualizationService` ensures that the file descriptor numbers inherited by\ncrosvm are the same as the file descriptor numbers which `VirtualizationService`\nused in creating the composite images. The composite disk image uses filenames\nin the form to `/proc/self/fd/N` to represent each partition file.\n\nFor Microdroid pVMs, AVF includes a bootloader, which loads the kernel from\na partition of a composite disk image, following the standard Android\nVerified Boot flow.\n\nVM Sockets (vsock)\n------------------\n\nThe primary interface for communication between pVMs is vsock, a standard\nvirtio socket interface. Each VM is identified by a 32-bit context identifier\n(CID), which is analogous to an IP address, which\n`VirtualizationServiceInternal` assigns to the VM when `VirtualizationService`\ncreates the VM, and can expose services on whatever port numbers the VM chooses.\nThe CID is unique while the VM is running, but the CID value can be recycled\nwhen the VM is terminated and all the `IVirtualMachine` binder handles to the VM\nhave been dropped.\n\nDebug interface\n---------------\n\nThe `vm` command is provided for debug purposes. This command lets a developer\nstart a VM from the shell, view its logs, and terminate the VM. With the `vm`\ncommand or other interfaces provided by AVF, a VM can start in either\ndebuggable (FULL) or non-debuggable (NONE) mode. With a debuggable VM, you can\nsee OS-level logs, access the ADB shell, and capture crash-dump or app payload.\nIt's recommended to use a non-debuggable VM in production. For more on\nthe command line tool and other debug interfaces that AVF provides, see\n[debug/README.md](https://cs.android.com/android/platform/superproject/+/android-latest-release:packages/modules/Virtualization/docs/debug/README.md \"Debugging protected VMs\")."]]