• Home
Name Date Size #Lines LOC

..--

init_debug_policy/03-May-2024-7446

initrd/03-May-2024-345273

kdump/03-May-2024-258180

kernel/03-May-2024-107,835107,802

payload/03-May-2024-820568

Android.bpD03-May-202411.7 KiB464419

README.mdD03-May-20246.7 KiB233176

bootconfig.arm64D03-May-202437 21

bootconfig.commonD03-May-202495 43

bootconfig.debuggableD03-May-2024374 129

bootconfig.normalD03-May-2024209 107

bootconfig.x86_64D03-May-202499 21

build.propD03-May-2024197 86

dummy_dtb.imgD03-May-202411 21

fstab.microdroidD03-May-2024194 32

init.rcD03-May-20245.6 KiB169133

linker.config.jsonD03-May-2024421 2120

microdroid.jsonD03-May-2024445 2120

microdroid_event-log-tagsD03-May-20242.5 KiB5251

microdroid_manifest.xmlD03-May-2024112 53

microdroid_vendor_compatibility_matrix.xmlD03-May-2024133 53

microdroid_vendor_manifest.xmlD03-May-202480 32

uboot-env.txtD03-May-202445 21

ueventd.rcD03-May-20241.2 KiB3527

README.md

1# Microdroid
2
3Microdroid is a (very) lightweight version of Android that is intended to run on
4on-device virtual machines. It is built from the same source code as the regular
5Android, but it is much smaller; no system server, no HALs, no GUI, etc. It is
6intended to host headless & native workloads only.
7
8## Prerequisites
9
10Any 64-bit target (either x86\_64 or arm64) is supported. 32-bit target is not
11supported. Note that we currently don't support user builds; only userdebug
12builds are supported.
13
14The only remaining requirement is that `com.android.virt` APEX has to be
15pre-installed. To do this, add the following line in your product makefile.
16
17```make
18$(call inherit-product, packages/modules/Virtualization/apex/product_packages.mk)
19```
20
21Build the target after adding the line, and flash it. This step needs to be done
22only once for the target.
23
24If you are using `aosp_oriole` (Pixel 6) or `aosp_cf_x86_64_phone` (Cuttlefish),
25adding above line is not necessary as it's already done.
26
27## Building and installing microdroid
28
29Microdroid is part of the `com.android.virt` APEX. To build it and install to
30the device:
31
32```sh
33banchan com.android.virt aosp_arm64
34UNBUNDLED_BUILD_SDKS_FROM_SOURCE=true m apps_only dist
35adb install out/dist/com.android.virt.apex
36adb reboot
37```
38
39If your target is x86\_64 (e.g. `aosp_cf_x86_64_phone`), replace `aosp_arm64`
40with `aosp_x86_64`.
41
42## Building an app
43
44An app in microdroid is a shared library file embedded in an APK. The shared
45library should have an entry point `AVmPayload_main` as shown below:
46
47```C++
48extern "C" int AVmPayload_main() {
49  printf("Hello Microdroid!\n");
50}
51```
52
53Then build it as a shared library:
54
55```
56cc_library_shared {
57  name: "MyMicrodroidApp",
58  srcs: ["**/*.cpp"],
59  sdk_version: "current",
60}
61```
62
63Then you need a configuration file in JSON format that defines what to load and
64execute in microdroid. The name of the file can be anything and you may have
65multiple configuration files if needed.
66
67```json
68{
69  "os": { "name": "microdroid" },
70  "task": {
71    "type": "microdroid_launcher",
72    "command": "MyMicrodroidApp.so"
73  }
74}
75```
76
77The value of `task.command` should match with the name of the shared library
78defined above. If your app requires APEXes to be imported, you can declare the
79list in `apexes` key like following.
80
81```json
82{
83  "os": ...,
84  "task": ...,
85  "apexes": [
86    {"name": "com.android.awesome_apex"}
87  ]
88}
89```
90
91Embed the shared library and the VM configuration file in an APK:
92
93```
94android_app {
95  name: "MyApp",
96  srcs: ["**/*.java"], // if there is any java code
97  jni_libs: ["MyMicrodroidApp"],
98  use_embedded_native_libs: true,
99  sdk_version: "current",
100}
101
102// The VM configuration file can be embedded by simply placing it at `./assets`
103// directory.
104```
105
106Finally, you build the APK.
107
108```sh
109TARGET_BUILD_APPS=MyApp m apps_only dist
110```
111
112## Running the app on microdroid
113
114First of all, install the APK to the target device.
115
116```sh
117adb install out/dist/MyApp.apk
118```
119
120`ALL_CAP`s below are placeholders. They need to be replaced with correct
121values:
122
123* `VM_CONFIG_FILE`: the name of the VM config file that you embedded in the APK.
124  (e.g. `vm_config.json`)
125* `PACKAGE_NAME_OF_YOUR_APP`: package name of your app (e.g. `com.acme.app`).
126* `PATH_TO_YOUR_APP`: path to the installed APK on the device. Can be obtained
127  via the following command.
128  ```sh
129  adb shell pm path PACKAGE_NAME_OF_YOUR_APP
130  ```
131  It shall report a cryptic path similar to `/data/app/~~OgZq==/com.acme.app-HudMahQ==/base.apk`.
132
133Execute the following commands to launch a VM. The VM will boot to microdroid
134and then automatically execute your app (the shared library
135`MyMicrodroidApp.so`).
136
137```sh
138TEST_ROOT=/data/local/tmp/virt
139adb shell /apex/com.android.virt/bin/vm run-app \
140--log $TEST_ROOT/log.txt \
141--console $TEST_ROOT/console.txt \
142PATH_TO_YOUR_APP \
143$TEST_ROOT/MyApp.apk.idsig \
144$TEST_ROOT/instance.img \
145--config-path assets/VM_CONFIG_FILE
146```
147
148The last command lets you know the CID assigned to the VM. The console output
149from the VM is stored to `$TEST_ROOT/console.txt` and logcat is stored to
150`$TEST_ROOT/log.txt` file for debugging purpose. If you omit `--log` or
151`--console` option, they will be emitted to the current console.
152
153Stopping the VM can be done as follows:
154
155```sh
156adb shell /apex/com.android.virt/bin/vm stop $CID
157```
158
159, where `$CID` is the reported CID value. This works only when the `vm` was
160invoked with the `--daemonize` flag. If the flag was not used, press Ctrl+C on
161the console where the `vm run-app` command was invoked.
162
163## Debuggable microdroid
164
165### Debugging features
166Microdroid supports following debugging features:
167
168- VM log
169- console output
170- kernel output
171- logcat output
172- [ramdump](../docs/debug/ramdump.md)
173- crashdump
174- [adb](#adb)
175- [gdb](#debugging-the-payload-on-microdroid)
176
177### Enabling debugging features
178There's two ways to enable the debugging features:
179
180#### Option 1) Running microdroid on AVF debug policy configured device
181
182microdroid can be started with debugging features by debug policies from the
183host. Host bootloader may provide debug policies to host OS's device tree for
184VMs. Host bootloader MUST NOT provide debug policies for locked devices for
185security reasons.
186
187For protected VM, such device tree will be available in microdroid. microdroid
188can check which debuging features is enabled.
189
190Here are list of device tree properties for debugging features.
191
192- `/avf/guest/common/log`: `<1>` to enable kernel log and logcat. Ignored
193  otherwise.
194- `/avf/guest/common/ramdump`: `<1>` to enable ramdump. Ignored otherwise.
195- `/avf/guest/microdroid/adb`: `<1>` to enable `adb`. Ignored otherwise.
196
197#### Option 2) Lauching microdroid with debug level.
198
199microdroid can be started with debugging features. To do so, first, delete
200`$TEST_ROOT/instance.img`; this is because changing debug settings requires a
201new instance. Then add the `--debug=full` flag to the
202`/apex/com.android.virt/bin/vm run-app` command. This will enable all debugging
203features.
204
205### ADB
206
207If `adb` connection is enabled, launch following command.
208
209```sh
210vm_shell
211```
212
213Done. Now you are logged into Microdroid. Have fun!
214
215Once you have an adb connection with `vm_shell`, `localhost:8000` will be the
216serial of microdroid.
217
218### Debugging the payload on microdroid
219
220Like a normal adb device, you can debug native processes using `lldbclient.py`
221script, either by running a new process, or attaching to an existing process.
222Use `vm_shell` tool above, and then run `lldbclient.py`.
223
224```sh
225adb -s localhost:8000 shell 'mount -o remount,exec /data'
226development/scripts/lldbclient.py -s localhost:8000 --chroot . --user '' \
227    (-p PID | -n NAME | -r ...)
228```
229
230**Note:** We need to pass `--chroot .` to skip verifying device, because
231microdroid doesn't match with the host's lunch target. We need to also pass
232`--user ''` as there is no `su` binary in microdroid.
233