Jiyong Park | 17e3ed4 | 2022-08-29 17:27:00 +0900 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # Copyright 2020 Google Inc. All rights reserved. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
Nikita Ioffe | 48d4039 | 2022-12-21 15:59:40 +0000 | [diff] [blame] | 17 | # vm_shell.sh: utilities to interact with Microdroid VMs |
| 18 | |
| 19 | function print_help() { |
| 20 | echo "vm_shell.sh provides utilities to interact with Microdroid VMs" |
| 21 | echo "" |
| 22 | echo "Available commands:" |
| 23 | echo " connect [cid] - establishes adb connection with the VM" |
| 24 | echo " cid - cid of the VM to connect to. If not specified user will " |
| 25 | echo " be promted to select one from the list of available cids" |
| 26 | echo "" |
Nikita Ioffe | 50121bf | 2022-12-21 17:50:39 +0000 | [diff] [blame] | 27 | echo " start-microdroid [--auto-connect] [-- extra_args]" |
| 28 | echo " Starts a Microdroid VM. Args after the -- will be" |
| 29 | echo " passed through to the invocation of the " |
| 30 | echo " /apex/com.android.virt/bin/vm run-microdroid binary." |
| 31 | echo "" |
| 32 | echo " E.g.:" |
| 33 | echo " vm_shell start-microdroid -- --cpu 5" |
| 34 | echo "" |
| 35 | echo " --auto-connect - automatically connects to the started VMs" |
| 36 | echo "" |
Nikita Ioffe | 48d4039 | 2022-12-21 15:59:40 +0000 | [diff] [blame] | 37 | echo " help - prints this help message" |
| 38 | } |
Jiyong Park | 17e3ed4 | 2022-08-29 17:27:00 +0900 | [diff] [blame] | 39 | |
| 40 | function connect_vm() { |
| 41 | cid=$1 |
| 42 | echo Connecting to CID ${cid} |
Nikita Ioffe | 48d4039 | 2022-12-21 15:59:40 +0000 | [diff] [blame] | 43 | adb disconnect localhost:8000 2>/dev/null |
Jiyong Park | 17e3ed4 | 2022-08-29 17:27:00 +0900 | [diff] [blame] | 44 | adb forward tcp:8000 vsock:${cid}:5555 |
| 45 | adb connect localhost:8000 |
| 46 | adb -s localhost:8000 root |
Jiyong Park | bffe192 | 2022-10-24 10:16:37 +0900 | [diff] [blame] | 47 | adb -s localhost:8000 wait-for-device |
Jiyong Park | 17e3ed4 | 2022-08-29 17:27:00 +0900 | [diff] [blame] | 48 | adb -s localhost:8000 shell |
| 49 | exit 0 |
| 50 | } |
| 51 | |
Nikita Ioffe | 48d4039 | 2022-12-21 15:59:40 +0000 | [diff] [blame] | 52 | function list_cids() { |
| 53 | local selected_cid=$1 |
| 54 | local available_cids=$(adb shell /apex/com.android.virt/bin/vm list | awk 'BEGIN { FS="[:,]" } /cid/ { print $2; }') |
| 55 | echo "${available_cids}" |
| 56 | } |
Jiyong Park | 17e3ed4 | 2022-08-29 17:27:00 +0900 | [diff] [blame] | 57 | |
Nikita Ioffe | 48d4039 | 2022-12-21 15:59:40 +0000 | [diff] [blame] | 58 | function handle_connect_cmd() { |
| 59 | selected_cid=$1 |
Jiyong Park | 17e3ed4 | 2022-08-29 17:27:00 +0900 | [diff] [blame] | 60 | |
Nikita Ioffe | 48d4039 | 2022-12-21 15:59:40 +0000 | [diff] [blame] | 61 | available_cids=$(list_cids) |
Jiyong Park | 17e3ed4 | 2022-08-29 17:27:00 +0900 | [diff] [blame] | 62 | |
Nikita Ioffe | 48d4039 | 2022-12-21 15:59:40 +0000 | [diff] [blame] | 63 | if [ -z "${available_cids}" ]; then |
| 64 | echo No VM is available |
| 65 | exit 1 |
| 66 | fi |
Steven Moreland | 187c96e | 2022-10-21 23:15:42 +0000 | [diff] [blame] | 67 | |
Nikita Ioffe | 48d4039 | 2022-12-21 15:59:40 +0000 | [diff] [blame] | 68 | if [ ! -n "${selected_cid}" ]; then |
| 69 | PS3="Select CID of VM to adb-shell into: " |
| 70 | select cid in ${available_cids} |
| 71 | do |
| 72 | selected_cid=${cid} |
| 73 | break |
| 74 | done |
| 75 | fi |
| 76 | |
| 77 | if [[ ! " ${available_cids[*]} " =~ " ${selected_cid} " ]]; then |
| 78 | echo VM of CID $selected_cid does not exist. Available CIDs: ${available_cids} |
| 79 | exit 1 |
| 80 | fi |
| 81 | |
| 82 | connect_vm ${selected_cid} |
| 83 | } |
| 84 | |
Nikita Ioffe | 50121bf | 2022-12-21 17:50:39 +0000 | [diff] [blame] | 85 | function handle_start_microdroid_cmd() { |
| 86 | while [[ "$#" -gt 0 ]]; do |
| 87 | case $1 in |
| 88 | --auto-connect) auto_connect=true; ;; |
| 89 | --) shift; passthrough_args="$@"; break ;; |
| 90 | *) echo "Unknown argument: $1"; exit 1 ;; |
| 91 | esac |
| 92 | shift |
| 93 | done |
| 94 | if [[ "${auto_connect}" == true ]]; then |
David Brazdil | 878b5da | 2023-01-11 13:49:38 +0000 | [diff] [blame^] | 95 | adb shell /apex/com.android.virt/bin/vm run-microdroid "${passthrough_args}" & |
| 96 | trap "kill $!" EXIT |
Nikita Ioffe | 50121bf | 2022-12-21 17:50:39 +0000 | [diff] [blame] | 97 | sleep 2 |
| 98 | handle_connect_cmd |
| 99 | else |
| 100 | adb shell /apex/com.android.virt/bin/vm run-microdroid "${passthrough_args}" |
| 101 | fi |
| 102 | } |
| 103 | |
Nikita Ioffe | 48d4039 | 2022-12-21 15:59:40 +0000 | [diff] [blame] | 104 | cmd=$1 |
| 105 | shift |
| 106 | |
| 107 | case $cmd in |
| 108 | connect) handle_connect_cmd "$@" ;; |
Nikita Ioffe | 50121bf | 2022-12-21 17:50:39 +0000 | [diff] [blame] | 109 | start-microdroid) handle_start_microdroid_cmd "$@" ;; |
Nikita Ioffe | 48d4039 | 2022-12-21 15:59:40 +0000 | [diff] [blame] | 110 | help) print_help ;; |
| 111 | *) print_help; exit 1 ;; |
| 112 | esac |