blob: ec9243bb118ef68c9fd60345edb352eea266bfbd [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
43if [ -n "${selected_cid}" ]; then
44 if [[ ! " ${available_cids[*]} " =~ " ${selected_cid} " ]]; then
45 echo VM of CID $selected_cid does not exist. Available CIDs: ${available_cids}
46 exit 1
47 fi
48else
49 PS3="Select CID of VM to adb-shell into: "
50 select cid in ${available_cids}
51 do
52 selected_cid=${cid}
53 break
54 done
55fi
56
57connect_vm ${selected_cid}