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