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