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 "" |
| 27 | echo " help - prints this help message" |
| 28 | } |
Jiyong Park | 17e3ed4 | 2022-08-29 17:27:00 +0900 | [diff] [blame] | 29 | |
| 30 | function connect_vm() { |
| 31 | cid=$1 |
| 32 | echo Connecting to CID ${cid} |
Nikita Ioffe | 48d4039 | 2022-12-21 15:59:40 +0000 | [diff] [blame^] | 33 | adb disconnect localhost:8000 2>/dev/null |
Jiyong Park | 17e3ed4 | 2022-08-29 17:27:00 +0900 | [diff] [blame] | 34 | adb forward tcp:8000 vsock:${cid}:5555 |
| 35 | adb connect localhost:8000 |
| 36 | adb -s localhost:8000 root |
Jiyong Park | bffe192 | 2022-10-24 10:16:37 +0900 | [diff] [blame] | 37 | adb -s localhost:8000 wait-for-device |
Jiyong Park | 17e3ed4 | 2022-08-29 17:27:00 +0900 | [diff] [blame] | 38 | adb -s localhost:8000 shell |
| 39 | exit 0 |
| 40 | } |
| 41 | |
Nikita Ioffe | 48d4039 | 2022-12-21 15:59:40 +0000 | [diff] [blame^] | 42 | function list_cids() { |
| 43 | local selected_cid=$1 |
| 44 | local available_cids=$(adb shell /apex/com.android.virt/bin/vm list | awk 'BEGIN { FS="[:,]" } /cid/ { print $2; }') |
| 45 | echo "${available_cids}" |
| 46 | } |
Jiyong Park | 17e3ed4 | 2022-08-29 17:27:00 +0900 | [diff] [blame] | 47 | |
Nikita Ioffe | 48d4039 | 2022-12-21 15:59:40 +0000 | [diff] [blame^] | 48 | function handle_connect_cmd() { |
| 49 | selected_cid=$1 |
Jiyong Park | 17e3ed4 | 2022-08-29 17:27:00 +0900 | [diff] [blame] | 50 | |
Nikita Ioffe | 48d4039 | 2022-12-21 15:59:40 +0000 | [diff] [blame^] | 51 | available_cids=$(list_cids) |
Jiyong Park | 17e3ed4 | 2022-08-29 17:27:00 +0900 | [diff] [blame] | 52 | |
Nikita Ioffe | 48d4039 | 2022-12-21 15:59:40 +0000 | [diff] [blame^] | 53 | if [ -z "${available_cids}" ]; then |
| 54 | echo No VM is available |
| 55 | exit 1 |
| 56 | fi |
Steven Moreland | 187c96e | 2022-10-21 23:15:42 +0000 | [diff] [blame] | 57 | |
Nikita Ioffe | 48d4039 | 2022-12-21 15:59:40 +0000 | [diff] [blame^] | 58 | if [ ! -n "${selected_cid}" ]; then |
| 59 | PS3="Select CID of VM to adb-shell into: " |
| 60 | select cid in ${available_cids} |
| 61 | do |
| 62 | selected_cid=${cid} |
| 63 | break |
| 64 | done |
| 65 | fi |
| 66 | |
| 67 | if [[ ! " ${available_cids[*]} " =~ " ${selected_cid} " ]]; then |
| 68 | echo VM of CID $selected_cid does not exist. Available CIDs: ${available_cids} |
| 69 | exit 1 |
| 70 | fi |
| 71 | |
| 72 | connect_vm ${selected_cid} |
| 73 | } |
| 74 | |
| 75 | cmd=$1 |
| 76 | shift |
| 77 | |
| 78 | case $cmd in |
| 79 | connect) handle_connect_cmd "$@" ;; |
| 80 | help) print_help ;; |
| 81 | *) print_help; exit 1 ;; |
| 82 | esac |