-
Notifications
You must be signed in to change notification settings - Fork 2
/
launch-emulator.sh
253 lines (228 loc) · 8.25 KB
/
launch-emulator.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/bin/sh
#
# Copyright 2019 - The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
VERBOSE=3
ANDROID_HOME=/android-home
ANDROID_AVD_HOME=${ANDROID_HOME}/.android/avd
export ANDROID_HOME
export ANDROID_AVD_HOME
# Return the value of a given named variable.
# $1: variable name
#
# example:
# FOO=BAR
# BAR=ZOO
# echo `var_value $FOO`
# will print 'ZOO'
#
var_value () {
eval printf %s \"\$$1\"
}
# Return success if variable $1 is set and non-empty, failure otherwise.
# $1: Variable name.
# Usage example:
# if var_is_set FOO; then
# .. Do something the handle FOO condition.
# fi
var_is_set () {
test -n "$(var_value $1)"
}
_var_quote_value () {
printf %s "$1" | sed -e "s|'|\\'\"'\"\\'|g"
}
# Append a space-separated list of items to a given variable.
# $1: Variable name.
# $2+: Variable value.
# Example:
# FOO=
# var_append FOO foo (FOO is now 'foo')
# var_append FOO bar (FOO is now 'foo bar')
# var_append FOO zoo (FOO is now 'foo bar zoo')
var_append () {
local _var_append_varname
_var_append_varname=$1
shift
if test "$(var_value $_var_append_varname)"; then
eval $_var_append_varname=\$$_var_append_varname\'\ $(_var_quote_value "$*")\'
else
eval $_var_append_varname=\'$(_var_quote_value "$*")\'
fi
}
is_mounted () {
mount | grep "$1"
}
# Run a command, output depends on verbosity level
run () {
if [ "$VERBOSE" -lt 0 ]; then
VERBOSE=0
fi
if [ "$VERBOSE" -gt 1 ]; then
echo "COMMAND: $@"
fi
case $VERBOSE in
0|1)
eval "$@" >/dev/null 2>&1
;;
2)
eval "$@" >/dev/null
;;
*)
eval "$@"
;;
esac
}
log_version_info() {
# This function logs version info.
emulator/emulator -version | head -n 1 | sed -u 's/^/version: /g'
echo 'version: launch_script: {{version}}'
img=$ANDROID_SDK_ROOT/system-images/android
[ -f "$img/x86_64/source.properties" ] && cat "$img/x86_64/source.properties" | sed -u 's/^/version: /g'
[ -f "$img/x86/source.properties" ] && cat "$img/x86/source.properties" | sed -u 's/^/version: /g'
}
install_adb_keys() {
# We do not want to keep adb secrets around, if the emulator
# ever created the secrets itself we will never be able to connect.
run rm -f ${ANDROID_HOME}/.android/adbkey ${ANDROID_HOME}/.android/adbkey.pub
if [ -s "/run/secrets/adbkey" ]; then
echo "emulator: Copying private key from secret partition"
run cp /run/secrets/adbkey ${ANDROID_HOME}/.android
elif [ ! -z "${ADBKEY}" ]; then
echo "emulator: Using provided adb private key"
echo "-----BEGIN PRIVATE KEY-----" >${ANDROID_HOME}/.android/adbkey
echo $ADBKEY | tr " " "\\n" | sed -n "4,29p" >>${ANDROID_HOME}/.android/adbkey
echo "-----END PRIVATE KEY-----" >>${ANDROID_HOME}/.android/adbkey
elif [ ! -z "${ADBKEY_PUB}" ]; then
echo "emulator: Using provided adb public key"
echo $ADBKEY_PUB >>${ANDROID_HOME}/.android/adbkey.pub
else
echo "emulator: No adb key provided, creating internal one, you might not be able connect from adb."
run /android/sdk/platform-tools/adb keygen ${ANDROID_HOME}/.android/adbkey
fi
run chmod 600 ${ANDROID_HOME}/.android/adbkey
}
# Installs the console tokens, if any. The environment variable |TOKEN| will be
# non empty if a token has been set.
install_console_tokens() {
if [ -s "/run/secrets/token" ]; then
echo "emulator: Copying console token from secret partition"
run cp /run/secrets/token ${ANDROID_HOME}/.emulator_console_auth_token
TOKEN=yes
elif [ ! -z "${TOKEN}" ]; then
echo "emulator: Using provided emulator console token"
echo ${TOKEN} >${ANDROID_HOME}/.emulator_console_auth_token
else
echo "emulator: No console token provided, console disabled."
fi
if [ ! -z "${TOKEN}" ]; then
echo "emulator: forwarding the emulator console."
socat -d tcp-listen:5554,reuseaddr,fork tcp:127.0.0.1:5556 &
fi
}
install_grpc_certs() {
# Copy certs if they exists and are not empty.
[ -s "/run/secrets/grpc_cer" ] && cp /run/secrets/grpc_cer ${ANDROID_HOME}/.android/emulator-grpc.cer
[ -s "/run/secrets/grpc_key" ] && cp /run/secrets/grpc_key ${ANDROID_HOME}/.android/emulator-grpc.key
}
clean_up() {
# Delete any leftovers from hard exits.
run rm -rf /tmp/*
run rm -rf ${ANDROID_AVD_HOME}/Pixel2.avd/*.lock
# Check for core-dumps, that might be left over
if ls core* 1>/dev/null 2>&1; then
echo "emulator: ** WARNING ** WARNING ** WARNING **"
echo "emulator: Core dumps exist in this image. This means the emulator has crashed in the past."
fi
mkdir -p ${ANDROID_HOME}/.android
mkdir -p ${ANDROID_AVD_HOME}
}
setup_pulse_audio() {
# We need pulse audio for the webrtc video bridge, let's configure it.
run mkdir -p ${ANDROID_HOME}/.config/pulse
export PULSE_SERVER=unix:/tmp/pulse-socket
run pulseaudio -D -vvvv --log-time=1 --log-target=newfile:/tmp/pulseverbose.log --log-time=1 --exit-idle-time=-1
tail -f /tmp/pulseverbose.log -n +1 | sed -u 's/^/pulse: /g' &
run pactl list || exit 1
}
forward_loggers() {
run mkdir /tmp/android-unknown
run mkfifo /tmp/android-unknown/kernel.log
run mkfifo /tmp/android-unknown/logcat.log
echo "emulator: It is safe to ignore the warnings from tail. The files will come into existence soon."
tail --retry -f /tmp/android-unknown/goldfish_rtc_0 | sed -u 's/^/video: /g' &
cat /tmp/android-unknown/kernel.log | sed -u 's/^/kernel: /g' &
cat /tmp/android-unknown/logcat.log | sed -u 's/^/logcat: /g' &
}
initialize_data_part() {
# Check if we have mounted a data partition (tmpfs, or persistent)
# and if so, we will use that as our avd directory.
if is_mounted /data; then
run cp -fr /android-home/ /data
ln -sf /data/android-home ${ANDROID_AVD_HOME}
echo "path=${ANDROID_AVD_HOME}/Pixel2.avd" > ${ANDROID_AVD_HOME}/Pixel2.ini
else
ln -sf /android-home ${ANDROID_AVD_HOME}
fi
}
# Let us log the emulator,script and image version.
log_version_info
# initialize_data_part
clean_up
install_console_tokens
install_adb_keys
install_grpc_certs
if test "${NO_PULSE_AUDIO}" != "true"; then
setup_pulse_audio
fi
if test "${NO_FORWARD_LOGGERS}" != "true"; then
forward_loggers
fi
# copy config sekelton if config.ini not existing, and let its user override/append to it
if test ! -e ${ANDROID_AVD_HOME}/Pixel2.avd/config.ini; then
cp -dR /android-home-default/* ${ANDROID_AVD_HOME}/
if [ ! -z "${AVD_CONFIG}" ]; then
echo "Adding ${AVD_CONFIG} to config.ini"
echo "${AVD_CONFIG}" >>"${ANDROID_HOME}/Pixel2.avd/config.ini"
fi
echo "path=${ANDROID_AVD_HOME}/Pixel2.avd" > ${ANDROID_AVD_HOME}/Pixel2.ini
fi
# Basic launcher command, additional flags can be added.
LAUNCH_CMD=/android/sdk/emulator/emulator
var_append LAUNCH_CMD -avd Pixel2
var_append LAUNCH_CMD -ports 5556,5557 -grpc 8554
var_append LAUNCH_CMD -skip-adb-auth
if test "${NO_FORWARD_LOGGERS}" != "true"; then
var_append LAUNCH_CMD -shell-serial file:/tmp/android-unknown/kernel.log
var_append LAUNCH_CMD -logcat "*:V"
var_append LAUNCH_CMD -logcat-output /tmp/android-unknown/logcat.log
var_append LAUNCH_CMD -logcat "*:V"
fi
if [ ! -z "${EMULATOR_PARAMS}" ]; then
var_append LAUNCH_CMD $EMULATOR_PARAMS
fi
if [ ! -z "${ADD_EMULATOR_PARAMS}" ]; then
var_append LAUNCH_CMD $ADD_EMULATOR_PARAMS
fi
if [ ! -z "${TURN}" ]; then
var_append LAUNCH_CMD -turncfg \'${TURN}\'
fi
# Add qemu specific parameters
var_append LAUNCH_CMD -qemu -append panic=1
# Launch internal adb server, needed for our health check.
# Once we have the grpc status point we can use that instead.
/android/sdk/platform-tools/adb start-server
# All our ports are loopback devices, so setup a simple forwarder
socat -d tcp-listen:5555,reuseaddr,fork tcp:127.0.0.1:5557 &
# Kick off the emulator
run exec $LAUNCH_CMD