1#!/bin/bash
2# Copyright 2021 The gRPC Authors
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16# Source this rc script to setup and configure qemu userspace emulator on kokoro worker so that we can seamlessly emulate processes running
17# inside docker containers.
18
19# show pre-existing qemu registration
20cat /proc/sys/fs/binfmt_misc/qemu-aarch64
21
22# Kokoro ubuntu1604 workers have already qemu-user and qemu-user-static packages installed, but it's and old version that:
23# * prints warning about some syscalls (e.g "qemu: Unsupported syscall: 278")
24# * doesn't register with binfmt_misc with the persistent ("F") flag we need (see below)
25#
26# To overcome the above limitations, we use the https://github.com/multiarch/qemu-user-static
27# docker image to provide a new enough version of qemu-user-static and register it with
28# the desired binfmt_misc flags. The most important flag we need is "F" (set by "--persistent yes"),
29# which allows the qemu-aarch64-static binary to be loaded eagerly at the time of registration with binfmt_misc.
30# That way, we can emulate aarch64 binaries running inside docker containers transparently, without needing the emulator
31# binary to be accessible from the docker image we're emulating.
32# Note that on newer distributions (such as glinux), simply "apt install qemu-user-static" is sufficient
33# to install qemu-user-static with the right flags.
34docker run --rm --privileged multiarch/qemu-user-static:5.2.0-2 --reset --credential yes --persistent yes
35
36# Print current qemu reqistration to make sure everything is setup correctly.
37cat /proc/sys/fs/binfmt_misc/qemu-aarch64
38