blob: 25eb909a95c75d1b2ac094639a13c23e2fa54db8 [file] [log] [blame] [view]
David Brazdildd565102020-10-23 16:33:30 +00001# Getting started with Protected Virtual Machines
2
3## Prepare a device
4
Jiyong Park10b354a2021-11-17 17:46:53 +09005First you will need a device that is capable of running virtual machines. On arm64, this means a
6device which boots the kernel in EL2 and the kernel was built with KVM enabled. Unfortunately at the
7moment, we don't have an arm64 device in AOSP which does that. Instead, use cuttlefish which
8provides the same functionalities except that the virtual machines are not protected from the host
9(i.e. Android). This however should be enough for functional testing.
David Brazdildd565102020-10-23 16:33:30 +000010
Jiyong Park10b354a2021-11-17 17:46:53 +090011We support the following device:
David Brazdildd565102020-10-23 16:33:30 +000012
Jiyong Park10b354a2021-11-17 17:46:53 +090013* aosp_cf_x86_64_phone (Cuttlefish a.k.a. Cloud Android)
Jiyong Park60c8aef2022-06-10 15:51:22 +090014* oriole (Pixel 6)
15* raven (Pixel 6 Pro)
16
17### Cuttlefish
Jiyong Park10b354a2021-11-17 17:46:53 +090018
19Building Cuttlefish
20
21```shell
22source build/envsetup.sh
23lunch aosp_cf_x86_64_phone-userdebug
24m
25```
26
27Run Cuttlefish locally by
28
29```shell
30acloud create --local-instance --local-image
31```
32
Jiyong Park60c8aef2022-06-10 15:51:22 +090033### Pixel 6 and 6 Pro
34
35If the device is running Android 12, join the [Android Beta
Pierre-Clément Tosic1c69832022-10-14 18:53:31 +010036Program](https://www.google.com/android/beta) to upgrade to Android 13 Beta.
Jiyong Park60c8aef2022-06-10 15:51:22 +090037
38Once upgraded to Android 13, execute the following command to enable pKVM.
39
40```shell
41adb reboot bootloader
Jiyong Parka3953d12022-06-28 20:44:23 +090042fastboot flashing unlock
Jiyong Park60c8aef2022-06-10 15:51:22 +090043fastboot oem pkvm enable
44fastboot reboot
45```
46
Pierre-Clément Tosi072c11f2022-06-10 15:09:09 +010047Due to a bug in Android 13 for these devices, pKVM may stop working after an
48[OTA update](https://source.android.com/devices/tech/ota). To prevent this, it
Jiyong Parke21a24b2022-06-29 09:20:23 +090049is necessary to manually replicate the `pvmfw` partition to the other slot:
Pierre-Clément Tosi072c11f2022-06-10 15:09:09 +010050
51```shell
Jiyong Parke21a24b2022-06-29 09:20:23 +090052git -C <android_root>/packages/modules/Virtualization
53show de6b0b2ecf6225a0a7b43241de27e74fc3e6ceb2:pvmfw/pvmfw.img > /tmp/pvmfw.img
Pierre-Clément Tosi072c11f2022-06-10 15:09:09 +010054adb reboot bootloader
Jiyong Parke21a24b2022-06-29 09:20:23 +090055fastboot --slot other flash pvmfw /tmp/pvmfw.img
Pierre-Clément Tosi072c11f2022-06-10 15:09:09 +010056fastboot reboot
57```
58
59Otherwise, if an OTA has already made pKVM unusable, the working partition
Jiyong Parke21a24b2022-06-29 09:20:23 +090060should be copied to the "current" slot:
Pierre-Clément Tosi072c11f2022-06-10 15:09:09 +010061
62```shell
Pierre-Clément Tosi072c11f2022-06-10 15:09:09 +010063adb reboot bootloader
Jiyong Parke21a24b2022-06-29 09:20:23 +090064fastboot flash pvmfw /tmp/pvmfw.img
Pierre-Clément Tosi072c11f2022-06-10 15:09:09 +010065fastboot reboot
66```
67
68Finally, if the `pvmfw` partition has been corrupted, both slots may be flashed
Pierre-Clément Tosi39bce672022-10-14 18:54:04 +010069using the [`pvmfw.img` pre-built](https://android.googlesource.com/platform/packages/modules/Virtualization/+/08deac98acefd62e222edfa374d5292458cf97eb%5E/pvmfw/pvmfw.img)
Pierre-Clément Tosi072c11f2022-06-10 15:09:09 +010070as long as the bootloader remains unlocked. Otherwise, a fresh install of
71Android 13 followed by the manual steps above for flashing the `other` slot
72should be used as a last resort.
73
Pierre-Clément Tosi4e0fed72022-10-14 19:01:56 +010074Starting in Android 14, `pvmfw.img` can be built using the Android Build system:
75```
76lunch <target> # where PRODUCT_BUILD_PVMFW_IMAGE=true
77m pvmfwimage # partition image under ${ANDROID_PRODUCT_OUT}/pvmfw.img
78```
79Note that the result is not intended to be used in Android 13 as not
80backward-compatibility is guaranteed.
81
Jiyong Park10b354a2021-11-17 17:46:53 +090082## Running demo app
83
84The instruction is [here](../../demo/README.md).
David Brazdildd565102020-10-23 16:33:30 +000085
86## Running tests
87
Jiyong Park10b354a2021-11-17 17:46:53 +090088There are various tests that spawn guest VMs and check different aspects of the architecture. They
89all can run via `atest`.
Andrew Walbran0479a652021-04-12 11:17:13 +000090
91```shell
Jiyong Park29de5172022-02-08 00:37:05 +090092atest VirtualizationTestCases.64
Kalesh Singhb5070982021-12-14 23:21:39 -080093atest MicrodroidHostTestCases
94atest MicrodroidTestApp
David Brazdildd565102020-10-23 16:33:30 +000095```
96
97If you run into problems, inspect the logs produced by `atest`. Their location is printed at the
98end. The `host_log_*.zip` file should contain the output of individual commands as well as VM logs.
99
Jiyong Park10b354a2021-11-17 17:46:53 +0900100## Spawning your own VMs with custom kernel
David Brazdildd565102020-10-23 16:33:30 +0000101
Jiyong Park10b354a2021-11-17 17:46:53 +0900102You can spawn your own VMs by passing a JSON config file to the VirtualizationService via the `vm`
103tool on a rooted KVM-enabled device. If your device is attached over ADB, you can run:
Andrew Walbran0479a652021-04-12 11:17:13 +0000104
105```shell
Jiyong Park10b354a2021-11-17 17:46:53 +0900106cat > vm_config.json
Andrew Walbran0479a652021-04-12 11:17:13 +0000107{
108 "kernel": "/data/local/tmp/kernel",
109 "initrd": "/data/local/tmp/ramdisk",
110 "params": "rdinit=/bin/init"
111}
Jiyong Park10b354a2021-11-17 17:46:53 +0900112adb root
113adb push <kernel> /data/local/tmp/kernel
114adb push <ramdisk> /data/local/tmp/ramdisk
115adb push vm_config.json /data/local/tmp/vm_config.json
116adb shell "start virtualizationservice"
117adb shell "/apex/com.android.virt/bin/vm run /data/local/tmp/vm_config.json"
David Brazdildd565102020-10-23 16:33:30 +0000118```
119
Andrew Walbran0479a652021-04-12 11:17:13 +0000120The `vm` command also has other subcommands for debugging; run `/apex/com.android.virt/bin/vm help`
121for details.
David Brazdildd565102020-10-23 16:33:30 +0000122
Jaewan Kim46422812022-11-25 10:59:39 +0900123## Spawning your own VMs with custom pvmfw
124
125Set system property `hypervisor.pvmfw.path` to custom `pvmfw` on the device before using `vm` tool.
126`virtualizationservice` will pass the specified `pvmfw` to `crosvm` for protected VMs.
127
128```shell
129adb push pvmfw.img /data/local/tmp/pvmfw.img
130adb root # required for setprop
131adb shell setprop hypervisor.pvmfw.path /data/local/tmp/pvmfw.img
132```
133
Jiyong Park10b354a2021-11-17 17:46:53 +0900134## Spawning your own VMs with Microdroid
David Brazdildd565102020-10-23 16:33:30 +0000135
Jiyong Park10b354a2021-11-17 17:46:53 +0900136[Microdroid](../../microdroid/README.md) is a lightweight version of Android that is intended to run
Nikita Ioffeb0b67562022-11-22 15:48:06 +0000137on pVM. You can run a Microdroid with empty payload using the following command:
138
139```shell
140adb shell /apex/com.android.virt/bin/vm run-microdroid --debug full
141```
142
143The `instance.img` and `apk.idsig` files will be stored in a subdirectory under
144`/data/local/tmp/microdroid`, that `vm` will create.
145
146Atlernatively, you can manually run the demo app on top of Microdroid as follows:
Andrew Walbran0479a652021-04-12 11:17:13 +0000147
148```shell
Nikita Ioffe519deab2022-09-30 17:36:02 +0100149UNBUNDLED_BUILD_SDKS_FROM_SOURCE=true TARGET_BUILD_APPS=MicrodroidDemoApp m apps_only dist
Jiyong Park10b354a2021-11-17 17:46:53 +0900150adb shell mkdir -p /data/local/tmp/virt
151adb push out/dist/MicrodroidDemoApp.apk /data/local/tmp/virt/
152adb shell /apex/com.android.virt/bin/vm run-app \
153 --debug full \
154 /data/local/tmp/virt/MicrodroidDemoApp.apk \
155 /data/local/tmp/virt/MicrodroidDemoApp.apk.idsig \
Inseob Kim7b5f65c2022-11-15 14:27:04 +0900156 /data/local/tmp/virt/instance.img \
157 --payload-path MicrodroidTestNativeLib.so
Jiyong Park978b1e32021-02-04 20:23:40 +0900158```
David Brazdildd565102020-10-23 16:33:30 +0000159
Jiyong Parkb5d04d82022-03-18 16:11:03 +0900160## Building and updating CrosVM and VirtualizationService {#building-and-updating}
Jiyong Park10b354a2021-11-17 17:46:53 +0900161
162You can update CrosVM and the VirtualizationService by updating the `com.android.virt` APEX instead
163of rebuilding the entire image.
Jiyong Park978b1e32021-02-04 20:23:40 +0900164
Andrew Walbran0479a652021-04-12 11:17:13 +0000165```shell
Jiyong Park10b354a2021-11-17 17:46:53 +0900166banchan com.android.virt aosp_arm64 // or aosp_x86_64 if the device is cuttlefish
Jiyong Park5cd312a2022-03-11 21:37:53 +0900167UNBUNDLED_BUILD_SDKS_FROM_SOURCE=true m apps_only dist
Jiyong Park10b354a2021-11-17 17:46:53 +0900168adb install out/dist/com.android.virt.apex
169adb reboot
David Brazdildd565102020-10-23 16:33:30 +0000170```
Jiyong Parkb5d04d82022-03-18 16:11:03 +0900171
Jiyong Parkb8e05b62022-06-24 16:40:35 +0900172## Building and updating kernel inside Microdroid
Jiyong Parkb5d04d82022-03-18 16:11:03 +0900173
Jiyong Park1dabd562022-11-04 15:33:18 +0900174The instruction is [here](../../microdroid/kernel/README.md).