blob: 32db59ca3d9785d08c1ae1a9747141915a8df16d [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"
23FECR_DEFAULT_VERSION="R127-15916.0.0"
Jaewan Kim5ebeb222024-07-05 19:14:53 +090024FECR_DEVICE_DIR="/data/local/tmp"
Jaewan Kimb5fb9692024-06-10 14:18:12 +090025FECR_CONFIG_PATH="/data/local/tmp/vm_config.json" # hardcoded at VmLauncherApp
26FECR_CONSOLE_LOG_PATH="/data/data/\${pkg_name}/files/console.log"
27FECR_BOOT_COMPLETED_LOG="Have fun and send patches!"
28FECR_BOOT_TIMEOUT="300" # 5 minutes (300 seconds)
Jaewan Kim3ad58c32024-06-13 08:59:29 +000029ACTION_NAME="android.virtualization.VM_LAUNCHER"
Jaewan Kim920d4662024-07-09 05:02:36 +000030TRY_UNLOCK_MAX=10
Jaewan Kimb5fb9692024-06-10 14:18:12 +090031
32fecr_clean_up() {
33 trap - INT
34
Jaewan Kim8b82b4b2024-07-18 21:03:43 +090035 # Reset screen always on
36 adb shell svc power stayon false
37
Jaewan Kimb5fb9692024-06-10 14:18:12 +090038 if [[ -d ${fecr_dir} && -z ${fecr_keep} ]]; then
39 rm -rf ${fecr_dir}
40 fi
41}
42
43print_usage() {
Jaewan Kim3ad58c32024-06-13 08:59:29 +000044 echo "ferochrome: Launches ferrochrome image"
Jaewan Kimb5fb9692024-06-10 14:18:12 +090045 echo ""
46 echo "By default, this downloads ferrochrome image with version ${FECR_DEFAULT_VERSION},"
47 echo "launches, and waits for boot completed."
Jaewan Kim3ad58c32024-06-13 08:59:29 +000048 echo "When done, removes downloaded image on host while keeping pushed image on device."
Jaewan Kimb5fb9692024-06-10 14:18:12 +090049 echo ""
Jaewan Kim3ad58c32024-06-13 08:59:29 +000050 echo "Usage: ferrochrome [options]"
Jaewan Kimb5fb9692024-06-10 14:18:12 +090051 echo ""
52 echo "Options"
53 echo " --help or -h: This message"
Jaewan Kim3ad58c32024-06-13 08:59:29 +000054 echo " --dir DIR: Use ferrochrome images at the dir instead of downloading"
55 echo " --verbose: Verbose log message (set -x)"
Jaewan Kimb5fb9692024-06-10 14:18:12 +090056 echo " --skip: Skipping downloading and/or pushing images"
57 echo " --version \${version}: ferrochrome version to be downloaded"
58 echo " --keep: Keep downloaded ferrochrome image"
59}
60
Jaewan Kimb5fb9692024-06-10 14:18:12 +090061fecr_version=""
62fecr_dir=""
63fecr_keep=""
64fecr_skip=""
65fecr_script_path=$(dirname ${0})
Jaewan Kim3ad58c32024-06-13 08:59:29 +000066fecr_verbose=""
Jaewan Kimb5fb9692024-06-10 14:18:12 +090067
68# Parse parameters
69while (( "${#}" )); do
70 case "${1}" in
Jaewan Kim3ad58c32024-06-13 08:59:29 +000071 --verbose)
72 fecr_verbose="true"
73 ;;
Jaewan Kimb5fb9692024-06-10 14:18:12 +090074 --version)
75 shift
76 fecr_version="${1}"
77 ;;
78 --dir)
79 shift
80 fecr_dir="${1}"
81 fecr_keep="true"
82 ;;
83 --keep)
84 fecr_keep="true"
85 ;;
86 --skip)
87 fecr_skip="true"
88 ;;
89 -h|--help)
90 print_usage
91 exit 0
92 ;;
93 *)
94 print_usage
95 exit 1
96 ;;
97 esac
98 shift
99done
100
101trap fecr_clean_up INT
102trap fecr_clean_up EXIT
103
Jaewan Kim3ad58c32024-06-13 08:59:29 +0000104if [[ -n "${fecr_verbose}" ]]; then
105 set -x
106fi
107
108. "${fecr_script_path}/ferrochrome-precondition-checker.sh"
109
110resolved_activities=$(adb shell pm query-activities --components -a ${ACTION_NAME})
111
112if [[ "$(echo ${resolved_activities} | wc -l)" != "1" ]]; then
113 >&2 echo "Multiple VM launchers exists"
114 exit 1
115fi
116
117pkg_name=$(dirname ${resolved_activities})
118
119adb shell pm grant ${pkg_name} android.permission.USE_CUSTOM_VIRTUAL_MACHINE > /dev/null
120adb shell pm clear ${pkg_name} > /dev/null
121
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900122if [[ -z "${fecr_skip}" ]]; then
123 if [[ -z "${fecr_dir}" ]]; then
124 # Download fecr image archive, and extract necessary files
125 # DISCLAIMER: Image is too large (1.5G+ for compressed, 6.5G+ for uncompressed), so can't submit.
126 fecr_dir=$(mktemp -d)
127
Jaewan Kim94455ba2024-06-18 14:22:44 +0900128 echo "Downloading & extracting ferrochrome image to ${fecr_dir}"
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900129 fecr_version=${fecr_version:-${FECR_DEFAULT_VERSION}}
Jaewan Kim94455ba2024-06-18 14:22:44 +0900130 curl ${FECR_GS_URL}/${fecr_version}/chromiumos_test_image.tar.xz | tar xfJ - -C ${fecr_dir}
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900131 fi
132
133 echo "Pushing ferrochrome image to ${FECR_DEVICE_DIR}"
134 adb shell mkdir -p ${FECR_DEVICE_DIR} > /dev/null || true
135 adb push ${fecr_dir}/chromiumos_test_image.bin ${FECR_DEVICE_DIR}
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900136 adb push ${fecr_script_path}/assets/vm_config.json ${FECR_CONFIG_PATH}
137fi
138
Jaewan Kim920d4662024-07-09 05:02:36 +0000139echo "Ensure screen unlocked"
Jaewan Kim8b82b4b2024-07-18 21:03:43 +0900140adb shell svc power stayon true
141adb shell wm dismiss-keyguard
Jaewan Kim920d4662024-07-09 05:02:36 +0000142
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900143echo "Starting ferrochrome"
Jaewan Kim3ad58c32024-06-13 08:59:29 +0000144adb shell am start-activity -a ${ACTION_NAME} > /dev/null
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900145
Jaewan Kim56f38342024-07-03 07:54:15 +0000146if [[ $(adb shell getprop ro.fw.mu.headless_system_user) == "true" ]]; then
147 current_user=$(adb shell am get-current-user)
148 log_path="/data/user/${current_user}/${pkg_name}/files/console.log"
149else
150 log_path="/data/data/${pkg_name}/files/console.log"
151fi
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900152fecr_start_time=${EPOCHSECONDS}
153
154while [[ $((EPOCHSECONDS - fecr_start_time)) -lt ${FECR_BOOT_TIMEOUT} ]]; do
155 adb shell grep -sF \""${FECR_BOOT_COMPLETED_LOG}"\" "${log_path}" && exit 0
156 sleep 10
157done
158
Jaewan Kim3ad58c32024-06-13 08:59:29 +0000159>&2 echo "Ferrochrome failed to boot. Dumping console log"
160>&2 adb shell cat ${log_path}
161
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900162exit 1