blob: 1df5d4f85bed5e1b3e458309c6da0e59379db57f [file] [log] [blame]
Nicolas Geoffrayd2d62d12015-03-18 11:23:56 +00001#!/bin/bash
2#
3# Copyright (C) 2015 The Android Open Source Project
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
Orion Hodson1c5b1ea2020-01-22 15:34:33 +000017# The work does by this script is (mostly) undone by tools/buildbot-teardown-device.sh.
Roland Levillain56049382018-05-23 18:26:22 +010018# Make sure to keep these files in sync.
19
Ulya Trafimovich95977482021-11-09 14:05:14 +000020. "$(dirname $0)/buildbot-utils.sh"
Nicolas Geoffrayd2d62d12015-03-18 11:23:56 +000021
Martin Stjernholmc71aacb2019-02-18 16:35:44 +000022if [ "$1" = --verbose ]; then
23 verbose=true
24else
25 verbose=false
26fi
27
Ulya Trafimovich513801a2023-01-19 18:26:43 +000028# Testing on a Linux VM requires special setup.
29if [[ -n "$ART_TEST_ON_VM" ]]; then
30 [[ -d "$ART_TEST_VM_DIR" ]] || { msgfatal "no VM found in $ART_TEST_VM_DIR"; }
31 $ART_SSH_CMD "true" || { msgerror "no VM (tried \"$ART_SSH_CMD true\""; }
32 $ART_SSH_CMD "
33 mkdir $ART_TEST_CHROOT
34
35 mkdir $ART_TEST_CHROOT/apex
36 mkdir $ART_TEST_CHROOT/bin
Jaeheon Yi1ec83c32023-04-26 14:56:06 -070037 mkdir -p $ART_TEST_CHROOT/data/local/tmp
Ulya Trafimovich513801a2023-01-19 18:26:43 +000038 mkdir $ART_TEST_CHROOT/dev
39 mkdir $ART_TEST_CHROOT/etc
40 mkdir $ART_TEST_CHROOT/lib
41 mkdir $ART_TEST_CHROOT/linkerconfig
42 mkdir $ART_TEST_CHROOT/proc
43 mkdir $ART_TEST_CHROOT/sys
44 mkdir $ART_TEST_CHROOT/system
45 mkdir $ART_TEST_CHROOT/tmp
Jaeheon Yi1ec83c32023-04-26 14:56:06 -070046 mkdir -p $ART_TEST_CHROOT/usr/lib
47 mkdir -p $ART_TEST_CHROOT/usr/share/gdb
Ulya Trafimovich513801a2023-01-19 18:26:43 +000048
Jaeheon Yib45b07a2023-06-06 10:59:19 -070049 sudo mount -t proc /proc $ART_TEST_CHROOT_BASENAME/proc
50 sudo mount -t sysfs /sys $ART_TEST_CHROOT_BASENAME/sys
51 sudo mount --bind /dev $ART_TEST_CHROOT_BASENAME/dev
52 sudo mount --bind /bin $ART_TEST_CHROOT_BASENAME/bin
53 sudo mount --bind /lib $ART_TEST_CHROOT_BASENAME/lib
54 sudo mount --bind /lib $ART_TEST_CHROOT_BASENAME/usr/lib
55 sudo mount --bind /usr/share/gdb $ART_TEST_CHROOT_BASENAME/usr/share/gdb
Ulya Trafimovich513801a2023-01-19 18:26:43 +000056 $ART_CHROOT_CMD echo \"Hello from chroot! I am \$(uname -a).\"
57 "
58 exit 0
59fi
60
61# Regular Android device. Setup as root, as some actions performed here require it.
David Srbecky7a2ce222022-12-08 16:22:31 +000062adb version
Roland Levillain2aab06b2017-03-01 14:14:10 +000063adb root
64adb wait-for-device
65
Ulya Trafimovich5298bcd2021-11-09 14:46:22 +000066msginfo "Date on host"
Roland Levillain2aab06b2017-03-01 14:14:10 +000067date
68
Ulya Trafimovich5298bcd2021-11-09 14:46:22 +000069msginfo "Date on device"
Nicolas Geoffrayd2d62d12015-03-18 11:23:56 +000070adb shell date
71
Roland Levillain2aab06b2017-03-01 14:14:10 +000072host_seconds_since_epoch=$(date -u +%s)
Orion Hodsone7724582022-10-20 15:28:01 +010073
74# Get the device time in seconds, but filter the output as some
75# devices emit CRLF at the end of the command which then breaks the
76# time comparisons in this script (Hammerhead, MRA59G 2457013).
77device_seconds_since_epoch=$(adb shell date -u +%s | tr -c -d '[:digit:]')
Roland Levillain2aab06b2017-03-01 14:14:10 +000078
79abs_time_difference_in_seconds=$(expr $host_seconds_since_epoch - $device_seconds_since_epoch)
80if [ $abs_time_difference_in_seconds -lt 0 ]; then
81 abs_time_difference_in_seconds=$(expr 0 - $abs_time_difference_in_seconds)
82fi
83
84seconds_per_hour=3600
85
David Srbecky06884de2021-05-05 16:00:37 +010086# b/187295147 : Disable live-lock kill daemon.
87# It can confuse long running processes for issues and kill them.
88# This usually manifests as temporarily lost adb connection.
Ulya Trafimovich5298bcd2021-11-09 14:46:22 +000089msginfo "Killing llkd, seen killing adb"
David Srbecky06884de2021-05-05 16:00:37 +010090adb shell setprop ctl.stop llkd-0
91adb shell setprop ctl.stop llkd-1
92
Peter Collingbourne93317bd2022-03-10 11:06:45 -080093product_name=$(adb shell getprop ro.build.product)
94
95if [ "x$product_name" = xfugu ]; then
96 # Kill logd first, so that when we set the adb buffer size later in this file,
97 # it is brought up again.
98 msginfo "Killing logd, seen leaking on fugu/N"
99 adb shell pkill -9 -U logd logd && msginfo "...logd killed"
100fi
Nicolas Geoffrayc2d199b2017-05-22 16:05:06 +0100101
Roland Levillain2aab06b2017-03-01 14:14:10 +0000102# Update date on device if the difference with host is more than one hour.
103if [ $abs_time_difference_in_seconds -gt $seconds_per_hour ]; then
Ulya Trafimovich5298bcd2021-11-09 14:46:22 +0000104 msginfo "Update date on device"
Roland Levillain2aab06b2017-03-01 14:14:10 +0000105 adb shell date -u @$host_seconds_since_epoch
106fi
107
Ulya Trafimovich5298bcd2021-11-09 14:46:22 +0000108msginfo "Turn off selinux"
Nicolas Geoffrayd2d62d12015-03-18 11:23:56 +0000109adb shell setenforce 0
Martin Stjernholmc71aacb2019-02-18 16:35:44 +0000110$verbose && adb shell getenforce
Nicolas Geoffrayd2d62d12015-03-18 11:23:56 +0000111
Ulya Trafimovich5298bcd2021-11-09 14:46:22 +0000112msginfo "Setting local loopback"
Nicolas Geoffray0a38a0e2015-03-25 17:22:34 +0000113adb shell ifconfig lo up
Martin Stjernholmc71aacb2019-02-18 16:35:44 +0000114$verbose && adb shell ifconfig
Nicolas Geoffray0a38a0e2015-03-25 17:22:34 +0000115
Martin Stjernholmc71aacb2019-02-18 16:35:44 +0000116if $verbose; then
Ulya Trafimovich5298bcd2021-11-09 14:46:22 +0000117 msginfo "List properties"
Martin Stjernholmc71aacb2019-02-18 16:35:44 +0000118 adb shell getprop
Nicolas Geoffrayb8703d62015-10-30 11:18:52 +0000119
Ulya Trafimovich5298bcd2021-11-09 14:46:22 +0000120 msginfo "Uptime"
Martin Stjernholmc71aacb2019-02-18 16:35:44 +0000121 adb shell uptime
Nicolas Geoffrayb8703d62015-10-30 11:18:52 +0000122
Ulya Trafimovich5298bcd2021-11-09 14:46:22 +0000123 msginfo "Battery info"
Martin Stjernholmc71aacb2019-02-18 16:35:44 +0000124 adb shell dumpsys battery
125fi
Nicolas Geoffray7ea57472016-02-24 09:53:09 +0000126
Nicolas Geoffrayff43ade2018-07-19 14:17:50 +0100127# Fugu only handles buffer size up to 16MB.
Nicolas Geoffrayff43ade2018-07-19 14:17:50 +0100128if [ "x$product_name" = xfugu ]; then
129 buffer_size=16MB
130else
131 buffer_size=32MB
132fi
133
Ulya Trafimovich5298bcd2021-11-09 14:46:22 +0000134msginfo "Setting adb buffer size to ${buffer_size}"
Nicolas Geoffrayff43ade2018-07-19 14:17:50 +0100135adb logcat -G ${buffer_size}
Martin Stjernholmc71aacb2019-02-18 16:35:44 +0000136$verbose && adb logcat -g
Nicolas Geoffray80d9c852016-03-04 15:28:35 +0000137
Ulya Trafimovich5298bcd2021-11-09 14:46:22 +0000138msginfo "Removing adb spam filter"
Nicolas Geoffray80d9c852016-03-04 15:28:35 +0000139adb logcat -P ""
Martin Stjernholmc71aacb2019-02-18 16:35:44 +0000140$verbose && adb logcat -p
Nicolas Geoffray80d9c852016-03-04 15:28:35 +0000141
Ulya Trafimovich5298bcd2021-11-09 14:46:22 +0000142msginfo "Kill stalled dalvikvm processes"
Nicolas Geoffrayfe6f0b62016-03-07 13:33:37 +0000143# 'ps' on M can sometimes hang.
Vladimir Marko68636922023-08-24 11:42:39 +0000144timeout 5s adb shell "ps" >/dev/null
Artem Serov381d35c2021-02-10 21:27:00 +0000145if [[ $? == 124 ]] && [[ "$ART_TEST_RUN_ON_ARM_FVP" != true ]]; then
Ulya Trafimovich5298bcd2021-11-09 14:46:22 +0000146 msginfo "Rebooting device to fix 'ps'"
Nicolas Geoffrayfe6f0b62016-03-07 13:33:37 +0000147 adb reboot
148 adb wait-for-device root
149else
150 processes=$(adb shell "ps" | grep dalvikvm | awk '{print $2}')
151 for i in $processes; do adb shell kill -9 $i; done
152fi
Roland Levillaine4f1c512017-10-30 13:28:28 +0000153
Roland Levillain72f67742019-03-06 15:48:08 +0000154# Chroot environment.
155# ===================
156
Roland Levillaine4f1c512017-10-30 13:28:28 +0000157if [[ -n "$ART_TEST_CHROOT" ]]; then
158 # Prepare the chroot dir.
Ulya Trafimovich5298bcd2021-11-09 14:46:22 +0000159 msginfo "Prepare the chroot dir in $ART_TEST_CHROOT"
Roland Levillaine4f1c512017-10-30 13:28:28 +0000160
161 # Check that ART_TEST_CHROOT is correctly defined.
162 [[ "x$ART_TEST_CHROOT" = x/* ]] || { echo "$ART_TEST_CHROOT is not an absolute path"; exit 1; }
163
164 # Create chroot.
165 adb shell mkdir -p "$ART_TEST_CHROOT"
166
167 # Provide property_contexts file(s) in chroot.
168 # This is required to have Android system properties work from the chroot.
169 # Notes:
170 # - In Android N, only '/property_contexts' is expected.
Roland Levillain72f67742019-03-06 15:48:08 +0000171 # - In Android O+, property_context files are expected under /system and /vendor.
172 # (See bionic/libc/bionic/system_properties.cpp or
173 # bionic/libc/system_properties/contexts_split.cpp for more information.)
Roland Levillaine4f1c512017-10-30 13:28:28 +0000174 property_context_files="/property_contexts \
175 /system/etc/selinux/plat_property_contexts \
176 /vendor/etc/selinux/nonplat_property_context \
177 /plat_property_contexts \
178 /nonplat_property_contexts"
179 for f in $property_context_files; do
180 adb shell test -f "$f" \
181 "&&" mkdir -p "$ART_TEST_CHROOT$(dirname $f)" \
182 "&&" cp -f "$f" "$ART_TEST_CHROOT$f"
183 done
184
185 # Create directories required for ART testing in chroot.
186 adb shell mkdir -p "$ART_TEST_CHROOT/tmp"
187 adb shell mkdir -p "$ART_TEST_CHROOT/data/dalvik-cache"
188 adb shell mkdir -p "$ART_TEST_CHROOT/data/local/tmp"
189
190 # Populate /etc in chroot with required files.
191 adb shell mkdir -p "$ART_TEST_CHROOT/system/etc"
Roland Levillain433e49f2020-07-27 13:56:46 +0100192 adb shell test -L "$ART_TEST_CHROOT/etc" \
193 || adb shell ln -s system/etc "$ART_TEST_CHROOT/etc"
Roland Levillaine4f1c512017-10-30 13:28:28 +0000194
195 # Provide /proc in chroot.
196 adb shell mkdir -p "$ART_TEST_CHROOT/proc"
197 adb shell mount | grep -q "^proc on $ART_TEST_CHROOT/proc type proc " \
198 || adb shell mount -t proc proc "$ART_TEST_CHROOT/proc"
199
200 # Provide /sys in chroot.
201 adb shell mkdir -p "$ART_TEST_CHROOT/sys"
202 adb shell mount | grep -q "^sysfs on $ART_TEST_CHROOT/sys type sysfs " \
203 || adb shell mount -t sysfs sysfs "$ART_TEST_CHROOT/sys"
204 # Provide /sys/kernel/debug in chroot.
205 adb shell mount | grep -q "^debugfs on $ART_TEST_CHROOT/sys/kernel/debug type debugfs " \
206 || adb shell mount -t debugfs debugfs "$ART_TEST_CHROOT/sys/kernel/debug"
Pierre Langlois338b3e42023-10-10 13:39:42 +0100207 # Provide /sys/kernel/tracing in chroot. Using a bind mount is important,
208 # otherwise mounting tracefs multiple times confuses the
209 # android.hardware.atrace service.
210 adb shell mount | grep -q "^tracefs on $ART_TEST_CHROOT/sys/kernel/tracing type tracefs " \
211 || adb shell mount -o bind /sys/kernel/tracing "$ART_TEST_CHROOT/sys/kernel/tracing"
Roland Levillaine4f1c512017-10-30 13:28:28 +0000212
213 # Provide /dev in chroot.
214 adb shell mkdir -p "$ART_TEST_CHROOT/dev"
215 adb shell mount | grep -q "^tmpfs on $ART_TEST_CHROOT/dev type tmpfs " \
216 || adb shell mount -o bind /dev "$ART_TEST_CHROOT/dev"
Peter Collingbourneb2c68b32022-03-03 12:17:02 -0800217 adb shell mount | grep -q "^devpts on $ART_TEST_CHROOT/dev/pts type devpts " \
218 || adb shell mount -o bind /dev/pts "$ART_TEST_CHROOT/dev/pts"
Jiakai Zhangcc74dec2022-08-01 19:38:44 +0100219 adb shell mount | grep -q " on $ART_TEST_CHROOT/dev/cpuset type cgroup " \
220 || adb shell mount -o bind /dev/cpuset "$ART_TEST_CHROOT/dev/cpuset"
Roland Levillain7b7ea792019-01-08 19:47:50 +0000221
Roland Levillain0f9823e2019-06-18 16:49:24 +0100222 # Create /apex directory in chroot.
Roland Levillain7b7ea792019-01-08 19:47:50 +0000223 adb shell mkdir -p "$ART_TEST_CHROOT/apex"
Roland Levillain15ff34d2020-02-05 19:55:34 +0000224
225 # Create /linkerconfig directory in chroot.
226 adb shell mkdir -p "$ART_TEST_CHROOT/linkerconfig"
Martin Stjernholm8c7e2192020-06-26 15:54:16 +0100227
228 # Create /bin symlink for shebang compatibility.
Roland Levillain433e49f2020-07-27 13:56:46 +0100229 adb shell test -L "$ART_TEST_CHROOT/bin" \
230 || adb shell ln -s system/bin "$ART_TEST_CHROOT/bin"
Roland Levillaine4f1c512017-10-30 13:28:28 +0000231fi