Add vm_shell script

It's a small script that makes it easier to connect to a Microdroid VM
running in the Android device.

Usage: vm_shell [cid]

If cid is omitted it displays the CIDs of available VMs and lets you
select one.

Bug: N/A
Test: m vm_shell and run it
Change-Id: I762f1161024d151a2f9e75fdb217037f1f9a1e1d
diff --git a/apex/Android.bp b/apex/Android.bp
index 923c378..83985cc 100644
--- a/apex/Android.bp
+++ b/apex/Android.bp
@@ -69,6 +69,9 @@
     ],
     file_contexts: ":com.android.virt-file_contexts",
     canned_fs_config: "canned_fs_config",
+    host_required: [
+        "vm_shell",
+    ],
 }
 
 apex_key {
diff --git a/microdroid/README.md b/microdroid/README.md
index fef71ce..5cfa523 100644
--- a/microdroid/README.md
+++ b/microdroid/README.md
@@ -167,17 +167,7 @@
 `/apex/com.android.virt/bin/vm run-app` command, and then
 
 ```sh
-adb forward tcp:8000 vsock:$CID:5555
-adb connect localhost:8000
+vm_shell
 ```
 
-`$CID` should be the CID that `vm` reported upon execution of the `vm run`
-command in the above. You can also check it with
-`adb shell "/apex/com.android.virt/bin/vm list"`. `5555` must be the value.
-`8000` however can be any port on the development machine.
-
-Done. Now you can log into microdroid. Have fun!
-
-```sh
-$ adb -s localhost:8000 shell
-```
+Done. Now you are logged into Microdroid. Have fun!
diff --git a/microdroid/kernel/arm64/kernel-5.15 b/microdroid/kernel/arm64/kernel-5.15
index 33c8c13..49b7f69 100644
--- a/microdroid/kernel/arm64/kernel-5.15
+++ b/microdroid/kernel/arm64/kernel-5.15
Binary files differ
diff --git a/vm/Android.bp b/vm/Android.bp
index f9eac4d..2e914c4 100644
--- a/vm/Android.bp
+++ b/vm/Android.bp
@@ -28,3 +28,8 @@
         "com.android.virt",
     ],
 }
+
+sh_binary_host {
+    name: "vm_shell",
+    src: "vm_shell.sh",
+}
diff --git a/vm/vm_shell.sh b/vm/vm_shell.sh
new file mode 100755
index 0000000..ec9243b
--- /dev/null
+++ b/vm/vm_shell.sh
@@ -0,0 +1,57 @@
+#!/bin/bash
+
+# Copyright 2020 Google Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# 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
+
+function connect_vm() {
+    cid=$1
+    echo Connecting to CID ${cid}
+    adb disconnect localhost:8000
+    adb forward tcp:8000 vsock:${cid}:5555
+    adb connect localhost:8000
+    adb -s localhost:8000 root
+    sleep 2
+    adb -s localhost:8000 shell
+    exit 0
+}
+
+selected_cid=$1
+available_cids=$(adb shell /apex/com.android.virt/bin/vm list | awk 'BEGIN { FS="[:,]" } /cid/ { print $2; }')
+
+if [ -z "${available_cids}" ]; then
+    echo No VM is available
+    exit 1
+fi
+
+if [ -n "${selected_cid}" ]; then
+    if [[ ! " ${available_cids[*]} " =~ " ${selected_cid} " ]]; then
+        echo VM of CID $selected_cid does not exist. Available CIDs: ${available_cids}
+        exit 1
+    fi
+else
+    PS3="Select CID of VM to adb-shell into: "
+    select cid in ${available_cids}
+    do
+        selected_cid=${cid}
+        break
+    done
+fi
+
+connect_vm ${selected_cid}