|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Installs the pod isolation runtime, which runs a pod inside its own Firecracker |
| 4 | +# microVM as a Nitro Enclave sidecar instead of beside its neighbours under runc. |
| 5 | +# A pod opts in through the isolated-sandbox or confidential-sandbox RuntimeClass. |
| 6 | +# |
| 7 | +# Nodes must be launched with EnclaveOptions.Enabled=true. |
| 8 | + |
| 9 | +set -o pipefail |
| 10 | +set -o nounset |
| 11 | +set -o errexit |
| 12 | + |
| 13 | +if [ "${ENABLE_POD_ISOLATION}" != "true" ]; then |
| 14 | + echo "Pod isolation is not enabled, skipping" |
| 15 | + exit 0 |
| 16 | +fi |
| 17 | + |
| 18 | +# The runtime and guest image are published for these Kubernetes minor versions |
| 19 | +# only. |
| 20 | +case "$(echo "${KUBERNETES_VERSION}" | cut -d. -f1,2)" in |
| 21 | + 1.36 | 1.37) ;; |
| 22 | + *) |
| 23 | + echo "ERROR: pod isolation supports Kubernetes 1.36 and 1.37, not ${KUBERNETES_VERSION}" >&2 |
| 24 | + exit 1 |
| 25 | + ;; |
| 26 | +esac |
| 27 | + |
| 28 | +################################################################################ |
| 29 | +### Configuration ############################################################## |
| 30 | +################################################################################ |
| 31 | + |
| 32 | +# The first stock Amazon Linux kernel with the Nitro sandbox extensions, as a dnf |
| 33 | +# version constraint. |
| 34 | +readonly MIN_KERNEL="1:6.18.51" |
| 35 | + |
| 36 | +# The Nitro Sandbox memory pool, as a nitro_enclaves.mempool= kernel argument. |
| 37 | +readonly ENCLAVE_MEMPOOL="95%-1G" |
| 38 | + |
| 39 | +# The first Firecracker release that can drive a Nitro Sandbox. |
| 40 | +readonly MIN_FIRECRACKER_VERSION="v1.18.0" |
| 41 | + |
| 42 | +# The runtime and guest image come from the same bundle as the other node |
| 43 | +# components (see install-worker.sh), as <artifact>/linux/<arch>/ under it. |
| 44 | +readonly BUNDLE_S3_URI="s3://${BINARY_BUCKET_NAME}/${KUBERNETES_VERSION}/${KUBERNETES_BUILD_DATE}" |
| 45 | + |
| 46 | +# Guest boot artifacts. |
| 47 | +readonly IMAGES_DIR="/var/lib/nitro-sandbox-images" |
| 48 | + |
| 49 | +# Jailer chroot base, per-VM drive images, sockets and the shim log. |
| 50 | +readonly STATE_DIR="/var/lib/nitro-sandbox" |
| 51 | + |
| 52 | +# cri-proxy and the sandbox device plugin are subcommands of this binary, so |
| 53 | +# neither installs a binary of its own. |
| 54 | +readonly SHIM_BINARY="containerd-shim-nitro-sandbox-v2" |
| 55 | + |
| 56 | +readonly OVERLAY="${WORKING_DIR}/pod-isolation" |
| 57 | + |
| 58 | +# `uname -m` names, as Firecracker and the jailer are keyed. |
| 59 | +case "$(uname -m)" in |
| 60 | + x86_64 | aarch64) ARCH="$(uname -m)" ;; |
| 61 | + *) |
| 62 | + echo "ERROR: unsupported architecture $(uname -m)" >&2 |
| 63 | + exit 1 |
| 64 | + ;; |
| 65 | +esac |
| 66 | +readonly ARCH |
| 67 | + |
| 68 | +# The artifact bucket keys by Go-style `amd64` / `arm64`, not `uname -m` names. |
| 69 | +case "${ARCH}" in |
| 70 | + x86_64) GUEST_ARCH="amd64" ;; |
| 71 | + aarch64) GUEST_ARCH="arm64" ;; |
| 72 | +esac |
| 73 | +readonly GUEST_ARCH |
| 74 | + |
| 75 | +echo "Installing the pod isolation runtime for ${ARCH} from ${BUNDLE_S3_URI}" |
| 76 | + |
| 77 | +sudo install -d -m 0755 "${IMAGES_DIR}" "${STATE_DIR}" |
| 78 | + |
| 79 | +# shellcheck disable=SC1090 |
| 80 | +source "${WORKING_DIR}/helpers/aws-clients.sh" |
| 81 | + |
| 82 | +# Download one object from a given S3 base URI. Integrity is the caller's |
| 83 | +# problem; see `fetch_from` for the `.sha256`-verified variant. aws_s3 retries |
| 84 | +# unsigned, so a public bucket is readable without an instance profile. |
| 85 | +download_from() { |
| 86 | + local base="$1" key="$2" dest="$3" mode="$4" |
| 87 | + local tmp="${WORKING_DIR}/download.$$" |
| 88 | + |
| 89 | + echo "Fetching ${base}/${key}" |
| 90 | + if ! aws_s3 cp --quiet --region "${BINARY_BUCKET_REGION}" "${base}/${key}" "${tmp}"; then |
| 91 | + echo "ERROR: could not fetch ${base}/${key}." >&2 |
| 92 | + return 1 |
| 93 | + fi |
| 94 | + sudo mv "${tmp}" "${dest}" |
| 95 | + sudo chown root:root "${dest}" |
| 96 | + sudo chmod "${mode}" "${dest}" |
| 97 | +} |
| 98 | + |
| 99 | +# Download from a given S3 base URI and verify against the `.sha256` sidecar |
| 100 | +# beside the object. Compared by hash value, not `sha256sum -c`, since the name |
| 101 | +# the sidecar records is relative to where the checksums were generated. |
| 102 | +fetch_from() { |
| 103 | + local base="$1" key="$2" dest="$3" mode="$4" |
| 104 | + local expected actual |
| 105 | + |
| 106 | + download_from "${base}" "${key}" "${dest}" "${mode}" |
| 107 | + |
| 108 | + expected=$(aws_s3 cp --quiet --region "${BINARY_BUCKET_REGION}" "${base}/${key}.sha256" - | awk '{print $1}') |
| 109 | + actual=$(sudo sha256sum "${dest}" | awk '{print $1}') |
| 110 | + if [ "${expected}" != "${actual}" ]; then |
| 111 | + echo "ERROR: checksum mismatch for ${key}" >&2 |
| 112 | + echo " expected ${expected}" >&2 |
| 113 | + echo " actual ${actual}" >&2 |
| 114 | + return 1 |
| 115 | + fi |
| 116 | + echo " -> ${dest}" |
| 117 | +} |
| 118 | + |
| 119 | +################################################################################ |
| 120 | +### Packages ################################################################### |
| 121 | +################################################################################ |
| 122 | +# The shim execs `mkfs.erofs` to build the erofs image the guest mounts for a |
| 123 | +# container's root filesystem. Without it, container start fails with ENOENT, |
| 124 | +# which reads like a missing file rather than a missing package. |
| 125 | +sudo dnf install -y erofs-utils |
| 126 | + |
| 127 | +################################################################################ |
| 128 | +### Host kernel ################################################################ |
| 129 | +################################################################################ |
| 130 | +# The sandbox extensions to the nitro_enclaves driver are in the stock Amazon |
| 131 | +# Linux kernel from ${MIN_KERNEL} onwards, so no kernel is |
| 132 | +# installed from elsewhere. Earlier kernels carry the driver but not the |
| 133 | +# extensions: the sidecar ioctls are missing, the node advertises no sandbox |
| 134 | +# capacity, and nothing can be scheduled. Standalone enclaves work there; |
| 135 | +# sidecars do not. |
| 136 | +# |
| 137 | +# The base image usually already satisfies this, in which case dnf reports |
| 138 | +# nothing to do. Asserting it here rather than assuming it means an older base |
| 139 | +# image fails the build instead of producing an AMI that registers fine and then |
| 140 | +# refuses every sandboxed pod. |
| 141 | +sudo dnf install -y "kernel6.18 >= ${MIN_KERNEL}" |
| 142 | + |
| 143 | +################################################################################ |
| 144 | +### Boot arguments ############################################################# |
| 145 | +################################################################################ |
| 146 | +# The Nitro Sandbox memory pool must be reserved at boot with the |
| 147 | +# nitro_enclaves.mempool kernel parameter; a sandbox fails to start if the pool is |
| 148 | +# not reserved. The shim checks it before starting every sandbox, so a node |
| 149 | +# without it registers fine and then refuses every sandboxed pod. Each sandbox |
| 150 | +# takes its full memory from the pool when it starts and returns it only when it |
| 151 | +# terminates. |
| 152 | +set-kernel-arg "nitro_enclaves.mempool=${ENCLAVE_MEMPOOL}" |
| 153 | + |
| 154 | +################################################################################ |
| 155 | +### Runtime #################################################################### |
| 156 | +################################################################################ |
| 157 | + |
| 158 | +# The Firecracker release to install: POD_ISOLATION_FIRECRACKER_VERSION, or the |
| 159 | +# newest release when that is "latest". Either way it must be at least |
| 160 | +# MIN_FIRECRACKER_VERSION. |
| 161 | +resolve_firecracker_version() { |
| 162 | + local version="${POD_ISOLATION_FIRECRACKER_VERSION}" |
| 163 | + |
| 164 | + if [ "${version}" = "latest" ]; then |
| 165 | + # releases/latest redirects to releases/tag/<version>. |
| 166 | + version=$(curl --fail --silent --show-error --location --retry 3 --output /dev/null \ |
| 167 | + --write-out '%{url_effective}' https://github.com/firecracker-microvm/firecracker/releases/latest) |
| 168 | + version="${version##*/}" |
| 169 | + fi |
| 170 | + if [ -z "${version}" ] || [ "$(printf '%s\n%s\n' "${MIN_FIRECRACKER_VERSION}" "${version}" | sort -V | head -n 1)" != "${MIN_FIRECRACKER_VERSION}" ]; then |
| 171 | + echo "ERROR: Firecracker ${version:-<none>} is older than ${MIN_FIRECRACKER_VERSION}" >&2 |
| 172 | + return 1 |
| 173 | + fi |
| 174 | + echo "${version}" |
| 175 | +} |
| 176 | + |
| 177 | +# The VMM each isolated pod runs inside, and the jailer that confines it, from |
| 178 | +# the upstream Firecracker release. The tarball is checked against the |
| 179 | +# .sha256.txt published beside it before anything is extracted. |
| 180 | +install_firecracker() { |
| 181 | + local version |
| 182 | + version=$(resolve_firecracker_version) |
| 183 | + local name="firecracker-${version}-${ARCH}.tgz" |
| 184 | + local url="https://github.com/firecracker-microvm/firecracker/releases/download/${version}/${name}" |
| 185 | + local dir="${WORKING_DIR}/firecracker" |
| 186 | + local expected actual |
| 187 | + |
| 188 | + mkdir -p "${dir}" |
| 189 | + echo "Fetching ${url}" |
| 190 | + curl --fail --silent --show-error --location --retry 3 --output "${dir}/${name}" "${url}" |
| 191 | + expected=$(curl --fail --silent --show-error --location --retry 3 "${url}.sha256.txt" | awk '{print $1}') |
| 192 | + actual=$(sha256sum "${dir}/${name}" | awk '{print $1}') |
| 193 | + if [ -z "${expected}" ] || [ "${expected}" != "${actual}" ]; then |
| 194 | + echo "ERROR: checksum mismatch for ${name}" >&2 |
| 195 | + echo " expected ${expected}" >&2 |
| 196 | + echo " actual ${actual}" >&2 |
| 197 | + return 1 |
| 198 | + fi |
| 199 | + |
| 200 | + # The tarball holds release-<version>-<arch>/{firecracker,jailer}-<version>-<arch>. |
| 201 | + tar -xzf "${dir}/${name}" -C "${dir}" |
| 202 | + for binary in firecracker jailer; do |
| 203 | + sudo install -m 0755 "${dir}/release-${version}-${ARCH}/${binary}-${version}-${ARCH}" "/usr/bin/${binary}" |
| 204 | + echo " -> /usr/bin/${binary}" |
| 205 | + done |
| 206 | + |
| 207 | + rm -rf "${dir}" |
| 208 | +} |
| 209 | + |
| 210 | +install_firecracker |
| 211 | + |
| 212 | +# The runtime containerd hands a sandbox to, in the slot runc occupies for an |
| 213 | +# ordinary pod. The release archive is checked against its `.sha256` sidecar like |
| 214 | +# the other bundle artifacts. |
| 215 | +# |
| 216 | +# The archive also carries the guest agent, the ocicrypt keyprovider, both |
| 217 | +# genpolicy binaries and the policy evaluator. Only the shim is installed: the |
| 218 | +# agent lives in the guest image, and the rest are workstation tools. |
| 219 | +install_runtime_archive() { |
| 220 | + local dir="${WORKING_DIR}/runtime" |
| 221 | + |
| 222 | + mkdir -p "${dir}" |
| 223 | + fetch_from "${BUNDLE_S3_URI}/nitro-sandboxes-runtime/linux/${GUEST_ARCH}" \ |
| 224 | + "nitro-sandboxes-runtime.tar.gz" "${dir}/nitro-sandboxes-runtime.tar.gz" 0644 |
| 225 | + |
| 226 | + tar -xzf "${dir}/nitro-sandboxes-runtime.tar.gz" -C "${dir}" "${SHIM_BINARY}" |
| 227 | + sudo install -m 0755 "${dir}/${SHIM_BINARY}" "/usr/bin/${SHIM_BINARY}" |
| 228 | + echo " -> /usr/bin/${SHIM_BINARY}" |
| 229 | + |
| 230 | + rm -rf "${dir}" |
| 231 | +} |
| 232 | + |
| 233 | +install_runtime_archive |
| 234 | + |
| 235 | +# The guest, as an EIF plus its disk. Firecracker loads the EIF directly with the kernel command line baked in, |
| 236 | +# which is why nitro-sandbox.toml sets no boot_args. The pair must come from one |
| 237 | +# build: the disk is a dm-verity image whose root hash is in the EIF's measured |
| 238 | +# command line, so a mismatch fails at guest boot. |
| 239 | +fetch_from "${BUNDLE_S3_URI}/nitro-sandbox-guest/linux/${GUEST_ARCH}" "nitro-sandbox-guest.eif" "${IMAGES_DIR}/latest.eif" 0644 |
| 240 | +fetch_from "${BUNDLE_S3_URI}/nitro-sandbox-guest/linux/${GUEST_ARCH}" "nitro-sandbox-guest-disk.img" "${IMAGES_DIR}/latest-disk.img" 0644 |
| 241 | + |
| 242 | +# The shim attaches the guest disk as "rootfs.ext4" from the directory holding |
| 243 | +# the kernel it was given. |
| 244 | +sudo ln -sf latest-disk.img "${IMAGES_DIR}/rootfs.ext4" |
| 245 | + |
| 246 | +# The guest agent needs no host install; it is part of the guest image. |
| 247 | + |
| 248 | +sudo install -D -m 0644 "${OVERLAY}/nitro-sandbox.toml" /etc/containerd/nitro-sandbox.toml |
| 249 | + |
| 250 | +# The shim creates a TAP device per microVM; without tun, sandbox creation fails. |
| 251 | +sudo install -D -m 0644 "${OVERLAY}/nitro-sandbox-modules.conf" /etc/modules-load.d/nitro-sandbox.conf |
| 252 | + |
| 253 | +################################################################################ |
| 254 | +### CRI proxy ################################################################## |
| 255 | +################################################################################ |
| 256 | +# Routes isolated pods to the right runtime in front of kubelet and advertises |
| 257 | +# how many sandboxes the node can hold. |
| 258 | +sudo install -D -m 0644 "${OVERLAY}/cri-proxy.service" /etc/systemd/system/cri-proxy.service |
| 259 | +sudo install -D -m 0644 "${OVERLAY}/restart-with-cri-proxy.conf" \ |
| 260 | + /etc/systemd/system/kubelet.service.d/restart-with-cri-proxy.conf |
| 261 | + |
| 262 | +# Enabled, not started: there is no containerd to talk to on the builder. |
| 263 | +sudo systemctl daemon-reload |
| 264 | +sudo systemctl enable cri-proxy.service |
| 265 | + |
| 266 | +################################################################################ |
| 267 | +### containerd and kubelet configuration ####################################### |
| 268 | +################################################################################ |
| 269 | +# nodeadm regenerates /etc/containerd/config.toml and the kubelet config from |
| 270 | +# templates on every boot, so edits made here would be discarded before |
| 271 | +# containerd first starts. The runtime handlers, discard_unpacked_layers and |
| 272 | +# kubelet's containerRuntimeEndpoint ship as a NodeConfig drop-in instead, which |
| 273 | +# nodeadm reads as a config source and merges over user data. |
| 274 | +sudo install -D -m 0644 "${OVERLAY}/10-pod-isolation.yaml" /etc/eks/nodeadm.d/10-pod-isolation.yaml |
| 275 | + |
| 276 | +echo "Pod isolation runtime installed" |
0 commit comments