• 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 -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
96# Update date on device if the difference with host is more than one hour.
97if [ $abs_time_difference_in_seconds -gt $seconds_per_hour ]; then
98  msginfo "Update date on device"
99  adb shell date -u @$host_seconds_since_epoch
100fi
101
102msginfo "Turn off selinux"
103adb shell setenforce 0
104$verbose && adb shell getenforce
105
106msginfo "Setting local loopback"
107adb shell ifconfig lo up
108$verbose && adb shell ifconfig
109
110if $verbose; then
111  msginfo "List properties"
112  adb shell getprop
113
114  msginfo "Uptime"
115  adb shell uptime
116
117  msginfo "Battery info"
118  adb shell dumpsys battery
119fi
120
121# Fugu only handles buffer size up to 16MB.
122if [ "x$product_name" = xfugu ]; then
123  buffer_size=16MB
124else
125  buffer_size=32MB
126fi
127
128msginfo "Setting adb buffer size to ${buffer_size}"
129adb logcat -G ${buffer_size}
130$verbose && adb logcat -g
131
132msginfo "Removing adb spam filter"
133adb logcat -P ""
134$verbose && adb logcat -p
135
136msginfo "Kill stalled dalvikvm processes"
137# 'ps' on M can sometimes hang.
138timeout 5s adb shell "ps" >/dev/null
139if [[ $? == 124 ]] && [[ "$ART_TEST_RUN_ON_ARM_FVP" != true ]]; then
140  msginfo "Rebooting device to fix 'ps'"
141  adb reboot
142  adb wait-for-device root
143else
144  processes=$(adb shell "ps" | grep dalvikvm | awk '{print $2}')
145  for i in $processes; do adb shell kill -9 $i; done
146fi
147
148# Chroot environment.
149# ===================
150
151if [[ -n "$ART_TEST_CHROOT" ]]; then
152  # Prepare the chroot dir.
153  msginfo "Prepare the chroot dir in $ART_TEST_CHROOT"
154
155  # Check that ART_TEST_CHROOT is correctly defined.
156  [[ "x$ART_TEST_CHROOT" = x/* ]] || { echo "$ART_TEST_CHROOT is not an absolute path"; exit 1; }
157
158  # Create chroot.
159  adb shell mkdir -p "$ART_TEST_CHROOT"
160
161  # Provide property_contexts file(s) in chroot.
162  # This is required to have Android system properties work from the chroot.
163  # Notes:
164  # - In Android N, only '/property_contexts' is expected.
165  # - In Android O+, property_context files are expected under /system and /vendor.
166  # (See bionic/libc/bionic/system_properties.cpp or
167  # bionic/libc/system_properties/contexts_split.cpp for more information.)
168  property_context_files="/property_contexts \
169    /system/etc/selinux/plat_property_contexts \
170    /vendor/etc/selinux/nonplat_property_context \
171    /plat_property_contexts \
172    /nonplat_property_contexts"
173  for f in $property_context_files; do
174    adb shell test -f "$f" \
175      "&&" mkdir -p "$ART_TEST_CHROOT$(dirname $f)" \
176      "&&" cp -f "$f" "$ART_TEST_CHROOT$f"
177  done
178
179  # Create directories required for ART testing in chroot.
180  adb shell mkdir -p "$ART_TEST_CHROOT/tmp"
181  adb shell mkdir -p "$ART_TEST_CHROOT/data/dalvik-cache"
182  adb shell mkdir -p "$ART_TEST_CHROOT/data/local/tmp"
183
184  # Populate /etc in chroot with required files.
185  adb shell mkdir -p "$ART_TEST_CHROOT/system/etc"
186  adb shell test -L "$ART_TEST_CHROOT/etc" \
187    || adb shell ln -s system/etc "$ART_TEST_CHROOT/etc"
188
189  # Provide /proc in chroot.
190  adb shell mkdir -p "$ART_TEST_CHROOT/proc"
191  adb shell mount | grep -q "^proc on $ART_TEST_CHROOT/proc type proc " \
192    || adb shell mount -t proc proc "$ART_TEST_CHROOT/proc"
193
194  # Provide /sys in chroot.
195  adb shell mkdir -p "$ART_TEST_CHROOT/sys"
196  adb shell mount | grep -q "^sysfs on $ART_TEST_CHROOT/sys type sysfs " \
197    || adb shell mount -t sysfs sysfs "$ART_TEST_CHROOT/sys"
198  # Provide /sys/kernel/debug in chroot.
199  adb shell mount | grep -q "^debugfs on $ART_TEST_CHROOT/sys/kernel/debug type debugfs " \
200    || adb shell mount -t debugfs debugfs "$ART_TEST_CHROOT/sys/kernel/debug"
201  # Provide /sys/kernel/tracing in chroot. Using a bind mount is important,
202  # otherwise mounting tracefs multiple times confuses the
203  # android.hardware.atrace service.
204  adb shell mount | grep -q "^tracefs on $ART_TEST_CHROOT/sys/kernel/tracing type tracefs " \
205    || adb shell mount -o bind /sys/kernel/tracing "$ART_TEST_CHROOT/sys/kernel/tracing"
206
207  # Provide /dev in chroot.
208  adb shell mkdir -p "$ART_TEST_CHROOT/dev"
209  adb shell mount | grep -q "^tmpfs on $ART_TEST_CHROOT/dev type tmpfs " \
210    || adb shell mount -o bind /dev "$ART_TEST_CHROOT/dev"
211  adb shell mount | grep -q "^devpts on $ART_TEST_CHROOT/dev/pts type devpts " \
212    || adb shell mount -o bind /dev/pts "$ART_TEST_CHROOT/dev/pts"
213  adb shell mount | grep -q " on $ART_TEST_CHROOT/dev/cpuset type cgroup " \
214    || adb shell mount -o bind /dev/cpuset "$ART_TEST_CHROOT/dev/cpuset"
215
216  # Create /apex directory in chroot.
217  adb shell mkdir -p "$ART_TEST_CHROOT/apex"
218
219  # Create /linkerconfig directory in chroot.
220  adb shell mkdir -p "$ART_TEST_CHROOT/linkerconfig"
221
222  # Create /bin symlink for shebang compatibility.
223  adb shell test -L "$ART_TEST_CHROOT/bin" \
224    || adb shell ln -s system/bin "$ART_TEST_CHROOT/bin"
225fi
226