David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | # Copyright 2019 The Hafnium Authors. |
| 3 | # |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame^] | 4 | # Use of this source code is governed by a BSD-style |
| 5 | # license that can be found in the LICENSE file or at |
| 6 | # https://opensource.org/licenses/BSD-3-Clause. |
Andrew Walbran | 5e71e9b | 2020-06-17 15:44:49 +0100 | [diff] [blame] | 7 | |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 8 | set -euo pipefail |
| 9 | |
David Brazdil | 4a51d65 | 2019-12-20 13:27:54 +0000 | [diff] [blame] | 10 | SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 11 | ROOT_DIR="$(dirname ${SCRIPT_DIR})" |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 12 | |
| 13 | source "${SCRIPT_DIR}/docker/common.inc" |
| 14 | |
| 15 | if [ "${HAFNIUM_HERMETIC_BUILD:-}" == "inside" ] |
| 16 | then |
| 17 | echo "ERROR: Invoked $0 recursively" 1>&2 |
| 18 | exit 1 |
| 19 | fi |
| 20 | |
| 21 | # Set up a temp directory and register a cleanup function on exit. |
| 22 | TMP_DIR="$(mktemp -d)" |
| 23 | function cleanup() { |
| 24 | rm -rf "${TMP_DIR}" |
| 25 | } |
| 26 | trap cleanup EXIT |
| 27 | |
| 28 | # Build local image and write its hash to a temporary file. |
| 29 | IID_FILE="${TMP_DIR}/imgid.txt" |
| 30 | "${DOCKER}" build \ |
| 31 | --build-arg LOCAL_UID="$(id -u)" \ |
| 32 | --build-arg LOCAL_GID="$(id -g)" \ |
| 33 | --iidfile="${IID_FILE}" \ |
| 34 | -f "${SCRIPT_DIR}/docker/Dockerfile.local" \ |
| 35 | "${SCRIPT_DIR}/docker" |
| 36 | IMAGE_ID="$(cat ${IID_FILE})" |
| 37 | |
David Brazdil | 5e0484e | 2019-08-07 15:06:57 +0100 | [diff] [blame] | 38 | # Parse command line arguments |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 39 | INTERACTIVE=false |
David Brazdil | 5e0484e | 2019-08-07 15:06:57 +0100 | [diff] [blame] | 40 | ALLOW_PTRACE=false |
| 41 | while true |
| 42 | do |
| 43 | case "${1:-}" in |
| 44 | -i) |
| 45 | INTERACTIVE=true |
| 46 | shift |
| 47 | ;; |
| 48 | -p) |
| 49 | ALLOW_PTRACE=true |
| 50 | shift |
| 51 | ;; |
| 52 | -*) |
| 53 | echo "ERROR: Unknown command line flag: $1" 1>&2 |
| 54 | echo "Usage: $0 [-i] [-p] <command>" |
| 55 | exit 1 |
| 56 | ;; |
| 57 | *) |
| 58 | break |
| 59 | ;; |
| 60 | esac |
| 61 | done |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 62 | |
| 63 | ARGS=() |
| 64 | # Run with a pseduo-TTY for nicer logging. |
| 65 | ARGS+=(-t) |
| 66 | # Run interactive if this script was invoked with '-i'. |
| 67 | if [ "${INTERACTIVE}" == "true" ] |
| 68 | then |
| 69 | ARGS+=(-i) |
| 70 | fi |
David Brazdil | 5e0484e | 2019-08-07 15:06:57 +0100 | [diff] [blame] | 71 | # Allow ptrace() syscall if invoked with '-p'. |
| 72 | if [ "${ALLOW_PTRACE}" == "true" ] |
| 73 | then |
| 74 | echo "WARNING: Docker seccomp profile is disabled!" 1>&2 |
| 75 | ARGS+=(--cap-add=SYS_PTRACE --security-opt seccomp=unconfined) |
| 76 | fi |
David Brazdil | 3cc24aa | 2019-09-27 10:24:41 +0100 | [diff] [blame] | 77 | # Propagate "HAFNIUM_*" environment variables. |
| 78 | # Note: Cannot use `env | while` because the loop would run inside a child |
| 79 | # process and would not have any effect on variables in the parent. |
| 80 | while read -r ENV_LINE |
| 81 | do |
| 82 | VAR_NAME="$(echo ${ENV_LINE} | cut -d= -f1)" |
| 83 | case "${VAR_NAME}" in |
| 84 | HAFNIUM_HERMETIC_BUILD) |
| 85 | # Skip this one. It will be overridden below. |
| 86 | ;; |
| 87 | HAFNIUM_*) |
| 88 | ARGS+=(-e "${ENV_LINE}") |
| 89 | ;; |
| 90 | esac |
| 91 | done <<< "$(env)" |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 92 | # Set environment variable informing the build that we are running inside |
| 93 | # a container. |
| 94 | ARGS+=(-e HAFNIUM_HERMETIC_BUILD=inside) |
| 95 | # Bind-mount the Hafnium root directory. We mount it at the same absolute |
| 96 | # location so that all paths match across the host and guest. |
| 97 | ARGS+=(-v "${ROOT_DIR}":"${ROOT_DIR}") |
| 98 | # Make all files outside of the Hafnium directory read-only to ensure that all |
| 99 | # generated files are written there. |
| 100 | ARGS+=(--read-only) |
| 101 | # Mount a writable /tmp folder. Required by LLVM/Clang for intermediate files. |
| 102 | ARGS+=(--tmpfs /tmp) |
| 103 | # Set working directory. |
| 104 | ARGS+=(-w "${ROOT_DIR}") |
| 105 | |
| 106 | echo "Running in container: $*" 1>&2 |
| 107 | ${DOCKER} run \ |
| 108 | ${ARGS[@]} \ |
| 109 | "${IMAGE_ID}" \ |
David Brazdil | 4a51d65 | 2019-12-20 13:27:54 +0000 | [diff] [blame] | 110 | /bin/bash -c "$*" |