blob: cac5781f4199d39a0752f39a47025d0ee3ff5f6a [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
Nikita Ioffe48d40392022-12-21 15:59:40 +000017# vm_shell.sh: utilities to interact with Microdroid VMs
18
19function 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 Ioffe50121bf2022-12-21 17:50:39 +000027 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.:"
Pierre-Clément Tosi8d6bfc22024-11-29 09:54:08 +000033 echo " vm_shell start-microdroid -- --protected --debug full"
Nikita Ioffe50121bf2022-12-21 17:50:39 +000034 echo ""
35 echo " --auto-connect - automatically connects to the started VMs"
36 echo ""
Nikita Ioffe48d40392022-12-21 15:59:40 +000037 echo " help - prints this help message"
38}
Jiyong Park17e3ed42022-08-29 17:27:00 +090039
40function connect_vm() {
41 cid=$1
42 echo Connecting to CID ${cid}
Nikita Ioffe48d40392022-12-21 15:59:40 +000043 adb disconnect localhost:8000 2>/dev/null
Jiyong Park17e3ed42022-08-29 17:27:00 +090044 adb forward tcp:8000 vsock:${cid}:5555
45 adb connect localhost:8000
46 adb -s localhost:8000 root
Jiyong Parkbffe1922022-10-24 10:16:37 +090047 adb -s localhost:8000 wait-for-device
Jiyong Park17e3ed42022-08-29 17:27:00 +090048 adb -s localhost:8000 shell
49 exit 0
50}
51
Nikita Ioffe48d40392022-12-21 15:59:40 +000052function list_cids() {
Frederick Mayle72f78902025-03-04 23:33:32 -080053 adb shell /apex/com.android.virt/bin/vm list | awk 'BEGIN { FS="[:,]" } /cid/ { print $2; }'
Nikita Ioffe48d40392022-12-21 15:59:40 +000054}
Jiyong Park17e3ed42022-08-29 17:27:00 +090055
Nikita Ioffe48d40392022-12-21 15:59:40 +000056function handle_connect_cmd() {
57 selected_cid=$1
Jiyong Park17e3ed42022-08-29 17:27:00 +090058
Frederick Mayle72f78902025-03-04 23:33:32 -080059 available_cids=($(list_cids))
Jiyong Park17e3ed42022-08-29 17:27:00 +090060
Nikita Ioffe48d40392022-12-21 15:59:40 +000061 if [ -z "${available_cids}" ]; then
62 echo No VM is available
63 exit 1
64 fi
Steven Moreland187c96e2022-10-21 23:15:42 +000065
Nikita Ioffe48d40392022-12-21 15:59:40 +000066 if [ ! -n "${selected_cid}" ]; then
Frederick Mayle72f78902025-03-04 23:33:32 -080067 if [ ${#available_cids[@]} -eq 1 ]; then
Nikita Ioffe47f71f62023-01-19 22:13:30 +000068 selected_cid=${available_cids[0]}
69 else
70 PS3="Select CID of VM to adb-shell into: "
Frederick Mayle72f78902025-03-04 23:33:32 -080071 select cid in ${available_cids[@]}
Nikita Ioffe47f71f62023-01-19 22:13:30 +000072 do
73 selected_cid=${cid}
74 break
75 done
76 fi
Nikita Ioffe48d40392022-12-21 15:59:40 +000077 fi
78
79 if [[ ! " ${available_cids[*]} " =~ " ${selected_cid} " ]]; then
80 echo VM of CID $selected_cid does not exist. Available CIDs: ${available_cids}
81 exit 1
82 fi
83
84 connect_vm ${selected_cid}
85}
86
Nikita Ioffe50121bf2022-12-21 17:50:39 +000087function handle_start_microdroid_cmd() {
88 while [[ "$#" -gt 0 ]]; do
89 case $1 in
90 --auto-connect) auto_connect=true; ;;
91 --) shift; passthrough_args="$@"; break ;;
92 *) echo "Unknown argument: $1"; exit 1 ;;
93 esac
94 shift
95 done
96 if [[ "${auto_connect}" == true ]]; then
David Brazdil878b5da2023-01-11 13:49:38 +000097 adb shell /apex/com.android.virt/bin/vm run-microdroid "${passthrough_args}" &
98 trap "kill $!" EXIT
Nikita Ioffe50121bf2022-12-21 17:50:39 +000099 sleep 2
100 handle_connect_cmd
101 else
102 adb shell /apex/com.android.virt/bin/vm run-microdroid "${passthrough_args}"
103 fi
104}
105
Nikita Ioffe48d40392022-12-21 15:59:40 +0000106cmd=$1
107shift
108
109case $cmd in
110 connect) handle_connect_cmd "$@" ;;
Nikita Ioffe50121bf2022-12-21 17:50:39 +0000111 start-microdroid) handle_start_microdroid_cmd "$@" ;;
Nikita Ioffe48d40392022-12-21 15:59:40 +0000112 help) print_help ;;
113 *) print_help; exit 1 ;;
114esac