Merge "Update documentation with new test name and Virt Manager."
diff --git a/apex/Android.bp b/apex/Android.bp
index bb73630..bc9e084 100644
--- a/apex/Android.bp
+++ b/apex/Android.bp
@@ -20,22 +20,40 @@
                 "authfs", // TODO(victorhsieh): move to microdroid once we can run the test in VM.
                 "crosvm",
             ],
+            filesystems: [
+                "microdroid_super",
+                "microdroid_boot-5.10",
+                "microdroid_vendor_boot-5.10",
+                "microdroid_vbmeta",
+                "microdroid_vbmeta_system",
+            ],
         },
         x86_64: {
             binaries: [
                 "authfs", // TODO(victorhsieh): move to microdroid once we can run the test in VM.
                 "crosvm",
             ],
+            filesystems: [
+                "microdroid_super",
+                "microdroid_boot-5.10",
+                "microdroid_vendor_boot-5.10",
+                "microdroid_vbmeta",
+                "microdroid_vbmeta_system",
+            ],
         },
     },
     binaries: [
-        "assemble_cvd",
+        "mk_cdisk",
         "fd_server",
         "virtmanager",
         "vm",
     ],
-    filesystems: ["microdroid"],
-    prebuilts: ["com.android.virt.init.rc"],
+    prebuilts: [
+        "com.android.virt.init.rc",
+        "microdroid_cdisk.json",
+        "microdroid_uboot_env",
+        "microdroid_bootloader",
+    ],
     file_contexts: ":com.android.virt-file_contexts",
 }
 
diff --git a/authfs/TEST_MAPPING b/authfs/TEST_MAPPING
index d2dc1d8..d0c0b09 100644
--- a/authfs/TEST_MAPPING
+++ b/authfs/TEST_MAPPING
@@ -2,9 +2,6 @@
   "presubmit": [
     {
       "name": "authfs_device_test_src_lib"
-    },
-    {
-      "name": "AuthFsTestCases"
     }
   ]
 }
diff --git a/microdroid/Android.bp b/microdroid/Android.bp
index 8ffb3cb..8456591 100644
--- a/microdroid/Android.bp
+++ b/microdroid/Android.bp
@@ -225,6 +225,41 @@
 }
 
 prebuilt_etc {
+    name: "microdroid_bootloader",
+    src: ":microdroid_bootloader_gen",
+    filename: "microdroid_bootloader",
+}
+
+// See external/avb/avbtool.py
+// MAX_VBMETA_SIZE=64KB, MAX_FOOTER_SIZE=4KB
+avb_hash_footer_kb = "68"
+
+genrule {
+    name: "microdroid_bootloader_gen",
+    tools: ["avbtool"],
+    srcs: [
+        ":cuttlefish_crosvm_bootloader",
+        ":avb_testkey_rsa4096",
+    ],
+    out: ["bootloader-signed"],
+    // 1. Copy the input to the output becaise avbtool modifies --image in
+    // place.
+    // 2. Check if the file is big enough. For arm and x86 we have fake
+    // bootloader file whose size is 1. It can't pass avbtool.
+    // 3. Add the hash footer. The partition size is set to (image size + 68KB)
+    // rounded up to 4KB boundary.
+    cmd: "cp $(location :cuttlefish_crosvm_bootloader) $(out) && " +
+        "if [ $$(stat --format=%s $(out)) -gt 4096 ]; then " +
+        "$(location avbtool) add_hash_footer " +
+        "--algorithm SHA256_RSA4096 " +
+        "--partition_name bootloader " +
+        "--key $(location :avb_testkey_rsa4096) " +
+        "--partition_size $$(( " + avb_hash_footer_kb + " * 1024 + ( $$(stat --format=%s $(out)) + 4096 - 1 ) / 4096 * 4096 )) " +
+        "--image $(out)" +
+        "; fi",
+}
+
+prebuilt_etc {
     name: "microdroid_uboot_env",
     src: ":microdroid_uboot_env_gen",
     filename: "uboot_env.img",
@@ -298,3 +333,8 @@
         "microdroid",
     ],
 }
+
+prebuilt_etc {
+    name: "microdroid_cdisk.json",
+    src: "microdroid_cdisk.json",
+}
diff --git a/microdroid/README.md b/microdroid/README.md
index d737e22..db9ed1a 100644
--- a/microdroid/README.md
+++ b/microdroid/README.md
@@ -10,52 +10,32 @@
 You need a VIM3L board. Instructions for building Android for the target, and
 flashing the image can be found [here](../docs/getting_started/yukawa.md).
 
-Then you build microdroid. Note that the instruction below is very likely to
-change in the future, because this is in active development. For example, the
-`microdroid_*` modules will eventually be included in the `com.android.virt`
-APEX, which is already in the `yukawa` (VIM3L) target.
+Then you install `com.android.virt` APEX. All files needed to run microdroid are
+included in the APEX, which is already in the `yukawa` (VIM3L) target. You can
+of course build and install the APEX manually.
 
 ```
 $ source build/envsetup.sh
 $ choosecombo 1 aosp_arm64 userdebug // actually, any arm64-based target is ok
-$ m microdroid_super
-$ m microdroid_boot-5.10
-$ m microdroid_vendor_boot-5.10
-$ m microdroid_uboot_env
-$ m microdroid_vbmeta
-$ m microdroid_vbmeta_system
-```
-
-## Installing
-
-Push the built files to the device. In addition to that, some other files have
-to be manually created, for now. In the future, you won't need these.
-
-```
-$ adb push device/google/cuttlefish_prebuilts/bootloader/crosvm_aarch64/u-boot.bin /data/local/tmp/bootloader
-$ adb push $ANDROID_PRODUCT_OUT/system/etc/microdroid_super.img /data/local/tmp/super.img
-$ adb push $ANDROID_PRODUCT_OUT/system/etc/microdroid_boot-5.10.img /data/local/tmp/boot.img
-$ adb push $ANDROID_PRODUCT_OUT/system/etc/microdroid_vendor_boot-5.10.img /data/local/tmp/vendor_boot.img
-$ adb push $ANDROID_PRODUCT_OUT/system/etc/microdroid_vbmeta.img /data/local/tmp/vbmeta.img
-$ adb push $ANDROID_PRODUCT_OUT/system/etc/microdroid_vbmeta_system.img /data/local/tmp/vbmeta_system.img
-$ adb shell mkdir /data/local/tmp/cuttlefish_runtime.1/
-$ adb push $ANDROID_PRODUCT_OUT/system/etc/uboot_env.img /data/local/tmp/cuttlefish_runtime.1/
-$ adb shell mkdir -p /data/local/tmp/etc/cvd_config
-$ adb shell 'echo "{}" > /data/local/tmp/etc/cvd_config/cvd_config_phone.json'
-$ dd if=/dev/zero of=empty.img bs=4k count=600
-$ mkfs.ext4 -F empty.img
-$ adb push empty.img /data/local/tmp/userdata.img
-$ adb push empty.img /data/local/tmp/cache.img
+$ m com.android.virt
+$ adb install $ANDROID_PRODUCT_OUT/system/apex/com.android.virt.apex
+$ adb reboot
 ```
 
 ## Running
 
-Create the composite image using `assemble_cvd` and run it via `crosvm`. In the
-future, this shall be done via [`virtmanager`](../virtmanager/).
+Copy the artifacts to the temp directory, create the composite image using
+`mk_cdisk`, and run it via `crosvm`. For now, some other files have to be
+manually created. In the future, you won't need these, and this shall be done
+via [`virtmanager`](../virtmanager/).
 
 ```
-$ adb shell 'HOME=/data/local/tmp; PATH=$PATH:/apex/com.android.virt/bin; assemble_cvd -protected_vm < /dev/null'
-$ adb shell 'cd /data/local/tmp; /apex/com.android.virt/bin/crosvm run --cid=5 --disable-sandbox --bios=bootloader --serial=type=stdout --disk=cuttlefish_runtime/os_composite.img'
+$ adb shell 'cp /apex/com.android.virt/etc/microdroid_bootloader /data/local/tmp/bootloader'
+$ adb shell 'cp /apex/com.android.virt/etc/fs/*.img /data/local/tmp'
+$ adb shell 'cp /apex/com.android.virt/etc/uboot_env.img /data/local/tmp'
+$ adb shell 'dd if=/dev/zero of=/data/local/tmp/misc.img bs=4k count=256'
+$ adb shell 'cd /data/local/tmp; /apex/com.android.virt/bin/mk_cdisk /apex/com.android.virt/etc/microdroid_cdisk.json os_composite.img'
+$ adb shell 'cd /data/local/tmp; /apex/com.android.virt/bin/crosvm run --cid=5 --disable-sandbox --bios=bootloader --serial=type=stdout --disk=os_composite.img'
 ```
 
 The CID in `--cid` parameter can be anything greater than 2 (`VMADDR_CID_HOST`).
diff --git a/microdroid/microdroid_cdisk.json b/microdroid/microdroid_cdisk.json
new file mode 100644
index 0000000..88b7183
--- /dev/null
+++ b/microdroid/microdroid_cdisk.json
@@ -0,0 +1,59 @@
+{
+  "partitions": [
+    {
+      "label": "uboot_env",
+      "path": "uboot_env.img",
+      "read_only": true
+    },
+    {
+      "label": "misc",
+      "path": "misc.img",
+      "read_only": true
+    },
+    {
+      "label": "boot_a",
+      "path": "microdroid_boot-5.10.img",
+      "read_only": true
+    },
+    {
+      "label": "boot_b",
+      "path": "microdroid_boot-5.10.img",
+      "read_only": true
+    },
+    {
+      "label": "vendor_boot_a",
+      "path": "microdroid_vendor_boot-5.10.img",
+      "read_only": true
+    },
+    {
+      "label": "vendor_boot_b",
+      "path": "microdroid_vendor_boot-5.10.img",
+      "read_only": true
+    },
+    {
+      "label": "vbmeta_a",
+      "path": "microdroid_vbmeta.img",
+      "read_only": true
+    },
+    {
+      "label": "vbmeta_b",
+      "path": "microdroid_vbmeta.img",
+      "read_only": true
+    },
+    {
+      "label": "vbmeta_system_a",
+      "path": "microdroid_vbmeta_system.img",
+      "read_only": true
+    },
+    {
+      "label": "vbmeta_system_b",
+      "path": "microdroid_vbmeta_system.img",
+      "read_only": true
+    },
+    {
+      "label": "super",
+      "path": "microdroid_super.img",
+      "read_only": true
+    }
+  ]
+}
diff --git a/tests/hostside/Android.bp b/tests/hostside/Android.bp
index c030e8d..429b737 100644
--- a/tests/hostside/Android.bp
+++ b/tests/hostside/Android.bp
@@ -7,23 +7,4 @@
     srcs: ["java/**/*.java"],
     test_suites: ["device-tests"],
     libs: ["tradefed"],
-    data: [
-        ":microdroid_super",
-        ":microdroid_boot-5.10",
-        ":microdroid_vendor_boot-5.10",
-        ":microdroid_uboot_env",
-        ":cuttlefish_crosvm_bootloader",
-        ":MicrodroidHostTestCase_EmptyImage",
-        ":microdroid_vbmeta",
-        ":microdroid_vbmeta_system",
-    ],
-}
-
-genrule {
-    name: "MicrodroidHostTestCase_EmptyImage",
-    tools: ["mke2fs"],
-    out: ["empty.img"],
-    cmd: "dd if=/dev/zero of=$(out) bs=4k count=600 &&" +
-        "$(location mke2fs) -t ext4 $(out)",
-    visibility: ["//visibility:private"],
 }
diff --git a/tests/hostside/java/android/virt/test/MicrodroidTestCase.java b/tests/hostside/java/android/virt/test/MicrodroidTestCase.java
index c5c86a6..4acdf4b 100644
--- a/tests/hostside/java/android/virt/test/MicrodroidTestCase.java
+++ b/tests/hostside/java/android/virt/test/MicrodroidTestCase.java
@@ -30,9 +30,6 @@
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
-import java.io.File;
-import java.nio.file.Path;
-import java.nio.file.Paths;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 
@@ -45,17 +42,6 @@
     private static final String MICRODROID_SERIAL = "localhost:" + TEST_VM_ADB_PORT;
     private static final long MICRODROID_BOOT_TIMEOUT_MILLIS = 15000;
 
-    private void pushFile(String localName, String remoteName) {
-        try {
-            File localFile = getTestInformation().getDependencyFile(localName, false);
-            Path remotePath = Paths.get(TEST_ROOT, remoteName);
-            getDevice().executeShellCommand("mkdir -p " + remotePath.getParent());
-            getDevice().pushFile(localFile, remotePath.toString());
-        } catch (Exception e) {
-            throw new RuntimeException(e);
-        }
-    }
-
     private String executeCommand(String cmd) {
         final long defaultCommandTimeoutMillis = 1000; // 1 sec
         return executeCommand(defaultCommandTimeoutMillis, cmd);
@@ -69,44 +55,44 @@
     @Test
     public void testMicrodroidBoots() throws Exception {
         // Prepare input files
-        pushFile("u-boot.bin", "bootloader");
-        pushFile("microdroid_super.img", "super.img");
-        pushFile("microdroid_boot-5.10.img", "boot.img");
-        pushFile("microdroid_vendor_boot-5.10.img", "vendor_boot.img");
-        pushFile("uboot_env.img", "cuttlefish_runtime.1/uboot_env.img");
-        pushFile("empty.img", "userdata.img");
-        pushFile("microdroid_vbmeta.img", "vbmeta.img");
-        pushFile("microdroid_vbmeta_system.img", "vbmeta_system.img");
-        pushFile("empty.img", "cache.img");
-        getDevice().executeShellCommand("mkdir -p " + TEST_ROOT + "etc/cvd_config");
-        getDevice().pushString("{}", TEST_ROOT + "etc/cvd_config/cvd_config_phone.json");
+        String prepareImagesCmd =
+                String.format(
+                        "mkdir -p %s; cd %s; "
+                                + "cp %setc/microdroid_bootloader bootloader && "
+                                + "cp %setc/fs/*.img . && "
+                                + "cp %setc/uboot_env.img . && "
+                                + "dd if=/dev/zero of=misc.img bs=4k count=256",
+                        TEST_ROOT, TEST_ROOT, VIRT_APEX, VIRT_APEX, VIRT_APEX);
+        getDevice().executeShellCommand(prepareImagesCmd);
 
-        // Run assemble_cvd to create os_composite.img
-        getDevice().executeShellCommand("HOME=" + TEST_ROOT + "; "
-                + "PATH=$PATH:" + VIRT_APEX + "bin; "
-                + VIRT_APEX + "bin/assemble_cvd -protected_vm < /dev/null");
+        // Create os_composite.img
+        String makeOsCompositeCmd =
+                String.format(
+                        "cd %s; %sbin/mk_cdisk %setc/microdroid_cdisk.json os_composite.img",
+                        TEST_ROOT, VIRT_APEX, VIRT_APEX);
+        getDevice().executeShellCommand(makeOsCompositeCmd);
 
         // Make sure that os_composite.img is created
-        final String compositeImg = TEST_ROOT + "cuttlefish_runtime/os_composite.img";
+        final String compositeImg = TEST_ROOT + "/os_composite.img";
         CommandResult result = getDevice().executeShellV2Command("du -b " + compositeImg);
         assertThat(result.getExitCode(), is(0));
         assertThat(result.getStdout(), is(not("")));
 
         // Start microdroid using crosvm
         ExecutorService executor = Executors.newFixedThreadPool(1);
-        executor.execute(() -> {
-            try {
-                getDevice().executeShellV2Command("cd " + TEST_ROOT + "; "
-                        + VIRT_APEX + "bin/crosvm run "
-                        + "--cid=" + TEST_VM_CID + " "
-                        + "--disable-sandbox "
-                        + "--bios=bootloader "
-                        + "--serial=type=syslog "
-                        + "--disk=cuttlefish_runtime/os_composite.img");
-            } catch (Exception e) {
-                throw new RuntimeException(e);
-            }
-        });
+        String runMicrodroidCmd =
+                String.format(
+                        "cd %s; %sbin/crosvm run --cid=%d --disable-sandbox --bios=bootloader"
+                                + " --serial=type=syslog --disk=os_composite.img",
+                        TEST_ROOT, VIRT_APEX, TEST_VM_CID);
+        executor.execute(
+                () -> {
+                    try {
+                        getDevice().executeShellV2Command(runMicrodroidCmd);
+                    } catch (Exception e) {
+                        throw new RuntimeException(e);
+                    }
+                });
         // .. and wait for microdroid to boot
         // TODO(jiyong): don't wait too long. We can wait less by monitoring log from microdroid
         Thread.sleep(MICRODROID_BOOT_TIMEOUT_MILLIS);
diff --git a/virtmanager/Android.bp b/virtmanager/Android.bp
index 1b2aec1..37c3bcc 100644
--- a/virtmanager/Android.bp
+++ b/virtmanager/Android.bp
@@ -7,6 +7,7 @@
     crate_name: "virtmanager",
     srcs: ["src/main.rs"],
     edition: "2018",
+    prefer_rlib: true,
     rustlibs: [
         "android.system.virtmanager-rust",
         "libandroid_logger",
diff --git a/vm/src/main.rs b/vm/src/main.rs
index 1f39e4a..6c1052f 100644
--- a/vm/src/main.rs
+++ b/vm/src/main.rs
@@ -24,7 +24,7 @@
 use std::fs::File;
 use std::io;
 use std::os::unix::io::{AsRawFd, FromRawFd};
-use std::path::PathBuf;
+use std::path::{Path, PathBuf};
 use structopt::clap::AppSettings;
 use structopt::StructOpt;
 use sync::AtomicFlag;
@@ -73,7 +73,7 @@
 /// Run a VM from the given configuration file.
 fn command_run(
     virt_manager: Strong<dyn IVirtManager>,
-    config_path: &PathBuf,
+    config_path: &Path,
     daemonize: bool,
 ) -> Result<(), Error> {
     let config_filename = config_path.to_str().context("Failed to parse VM config path")?;