xref: /aosp_15_r20/art/tools/buildbot-setup-device.sh (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1#!/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
17# The work does by this script is (mostly) undone by tools/buildbot-teardown-device.sh.
18# Make sure to keep these files in sync.
19
20. "$(dirname $0)/buildbot-utils.sh"
21
22if [ "$1" = --verbose ]; then
23  verbose=true
24else
25  verbose=false
26fi
27
28# 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
37    mkdir -p $ART_TEST_CHROOT/data/local/tmp
38    mkdir -p $ART_TEST_CHROOT/data/misc/trace
39    mkdir $ART_TEST_CHROOT/dev
40    mkdir $ART_TEST_CHROOT/etc
41    mkdir $ART_TEST_CHROOT/lib
42    mkdir $ART_TEST_CHROOT/linkerconfig
43    mkdir $ART_TEST_CHROOT/proc
44    mkdir $ART_TEST_CHROOT/sys
45    mkdir $ART_TEST_CHROOT/system
46    mkdir $ART_TEST_CHROOT/tmp
47    mkdir -p $ART_TEST_CHROOT/usr/lib
48    mkdir -p $ART_TEST_CHROOT/usr/share/gdb
49
50    sudo mount -t proc /proc $ART_TEST_CHROOT_BASENAME/proc
51    sudo mount -t sysfs /sys $ART_TEST_CHROOT_BASENAME/sys
52    sudo mount --bind /dev $ART_TEST_CHROOT_BASENAME/dev
53    sudo mount --bind /bin $ART_TEST_CHROOT_BASENAME/bin
54    sudo mount --bind /lib $ART_TEST_CHROOT_BASENAME/lib
55    sudo mount --bind /lib $ART_TEST_CHROOT_BASENAME/usr/lib
56    sudo mount --bind /usr/share/gdb $ART_TEST_CHROOT_BASENAME/usr/share/gdb
57    $ART_CHROOT_CMD echo \"Hello from chroot! I am \$(uname -a).\"
58  "
59  exit 0
60fi
61
62# Regular Android device. Setup as root, as some actions performed here require it.
63adb version
64adb root
65adb wait-for-device
66
67msginfo "Date on host"
68date
69
70msginfo "Date on device"
71adb shell date
72
73host_seconds_since_epoch=$(date -u +%s)
74
75# Get the device time in seconds, but filter the output as some
76# devices emit CRLF at the end of the command which then breaks the
77# time comparisons in this script (Hammerhead, MRA59G 2457013).
78device_seconds_since_epoch=$(adb shell date -u +%s | tr -c -d '[:digit:]')
79
80abs_time_difference_in_seconds=$(expr $host_seconds_since_epoch - $device_seconds_since_epoch)
81if [ $abs_time_difference_in_seconds -lt 0 ]; then
82  abs_time_difference_in_seconds=$(expr 0 - $abs_time_difference_in_seconds)
83fi
84
85seconds_per_hour=3600
86
87# b/187295147 : Disable live-lock kill daemon.
88# It can confuse long running processes for issues and kill them.
89# This usually manifests as temporarily lost adb connection.
90msginfo "Killing llkd, seen killing adb"
91adb shell setprop ctl.stop llkd-0
92adb shell setprop ctl.stop llkd-1
93
94product_name=$(adb shell getprop ro.build.product)
95
96if [ "x$product_name" = xfugu ]; then
97  # Kill logd first, so that when we set the adb buffer size later in this file,
98  # it is brought up again.
99  msginfo "Killing logd, seen leaking on fugu/N"
100  adb shell pkill -9 -U logd logd && msginfo "...logd killed"
101fi
102
103# Update date on device if the difference with host is more than one hour.
104if [ $abs_time_difference_in_seconds -gt $seconds_per_hour ]; then
105  msginfo "Update date on device"
106  adb shell date -u @$host_seconds_since_epoch
107fi
108
109msginfo "Turn off selinux"
110adb shell setenforce 0
111$verbose && adb shell getenforce
112
113msginfo "Setting local loopback"
114adb shell ifconfig lo up
115$verbose && adb shell ifconfig
116
117if $verbose; then
118  msginfo "List properties"
119  adb shell getprop
120
121  msginfo "Uptime"
122  adb shell uptime
123
124  msginfo "Battery info"
125  adb shell dumpsys battery
126fi
127
128# Fugu only handles buffer size up to 16MB.
129if [ "x$product_name" = xfugu ]; then
130  buffer_size=16MB
131else
132  buffer_size=32MB
133fi
134
135msginfo "Setting adb buffer size to ${buffer_size}"
136adb logcat -G ${buffer_size}
137$verbose && adb logcat -g
138
139msginfo "Removing adb spam filter"
140adb logcat -P ""
141$verbose && adb logcat -p
142
143msginfo "Kill stalled dalvikvm processes"
144# 'ps' on M can sometimes hang.
145timeout 5s adb shell "ps" >/dev/null
146if [[ $? == 124 ]] && [[ "$ART_TEST_RUN_ON_ARM_FVP" != true ]]; then
147  msginfo "Rebooting device to fix 'ps'"
148  adb reboot
149  adb wait-for-device root
150else
151  processes=$(adb shell "ps" | grep dalvikvm | awk '{print $2}')
152  for i in $processes; do adb shell kill -9 $i; done
153fi
154
155# Chroot environment.
156# ===================
157
158if [[ -n "$ART_TEST_CHROOT" ]]; then
159  # Prepare the chroot dir.
160  msginfo "Prepare the chroot dir in $ART_TEST_CHROOT"
161
162  # Check that ART_TEST_CHROOT is correctly defined.
163  [[ "x$ART_TEST_CHROOT" = x/* ]] || { echo "$ART_TEST_CHROOT is not an absolute path"; exit 1; }
164
165  # Create chroot.
166  adb shell mkdir -p "$ART_TEST_CHROOT"
167
168  # Provide property_contexts file(s) in chroot.
169  # This is required to have Android system properties work from the chroot.
170  # Notes:
171  # - In Android N, only '/property_contexts' is expected.
172  # - In Android O+, property_context files are expected under /system and /vendor.
173  # (See bionic/libc/bionic/system_properties.cpp or
174  # bionic/libc/system_properties/contexts_split.cpp for more information.)
175  property_context_files="/property_contexts \
176    /system/etc/selinux/plat_property_contexts \
177    /vendor/etc/selinux/nonplat_property_context \
178    /plat_property_contexts \
179    /nonplat_property_contexts"
180  for f in $property_context_files; do
181    adb shell test -f "$f" \
182      "&&" mkdir -p "$ART_TEST_CHROOT$(dirname $f)" \
183      "&&" cp -f "$f" "$ART_TEST_CHROOT$f"
184  done
185
186  # Create directories required for ART testing in chroot.
187  adb shell mkdir -p "$ART_TEST_CHROOT/tmp"
188  adb shell mkdir -p "$ART_TEST_CHROOT/data/dalvik-cache"
189  adb shell mkdir -p "$ART_TEST_CHROOT/data/local/tmp"
190
191  # Populate /etc in chroot with required files.
192  adb shell mkdir -p "$ART_TEST_CHROOT/system/etc"
193  adb shell test -L "$ART_TEST_CHROOT/etc" \
194    || adb shell ln -s system/etc "$ART_TEST_CHROOT/etc"
195
196  # Provide /proc in chroot.
197  adb shell mkdir -p "$ART_TEST_CHROOT/proc"
198  adb shell mount | grep -q "^proc on $ART_TEST_CHROOT/proc type proc " \
199    || adb shell mount -t proc proc "$ART_TEST_CHROOT/proc"
200
201  # Provide /sys in chroot.
202  adb shell mkdir -p "$ART_TEST_CHROOT/sys"
203  adb shell mount | grep -q "^sysfs on $ART_TEST_CHROOT/sys type sysfs " \
204    || adb shell mount -t sysfs sysfs "$ART_TEST_CHROOT/sys"
205  # Provide /sys/kernel/debug in chroot.
206  adb shell mount | grep -q "^debugfs on $ART_TEST_CHROOT/sys/kernel/debug type debugfs " \
207    || adb shell mount -t debugfs debugfs "$ART_TEST_CHROOT/sys/kernel/debug"
208  # Provide /sys/kernel/tracing in chroot. Using a bind mount is important,
209  # otherwise mounting tracefs multiple times confuses the
210  # android.hardware.atrace service.
211  adb shell mount | grep -q "^tracefs on $ART_TEST_CHROOT/sys/kernel/tracing type tracefs " \
212    || adb shell mount -o bind /sys/kernel/tracing "$ART_TEST_CHROOT/sys/kernel/tracing"
213
214  # Provide /dev in chroot.
215  adb shell mkdir -p "$ART_TEST_CHROOT/dev"
216  adb shell mount | grep -q "^tmpfs on $ART_TEST_CHROOT/dev type tmpfs " \
217    || adb shell mount -o bind /dev "$ART_TEST_CHROOT/dev"
218  adb shell mount | grep -q "^devpts on $ART_TEST_CHROOT/dev/pts type devpts " \
219    || adb shell mount -o bind /dev/pts "$ART_TEST_CHROOT/dev/pts"
220  adb shell mount | grep -q " on $ART_TEST_CHROOT/dev/cpuset type cgroup " \
221    || adb shell mount -o bind /dev/cpuset "$ART_TEST_CHROOT/dev/cpuset"
222
223  # Create /apex directory in chroot.
224  adb shell mkdir -p "$ART_TEST_CHROOT/apex"
225
226  # Create /linkerconfig directory in chroot.
227  adb shell mkdir -p "$ART_TEST_CHROOT/linkerconfig"
228
229  # Create /bin symlink for shebang compatibility.
230  adb shell test -L "$ART_TEST_CHROOT/bin" \
231    || adb shell ln -s system/bin "$ART_TEST_CHROOT/bin"
232fi
233