emu.sh (8895B)
1 #!/bin/bash 2 # This file is part of GNU Taler 3 # (C) 2026 Taler Systems S.A. 4 # 5 # GNU Taler is free software; you can redistribute it and/or modify it under the 6 # terms of the GNU General Public License as published by the Free Software 7 # Foundation; either version 3, or (at your option) any later version. 8 # 9 # GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY 10 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 # A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 # 13 # You should have received a copy of the GNU General Public License along with 14 # GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 # 16 # Emulator lifecycle for the wallet simulator. One AVD per worker; the worker 17 # index picks both the AVD name and the (even) console port, so several workers 18 # can run side by side against the same Taler stack. 19 # 20 # Usage: 21 # emu.sh create <worker> create/replace the AVD 22 # emu.sh boot <worker> boot headless and wait for boot_completed 23 # emu.sh prepare <worker> <apk> install the wallet, grant permissions, 24 # kill animations, map the stack ports 25 # emu.sh reverse <worker> (re)map the stack ports into the emulator 26 # emu.sh snapshot <worker> <name> save a snapshot 27 # emu.sh restore <worker> <name> load a snapshot (fast episode reset) 28 # emu.sh serial <worker> print the adb serial 29 # emu.sh kill <worker> shut the emulator down 30 31 set -eu 32 33 SIM_ROOT=${SIM_ROOT:-/opt/taler-sim} 34 export ANDROID_SDK_ROOT=${ANDROID_SDK_ROOT:-/opt/android-sdk} 35 export ANDROID_HOME=$ANDROID_SDK_ROOT 36 export ANDROID_AVD_HOME=${ANDROID_AVD_HOME:-$SIM_ROOT/avd} 37 ADB=$ANDROID_SDK_ROOT/platform-tools/adb 38 EMULATOR=$ANDROID_SDK_ROOT/emulator/emulator 39 AVDMANAGER=$ANDROID_SDK_ROOT/cmdline-tools/latest/bin/avdmanager 40 IMAGE=${SIM_SYSTEM_IMAGE:-"system-images;android-36;google_apis;x86_64"} 41 # A PNG to hang on the wall of the emulator's virtual scene, so the camera has 42 # something real to decode. Only settable at launch, hence one boot per image. 43 POSTER=${SIM_POSTER:-} 44 PKG=${SIM_PKG:-net.taler.wallet.fdroid.debug} 45 BOOT_TIMEOUT=${SIM_BOOT_TIMEOUT:-300} 46 47 die() { echo "emu.sh: $*" >&2; exit 1; } 48 49 worker=${2:-0} 50 avd_name() { echo "taler-sim-$worker"; } 51 # The API level the image carries, so the AVD can be tuned per platform. 52 api_level() { echo "$IMAGE" | sed -n 's/.*android-\([0-9][0-9]*\).*/\1/p'; } 53 # The virtual scene camera, which is what lets a QR code be shown to the 54 # device, needs an image that can deliver NV21 frames: below API 26 the camera 55 # service rejects the format ("Unsupported camera format for virtual scene 56 # views 825382478" -- that number is the NV21 fourcc) and retries until the 57 # boot times out. Those levels get the old emulated camera instead, which no 58 # scenario in the version matrix needs to decode from. 59 camera_back() { [ "$(api_level)" -ge 26 ] 2>/dev/null && echo virtualscene || echo emulated; } 60 # An old system image will not boot on a new image's budget. API 24 never 61 # finished booting with 4 GB of RAM and a 6 GB data partition -- 1200 seconds, 62 # twice, from a clean start and with the emulated camera, so neither the 63 # teardown nor the camera was the cause -- and boots in 35 seconds with 2 GB 64 # and 2 GB. The device profile is not the problem: pixel_6 boots fine at that 65 # size. Levels below 29 therefore get the smaller budget. 66 old_image() { [ "$(api_level)" -lt 29 ] 2>/dev/null; } 67 ram_size() { old_image && echo 2048 || echo 4096; } 68 heap_size() { old_image && echo 256 || echo 512; } 69 data_partition() { old_image && echo 2048M || echo 6144M; } 70 console_port() { echo $((5554 + 2 * worker)); } 71 serial() { echo "emulator-$(console_port)"; } 72 73 create() { 74 mkdir -p "$ANDROID_AVD_HOME" 75 echo no | "$AVDMANAGER" create avd -n "$(avd_name)" -k "$IMAGE" \ 76 -d pixel_6 --force >/dev/null 77 local cfg="$ANDROID_AVD_HOME/$(avd_name).avd/config.ini" 78 python3 - "$cfg" "$(camera_back)" "$(ram_size)" "$(heap_size)" "$(data_partition)" <<'PY' 79 import sys 80 path = sys.argv[1] 81 cfg = {} 82 for line in open(path): 83 if '=' in line: 84 k, v = line.rstrip('\n').split('=', 1) 85 cfg[k] = v 86 # A temp data partition would throw away everything on shutdown, which breaks 87 # snapshots; the rest is headless tuning. 88 cfg.pop('disk.dataPartition.path', None) 89 cfg.update({ 90 'hw.ramSize': sys.argv[3], 91 'vm.heapSize': sys.argv[4], 92 'disk.dataPartition.size': sys.argv[5], 93 'hw.audioInput': 'no', 94 'hw.audioOutput': 'no', 95 'hw.camera.back': sys.argv[2], 96 'hw.camera.front': 'emulated', 97 'hw.keyboard': 'yes', 98 'hw.gpu.enabled': 'yes', 99 'hw.gpu.mode': 'swiftshader_indirect', 100 'hw.lcd.width': '1080', 101 'hw.lcd.height': '2400', 102 'hw.lcd.density': '420', 103 'showDeviceFrame': 'no', 104 }) 105 with open(path, 'w') as f: 106 f.writelines(f'{k}={v}\n' for k, v in sorted(cfg.items())) 107 PY 108 echo "created $(avd_name)" 109 } 110 111 kill_emu() { 112 local s waited=0 port pid 113 s=$(serial) 114 port=$(console_port) 115 "$ADB" -s "$s" emu kill >/dev/null 2>&1 || true 116 # Waiting matters: `emu kill` returns as soon as the console accepts it, 117 # and the next AVD is created with the same name. A process still holding 118 # that AVD's lock files is why every second level of the version matrix 119 # failed to boot -- 26 ok, 28 failed, 29 ok, 30 failed, and so on. 120 while [ "$waited" -lt 90 ]; do 121 "$ADB" devices 2>/dev/null | grep -q "^$s" || break 122 sleep 2 123 waited=$((waited + 2)) 124 done 125 for _ in $(seq 1 30); do 126 pid=$(pgrep -f -- "-port $port" | head -1) 127 [ -n "$pid" ] || break 128 kill "$pid" 2>/dev/null || true 129 sleep 2 130 done 131 rm -f "$ANDROID_AVD_HOME/$(avd_name).avd"/*.lock 2>/dev/null || true 132 echo "killed $s after ${waited}s" 133 } 134 135 boot() { 136 local log="$SIM_ROOT/run/emu-$worker.log" 137 mkdir -p "$SIM_ROOT/run" 138 "$ADB" start-server >/dev/null 2>&1 || true 139 local camera="$(camera_back)" 140 local poster_args=() 141 if [ -n "$POSTER" ] && [ "$camera" = virtualscene ]; then 142 [ -r "$POSTER" ] || die "no poster image at $POSTER" 143 poster_args=(-virtualscene-poster "wall=$POSTER") 144 fi 145 local wipe_args=() 146 [ "${SIM_KEEP_DATA:-0}" = "1" ] || wipe_args=(-wipe-data) 147 setsid "$EMULATOR" -avd "$(avd_name)" -port "$(console_port)" \ 148 -no-window -no-audio -no-boot-anim -gpu swiftshader_indirect \ 149 -no-snapshot-load -no-snapshot-save "${wipe_args[@]}" \ 150 -camera-back "$camera" "${poster_args[@]}" \ 151 > "$log" 2>&1 < /dev/null & 152 echo "booting $(avd_name) on $(serial), log $log" >&2 153 local waited=0 154 while [ "$waited" -lt "$BOOT_TIMEOUT" ]; do 155 if [ "$("$ADB" -s "$(serial)" shell getprop sys.boot_completed 2>/dev/null | tr -d '\r\n')" = "1" ]; then 156 echo "booted after ${waited}s" 157 return 0 158 fi 159 sleep 2 160 waited=$((waited + 2)) 161 done 162 tail -20 "$log" >&2 || true 163 die "emulator did not finish booting within ${BOOT_TIMEOUT}s" 164 } 165 166 reverse() { 167 local s env_json p 168 s=$(serial) 169 env_json=${SIM_RUN:-$SIM_ROOT/run/stack}/env.json 170 [ -r "$env_json" ] || die "no $env_json; start the stack first" 171 for p in $(python3 -c 'import json,sys; print(" ".join(str(p) for p in json.load(open(sys.argv[1]))["ports"]))' "$env_json"); do 172 "$ADB" -s "$s" reverse "tcp:$p" "tcp:$p" >/dev/null 173 done 174 echo "mapped stack ports into $s" 175 } 176 177 prepare() { 178 local s apk 179 s=$(serial) 180 apk=${3:-$SIM_ROOT/apk/wallet.apk} 181 [ -r "$apk" ] || die "no APK at $apk" 182 # Deterministic, animation-free UI: the driver reads the view hierarchy and 183 # animations make it race. 184 "$ADB" -s "$s" shell settings put global window_animation_scale 0 185 "$ADB" -s "$s" shell settings put global transition_animation_scale 0 186 "$ADB" -s "$s" shell settings put global animator_duration_scale 0 187 "$ADB" -s "$s" shell settings put secure long_press_timeout 250 188 "$ADB" -s "$s" shell svc power stayon true 189 # Leave the device in portrait: user_rotation survives app data wipes. 190 "$ADB" -s "$s" shell settings put system accelerometer_rotation 0 191 "$ADB" -s "$s" shell settings put system user_rotation 0 192 "$ADB" -s "$s" install -r -g "$apk" 193 "$ADB" -s "$s" shell pm grant "$PKG" android.permission.CAMERA 2>/dev/null || true 194 reverse 195 echo "prepared $s with $apk" 196 } 197 198 case "${1:-}" in 199 create) create ;; 200 boot) boot ;; 201 prepare) prepare "$@" ;; 202 reverse) reverse ;; 203 snapshot) "$ADB" -s "$(serial)" emu avd snapshot save "${3:-clean}" ;; 204 restore) "$ADB" -s "$(serial)" emu avd snapshot load "${3:-clean}" ;; 205 serial) serial ;; 206 kill) kill_emu ;; 207 *) die "usage: emu.sh create|boot|prepare|reverse|snapshot|restore|serial|kill <worker> [arg]" ;; 208 esac