mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
What a build VM downloads failed on a network's first hiccup: curl's --retry 3 skips a connection cut mid-transfer (exit 18) and waits for ever on a stall, apt's retries were off, and oh-my-bash's clone had none. - devvm-fetch: each try has --connect-timeout 20 and a stall limit (--speed-limit 1024 --speed-time 30), and any failure is tried again, five tries in all, from where it stopped (-C -): curl's own --retry, --retry-all-errors included, starts again from nothing (measured on curl 8.7). A server that can't resume (33, 36) starts it again; a file that came whole and checks out ends the loop; a file that doesn't check out is fetched again from nothing once, as a resume may have joined two versions. A redirect goes only to https (--proto-redir =https), so no try or resume moves to plain http. TestDevvmFetchTries runs it against a server that cuts, ignores ranges, changes the file, fails and redirects to http. - apt.sh: package lists still fail fast (exit 75 when no source answers), and once they're in, the build's installs get Acquire::Retries "3", with apt's own backoff. - crane (oci/build.sh, the --from conversion, which has no devvm-fetch): --retry 5 --retry-all-errors --retry-delay 2 and the same timeouts. - oh-my-bash: the clone is tried five times in all, with git's low-speed limit. Docs: images.md's Building and Base's packages, customizing.md's devvm-fetch; the CHANGELOG's second highlight, which says which downloads resume, and What to do 1, which names the layers and --from bases that rebuild and pull again; the release page's A first run on a bad network. Image keys change: base's (base.sh, apt.sh, omb.sh), every layer's (apt.sh is in each), and a --from conversion's (oci/build.sh). Tool images' keys don't (shared-tools/build.sh is unchanged). Co-Authored-By: Claude Opus 5.5 <[email protected]>
227 lines
13 KiB
Bash
Executable File
227 lines
13 KiB
Bash
Executable File
#!/bin/bash
|
|
# Essentials, and mise for the shared tools (shared-tools/).
|
|
# shellcheck disable=SC2034 # the checksums are read through devvm_sha
|
|
set -euxo pipefail
|
|
: "${devvm_arch:?}" "${devvm_libc:?}" # devvm's prelude (prelude.sh) comes before this script
|
|
# Pinned downloads (scripts/bump.sh updates them).
|
|
mise_version=2026.11.14
|
|
mise_sha256_arm64=6afd537b39e2b1fe6594d4448318293d7e8c85469cbb51a6a01b28a6f26fbaf5
|
|
mise_sha256_amd64=42a3ff51c24b4315d679a07bbc598e299dda12fea0dfc0d6f90a543495834831
|
|
mise_sha256_arm64_musl=bf73525892d7942b881b885867bb028cab98ed528356c4490364eb1001ef9deb
|
|
mise_sha256_amd64_musl=86a5c3d8cf3d62b45a10ddd6afe8225bada9423e517ec8df7d589aa7d02c171d
|
|
git_lfs_version=3.8.1
|
|
git_lfs_sha256_arm64=67895dd77b63329796bc5efdd47b57f218e1f7b98717ebaf936a8238dbcaec0f
|
|
git_lfs_sha256_amd64=e72cf723a4a660438862282e8aecf3885fc8123e88bfcad534cf6b941a354b75
|
|
|
|
export DEBIAN_FRONTEND=noninteractive
|
|
# Alpine (--from alpine): apk, OpenRC and musl. Its packages come from the release's repositories,
|
|
# which have no snapshots: they aren't pinned.
|
|
if [[ $devvm_libc == musl ]]; then
|
|
# GNU tar: devvm's launch sends files with it (busybox's lacks options it uses). sqlite: an export
|
|
# of agents' history copies opencode's database without its logins with it. icu-libs: the dotnet
|
|
# shared tool's .NET needs ICU. fd: the pi shared tool finds files with it, and downloads it
|
|
# itself, unpinned, where it's missing. dnsmasq, socat and nftables: transparent mode
|
|
# (guest/transparent.sh), whose service only a confined VM's setup enables.
|
|
sudo apk add --no-cache git curl wget ca-certificates build-base unzip jq ripgrep bash coreutils tar \
|
|
util-linux util-linux-misc losetup findmnt erofs-utils sqlite icu-libs fd dnsmasq socat nftables
|
|
echo "devvm: Alpine's packages aren't pinned" >&2
|
|
else
|
|
# The package lists are apt.sh begin's: base's packages are recorded at its first build and replayed
|
|
# from devvm's cache after (layers/apt.sh), and a --from image's come from its own sources, unpinned.
|
|
# erofs-utils: the shared tools' builds make their images with it, in a VM cloned from base. sqlite3:
|
|
# an export of agents' history copies opencode's database without its logins with it. ICU: the dotnet
|
|
# shared tool's .NET needs it, and its package's name has its version (libicu76), so it's the newest
|
|
# libicuN the package lists have (a replay's lists have only the one recorded). fd-find (fdfind): the
|
|
# pi shared tool finds files with it, and downloads fd itself, unpinned, where it's missing.
|
|
# dnsmasq-base (dnsmasq alone, no service of its own), socat and nftables: transparent mode
|
|
# (guest/transparent.sh), whose service only a confined VM's setup enables.
|
|
icu=$(apt-cache pkgnames libicu | grep -E '^libicu[0-9]+$' | sort -V | tail -1 || true)
|
|
sudo apt-get install -y git curl wget ca-certificates build-essential unzip jq ripgrep erofs-utils sqlite3 fd-find \
|
|
dnsmasq-base socat nftables ${icu:+"$icu"}
|
|
fi
|
|
|
|
# devvm-fetch URL SHA256 OUT: every layer downloads through this, so an image only ever contains
|
|
# files whose checksums are pinned in devvm's scripts. A SHA-512 (128 hex digits) is checked as one:
|
|
# the shared tools' build gets the .NET SDK's from the tools lock. A try that fails for any reason, a
|
|
# dropped connection or one slower than 1 KB/s for 30 s included, is tried again from where it
|
|
# stopped (-C -), five tries in all: curl's own --retry starts again from nothing. A server that
|
|
# can't resume (33, 36) starts it again from nothing, and so does a download that doesn't check out,
|
|
# once: a resume may have joined two files, when the server's has changed. A redirect goes only to
|
|
# https, so no try or resume moves to plain http.
|
|
sudo tee /usr/local/bin/devvm-fetch >/dev/null <<'SH'
|
|
#!/bin/sh
|
|
set -eu
|
|
url=$1 want=$2 out=$3
|
|
sum=sha256sum
|
|
[ "${#want}" -ne 128 ] || sum=sha512sum
|
|
checksum() { "$sum" "$out" | cut -d' ' -f1; }
|
|
# fetch: one download into $out from nothing, each failed try tried again from where it stopped.
|
|
fetch() {
|
|
rm -f "$out"
|
|
n=1
|
|
until curl -fsSL --proto-redir =https --connect-timeout 20 --speed-limit 1024 --speed-time 30 -C - -o "$out" "$url"; do
|
|
code=$?
|
|
# All of it came, and only the end of the answer went wrong.
|
|
[ ! -f "$out" ] || [ "$(checksum)" != "$want" ] || break
|
|
[ "$n" -lt 5 ] || { echo "devvm-fetch: $url failed $n times, the last with curl's exit $code" >&2; exit 1; }
|
|
case $code in 33|36) rm -f "$out" ;; esac
|
|
have=$(wc -c 2>/dev/null < "$out" | tr -d ' ')
|
|
echo "devvm-fetch: $url failed (curl's exit $code); trying again from byte ${have:-0}" >&2
|
|
sleep $((n * 2))
|
|
n=$((n + 1))
|
|
done
|
|
}
|
|
fetch
|
|
got=$(checksum)
|
|
if [ "$got" != "$want" ]; then
|
|
echo "devvm-fetch: $url doesn't match its checksum; fetching it again from nothing" >&2
|
|
fetch
|
|
got=$(checksum)
|
|
fi
|
|
if [ "$got" != "$want" ]; then
|
|
rm -f "$out"
|
|
printf 'devvm-fetch: checksum mismatch for %s\n expected %s\n got %s\n' "$url" "$want" "$got" >&2
|
|
exit 1
|
|
fi
|
|
SH
|
|
sudo chmod 755 /usr/local/bin/devvm-fetch
|
|
|
|
if [[ $devvm_libc == gnu ]]; then
|
|
# Ubuntu's cloud image is a server's: drop what a dev VM doesn't use (snapd, multipath, modem and disk
|
|
# managers, firmware updates, crash reporting, tracing tools, kernel headers), and apt's background
|
|
# updates, which hold apt's lock and, on a confined tier, show up as blocks. Measured: 0.9 GB less
|
|
# disk, 100 MB less memory, 7 fewer services and 9 fewer timers. The everyday tools the server
|
|
# metapackages brought stay. Only what's installed is touched (--from images have none of it).
|
|
keep="bc file htop lsof nano rsync screen strace tcpdump time tmux mtr-tiny pciutils usbutils
|
|
inetutils-telnet dmidecode lshw linux-image-virtual"
|
|
drop="ubuntu-server ubuntu-standard snapd multipath-tools modemmanager udisks2 unattended-upgrades
|
|
fwupd rsyslog networkd-dispatcher bpftrace bpfcc-tools linux-tools-common sysstat python3-boto3
|
|
linux-headers-generic linux-headers-virtual apport landscape-common lxd-installer lxd-agent-loader
|
|
open-iscsi plymouth needrestart update-notifier-common open-vm-tools"
|
|
installed() { # installed PACKAGE...: the ones that are
|
|
{ dpkg-query -Wf '${db:Status-Status} ${Package}\n' "$@" 2>/dev/null || true; } | awk '$1 == "installed" { print $2 }'
|
|
}
|
|
# shellcheck disable=SC2086 # package lists
|
|
{
|
|
k=$(installed $keep); [[ -z $k ]] || sudo apt-mark manual $k >/dev/null
|
|
d=$(installed $drop); [[ -z $d ]] || sudo apt-get purge -y --autoremove $d
|
|
}
|
|
sudo systemctl mask apt-daily.timer apt-daily-upgrade.timer
|
|
fi
|
|
|
|
# Interactive shell snippets from features go in ~/.bashrc.d.
|
|
mkdir -p ~/.bashrc.d
|
|
grep -qs bashrc.d ~/.bashrc || echo 'for f in ~/.bashrc.d/*.sh; do [ -r "$f" ] && . "$f"; done' >> ~/.bashrc
|
|
|
|
# mise runs the shared tools (the toolchains and the coding agents), read-only images mounted at
|
|
# /opt/devvm/tools by devvm-tools (guest/tools.sh). Each VM's tools and versions are in
|
|
# /etc/mise/config.toml (written by guest/sync.sh); the user's own mise config and a project's version
|
|
# files (.nvmrc, .python-version, ...) come first, and versions that aren't shared install into the VM.
|
|
# mise names amd64 x64, and its musl builds end -musl. Its .tar.gz is a third of its binary's size.
|
|
m=${devvm_arch/amd64/x64}; [[ $devvm_libc == gnu ]] || m+=-musl
|
|
t=$(mktemp -d)
|
|
devvm-fetch "https://github.com/jdx/mise/releases/download/v$mise_version/mise-v$mise_version-linux-$m.tar.gz" \
|
|
"$(devvm_sha mise)" "$t/mise.tar.gz"
|
|
tar -xzf "$t/mise.tar.gz" -C "$t" mise/bin/mise
|
|
sudo install -m 755 "$t/mise/bin/mise" /usr/local/bin/mise
|
|
rm -rf "$t"
|
|
|
|
# git-lfs: a --clone VM's clone of a repo that uses Git LFS gets the repo's LFS objects, and git there
|
|
# checks out each LFS file's content through git-lfs's filter, which the copy sets in that clone's own
|
|
# config, never the system's, so a VM that mounts its folder keeps the pointers as they are. One static
|
|
# build for glibc and musl.
|
|
devvm-fetch "https://github.com/git-lfs/git-lfs/releases/download/v$git_lfs_version/git-lfs-linux-$devvm_arch-v$git_lfs_version.tar.gz" \
|
|
"$(devvm_sha git_lfs)" /tmp/git-lfs.tar.gz
|
|
tar -xzf /tmp/git-lfs.tar.gz -C /tmp "git-lfs-$git_lfs_version/git-lfs"
|
|
sudo install -m 755 "/tmp/git-lfs-$git_lfs_version/git-lfs" /usr/local/bin/git-lfs
|
|
rm -rf /tmp/git-lfs.tar.gz "/tmp/git-lfs-$git_lfs_version"
|
|
# Every shell finds the tools: login shells through profile.d, interactive ones through mise activate
|
|
# (it follows `cd` into projects with other versions), and the rest (`bash -c` from agents, editors
|
|
# over SSH) through BASH_ENV and mise's shims. Read-only installs can't take global packages or update
|
|
# themselves, so global installs go into the VM's home, on the PATH, and the coding agents'
|
|
# self-updates are off. The toolchains' environment and their folders for the PATH (devvm_path), and
|
|
# the agents', come from /etc/devvm/toolchains.sh and /etc/devvm/agents.sh, which every launch's setup
|
|
# writes from devvm's registries, so a new toolchain or agent changes no image. Their telemetry and
|
|
# version checks are off here rather than in devvm-telemetry.sh, so a tool that a non-login shell runs
|
|
# (ssh VM pi, an editor's terminal) sends none either.
|
|
sudo tee /etc/profile.d/devvm-tools.sh >/dev/null <<'SH'
|
|
devvm_path=
|
|
if [ -r /etc/devvm/toolchains.sh ]; then . /etc/devvm/toolchains.sh; fi
|
|
case ":$PATH:" in *":$HOME/.local/share/mise/shims:"*) ;; *)
|
|
export PATH="$devvm_path$HOME/.local/bin:$HOME/.local/share/mise/shims:$PATH" ;;
|
|
esac
|
|
unset devvm_path
|
|
if [ -r /etc/devvm/agents.sh ]; then . /etc/devvm/agents.sh; fi
|
|
SH
|
|
grep -qs devvm-tools /etc/environment || echo 'BASH_ENV=/etc/profile.d/devvm-tools.sh' | sudo tee -a /etc/environment >/dev/null
|
|
echo 'eval "$(mise activate bash)"' > ~/.bashrc.d/mise.sh
|
|
# Mounted at boot, once the share is: devvm-tools reads the VM's list (/etc/devvm/tools).
|
|
if [[ $devvm_libc == musl ]]; then
|
|
sudo tee /etc/init.d/devvm-tools >/dev/null <<'RC'
|
|
#!/sbin/openrc-run
|
|
description="Mount devvm's shared tools"
|
|
depend() { need localmount; after netmount; }
|
|
start() { [ -x /usr/local/sbin/devvm-tools ] || return 0; /usr/local/sbin/devvm-tools; }
|
|
RC
|
|
sudo chmod 755 /etc/init.d/devvm-tools
|
|
sudo rc-update add devvm-tools default
|
|
else
|
|
sudo tee /etc/systemd/system/devvm-tools.service >/dev/null <<'UNIT'
|
|
[Unit]
|
|
Description=Mount devvm's shared tools
|
|
RequiresMountsFor=/mnt/devvm-tools
|
|
ConditionPathExists=/usr/local/sbin/devvm-tools
|
|
[Service]
|
|
Type=oneshot
|
|
RemainAfterExit=yes
|
|
ExecStart=/usr/local/sbin/devvm-tools
|
|
[Install]
|
|
WantedBy=multi-user.target
|
|
UNIT
|
|
sudo systemctl enable devvm-tools.service
|
|
fi
|
|
|
|
# No browser in the VM: tools that open one (xdg-open, $BROWSER) print the URL instead of crashing.
|
|
# Ports are forwarded, so the URL works on the host as-is.
|
|
sudo tee /usr/local/bin/xdg-open >/dev/null <<'SH'
|
|
#!/bin/sh
|
|
echo "devvm: no browser in the VM; open on your host: $*" >&2
|
|
SH
|
|
sudo chmod 755 /usr/local/bin/xdg-open
|
|
echo 'export BROWSER=xdg-open' | sudo tee /etc/profile.d/devvm-browser.sh >/dev/null
|
|
|
|
# Telemetry off for every tool that has a switch (installed by devvm or commonly by projects), but
|
|
# the toolchains' and the coding agents', which devvm-tools.sh turns off in every shell.
|
|
sudo tee /etc/profile.d/devvm-telemetry.sh >/dev/null <<'SH'
|
|
export DO_NOT_TRACK=1 # generic opt-out (bun and others)
|
|
export NEXT_TELEMETRY_DISABLED=1
|
|
export TURBO_TELEMETRY_DISABLED=1
|
|
export ASTRO_TELEMETRY_DISABLED=1
|
|
export NUXT_TELEMETRY_DISABLED=1
|
|
export GATSBY_TELEMETRY_DISABLED=1
|
|
export STORYBOOK_DISABLE_TELEMETRY=1
|
|
export YARN_ENABLE_TELEMETRY=0
|
|
SH
|
|
# Ubuntu: no news fetched for the login banner or apt output.
|
|
sudo sed -i 's/^ENABLED=1/ENABLED=0/' /etc/default/motd-news 2>/dev/null || true
|
|
sudo pro config set apt_news=false >/dev/null 2>&1 || true
|
|
|
|
# GitHub's SSH host keys (from https://api.github.com/meta), so the first git-over-SSH doesn't prompt.
|
|
sudo tee -a /etc/ssh/ssh_known_hosts >/dev/null <<'KEYS'
|
|
github.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl
|
|
github.com ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBEmKSENjQEezOmxkZMy7opKgwFB9nkt5YRrYMjNuG5N87uRgg6CLrbo5wAdT/y6v0mKV0U2w0WZ2YB/++Tpockg=
|
|
github.com ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCj7ndNxQowgcQnjshcLrqPEiiphnt+VTTvDP6mHBL9j1aNUkY4Ue1gvwnGLVlOhGeYrnZaMgRK6+PKCUXaDbC7qtbW8gIkhL7aGCsOr/C56SJMy/BCZfxd1nWzAOxSDPgVsmerOBYfNqltV9/hWCqBywINIR+5dIg6JTJ72pcEpEjcYgXkE2YEFXV1JHnsKgbLWNlhScqb2UmyRkQyytRLtL+38TGxkxCflmO+5Z8CSSNY7GidjMIZ7Q4zMjA2n1nGrlTDkzwDCsw+wqFPGQA179cnfGWOWRVruj16z6XyvxvjJwbz0wQZ75XK5tKSb7FNyeIEs4TT4jk+S4dhPeAUC5y+bDYirYgM4GC7uEnztnZyaVWQ7B381AK4Qdrwt51ZqExKbQpTUNn+EjqoTwvqNj4kqx5QUCI0ThS/YkOxJCXmPUWZbhjpCg56i+2aB6CmK2JGhn57K5mj0MNdBXA4/WnwH6XoPWJzK5Nyu2zB3nAZp+S5hpQs+p1vN1/wsjk=
|
|
KEYS
|
|
# The VM's sshd: local forwarding (Lima's own port and socket forwards, editors' -L and -D), and
|
|
# remote forwards of TCP ports only on 127.77.0.0/16, the addresses of the VM's links, which devvm
|
|
# on the host asks for by number (clientspecified binds the address asked for, which PermitListen
|
|
# matches as text, so 0.0.0.0, 127.0.0.1, ::1 and localhost are refused); no remote socket forwards,
|
|
# tunnels or X11. First in sshd_config.d, so it wins. It's a safeguard for an honest VM, as root in
|
|
# it can change it: the boundary is the host's ssh, which asks for no other remote forward and
|
|
# accepts a channel only for a forward it asked for.
|
|
if [[ -d /etc/ssh ]]; then
|
|
sudo mkdir -p /etc/ssh/sshd_config.d
|
|
printf '%s\n' 'AllowTcpForwarding all' 'AllowStreamLocalForwarding local' 'GatewayPorts clientspecified' \
|
|
'PermitListen 127.77.*:*' 'PermitTunnel no' 'X11Forwarding no' | sudo tee /etc/ssh/sshd_config.d/00-devvm.conf >/dev/null
|
|
fi
|