blob: f248eecb6360cecde187c9c114f306f3a4bbec14 [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"
Jaewan Kim4733d7d2024-08-06 16:00:42 +090024FECR_DEFAULT_SCREENSHOT_DIR="/data/local/tmp/ferrochrome_screenshots" # Hardcoded at AndroidTest.xml
Jaewan Kim4294ae12024-07-18 15:11:03 +090025FECR_TEST_IMAGE="chromiumos_test_image"
26FECR_BASE_IMAGE="chromiumos_base_image"
Jaewan Kim5ebeb222024-07-05 19:14:53 +090027FECR_DEVICE_DIR="/data/local/tmp"
Jaewan Kim4294ae12024-07-18 15:11:03 +090028FECR_IMAGE_VM_CONFIG_JSON="chromiumos_base_image.bin" # hardcoded at vm_config.json
Jaewan Kimb5fb9692024-06-10 14:18:12 +090029FECR_CONFIG_PATH="/data/local/tmp/vm_config.json" # hardcoded at VmLauncherApp
30FECR_CONSOLE_LOG_PATH="/data/data/\${pkg_name}/files/console.log"
Jaewan Kim4294ae12024-07-18 15:11:03 +090031FECR_TEST_IMAGE_BOOT_COMPLETED_LOG="Have fun and send patches!"
32FECR_BASE_IMAGE_BOOT_COMPLETED_LOG="Chrome started, our work is done, exiting"
Jaewan Kimb5fb9692024-06-10 14:18:12 +090033FECR_BOOT_TIMEOUT="300" # 5 minutes (300 seconds)
Jaewan Kim3ad58c32024-06-13 08:59:29 +000034ACTION_NAME="android.virtualization.VM_LAUNCHER"
Jaewan Kim4294ae12024-07-18 15:11:03 +090035
36# Match this with AndroidTest.xml and assets/vm_config.json
37FECR_DEFAULT_IMAGE="${FECR_BASE_IMAGE}"
38FECR_DEFAULT_BOOT_COMPLETED_LOG="${FECR_BASE_IMAGE_BOOT_COMPLETED_LOG}"
Jaewan Kimb5fb9692024-06-10 14:18:12 +090039
40fecr_clean_up() {
41 trap - INT
42
Jaewan Kim8b82b4b2024-07-18 21:03:43 +090043 # Reset screen always on
44 adb shell svc power stayon false
45
Jaewan Kimb5fb9692024-06-10 14:18:12 +090046 if [[ -d ${fecr_dir} && -z ${fecr_keep} ]]; then
47 rm -rf ${fecr_dir}
48 fi
49}
50
51print_usage() {
Jaewan Kim3ad58c32024-06-13 08:59:29 +000052 echo "ferochrome: Launches ferrochrome image"
Jaewan Kimb5fb9692024-06-10 14:18:12 +090053 echo ""
Jaewan Kim4294ae12024-07-18 15:11:03 +090054 echo "By default, this downloads ${FECR_DEFAULT_VERSION} with version ${FECR_DEFAULT_VERSION},"
Jaewan Kimb5fb9692024-06-10 14:18:12 +090055 echo "launches, and waits for boot completed."
Jaewan Kim3ad58c32024-06-13 08:59:29 +000056 echo "When done, removes downloaded image on host while keeping pushed image on device."
Jaewan Kimb5fb9692024-06-10 14:18:12 +090057 echo ""
Jaewan Kim3ad58c32024-06-13 08:59:29 +000058 echo "Usage: ferrochrome [options]"
Jaewan Kimb5fb9692024-06-10 14:18:12 +090059 echo ""
60 echo "Options"
61 echo " --help or -h: This message"
Jaewan Kim3ad58c32024-06-13 08:59:29 +000062 echo " --dir DIR: Use ferrochrome images at the dir instead of downloading"
63 echo " --verbose: Verbose log message (set -x)"
Jaewan Kimb5fb9692024-06-10 14:18:12 +090064 echo " --skip: Skipping downloading and/or pushing images"
65 echo " --version \${version}: ferrochrome version to be downloaded"
66 echo " --keep: Keep downloaded ferrochrome image"
Jaewan Kim4294ae12024-07-18 15:11:03 +090067 echo " --test: Download test image instead"
Jaewan Kimb5fb9692024-06-10 14:18:12 +090068}
69
Jaewan Kim4294ae12024-07-18 15:11:03 +090070fecr_version="${FECR_DEFAULT_VERSION}"
Jaewan Kimb5fb9692024-06-10 14:18:12 +090071fecr_dir=""
72fecr_keep=""
73fecr_skip=""
74fecr_script_path=$(dirname ${0})
Jaewan Kim3ad58c32024-06-13 08:59:29 +000075fecr_verbose=""
Jaewan Kim4294ae12024-07-18 15:11:03 +090076fecr_image="${FECR_DEFAULT_IMAGE}"
77fecr_boot_completed_log="${FECR_DEFAULT_BOOT_COMPLETED_LOG}"
Jaewan Kim4733d7d2024-08-06 16:00:42 +090078fecr_screenshot_dir="${FECR_DEFAULT_SCREENSHOT_DIR}"
Jaewan Kimb5fb9692024-06-10 14:18:12 +090079
80# Parse parameters
81while (( "${#}" )); do
82 case "${1}" in
Jaewan Kim3ad58c32024-06-13 08:59:29 +000083 --verbose)
84 fecr_verbose="true"
85 ;;
Jaewan Kimb5fb9692024-06-10 14:18:12 +090086 --version)
87 shift
88 fecr_version="${1}"
89 ;;
90 --dir)
91 shift
92 fecr_dir="${1}"
93 fecr_keep="true"
94 ;;
95 --keep)
96 fecr_keep="true"
97 ;;
98 --skip)
99 fecr_skip="true"
100 ;;
Jaewan Kim4294ae12024-07-18 15:11:03 +0900101 --test)
102 fecr_image="${FECR_TEST_IMAGE}"
103 fecr_boot_completed_log="${FECR_TEST_IMAGE_BOOT_COMPLETED_LOG}"
104 ;;
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900105 -h|--help)
106 print_usage
107 exit 0
108 ;;
109 *)
110 print_usage
111 exit 1
112 ;;
113 esac
114 shift
115done
116
117trap fecr_clean_up INT
118trap fecr_clean_up EXIT
119
Jaewan Kim3ad58c32024-06-13 08:59:29 +0000120if [[ -n "${fecr_verbose}" ]]; then
121 set -x
122fi
123
124. "${fecr_script_path}/ferrochrome-precondition-checker.sh"
125
126resolved_activities=$(adb shell pm query-activities --components -a ${ACTION_NAME})
127
128if [[ "$(echo ${resolved_activities} | wc -l)" != "1" ]]; then
129 >&2 echo "Multiple VM launchers exists"
130 exit 1
131fi
132
133pkg_name=$(dirname ${resolved_activities})
134
135adb shell pm grant ${pkg_name} android.permission.USE_CUSTOM_VIRTUAL_MACHINE > /dev/null
136adb shell pm clear ${pkg_name} > /dev/null
137
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900138if [[ -z "${fecr_skip}" ]]; then
139 if [[ -z "${fecr_dir}" ]]; then
140 # Download fecr image archive, and extract necessary files
141 # DISCLAIMER: Image is too large (1.5G+ for compressed, 6.5G+ for uncompressed), so can't submit.
142 fecr_dir=$(mktemp -d)
143
Jaewan Kim94455ba2024-06-18 14:22:44 +0900144 echo "Downloading & extracting ferrochrome image to ${fecr_dir}"
Jaewan Kim4294ae12024-07-18 15:11:03 +0900145 curl ${FECR_GS_URL}/${fecr_version}/${fecr_image}.tar.xz | tar xfJ - -C ${fecr_dir}
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900146 fi
147
148 echo "Pushing ferrochrome image to ${FECR_DEVICE_DIR}"
149 adb shell mkdir -p ${FECR_DEVICE_DIR} > /dev/null || true
Jaewan Kim4294ae12024-07-18 15:11:03 +0900150 adb push ${fecr_dir}/${fecr_image}.bin ${FECR_DEVICE_DIR}/${FECR_IMAGE_VM_CONFIG_JSON}
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900151 adb push ${fecr_script_path}/assets/vm_config.json ${FECR_CONFIG_PATH}
152fi
153
Jaewan Kim920d4662024-07-09 05:02:36 +0000154echo "Ensure screen unlocked"
Jaewan Kim8b82b4b2024-07-18 21:03:43 +0900155adb shell svc power stayon true
156adb shell wm dismiss-keyguard
Jaewan Kim920d4662024-07-09 05:02:36 +0000157
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900158echo "Starting ferrochrome"
Jaewan Kim3ad58c32024-06-13 08:59:29 +0000159adb shell am start-activity -a ${ACTION_NAME} > /dev/null
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900160
Jaewan Kim56f38342024-07-03 07:54:15 +0000161if [[ $(adb shell getprop ro.fw.mu.headless_system_user) == "true" ]]; then
162 current_user=$(adb shell am get-current-user)
163 log_path="/data/user/${current_user}/${pkg_name}/files/console.log"
164else
165 log_path="/data/data/${pkg_name}/files/console.log"
166fi
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900167fecr_start_time=${EPOCHSECONDS}
168
Jaewan Kim4733d7d2024-08-06 16:00:42 +0900169adb shell mkdir -p "${fecr_screenshot_dir}"
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900170while [[ $((EPOCHSECONDS - fecr_start_time)) -lt ${FECR_BOOT_TIMEOUT} ]]; do
Jaewan Kim4733d7d2024-08-06 16:00:42 +0900171 adb shell screencap -a -p "${fecr_screenshot_dir}/screenshot-${EPOCHSECONDS}.png"
Jaewan Kim4294ae12024-07-18 15:11:03 +0900172 adb shell grep -soF \""${fecr_boot_completed_log}"\" "${log_path}" && exit 0 || true
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900173 sleep 10
174done
175
Jaewan Kim3ad58c32024-06-13 08:59:29 +0000176>&2 echo "Ferrochrome failed to boot. Dumping console log"
177>&2 adb shell cat ${log_path}
178
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900179exit 1
Jaewan Kim4294ae12024-07-18 15:11:03 +0900180