David Brazdil | dd56510 | 2020-10-23 16:33:30 +0000 | [diff] [blame] | 1 | # Getting started with Protected Virtual Machines |
| 2 | |
| 3 | ## Prepare a device |
| 4 | |
Jiyong Park | 10b354a | 2021-11-17 17:46:53 +0900 | [diff] [blame] | 5 | First you will need a device that is capable of running virtual machines. On arm64, this means a |
| 6 | device which boots the kernel in EL2 and the kernel was built with KVM enabled. Unfortunately at the |
| 7 | moment, we don't have an arm64 device in AOSP which does that. Instead, use cuttlefish which |
| 8 | provides 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 Brazdil | dd56510 | 2020-10-23 16:33:30 +0000 | [diff] [blame] | 10 | |
Jiyong Park | 10b354a | 2021-11-17 17:46:53 +0900 | [diff] [blame] | 11 | We support the following device: |
David Brazdil | dd56510 | 2020-10-23 16:33:30 +0000 | [diff] [blame] | 12 | |
Jiyong Park | 10b354a | 2021-11-17 17:46:53 +0900 | [diff] [blame] | 13 | * aosp_cf_x86_64_phone (Cuttlefish a.k.a. Cloud Android) |
| 14 | |
| 15 | Building Cuttlefish |
| 16 | |
| 17 | ```shell |
| 18 | source build/envsetup.sh |
| 19 | lunch aosp_cf_x86_64_phone-userdebug |
| 20 | m |
| 21 | ``` |
| 22 | |
| 23 | Run Cuttlefish locally by |
| 24 | |
| 25 | ```shell |
| 26 | acloud create --local-instance --local-image |
| 27 | ``` |
| 28 | |
| 29 | ## Running demo app |
| 30 | |
| 31 | The instruction is [here](../../demo/README.md). |
David Brazdil | dd56510 | 2020-10-23 16:33:30 +0000 | [diff] [blame] | 32 | |
| 33 | ## Running tests |
| 34 | |
Jiyong Park | 10b354a | 2021-11-17 17:46:53 +0900 | [diff] [blame] | 35 | There are various tests that spawn guest VMs and check different aspects of the architecture. They |
| 36 | all can run via `atest`. |
Andrew Walbran | 0479a65 | 2021-04-12 11:17:13 +0000 | [diff] [blame] | 37 | |
| 38 | ```shell |
Jiyong Park | 29de517 | 2022-02-08 00:37:05 +0900 | [diff] [blame] | 39 | atest VirtualizationTestCases.64 |
Kalesh Singh | b507098 | 2021-12-14 23:21:39 -0800 | [diff] [blame] | 40 | atest MicrodroidHostTestCases |
| 41 | atest MicrodroidTestApp |
David Brazdil | dd56510 | 2020-10-23 16:33:30 +0000 | [diff] [blame] | 42 | ``` |
| 43 | |
| 44 | If you run into problems, inspect the logs produced by `atest`. Their location is printed at the |
| 45 | end. The `host_log_*.zip` file should contain the output of individual commands as well as VM logs. |
| 46 | |
Jiyong Park | 10b354a | 2021-11-17 17:46:53 +0900 | [diff] [blame] | 47 | ## Spawning your own VMs with custom kernel |
David Brazdil | dd56510 | 2020-10-23 16:33:30 +0000 | [diff] [blame] | 48 | |
Jiyong Park | 10b354a | 2021-11-17 17:46:53 +0900 | [diff] [blame] | 49 | You can spawn your own VMs by passing a JSON config file to the VirtualizationService via the `vm` |
| 50 | tool on a rooted KVM-enabled device. If your device is attached over ADB, you can run: |
Andrew Walbran | 0479a65 | 2021-04-12 11:17:13 +0000 | [diff] [blame] | 51 | |
| 52 | ```shell |
Jiyong Park | 10b354a | 2021-11-17 17:46:53 +0900 | [diff] [blame] | 53 | cat > vm_config.json |
Andrew Walbran | 0479a65 | 2021-04-12 11:17:13 +0000 | [diff] [blame] | 54 | { |
| 55 | "kernel": "/data/local/tmp/kernel", |
| 56 | "initrd": "/data/local/tmp/ramdisk", |
| 57 | "params": "rdinit=/bin/init" |
| 58 | } |
Jiyong Park | 10b354a | 2021-11-17 17:46:53 +0900 | [diff] [blame] | 59 | adb root |
| 60 | adb push <kernel> /data/local/tmp/kernel |
| 61 | adb push <ramdisk> /data/local/tmp/ramdisk |
| 62 | adb push vm_config.json /data/local/tmp/vm_config.json |
| 63 | adb shell "start virtualizationservice" |
| 64 | adb shell "/apex/com.android.virt/bin/vm run /data/local/tmp/vm_config.json" |
David Brazdil | dd56510 | 2020-10-23 16:33:30 +0000 | [diff] [blame] | 65 | ``` |
| 66 | |
Andrew Walbran | 0479a65 | 2021-04-12 11:17:13 +0000 | [diff] [blame] | 67 | The `vm` command also has other subcommands for debugging; run `/apex/com.android.virt/bin/vm help` |
| 68 | for details. |
David Brazdil | dd56510 | 2020-10-23 16:33:30 +0000 | [diff] [blame] | 69 | |
Jiyong Park | 10b354a | 2021-11-17 17:46:53 +0900 | [diff] [blame] | 70 | ## Spawning your own VMs with Microdroid |
David Brazdil | dd56510 | 2020-10-23 16:33:30 +0000 | [diff] [blame] | 71 | |
Jiyong Park | 10b354a | 2021-11-17 17:46:53 +0900 | [diff] [blame] | 72 | [Microdroid](../../microdroid/README.md) is a lightweight version of Android that is intended to run |
| 73 | on pVM. You can manually run the demo app on top of Microdroid as follows: |
Andrew Walbran | 0479a65 | 2021-04-12 11:17:13 +0000 | [diff] [blame] | 74 | |
| 75 | ```shell |
Jiyong Park | 10b354a | 2021-11-17 17:46:53 +0900 | [diff] [blame] | 76 | TARGET_BUILD_APPS=MicrodroidDemoApp m apps_only dist |
| 77 | adb shell mkdir -p /data/local/tmp/virt |
| 78 | adb push out/dist/MicrodroidDemoApp.apk /data/local/tmp/virt/ |
| 79 | adb shell /apex/com.android.virt/bin/vm run-app \ |
| 80 | --debug full \ |
| 81 | /data/local/tmp/virt/MicrodroidDemoApp.apk \ |
| 82 | /data/local/tmp/virt/MicrodroidDemoApp.apk.idsig \ |
| 83 | /data/local/tmp/virt/instance.img assets/vm_config.json |
Jiyong Park | 978b1e3 | 2021-02-04 20:23:40 +0900 | [diff] [blame] | 84 | ``` |
David Brazdil | dd56510 | 2020-10-23 16:33:30 +0000 | [diff] [blame] | 85 | |
Jiyong Park | b5d04d8 | 2022-03-18 16:11:03 +0900 | [diff] [blame] | 86 | ## Building and updating CrosVM and VirtualizationService {#building-and-updating} |
Jiyong Park | 10b354a | 2021-11-17 17:46:53 +0900 | [diff] [blame] | 87 | |
| 88 | You can update CrosVM and the VirtualizationService by updating the `com.android.virt` APEX instead |
| 89 | of rebuilding the entire image. |
Jiyong Park | 978b1e3 | 2021-02-04 20:23:40 +0900 | [diff] [blame] | 90 | |
Andrew Walbran | 0479a65 | 2021-04-12 11:17:13 +0000 | [diff] [blame] | 91 | ```shell |
Jiyong Park | 10b354a | 2021-11-17 17:46:53 +0900 | [diff] [blame] | 92 | banchan com.android.virt aosp_arm64 // or aosp_x86_64 if the device is cuttlefish |
Jiyong Park | 5cd312a | 2022-03-11 21:37:53 +0900 | [diff] [blame] | 93 | UNBUNDLED_BUILD_SDKS_FROM_SOURCE=true m apps_only dist |
Jiyong Park | 10b354a | 2021-11-17 17:46:53 +0900 | [diff] [blame] | 94 | adb install out/dist/com.android.virt.apex |
| 95 | adb reboot |
David Brazdil | dd56510 | 2020-10-23 16:33:30 +0000 | [diff] [blame] | 96 | ``` |
Jiyong Park | b5d04d8 | 2022-03-18 16:11:03 +0900 | [diff] [blame] | 97 | |
| 98 | ## Building and updating GKI inside Microdroid |
| 99 | |
| 100 | Checkout the Android common kernel and build it following the [official |
| 101 | guideline](https://source.android.com/setup/build/building-kernels). |
| 102 | |
| 103 | ```shell |
| 104 | mkdir android-kernel && cd android-kernel |
| 105 | repo init -u https://android.googlesource.com/kernel/manifest -b common-android12-5.10 |
| 106 | repo sync |
| 107 | FAST_BUILD=1 DIST_DIR=out/dist BUILD_CONFIG=common/build.config.gki.aarch64 build/build.sh -j80 |
| 108 | ``` |
| 109 | |
| 110 | Replace `build.config.gki.aarch64` with `build.config.gki.x86_64` if building |
| 111 | for x86. |
| 112 | |
| 113 | Then copy the built kernel to the Android source tree. |
| 114 | |
| 115 | ``` |
| 116 | cp out/dist/Image <android_root>/kernel/prebuilts/5.10/arm64/kernel-5.10 |
| 117 | ``` |
| 118 | |
| 119 | Finally rebuild the `com.android.virt` APEX and install it by following the |
| 120 | steps shown in [Building and updating Crosvm and |
| 121 | Virtualization](#building-and-updating). |