David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 1 | # Hermetic build |
| 2 | |
| 3 | Hafnium build is not hermetic as it uses some system tools and libraries, e.g. |
| 4 | `bison` and `libssl`. To ensure consistency and repeatability, the team |
| 5 | maintains and periodically publishes a container image as the reference build |
| 6 | environment. The image is hosted on Google Cloud Platform as |
| 7 | `eu.gcr.io/hafnium-build/hafnium_ci`. |
| 8 | |
| 9 | Building inside a container is always enabled only for Kokoro pre-submit tests |
| 10 | but can be enabled for local builds too. It is disabled by default as it |
Andrew Walbran | 6f8fd4c | 2019-08-05 13:28:17 +0100 | [diff] [blame] | 11 | requires the use of Docker which currently supports rootless containers only in |
| 12 | nightly builds. As rootless container tools mature, Hafnium may change the |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 13 | default settings. For now, running the hermetic build locally is intended |
| 14 | primarily to reproduce issues in pre-submit tests. |
| 15 | |
| 16 | ## Installing Docker |
| 17 | |
| 18 | ### Stable |
| 19 | |
| 20 | If you don't mind running a Docker daemon with root privileges on your system, |
| 21 | you can follow the [official guide](https://docs.docker.com/install/) to install |
| 22 | Docker, or [go/installdocker](https://goto.google.com/installdocker) if you are |
| 23 | a Googler. |
| 24 | |
| 25 | Because the daemon runs as root, files generated by the container are owned by |
| 26 | root as well. To work around this, the build will automatically derive a local |
| 27 | container image from the base container, adding user `hafnium` with the same |
| 28 | UID/GID as the local user. |
| 29 | |
| 30 | ### Nightly with rootless |
| 31 | |
| 32 | The latest nightly version of Docker has support for running containers with |
Andrew Walbran | 6f8fd4c | 2019-08-05 13:28:17 +0100 | [diff] [blame] | 33 | user namespaces, thus eliminating the need for a daemon with root privileges. It |
| 34 | can be installed into the local user's `bin` directory with a script: |
| 35 | |
| 36 | ```shell |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 37 | curl -fsSL https://get.docker.com/rootless -o get-docker.sh |
| 38 | sh get-docker.sh |
| 39 | ``` |
| 40 | |
Andrew Walbran | 6f8fd4c | 2019-08-05 13:28:17 +0100 | [diff] [blame] | 41 | The script will also walk you through the installation of dependencies, changes |
| 42 | to system configuration files and environment variable values needed by the |
| 43 | client to discover the rootless daemon. |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 44 | |
| 45 | ## Enabling for local builds |
| 46 | |
| 47 | Hermetic builds are controlled by the `HAFNIUM_HERMETIC_BUILD` environment |
| 48 | variable. Setting it to `true` instructs the build to run commands inside the |
| 49 | container. Any other value disables the feature. |
| 50 | |
| 51 | To always enable hermetic builds, put this line in your `~/.bashrc`: |
Andrew Walbran | 6f8fd4c | 2019-08-05 13:28:17 +0100 | [diff] [blame] | 52 | |
| 53 | ```shell |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 54 | export HAFNIUM_HERMETIC_BUILD=true |
| 55 | ``` |
| 56 | |
| 57 | When you now run `make`, you should see the following line: |
Andrew Walbran | 6f8fd4c | 2019-08-05 13:28:17 +0100 | [diff] [blame] | 58 | |
| 59 | ```shell |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 60 | $ make |
| 61 | Running in container: make all |
| 62 | ... |
| 63 | ``` |
Andrew Walbran | 6f8fd4c | 2019-08-05 13:28:17 +0100 | [diff] [blame] | 64 | |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 65 | ## Running commands inside the container |
| 66 | |
| 67 | An arbitrary command can be executed inside the container with |
Andrew Walbran | 6f8fd4c | 2019-08-05 13:28:17 +0100 | [diff] [blame] | 68 | `build/run_in_container.sh [-i] <command> ...`. This is done automatically |
Andrew Walbran | 219ceaf | 2020-01-10 15:44:29 +0000 | [diff] [blame] | 69 | inside `Makefile` and `kokoro/build.sh` which detect whether they are already |
| 70 | running inside the container and respawn themselves using `run_in_container.sh` |
| 71 | if not. |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 72 | |
| 73 | For example, you can spawn a shell with: |
Andrew Walbran | 6f8fd4c | 2019-08-05 13:28:17 +0100 | [diff] [blame] | 74 | |
| 75 | ```shell |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 76 | ./build/run_in_container.sh -i bash |
| 77 | ``` |
| 78 | |
| 79 | ## Building container image |
| 80 | |
| 81 | The container image is defined in `build/docker/Dockerfile` and can be built |
| 82 | locally: |
Andrew Walbran | 6f8fd4c | 2019-08-05 13:28:17 +0100 | [diff] [blame] | 83 | |
| 84 | ```shell |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 85 | ./build/docker/build.sh |
| 86 | ``` |
| 87 | |
Andrew Walbran | 6f8fd4c | 2019-08-05 13:28:17 +0100 | [diff] [blame] | 88 | Owners of the `hafnium-build` GCP repository can publish the new image (requires |
| 89 | [go/cloud-sdk](https://goto.google.com/cloud-sdk) installed and authenticated): |
| 90 | |
| 91 | ```shell |
David Brazdil | 5ecf75f | 2019-07-21 10:39:47 +0200 | [diff] [blame] | 92 | ./build/docker/publish.sh |
| 93 | ``` |