blob: 8217901ce935e6f97bb9cffd9d62460081bb8eb1 [file] [log] [blame]
Jiyong Park17e3ed42022-08-29 17:27:00 +09001#!/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
23function 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
35selected_cid=$1
36available_cids=$(adb shell /apex/com.android.virt/bin/vm list | awk 'BEGIN { FS="[:,]" } /cid/ { print $2; }')
37
38if [ -z "${available_cids}" ]; then
39 echo No VM is available
40 exit 1
41fi
42
Steven Moreland187c96e2022-10-21 23:15:42 +000043if [ ! -n "${selected_cid}" ]; then
Jiyong Park17e3ed42022-08-29 17:27:00 +090044 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
50fi
51
Steven Moreland187c96e2022-10-21 23:15:42 +000052if [[ ! " ${available_cids[*]} " =~ " ${selected_cid} " ]]; then
53 echo VM of CID $selected_cid does not exist. Available CIDs: ${available_cids}
54 exit 1
55fi
56
Jiyong Park17e3ed42022-08-29 17:27:00 +090057connect_vm ${selected_cid}