1# Early VM
2
3Early VMs are specialized virtual machines that can run even before the `/data`
4partition is mounted, unlike regular VMs. `early_virtmgr` is a binary that
5serves as the interface for early VMs, functioning similarly to `virtmgr`,
6which provides the [`IVirtualizationService`](../android/virtualizationservice/aidl/android/system/virtualizationservice/IVirtualizationService.aidl)
7aidl interface.
8
9To run an early VM, clients must follow these steps.
10
111) Early VMs must be defined in XML files located at
12`{partition}/etc/avf/early_vms*.xml`. Schema for these files is defined in
13[`early_vms.xsd`](../android/virtmgr/early_vms.xsd).
14
15```early_vms.xml
16<early_vms>
17    <early_vm>
18        <name>vm_demo_native_early</name>
19        <cid>123</cid>
20        <path>/system/bin/vm_demo_native_early</path>
21    </early_vm>
22</early_vms>
23```
24
25In this example, the binary `/system/bin/vm_demo_native_early` can establish a
26connection with `early_virtmgr` and create a VM named `vm_demo_native_early`,
27which will be assigned the static CID 123.
28
29Multiple XML files matching the glob pattern
30`{partition}/etc/avf/early_vms*.xml` can be used to define early VMs.
31
322) The client must have the following three or four capabilities.
33
34* `IPC_LOCK`
35* `NET_BIND_SERVICE`
36* `SYS_NICE` (required if `RELEASE_AVF_ENABLE_VIRT_CPUFREQ` is enabled)
37* `SYS_RESOURCES`
38
39Typically, the client is defined as a service in an init script, where
40capabilities can also be specified.
41
42```vm_demo_native_early.rc
43service vm_demo_native_early /system/bin/vm_demo_native_early
44    user system
45    group system virtualmachine
46    capabilities IPC_LOCK NET_BIND_SERVICE SYS_RESOURCE SYS_NICE
47    oneshot
48    stdio_to_kmsg
49    class early_hal
50```
51
523) The client forks `early_virtmgr` instead of `virtmgr`.
53
54The remaining steps are identical to those for regular VMs: connect to
55`early_virtmgr`, obtain the `IVirtualizationService` interface, then create and
56run the VM.
57