blob: 683b82e0cd5124c821280b1d9e861f28707e6a6c [file] [log] [blame]
Jaewan Kimb5fb9692024-06-10 14:18:12 +09001#!/bin/bash
2
3# Copyright 2024 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## Booting tests for ferrochrome
18## Keep this file synced with docs/custom_vm.md
19
20set -e
21
22FECR_GS_URL="https://storage.googleapis.com/chromiumos-image-archive/ferrochrome-public"
Jaewan Kim4294ae12024-07-18 15:11:03 +090023FECR_DEFAULT_VERSION="R128-15958.0.0"
24FECR_TEST_IMAGE="chromiumos_test_image"
25FECR_BASE_IMAGE="chromiumos_base_image"
Jaewan Kim5ebeb222024-07-05 19:14:53 +090026FECR_DEVICE_DIR="/data/local/tmp"
Jaewan Kim4294ae12024-07-18 15:11:03 +090027FECR_IMAGE_VM_CONFIG_JSON="chromiumos_base_image.bin" # hardcoded at vm_config.json
Jaewan Kimb5fb9692024-06-10 14:18:12 +090028FECR_CONFIG_PATH="/data/local/tmp/vm_config.json" # hardcoded at VmLauncherApp
29FECR_CONSOLE_LOG_PATH="/data/data/\${pkg_name}/files/console.log"
Jaewan Kim4294ae12024-07-18 15:11:03 +090030FECR_TEST_IMAGE_BOOT_COMPLETED_LOG="Have fun and send patches!"
31FECR_BASE_IMAGE_BOOT_COMPLETED_LOG="Chrome started, our work is done, exiting"
Jaewan Kimb5fb9692024-06-10 14:18:12 +090032FECR_BOOT_TIMEOUT="300" # 5 minutes (300 seconds)
Jaewan Kim3ad58c32024-06-13 08:59:29 +000033ACTION_NAME="android.virtualization.VM_LAUNCHER"
Jaewan Kim4294ae12024-07-18 15:11:03 +090034
35# Match this with AndroidTest.xml and assets/vm_config.json
36FECR_DEFAULT_IMAGE="${FECR_BASE_IMAGE}"
37FECR_DEFAULT_BOOT_COMPLETED_LOG="${FECR_BASE_IMAGE_BOOT_COMPLETED_LOG}"
Jaewan Kimb5fb9692024-06-10 14:18:12 +090038
39fecr_clean_up() {
40 trap - INT
41
Jaewan Kim8b82b4b2024-07-18 21:03:43 +090042 # Reset screen always on
43 adb shell svc power stayon false
44
Jaewan Kimb5fb9692024-06-10 14:18:12 +090045 if [[ -d ${fecr_dir} && -z ${fecr_keep} ]]; then
46 rm -rf ${fecr_dir}
47 fi
48}
49
50print_usage() {
Jaewan Kim3ad58c32024-06-13 08:59:29 +000051 echo "ferochrome: Launches ferrochrome image"
Jaewan Kimb5fb9692024-06-10 14:18:12 +090052 echo ""
Jaewan Kim4294ae12024-07-18 15:11:03 +090053 echo "By default, this downloads ${FECR_DEFAULT_VERSION} with version ${FECR_DEFAULT_VERSION},"
Jaewan Kimb5fb9692024-06-10 14:18:12 +090054 echo "launches, and waits for boot completed."
Jaewan Kim3ad58c32024-06-13 08:59:29 +000055 echo "When done, removes downloaded image on host while keeping pushed image on device."
Jaewan Kimb5fb9692024-06-10 14:18:12 +090056 echo ""
Jaewan Kim3ad58c32024-06-13 08:59:29 +000057 echo "Usage: ferrochrome [options]"
Jaewan Kimb5fb9692024-06-10 14:18:12 +090058 echo ""
59 echo "Options"
60 echo " --help or -h: This message"
Jaewan Kim3ad58c32024-06-13 08:59:29 +000061 echo " --dir DIR: Use ferrochrome images at the dir instead of downloading"
62 echo " --verbose: Verbose log message (set -x)"
Jaewan Kimb5fb9692024-06-10 14:18:12 +090063 echo " --skip: Skipping downloading and/or pushing images"
64 echo " --version \${version}: ferrochrome version to be downloaded"
65 echo " --keep: Keep downloaded ferrochrome image"
Jaewan Kim4294ae12024-07-18 15:11:03 +090066 echo " --test: Download test image instead"
Jaewan Kimb5fb9692024-06-10 14:18:12 +090067}
68
Jaewan Kim4294ae12024-07-18 15:11:03 +090069fecr_version="${FECR_DEFAULT_VERSION}"
Jaewan Kimb5fb9692024-06-10 14:18:12 +090070fecr_dir=""
71fecr_keep=""
72fecr_skip=""
73fecr_script_path=$(dirname ${0})
Jaewan Kim3ad58c32024-06-13 08:59:29 +000074fecr_verbose=""
Jaewan Kim4294ae12024-07-18 15:11:03 +090075fecr_image="${FECR_DEFAULT_IMAGE}"
76fecr_boot_completed_log="${FECR_DEFAULT_BOOT_COMPLETED_LOG}"
Jaewan Kimb5fb9692024-06-10 14:18:12 +090077
78# Parse parameters
79while (( "${#}" )); do
80 case "${1}" in
Jaewan Kim3ad58c32024-06-13 08:59:29 +000081 --verbose)
82 fecr_verbose="true"
83 ;;
Jaewan Kimb5fb9692024-06-10 14:18:12 +090084 --version)
85 shift
86 fecr_version="${1}"
87 ;;
88 --dir)
89 shift
90 fecr_dir="${1}"
91 fecr_keep="true"
92 ;;
93 --keep)
94 fecr_keep="true"
95 ;;
96 --skip)
97 fecr_skip="true"
98 ;;
Jaewan Kim4294ae12024-07-18 15:11:03 +090099 --test)
100 fecr_image="${FECR_TEST_IMAGE}"
101 fecr_boot_completed_log="${FECR_TEST_IMAGE_BOOT_COMPLETED_LOG}"
102 ;;
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900103 -h|--help)
104 print_usage
105 exit 0
106 ;;
107 *)
108 print_usage
109 exit 1
110 ;;
111 esac
112 shift
113done
114
115trap fecr_clean_up INT
116trap fecr_clean_up EXIT
117
Jaewan Kim3ad58c32024-06-13 08:59:29 +0000118if [[ -n "${fecr_verbose}" ]]; then
119 set -x
120fi
121
122. "${fecr_script_path}/ferrochrome-precondition-checker.sh"
123
124resolved_activities=$(adb shell pm query-activities --components -a ${ACTION_NAME})
125
126if [[ "$(echo ${resolved_activities} | wc -l)" != "1" ]]; then
127 >&2 echo "Multiple VM launchers exists"
128 exit 1
129fi
130
131pkg_name=$(dirname ${resolved_activities})
132
133adb shell pm grant ${pkg_name} android.permission.USE_CUSTOM_VIRTUAL_MACHINE > /dev/null
134adb shell pm clear ${pkg_name} > /dev/null
135
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900136if [[ -z "${fecr_skip}" ]]; then
137 if [[ -z "${fecr_dir}" ]]; then
138 # Download fecr image archive, and extract necessary files
139 # DISCLAIMER: Image is too large (1.5G+ for compressed, 6.5G+ for uncompressed), so can't submit.
140 fecr_dir=$(mktemp -d)
141
Jaewan Kim94455ba2024-06-18 14:22:44 +0900142 echo "Downloading & extracting ferrochrome image to ${fecr_dir}"
Jaewan Kim4294ae12024-07-18 15:11:03 +0900143 curl ${FECR_GS_URL}/${fecr_version}/${fecr_image}.tar.xz | tar xfJ - -C ${fecr_dir}
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900144 fi
145
146 echo "Pushing ferrochrome image to ${FECR_DEVICE_DIR}"
147 adb shell mkdir -p ${FECR_DEVICE_DIR} > /dev/null || true
Jaewan Kim4294ae12024-07-18 15:11:03 +0900148 adb push ${fecr_dir}/${fecr_image}.bin ${FECR_DEVICE_DIR}/${FECR_IMAGE_VM_CONFIG_JSON}
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900149 adb push ${fecr_script_path}/assets/vm_config.json ${FECR_CONFIG_PATH}
150fi
151
Jaewan Kim920d4662024-07-09 05:02:36 +0000152echo "Ensure screen unlocked"
Jaewan Kim8b82b4b2024-07-18 21:03:43 +0900153adb shell svc power stayon true
154adb shell wm dismiss-keyguard
Jaewan Kim920d4662024-07-09 05:02:36 +0000155
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900156echo "Starting ferrochrome"
Jaewan Kim3ad58c32024-06-13 08:59:29 +0000157adb shell am start-activity -a ${ACTION_NAME} > /dev/null
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900158
Jaewan Kim56f38342024-07-03 07:54:15 +0000159if [[ $(adb shell getprop ro.fw.mu.headless_system_user) == "true" ]]; then
160 current_user=$(adb shell am get-current-user)
161 log_path="/data/user/${current_user}/${pkg_name}/files/console.log"
162else
163 log_path="/data/data/${pkg_name}/files/console.log"
164fi
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900165fecr_start_time=${EPOCHSECONDS}
166
167while [[ $((EPOCHSECONDS - fecr_start_time)) -lt ${FECR_BOOT_TIMEOUT} ]]; do
Jaewan Kim4294ae12024-07-18 15:11:03 +0900168 adb shell grep -soF \""${fecr_boot_completed_log}"\" "${log_path}" && exit 0 || true
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900169 sleep 10
170done
171
Jaewan Kim3ad58c32024-06-13 08:59:29 +0000172>&2 echo "Ferrochrome failed to boot. Dumping console log"
173>&2 adb shell cat ${log_path}
174
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900175exit 1
Jaewan Kim4294ae12024-07-18 15:11:03 +0900176