#!/bin/bash # Copyright 2024 The gVisor Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. set -xe -o pipefail USE_TMPFS=true DISABLE_CONTAINERD_SNAPSHOTTER=false while [[ $# -gt 0 ]]; do case $1 in --no-overlay) # Legacy gVisor behavior: skip tmpfs and disable the containerd # snapshotter. This can fail on images with security.capability xattrs. USE_TMPFS=false DISABLE_CONTAINERD_SNAPSHOTTER=true shift ;; --disk-store) # Use the disk-backed /var/lib/docker volume but keep Docker 29's # containerd snapshotter. This supports file-capability xattrs. USE_TMPFS=false DISABLE_CONTAINERD_SNAPSHOTTER=false shift ;; *) shift ;; esac done # Temporary workaround for gVisor's session keyring leak. # # runc creates a new session keyring for every container it starts. gVisor # (through at least release-20261102.0) never releases those keyrings and caps # each user namespace's key set at 200. Once the cap is reached, creating any # new container fails with: # # unable to join session keyring: unable to create session key: disk quota exceeded # # Raise the cap far enough that a long-running CI dind lasts longer. This does # not stop the leak; remove it once gVisor releases session keyrings upstream. # Only write the sysctl inside gVisor: on a runc DinD this is the host-wide key # limit and changing it would affect the host. if grep -qi gvisor /proc/version; then key_maxkeys="${GVISOR_KEY_MAXKEYS:-50000}" current_key_maxkeys=$(cat /proc/sys/kernel/keys/maxkeys 2>/dev/null || true) if [[ "${key_maxkeys}" =~ ^[0-9]+$ ]] && (( key_maxkeys > 0 )) && \ [[ "${current_key_maxkeys}" =~ ^[0-9]+$ ]] && \ (( current_key_maxkeys < key_maxkeys )); then echo "Raising gVisor session keyring limit from ${current_key_maxkeys} to ${key_maxkeys}" echo "${key_maxkeys}" > /proc/sys/kernel/keys/maxkeys || \ echo "warning: failed to raise gVisor session keyring limit" >&2 fi fi # When dockerd uses the overlay storage driver, it will try to mount an overlay # filesystem under /var/lib/docker. gVisor only supports tmpfs as an upper # layer, so we mount a tmpfs if it isn't already present. if [[ "${USE_TMPFS}" == "true" ]]; then current_fs=$(stat -f -c %T /var/lib/docker 2>/dev/null || echo "none") if [[ "${current_fs}" != "tmpfs" ]]; then mkdir -p /var/lib/docker mount -t tmpfs -o size="${DOCKER_TMPFS_SIZE:-2G}" tmpfs /var/lib/docker else echo "/var/lib/docker is already tmpfs, skipping mount." fi fi EXTRA_DOCKERD_FLAGS=() docker_version=$(dockerd --version) # Docker 29+ does not automatically fall back to the vfs driver when it cannot # mount an overlayfs, so we explicitly disable it when --no-overlay is passed. if [[ ${docker_version} =~ version\ ([0-9]+) ]] && [[ ${BASH_REMATCH[1]} -ge 29 ]]; then if [[ "${DISABLE_CONTAINERD_SNAPSHOTTER}" == "true" ]]; then EXTRA_DOCKERD_FLAGS+=(--feature containerd-snapshotter=false) fi fi dev=$(ip route show default | sed 's/.*\sdev\s\(\S*\)\s.*$/\1/') addr=$(ip addr show dev "$dev" | grep -w inet | sed 's/^\s*inet\s\(\S*\)\/.*$/\1/') # TODO(gvisor.dev/issues/14011): until we investigate/fix gVisor's handling of # forwarding fragmented packets, match the host MTU in nested dockerd to make # it more reliable. mtu=$(ip -o link show dev "$dev" | sed -n 's/.*mtu \([0-9]*\).*/\1/p') IPTABLES="iptables" if command -v iptables-legacy >/dev/null 2>&1; then IPTABLES="iptables-legacy" fi echo 1 > /proc/sys/net/ipv4/ip_forward $IPTABLES -t nat -A POSTROUTING -o "$dev" -j SNAT --to-source "$addr" -p tcp $IPTABLES -t nat -A POSTROUTING -o "$dev" -j SNAT --to-source "$addr" -p udp exec dockerd-entrypoint.sh --iptables=false --ip6tables=false ${mtu:+--mtu="$mtu"} -D "${EXTRA_DOCKERD_FLAGS[@]}"