mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 21:36:40 -10:00
devvm space [--json] lists each kind of thing devvm keeps, on every backend, with its count, size (allocated blocks, as du), whether devvm makes it again (remade, only copy, log, state, leftover), what devvm gc frees of it now and what frees the rest, then a total: VMs and images per backend, shared tool builds per backend and C library, exports, network and run logs, devvm's own mise, the copies of devvm's files, the leftover state of VMs their backend no longer has, and the rest of devvm's state. The backends' own caches come apart and outside the total: Lima's download cache, smolvm's caches on macOS (pack disks, image disks, archives, registry; never its machines' folders) and Docker's labelled volumes that aren't one VM's own. backend.UsageReporter (optional): Lima, smolvm and Docker say what their VMs and caches take. Each backend gets 30 s; one that doesn't answer is named when a VM's state uses it (space_backend_unknown), and its VMs are never counted as leftovers. Each tool build now leaves <image>.target beside it (libc, format), so listings tell a Lima, smolvm and Docker build of a version apart; tools.Listed has Target, from the record or the target it's current for; Share.Delete deletes the record too. Messages: reading_space, space_failed, space_backend_unknown, space_caches_note. Stubs: docker volume ls; smolvm machine ls fails with smolvm-ls-fails. Docs: the What devvm keeps page leads with devvm space; commands table; images' Cleaning up; CLI reference. Review: space runs only listings (limactl list, smolvm machine ls and data-dir, docker info, container ls/inspect --size, system df, volume ls), each bounded by ctx with a timeout; the txtar checks no call creates, deletes, starts, stops or edits. Backend caches aren't in the total. Sizes checked against du on the e2e sandbox data and the user's caches (Lima 9.1G, smolvm pack disks 20G, both match du -sh). APFS clones count for both, as the page says. Co-Authored-By: Claude Opus 5.5 <[email protected]>
298 lines
15 KiB
Bash
Executable File
298 lines
15 KiB
Bash
Executable File
#!/bin/bash
|
|
# Stand-in for docker in tests. Logs every call to $STUB_LOG.
|
|
# For --from: `info` fails when $STUB_DIR/docker-down exists, `buildx version` when
|
|
# $STUB_DIR/no-buildx exists. `image inspect --format {{.Id}}` finds an image only when
|
|
# $STUB_DIR/docker-image exists, and prints it (its ID). `save -o F` writes a tarball whose
|
|
# manifest.json names that image's config ($STUB_DIR/docker-saved instead, if it exists: the tag
|
|
# moved meanwhile).
|
|
# For the docker backend, an engine: `info --format {{json .}}` prints $STUB_DIR/docker-info if it
|
|
# exists, else a Linux engine of this machine's architecture named stub-host (with
|
|
# $STUB_DIR/docker-denied, fails as a socket the user can't open does), with Sysbox's runtime when
|
|
# $STUB_DIR/docker-sysbox exists. Its containers, images and
|
|
# volumes are folders in $STUB_DIR/docker/{containers,images,volumes}/NAME, each with a labels file
|
|
# (KEY=VALUE lines) and the rest one file per field (status, image, cpus, memory, hostname, network,
|
|
# mounts, layers, env, runtime), which create, start, stop, rm, rename, commit, import, update, volume create
|
|
# and rm keep up to date; ls, inspect and system df answer from them. A container has an ID (found
|
|
# by inspect and exec too) and a start time, which each start changes. $STUB_DIR/docker-foreign-NAME
|
|
# gives a container or volume another label (someone else's). exec answers the libc check with
|
|
# gnu, the sshd probe with sshd (none when $STUB_DIR/no-sshd exists), the host key's cat with one, keeps the tar stream a VM's setup is sent as $STUB_DIR/sync.tar, appends a script's second
|
|
# line to $STUB_DIR/scripts when one is fed to bash -s or sh -s (failing with $STUB_DIR/script-fails
|
|
# for bash -s), and the tools build's build.sh writes the images it's asked for into the folder it
|
|
# mounts. run (the tools' unpacking) appends its arguments after -- to $STUB_DIR/unpacked.
|
|
# Networks are folders in $STUB_DIR/docker/networks/NAME (labels, internal, inhibit, ipv6), which network
|
|
# create, rm, connect and disconnect keep up to date (a container's networks file lists its own);
|
|
# network inspect answers with the running containers on one as its members. An exec that writes
|
|
# devvm's block in /etc/environment keeps its input as $STUB_DIR/environment. Every container create
|
|
# and run is also appended to $STUB_DIR/containers-made, which nothing empties.
|
|
set -euo pipefail
|
|
echo "docker $*" >> "$STUB_LOG"
|
|
store=$STUB_DIR/docker
|
|
mkdir -p "$store/containers" "$store/images" "$store/volumes" "$store/networks"
|
|
cmd="${1:-} ${2:-}"
|
|
case "$cmd" in
|
|
"container create"|"container run") echo "docker $*" >> "$STUB_DIR/containers-made" ;;
|
|
esac
|
|
case "$cmd" in
|
|
"info "*)
|
|
[[ ! -e "$STUB_DIR/docker-down" ]] || { echo "failed to connect to the docker API" >&2; exit 1; }
|
|
[[ ! -e "$STUB_DIR/docker-denied" ]] || { echo "permission denied while trying to connect" >&2; exit 1; }
|
|
[[ ${2:-} == --format ]] || exit 0
|
|
if [[ -f $STUB_DIR/docker-info ]]; then cat "$STUB_DIR/docker-info"; exit 0; fi
|
|
arch=$(uname -m); [[ $arch != arm64 ]] || arch=aarch64
|
|
rt='"runc":{}'; [[ ! -e "$STUB_DIR/docker-sysbox" ]] || rt+=',"sysbox-runc":{}'
|
|
printf '{"ServerVersion":"29.1.3","OSType":"linux","Architecture":"%s","OperatingSystem":"Ubuntu 26.04 LTS","Name":"stub-host","MemTotal":68719476736,"SecurityOptions":["name=seccomp,profile=builtin"],"Runtimes":{%s}}\n' "$arch" "$rt"
|
|
exit 0 ;;
|
|
"buildx version") [[ ! -e "$STUB_DIR/no-buildx" ]]; exit $? ;;
|
|
"save -o")
|
|
id=$(cat "$STUB_DIR/docker-saved" 2>/dev/null || cat "$STUB_DIR/docker-image")
|
|
d=$(mktemp -d)
|
|
printf '[{"Config":"blobs/sha256/%s","RepoTags":["%s"],"Layers":[]}]' "${id#sha256:}" "$4" > "$d/manifest.json"
|
|
tar -cf "$3" -C "$d" manifest.json
|
|
rm -rf "$d"
|
|
exit 0 ;;
|
|
esac
|
|
[[ ! -e "$STUB_DIR/docker-down" ]] || { echo "failed to connect to the docker API" >&2; exit 1; }
|
|
|
|
# json_labels DIR: an object's labels as JSON.
|
|
json_labels() { local s="" k v
|
|
while IFS='=' read -r k v; do [[ -n $k ]] && s+="${s:+,}\"$k\":\"$v\""; done < "$1/labels"
|
|
echo "{$s}"; }
|
|
# field DIR NAME [DEFAULT]: an object's field.
|
|
field() { cat "$1/$2" 2>/dev/null || echo "${3:-}"; }
|
|
# matches DIR FILTER...: whether an object has every label=K=V filter's label.
|
|
matches() { local d=$1 f; shift
|
|
for f in "$@"; do grep -qxF "${f#label=}" "$d/labels" || return 1; done; }
|
|
# labels_of NAME KIND: an object's labels file, someone else's when docker-foreign-NAME exists.
|
|
foreign() { [[ -e $STUB_DIR/docker-foreign-$1 ]]; }
|
|
missing() { echo "Error response from daemon: No such $1: $2" >&2; exit 1; }
|
|
# by_id NAME: the name of the container with that ID, else NAME.
|
|
by_id() { local d
|
|
for d in "$STUB_DIR"/docker/containers/*; do [[ -f $d/id && $(cat "$d/id") == "$1" ]] && { basename "$d"; return; }; done
|
|
echo "$1"; }
|
|
|
|
# Options: what devvm passes, read into variables; the rest are the arguments.
|
|
name="" labels=() filters=() mounts=() envs=() changes=() opts=() format="" cpus="" memory="" hostname="" network=""
|
|
user="" args=() input=false size=false internal=false runtime="" verb=${1:-}
|
|
case $verb in exec|update) shift 1 ;; *) shift 2 || true ;; esac
|
|
while (( $# )); do
|
|
case $1 in
|
|
--name) name=$2; shift ;;
|
|
--label) labels+=("$2"); shift ;;
|
|
--filter) filters+=("$2"); shift ;;
|
|
--mount) mounts+=("$2"); shift ;;
|
|
-v) if [[ $cmd == "container create" ]]; then mounts+=("volume=$2"); shift; fi ;;
|
|
-o) opts+=("$2"); shift ;;
|
|
--internal) internal=true ;;
|
|
--runtime) runtime=$2; shift ;;
|
|
--network-alias|--cap-drop|--security-opt|--pids-limit|--env-file) shift ;;
|
|
--read-only) ;;
|
|
-e) envs+=("$2"); shift ;;
|
|
--change) changes+=("$2"); shift ;;
|
|
--format) format=$2; shift ;;
|
|
--cpus) cpus=$2; shift ;;
|
|
--memory) memory=$2; shift ;;
|
|
--hostname) hostname=$2; shift ;;
|
|
--network) network=$2; shift ;;
|
|
-u|--user) user=$2; shift ;;
|
|
-w|--entrypoint|--memory-swap|-t) [[ $1 == -t && ${2:-} != [0-9]* ]] || shift ;;
|
|
-i) input=true ;;
|
|
-a|-q|--no-trunc|--init|--rm|-f|--size) [[ $1 != --size ]] || size=true ;;
|
|
*) args=("$@"); break ;;
|
|
esac
|
|
shift
|
|
done
|
|
C=$store/containers I=$store/images V=$store/volumes N=$store/networks
|
|
# json_networks DIR: a container's networks as inspect has them.
|
|
json_networks() { local s="" n
|
|
while read -r n; do [[ -n $n ]] && s+="${s:+,}\"$n\":{\"NetworkID\":\"id-net-$n\"}"; done < <(cat "$1/networks" 2>/dev/null)
|
|
echo "{$s}"; }
|
|
|
|
case "$cmd" in
|
|
"container ls")
|
|
for d in "$C"/*; do
|
|
[[ -d $d ]] && matches "$d" ${filters[@]+"${filters[@]}"} && basename "$d"
|
|
done
|
|
exit 0 ;;
|
|
"container inspect")
|
|
if $size; then [[ -d $C/${args[0]} ]] || missing container "${args[0]}"; field "$C/${args[0]}" sizerw 1048576; exit 0; fi
|
|
out="" sep=""
|
|
for n in "${args[@]}"; do
|
|
n=$(by_id "$n")
|
|
d=$C/$n
|
|
[[ -d $d ]] || missing container "$n"
|
|
lab=$(json_labels "$d")
|
|
foreign "$n" && lab='{"devvm.data":"/elsewhere"}'
|
|
if [[ $format == "{{.State.Running}} {{.State.StartedAt}}" ]]; then
|
|
[[ $(field "$d" status) == running ]] && echo "true $(field "$d" started)" || echo "false $(field "$d" started)"; exit 0
|
|
fi
|
|
out+="$sep{\"Id\":\"$(field "$d" id)\",\"Name\":\"/$n\",\"State\":{\"Status\":\"$(field "$d" status)\",\"StartedAt\":\"$(field "$d" started)\"},\"Config\":{\"Labels\":$lab,\"Env\":[]},"
|
|
out+="\"HostConfig\":{\"NanoCpus\":$(( $(field "$d" cpus 0) * 1000000000 )),\"Memory\":$(field "$d" memory 0)},"
|
|
out+="\"NetworkSettings\":{\"Networks\":$(json_networks "$d")},\"Image\":\"$(field "$d" image)\"}"
|
|
sep=,
|
|
done
|
|
echo "[$out]"; exit 0 ;;
|
|
"container create")
|
|
d=$C/$name
|
|
[[ ! -e $d ]] || { echo "Conflict. The container name \"/$name\" is already in use" >&2; exit 1; }
|
|
img=${args[0]}
|
|
if [[ $img == devvm-* ]]; then [[ -d $I/$img ]] || missing image "$img"; fi
|
|
mkdir -p "$d"
|
|
printf '%s\n' ${labels[@]+"${labels[@]}"} > "$d/labels"
|
|
echo created > "$d/status"; echo "$img" > "$d/image"; echo "${cpus:-0}" > "$d/cpus"
|
|
m=0; [[ -z $memory ]] || m=$(( ${memory%m} * 1048576 )); echo "$m" > "$d/memory"
|
|
echo "$hostname" > "$d/hostname"; echo "$network" > "$d/network"; echo "$network" > "$d/networks"
|
|
printf '%s\n' ${mounts[@]+"${mounts[@]}"} > "$d/mounts"
|
|
printf '%s\n' ${envs[@]+"${envs[@]}"} > "$d/env"
|
|
echo "$runtime" > "$d/runtime"
|
|
echo "id-$name-$RANDOM" > "$d/id"
|
|
[[ ! -d $I/$img ]] || cp "$I/$img/layers" "$d/layers"
|
|
exit 0 ;;
|
|
"container start")
|
|
d=$C/$(by_id "${args[0]}")
|
|
[[ -d $d ]] || missing container "${args[0]}"
|
|
[[ ! -e $STUB_DIR/docker-start-fails ]] || exit 1
|
|
n=$(( $(field "$d" starts 0) + 1 )); echo "$n" > "$d/starts"
|
|
echo "2026-01-01T00:00:0${n}Z" > "$d/started"
|
|
echo running > "$d/status"; exit 0 ;;
|
|
"container stop")
|
|
[[ -d $C/${args[0]} ]] || missing container "${args[0]}"
|
|
echo exited > "$C/${args[0]}/status"; exit 0 ;;
|
|
"container rm")
|
|
[[ -d $C/${args[0]} ]] || missing container "${args[0]}"
|
|
rm -rf "${C:?}/${args[0]}"; exit 0 ;;
|
|
"container rename")
|
|
[[ -d $C/${args[0]} && ! -e $C/${args[1]} ]] || exit 1
|
|
mv "$C/${args[0]}" "$C/${args[1]}"; exit 0 ;;
|
|
"container commit")
|
|
d=$C/${args[0]} ref=${args[1]}
|
|
[[ -d $d ]] || missing container "${args[0]}"
|
|
mkdir -p "$I/$ref"
|
|
cp "$d/labels" "$I/$ref/labels"
|
|
for ch in ${changes[@]+"${changes[@]}"}; do
|
|
kv=${ch#LABEL }; k=${kv%%=*}
|
|
grep -v "^$k=" "$I/$ref/labels" > "$I/$ref/labels.new" || true
|
|
echo "$kv" >> "$I/$ref/labels.new"; mv "$I/$ref/labels.new" "$I/$ref/labels"
|
|
done
|
|
echo $(( $(field "$d" layers 1) + 1 )) > "$I/$ref/layers"
|
|
exit 0 ;;
|
|
"container export") echo "a container's files"; exit 0 ;;
|
|
"container run")
|
|
cat > /dev/null
|
|
for ((i = 0; i < ${#args[@]}; i++)); do [[ ${args[i]} == -- ]] && break; done
|
|
echo "${args[*]:i+1}" >> "$STUB_DIR/unpacked"
|
|
[[ ! -e $STUB_DIR/unpack-fails ]] || exit 1
|
|
exit 0 ;;
|
|
"image inspect")
|
|
if [[ $format == '{{.Id}}' ]]; then
|
|
ref=${args[0]}
|
|
if [[ -d $I/$ref ]]; then echo "sha256:stub"; exit 0; fi
|
|
[[ -f "$STUB_DIR/docker-image" ]] || missing image "$ref"
|
|
cat "$STUB_DIR/docker-image"; exit 0
|
|
fi
|
|
d=$I/${args[0]}
|
|
[[ -d $d ]] || missing image "${args[0]}"
|
|
layers="" n=$(field "$d" layers 1)
|
|
for ((i = 0; i < n; i++)); do layers+="${layers:+,}\"sha256:$i\""; done
|
|
echo "[{\"Size\":$(field "$d" size 500000000),\"Config\":{\"Labels\":$(json_labels "$d"),\"Env\":[\"PATH=/usr/bin\"]},\"RootFS\":{\"Layers\":[$layers]}}]"
|
|
exit 0 ;;
|
|
"image ls")
|
|
for d in "$I"/*; do
|
|
[[ -d $d ]] && matches "$d" ${filters[@]+"${filters[@]}"} && basename "$d"
|
|
done
|
|
exit 0 ;;
|
|
"image rm")
|
|
[[ -d $I/${args[0]} ]] || missing image "${args[0]}"
|
|
rm -rf "${I:?}/${args[0]}"; exit 0 ;;
|
|
"image pull") exit 0 ;;
|
|
"image import")
|
|
cat > /dev/null
|
|
ref=${args[1]}
|
|
mkdir -p "$I/$ref"; echo 1 > "$I/$ref/layers"; : > "$I/$ref/labels"
|
|
for ch in ${changes[@]+"${changes[@]}"}; do
|
|
case $ch in LABEL\ *) kv=${ch#LABEL }; echo "${kv//\"/}" >> "$I/$ref/labels" ;; esac
|
|
done
|
|
exit 0 ;;
|
|
"network create")
|
|
[[ ! -e $N/${args[0]} ]] || { echo "network with name ${args[0]} already exists" >&2; exit 1; }
|
|
mkdir -p "$N/${args[0]}"; printf '%s\n' ${labels[@]+"${labels[@]}"} > "$N/${args[0]}/labels"
|
|
echo "$internal" > "$N/${args[0]}/internal"
|
|
inhibit=false; for o in ${opts[@]+"${opts[@]}"}; do [[ $o != com.docker.network.bridge.inhibit_ipv4=true ]] || inhibit=true; done
|
|
echo "$inhibit" > "$N/${args[0]}/inhibit"; exit 0 ;;
|
|
"network inspect")
|
|
d=$N/${args[0]}
|
|
[[ -d $d ]] || { echo "[]"; echo "Error response from daemon: network ${args[0]} not found" >&2; exit 1; }
|
|
lab=$(json_labels "$d"); foreign "${args[0]}" && lab='{"devvm.data":"/elsewhere"}'
|
|
members="" sep=""
|
|
for c in "$C"/*; do
|
|
if ! [[ -d $c && $(field "$c" status) == running ]] || ! grep -qx "${args[0]}" "$c/networks" 2>/dev/null; then continue; fi
|
|
members+="$sep\"$(field "$c" id)\":{\"Name\":\"$(basename "$c")\"}"; sep=,
|
|
done
|
|
opt=""; [[ $(field "$d" inhibit) != true ]] || opt='"com.docker.network.bridge.inhibit_ipv4":"true"'
|
|
echo "[{\"Id\":\"id-net-${args[0]}\",\"Driver\":\"bridge\",\"Internal\":$(field "$d" internal false),\"EnableIPv6\":$(field "$d" ipv6 false),\"Options\":{$opt},\"Labels\":$lab,\"Containers\":{$members}}]"
|
|
exit 0 ;;
|
|
"network rm")
|
|
[[ -d $N/${args[0]} ]] || { echo "network ${args[0]} not found" >&2; exit 1; }
|
|
rm -rf "${N:?}/${args[0]}"; exit 0 ;;
|
|
"network connect")
|
|
[[ -d $N/${args[0]} || ${args[0]} == bridge ]] || { echo "network ${args[0]} not found" >&2; exit 1; }
|
|
c=$C/$(by_id "${args[1]}"); [[ -d $c ]] || missing container "${args[1]}"
|
|
echo "${args[0]}" >> "$c/networks"; exit 0 ;;
|
|
"network disconnect")
|
|
c=$C/$(by_id "${args[1]}"); [[ -d $c ]] || missing container "${args[1]}"
|
|
grep -vx "${args[0]}" "$c/networks" > "$c/networks.new" || true; mv "$c/networks.new" "$c/networks"; exit 0 ;;
|
|
"volume create")
|
|
mkdir -p "$V/${args[0]}"; printf '%s\n' ${labels[@]+"${labels[@]}"} > "$V/${args[0]}/labels"; exit 0 ;;
|
|
"volume ls")
|
|
for d in "$V"/*; do
|
|
[[ -d $d ]] && ! foreign "$(basename "$d")" && matches "$d" ${filters[@]+"${filters[@]}"} && basename "$d"
|
|
done
|
|
exit 0 ;;
|
|
"volume inspect")
|
|
[[ -d $V/${args[0]} ]] || missing volume "${args[0]}"
|
|
if foreign "${args[0]}"; then echo '{"devvm.data":"/elsewhere"}'; else json_labels "$V/${args[0]}"; fi
|
|
exit 0 ;;
|
|
"volume rm")
|
|
[[ -d $V/${args[0]} ]] || missing volume "${args[0]}"
|
|
rm -rf "${V:?}/${args[0]}"; exit 0 ;;
|
|
"system df")
|
|
out="" sep=""
|
|
for d in "$V"/*; do
|
|
[[ -d $d ]] || continue
|
|
out+="$sep{\"Name\":\"$(basename "$d")\",\"Size\":\"$(field "$d" size 1.5GB)\"}"; sep=,
|
|
done
|
|
echo "[$out]"; exit 0 ;;
|
|
esac
|
|
|
|
case $verb in
|
|
update)
|
|
d=$C/${args[0]}
|
|
[[ -d $d ]] || missing container "${args[0]}"
|
|
[[ -z $cpus ]] || echo "$cpus" > "$d/cpus"
|
|
[[ -z $memory ]] || echo $(( ${memory%m} * 1048576 )) > "$d/memory"
|
|
exit 0 ;;
|
|
exec)
|
|
n=$(by_id "${args[0]}") line="${args[*]}"
|
|
[[ -d $C/$n ]] || missing container "$n"
|
|
[[ $(field "$C/$n" status) == running ]] || { echo "container $n is not running" >&2; exit 1; }
|
|
if [[ $line == *"command -v apk"* ]]; then cat "$STUB_DIR/build-libc" 2>/dev/null || echo gnu; exit 0; fi
|
|
if [[ $line == *"then echo sshd"* ]]; then if [[ -e $STUB_DIR/no-sshd ]]; then echo none; else echo sshd; fi; exit 0; fi
|
|
if [[ $line == *"/etc/ssh/ssh_host_"* ]]; then echo "ssh-ed25519 AAAAstub root@stub"; exit 0; fi
|
|
if [[ $line == *"tar -xf - "* ]]; then cat > "$STUB_DIR/sync.tar"; exit 0; fi
|
|
if [[ $line == *"devvm-proxy begin"* ]]; then cat > "$STUB_DIR/environment"; exit 0; fi
|
|
if [[ $n == devvm-tools-build-* && $line == *" bash -s -- "* ]]; then
|
|
tout=$(sed -n 's/^type=bind,src=\([^,]*\),dst=\/mnt\/devvm-tools$/\1/p' "$C/$n/mounts")
|
|
for ((i = 0; i < ${#args[@]}; i++)); do [[ ${args[i]} == -- ]] && break; done
|
|
for ((i = i + 3; i < ${#args[@]}; i += 3)); do echo "image ${args[i]}" > "$tout/${args[i]}"; done
|
|
cat > /dev/null; exit 0
|
|
fi
|
|
if $input && [[ $line == *"bash -s"* || $line == *"sh -s"* ]]; then
|
|
sed -n 2p >> "$STUB_DIR/scripts"; cat > /dev/null || true
|
|
[[ ! -e $STUB_DIR/script-fails || $line != *"bash -s"* ]] || exit 1
|
|
elif $input; then
|
|
cat > /dev/null || true
|
|
fi
|
|
: "$user"
|
|
exit 0 ;;
|
|
esac
|
|
exit 0
|