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 | |
| 17 | # vm_shell.sh shows the VMs running in the Android device and connects to it |
| 18 | # Usage: |
| 19 | # vm_shell [cid] |
| 20 | # |
| 21 | # cid: CID of the VM to connect to. If omitted, the list of CIDs available are shown |
| 22 | |
| 23 | function connect_vm() { |
| 24 | cid=$1 |
| 25 | echo Connecting to CID ${cid} |
| 26 | adb disconnect localhost:8000 |
| 27 | adb forward tcp:8000 vsock:${cid}:5555 |
| 28 | adb connect localhost:8000 |
| 29 | adb -s localhost:8000 root |
| 30 | sleep 2 |
| 31 | adb -s localhost:8000 shell |
| 32 | exit 0 |
| 33 | } |
| 34 | |
| 35 | selected_cid=$1 |
| 36 | available_cids=$(adb shell /apex/com.android.virt/bin/vm list | awk 'BEGIN { FS="[:,]" } /cid/ { print $2; }') |
| 37 | |
| 38 | if [ -z "${available_cids}" ]; then |
| 39 | echo No VM is available |
| 40 | exit 1 |
| 41 | fi |
| 42 | |
Steven Moreland | 187c96e | 2022-10-21 23:15:42 +0000 | [diff] [blame^] | 43 | if [ ! -n "${selected_cid}" ]; then |
Jiyong Park | 17e3ed4 | 2022-08-29 17:27:00 +0900 | [diff] [blame] | 44 | PS3="Select CID of VM to adb-shell into: " |
| 45 | select cid in ${available_cids} |
| 46 | do |
| 47 | selected_cid=${cid} |
| 48 | break |
| 49 | done |
| 50 | fi |
| 51 | |
Steven Moreland | 187c96e | 2022-10-21 23:15:42 +0000 | [diff] [blame^] | 52 | if [[ ! " ${available_cids[*]} " =~ " ${selected_cid} " ]]; then |
| 53 | echo VM of CID $selected_cid does not exist. Available CIDs: ${available_cids} |
| 54 | exit 1 |
| 55 | fi |
| 56 | |
Jiyong Park | 17e3ed4 | 2022-08-29 17:27:00 +0900 | [diff] [blame] | 57 | connect_vm ${selected_cid} |