blob: d72e8828a0a9e22dc765fe47425bfc2bdd3f8e52 [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"
24FECR_DEVICE_DIR="/data/local/tmp/ferrochrome"
25FECR_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 Kimb5fb9692024-06-10 14:18:12 +090030
31fecr_clean_up() {
32 trap - INT
33
34 if [[ -d ${fecr_dir} && -z ${fecr_keep} ]]; then
35 rm -rf ${fecr_dir}
36 fi
37}
38
39print_usage() {
Jaewan Kim3ad58c32024-06-13 08:59:29 +000040 echo "ferochrome: Launches ferrochrome image"
Jaewan Kimb5fb9692024-06-10 14:18:12 +090041 echo ""
42 echo "By default, this downloads ferrochrome image with version ${FECR_DEFAULT_VERSION},"
43 echo "launches, and waits for boot completed."
Jaewan Kim3ad58c32024-06-13 08:59:29 +000044 echo "When done, removes downloaded image on host while keeping pushed image on device."
Jaewan Kimb5fb9692024-06-10 14:18:12 +090045 echo ""
Jaewan Kim3ad58c32024-06-13 08:59:29 +000046 echo "Usage: ferrochrome [options]"
Jaewan Kimb5fb9692024-06-10 14:18:12 +090047 echo ""
48 echo "Options"
49 echo " --help or -h: This message"
Jaewan Kim3ad58c32024-06-13 08:59:29 +000050 echo " --dir DIR: Use ferrochrome images at the dir instead of downloading"
51 echo " --verbose: Verbose log message (set -x)"
Jaewan Kimb5fb9692024-06-10 14:18:12 +090052 echo " --skip: Skipping downloading and/or pushing images"
53 echo " --version \${version}: ferrochrome version to be downloaded"
54 echo " --keep: Keep downloaded ferrochrome image"
55}
56
Jaewan Kimb5fb9692024-06-10 14:18:12 +090057fecr_version=""
58fecr_dir=""
59fecr_keep=""
60fecr_skip=""
61fecr_script_path=$(dirname ${0})
Jaewan Kim3ad58c32024-06-13 08:59:29 +000062fecr_verbose=""
Jaewan Kimb5fb9692024-06-10 14:18:12 +090063
64# Parse parameters
65while (( "${#}" )); do
66 case "${1}" in
Jaewan Kim3ad58c32024-06-13 08:59:29 +000067 --verbose)
68 fecr_verbose="true"
69 ;;
Jaewan Kimb5fb9692024-06-10 14:18:12 +090070 --version)
71 shift
72 fecr_version="${1}"
73 ;;
74 --dir)
75 shift
76 fecr_dir="${1}"
77 fecr_keep="true"
78 ;;
79 --keep)
80 fecr_keep="true"
81 ;;
82 --skip)
83 fecr_skip="true"
84 ;;
85 -h|--help)
86 print_usage
87 exit 0
88 ;;
89 *)
90 print_usage
91 exit 1
92 ;;
93 esac
94 shift
95done
96
97trap fecr_clean_up INT
98trap fecr_clean_up EXIT
99
Jaewan Kim3ad58c32024-06-13 08:59:29 +0000100if [[ -n "${fecr_verbose}" ]]; then
101 set -x
102fi
103
104. "${fecr_script_path}/ferrochrome-precondition-checker.sh"
105
106resolved_activities=$(adb shell pm query-activities --components -a ${ACTION_NAME})
107
108if [[ "$(echo ${resolved_activities} | wc -l)" != "1" ]]; then
109 >&2 echo "Multiple VM launchers exists"
110 exit 1
111fi
112
113pkg_name=$(dirname ${resolved_activities})
114
115adb shell pm grant ${pkg_name} android.permission.USE_CUSTOM_VIRTUAL_MACHINE > /dev/null
116adb shell pm clear ${pkg_name} > /dev/null
117
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900118if [[ -z "${fecr_skip}" ]]; then
119 if [[ -z "${fecr_dir}" ]]; then
120 # Download fecr image archive, and extract necessary files
121 # DISCLAIMER: Image is too large (1.5G+ for compressed, 6.5G+ for uncompressed), so can't submit.
122 fecr_dir=$(mktemp -d)
123
Jaewan Kim94455ba2024-06-18 14:22:44 +0900124 echo "Downloading & extracting ferrochrome image to ${fecr_dir}"
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900125 fecr_version=${fecr_version:-${FECR_DEFAULT_VERSION}}
Jaewan Kim94455ba2024-06-18 14:22:44 +0900126 curl ${FECR_GS_URL}/${fecr_version}/chromiumos_test_image.tar.xz | tar xfJ - -C ${fecr_dir}
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900127 fi
128
129 echo "Pushing ferrochrome image to ${FECR_DEVICE_DIR}"
130 adb shell mkdir -p ${FECR_DEVICE_DIR} > /dev/null || true
131 adb push ${fecr_dir}/chromiumos_test_image.bin ${FECR_DEVICE_DIR}
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900132 adb push ${fecr_script_path}/assets/vm_config.json ${FECR_CONFIG_PATH}
133fi
134
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900135echo "Starting ferrochrome"
Jaewan Kim3ad58c32024-06-13 08:59:29 +0000136adb shell am start-activity -a ${ACTION_NAME} > /dev/null
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900137
138log_path="/data/data/${pkg_name}/files/console.log"
139fecr_start_time=${EPOCHSECONDS}
140
141while [[ $((EPOCHSECONDS - fecr_start_time)) -lt ${FECR_BOOT_TIMEOUT} ]]; do
142 adb shell grep -sF \""${FECR_BOOT_COMPLETED_LOG}"\" "${log_path}" && exit 0
143 sleep 10
144done
145
Jaewan Kim3ad58c32024-06-13 08:59:29 +0000146>&2 echo "Ferrochrome failed to boot. Dumping console log"
147>&2 adb shell cat ${log_path}
148
Jaewan Kimb5fb9692024-06-10 14:18:12 +0900149exit 1