1*e07d83d3SAndroid Build Coastguard Worker#!/bin/bash 2*e07d83d3SAndroid Build Coastguard Worker# 3*e07d83d3SAndroid Build Coastguard Worker# Setup and configure qemu userspace emulator on kokoro worker so that we can seamlessly emulate processes running 4*e07d83d3SAndroid Build Coastguard Worker# inside docker containers. 5*e07d83d3SAndroid Build Coastguard Worker 6*e07d83d3SAndroid Build Coastguard Workerset -ex 7*e07d83d3SAndroid Build Coastguard Worker 8*e07d83d3SAndroid Build Coastguard Worker# show pre-existing qemu registration 9*e07d83d3SAndroid Build Coastguard Workercat /proc/sys/fs/binfmt_misc/qemu-aarch64 10*e07d83d3SAndroid Build Coastguard Worker 11*e07d83d3SAndroid Build Coastguard Worker# Kokoro ubuntu1604 workers have already qemu-user and qemu-user-static packages installed, but it's and old version that: 12*e07d83d3SAndroid Build Coastguard Worker# * prints warning about some syscalls (e.g "qemu: Unsupported syscall: 278") 13*e07d83d3SAndroid Build Coastguard Worker# * doesn't register with binfmt_misc with the persistent ("F") flag we need (see below) 14*e07d83d3SAndroid Build Coastguard Worker# 15*e07d83d3SAndroid Build Coastguard Worker# To overcome the above limitations, we use the https://github.com/multiarch/qemu-user-static 16*e07d83d3SAndroid Build Coastguard Worker# docker image to provide a new enough version of qemu-user-static and register it with 17*e07d83d3SAndroid Build Coastguard Worker# the desired binfmt_misc flags. The most important flag we need is "F" (set by "--persistent yes"), 18*e07d83d3SAndroid Build Coastguard Worker# which allows the qemu-aarch64-static binary to be loaded eagerly at the time of registration with binfmt_misc. 19*e07d83d3SAndroid Build Coastguard Worker# That way, we can emulate aarch64 binaries running inside docker containers transparently, without needing the emulator 20*e07d83d3SAndroid Build Coastguard Worker# binary to be accessible from the docker image we're emulating. 21*e07d83d3SAndroid Build Coastguard Worker# Note that on newer distributions (such as glinux), simply "apt install qemu-user-static" is sufficient 22*e07d83d3SAndroid Build Coastguard Worker# to install qemu-user-static with the right flags. 23*e07d83d3SAndroid Build Coastguard Workerdocker run --rm --privileged multiarch/qemu-user-static:5.2.0-2 --reset --credential yes --persistent yes 24*e07d83d3SAndroid Build Coastguard Worker 25*e07d83d3SAndroid Build Coastguard Worker# Print current qemu reqistration to make sure everything is setup correctly. 26*e07d83d3SAndroid Build Coastguard Workercat /proc/sys/fs/binfmt_misc/qemu-aarch64 27