Files
Ryan El KochtaandgVisor bot 2d7fdd3e23 Configure docker-in-gVisor to use the uplink network interface's MTU
Until [#14011](https://gvisor.dev/issues/14011) is addressed, this hopefully
will reduce image_test's flakiness in asia-southeast.

Also pins dind to Docker 29 to avoid any surprises in the future.

PiperOrigin-RevId: 966634277
2026-08-18 09:24:13 -07:00

74 lines
2.5 KiB
Bash
Executable File

#!/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_OVERLAY_DRIVER=true
while [[ $# -gt 0 ]]; do
case $1 in
--no-overlay)
USE_OVERLAY_DRIVER=false
shift
;;
*)
shift
;;
esac
done
# 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_OVERLAY_DRIVER}" == "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 [[ "${USE_OVERLAY_DRIVER}" != "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[@]}"