Merge "Fix typo regarding path for microdroid kernel in README.md"
diff --git a/authfs/service/authfs_service.rc b/authfs/service/authfs_service.rc
index 7edb1ca..409e91c 100644
--- a/authfs/service/authfs_service.rc
+++ b/authfs/service/authfs_service.rc
@@ -1,3 +1,5 @@
 service authfs_service /system/bin/authfs_service
     disabled
     socket authfs_service stream 0666 root system
+    # SYS_ADMIN capability allows to mount FUSE filesystem
+    capabilities SYS_ADMIN
diff --git a/microdroid/Android.bp b/microdroid/Android.bp
index 8cc8daf..ecaadf8 100644
--- a/microdroid/Android.bp
+++ b/microdroid/Android.bp
@@ -46,6 +46,7 @@
     use_avb: true,
     avb_private_key: ":microdroid_sign_key",
     avb_algorithm: "SHA256_RSA4096",
+    avb_hash_algorithm: "sha256",
     partition_name: "system",
     deps: [
         "init_second_stage",
@@ -90,7 +91,7 @@
 
         "libvm_payload", // used by payload to interact with microdroid manager
 
-        "prng_seeder",
+        "prng_seeder_microdroid",
     ] + microdroid_shell_and_utilities,
     multilib: {
         common: {
@@ -215,6 +216,7 @@
     },
     avb_private_key: ":microdroid_sign_key",
     avb_algorithm: "SHA256_RSA4096",
+    avb_hash_algorithm: "sha256",
     file_contexts: ":microdroid_vendor_file_contexts.gen",
     // For deterministic output, use fake_timestamp, hard-coded uuid
     fake_timestamp: "1611569676",
diff --git a/vm/vm_shell.sh b/vm/vm_shell.sh
index c0dd38f..29cc7da 100755
--- a/vm/vm_shell.sh
+++ b/vm/vm_shell.sh
@@ -14,16 +14,33 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# vm_shell.sh shows the VMs running in the Android device and connects to it
-# Usage:
-# vm_shell [cid]
-#
-#   cid: CID of the VM to connect to. If omitted, the list of CIDs available are shown
+# vm_shell.sh: utilities to interact with Microdroid VMs
+
+function print_help() {
+    echo "vm_shell.sh provides utilities to interact with Microdroid VMs"
+    echo ""
+    echo "Available commands:"
+    echo "    connect [cid] - establishes adb connection with the VM"
+    echo "      cid - cid of the VM to connect to. If not specified user will "
+    echo "            be promted to select one from the list of available cids"
+    echo ""
+    echo "    start-microdroid [--auto-connect] [-- extra_args]"
+    echo "        Starts a Microdroid VM. Args after the -- will be"
+    echo "        passed through to the invocation of the "
+    echo "        /apex/com.android.virt/bin/vm run-microdroid binary."
+    echo ""
+    echo "        E.g.:"
+    echo "            vm_shell start-microdroid -- --cpu 5"
+    echo ""
+    echo "        --auto-connect - automatically connects to the started VMs"
+    echo ""
+    echo "    help - prints this help message"
+}
 
 function connect_vm() {
     cid=$1
     echo Connecting to CID ${cid}
-    adb disconnect localhost:8000
+    adb disconnect localhost:8000 2>/dev/null
     adb forward tcp:8000 vsock:${cid}:5555
     adb connect localhost:8000
     adb -s localhost:8000 root
@@ -32,26 +49,63 @@
     exit 0
 }
 
-selected_cid=$1
-available_cids=$(adb shell /apex/com.android.virt/bin/vm list | awk 'BEGIN { FS="[:,]" } /cid/ { print $2; }')
+function list_cids() {
+    local selected_cid=$1
+    local available_cids=$(adb shell /apex/com.android.virt/bin/vm list | awk 'BEGIN { FS="[:,]" } /cid/ { print $2; }')
+    echo "${available_cids}"
+}
 
-if [ -z "${available_cids}" ]; then
-    echo No VM is available
-    exit 1
-fi
+function handle_connect_cmd() {
+    selected_cid=$1
 
-if [ ! -n "${selected_cid}" ]; then
-    PS3="Select CID of VM to adb-shell into: "
-    select cid in ${available_cids}
-    do
-        selected_cid=${cid}
-        break
+    available_cids=$(list_cids)
+
+    if [ -z "${available_cids}" ]; then
+        echo No VM is available
+        exit 1
+    fi
+
+    if [ ! -n "${selected_cid}" ]; then
+        PS3="Select CID of VM to adb-shell into: "
+        select cid in ${available_cids}
+        do
+            selected_cid=${cid}
+            break
+        done
+    fi
+
+    if [[ ! " ${available_cids[*]} " =~ " ${selected_cid} " ]]; then
+        echo VM of CID $selected_cid does not exist. Available CIDs: ${available_cids}
+        exit 1
+    fi
+
+    connect_vm ${selected_cid}
+}
+
+function handle_start_microdroid_cmd() {
+    while [[ "$#" -gt 0 ]]; do
+        case $1 in
+          --auto-connect) auto_connect=true; ;;
+          --) shift; passthrough_args="$@"; break ;;
+          *) echo "Unknown argument: $1"; exit 1 ;;
+        esac
+        shift
     done
-fi
+    if [[ "${auto_connect}" == true ]]; then
+        adb shell /apex/com.android.virt/bin/vm run-microdroid -d "${passthrough_args}"
+        sleep 2
+        handle_connect_cmd
+    else
+        adb shell /apex/com.android.virt/bin/vm run-microdroid "${passthrough_args}"
+    fi
+}
 
-if [[ ! " ${available_cids[*]} " =~ " ${selected_cid} " ]]; then
-    echo VM of CID $selected_cid does not exist. Available CIDs: ${available_cids}
-    exit 1
-fi
+cmd=$1
+shift
 
-connect_vm ${selected_cid}
+case $cmd in
+  connect) handle_connect_cmd "$@" ;;
+  start-microdroid) handle_start_microdroid_cmd "$@" ;;
+  help) print_help ;;
+  *) print_help; exit 1 ;;
+esac