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