mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 21:36:40 -10:00
Every file devvm keeps for a project VM is in one folder, vms/<hash>/, where <hash> is the 14-hex hash of the project folder's path that the VM's name ends with: its state, its own rules (policy), its backend's files (backend/: record.json, key, known_hosts, proxy, ports and the port forwarder's ports.*), what its runs leave (run/: proxy.*, sandbox.sb, started, lifeline.pid, secrets.*, links.*) and its logs (logs/: network.jsonl, proxy.log, links.log). Build VMs keep the same layout in run/builds/<name>/. The top-level run/ holds only what isn't one VM's or must be short: the hashed control sockets, links' link-<hash>.sock/.ctl, the QEMU wrapper and the ports lock. A folder's VM is found by its path's hash (state.ReadHash), with the name and backend its state records, so devvm --backend docker then devvm in the same folder opens the one VM, and a backend's name limit applies to new VMs only. The state records the VM's name and its ssh alias, chosen once at creation (sshconfig.ChooseAlias) and never recomputed. Links' targets match recorded names, aliases and folders (project.Targets). One teardown (vmFolders.teardown) replaces Lifecycle.forget, undoCreate's own list, Leftovers.Forget's, Proxy.Forget and space's leftoverFiles: it stops the proxy and link session, then the pieces outside the folder (clone remote or .git locks, other VMs' links and the group, host keys, the backend's leftovers), removes the folder, and writes the ssh config. Each backend's Delete and Forget remove the VM's backend/ folder (vmssh.Forwards.Forget). A new VM made where an earlier one's folder was left clears it first. devvm space counts an orphan's whole folder as leftover state and the other VMs' logs as network logs. Path builders move into paths: Dirs.VMDir, Dirs.VM and paths.VM's files (secrets' BindingsFile/PolicyFile, netpolicy's run, EventLog, ProxyLog, LinkList, VMPolicyFile, Lima's profileFile, startedFile, forwards.file, vmssh.Forwards.file, links' LinksLog/linksPID, Docker's vmFile and lifeline), ToolLockConfig, ToolLockChecked and ToolsCheckLog; the user's cache folder for Lima's and smolvm's caches is Dirs.UserCache, from HOME, not os.UserCacheDir. Dirs.VMState, LimaVMs, SmolvmVM and DockerVM go. vmname.HashOf and IsHash, project.Hash. The proxy takes the links' sockets' folder (-link-sockets, devvm's run folder) apart from the links' list, which is in the VM's run folder. Tests: a VM made and removed (rm, gc --orphans) leaves nothing under vms/, and with network policy has no file named for it outside its folder (TestTeardownLeavesNothing); a new VM over an old folder starts clean; lookup finds a VM made on another backend with a long folder name and makes no second one (TestLookupOtherBackend, and in docker.txtar); the alias stays put when a second VM shares the folder name (TestChooseAlias, ssh.txtar); every socket fits under a 35-character home with the longest VM name (TestSocketsFit); space counts an orphan's whole folder (TestLeftoverFolders); paths.VM's files; state by hash and name; the proxy refuses a socket in the list's own folder. Co-Authored-By: Claude Opus 5.5 <[email protected]>
227 lines
11 KiB
Plaintext
227 lines
11 KiB
Plaintext
# --from: base from a container image, converted into a disk in a
|
|
# throwaway VM from Ubuntu's image, keyed by the image's digest: from its
|
|
# registry, else Docker's ID for an image only Docker has. Registries are
|
|
# never reached here: a pinned digest needs no lookup, and HTTPS_PROXY
|
|
# points at nothing, so every lookup is offline.
|
|
env IMAGES=$XDG_DATA_HOME/devvm/images
|
|
env LOCK=$XDG_DATA_HOME/devvm/oci-build.lock
|
|
env TMPDIR=$WORK/tmp
|
|
mkdir $TMPDIR
|
|
env PIN=node:24@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
|
env HTTPS_PROXY=http://127.0.0.1:9
|
|
env https_proxy=http://127.0.0.1:9
|
|
|
|
# Not an image reference: refused before anything runs, from the flag or
|
|
# from DEVVM_FROM.
|
|
! exec devvm --from 'node;x' build base
|
|
stderr '^devvm: bad_from from=node;x$'
|
|
env DEVVM_FROM='a b'
|
|
! exec devvm build base
|
|
stderr '^devvm: bad_from from=a b$'
|
|
env DEVVM_FROM=
|
|
! stub_calls
|
|
|
|
# A pinned digest: no lookup, no Docker. A build VM from Ubuntu's image,
|
|
# with only a folder of devvm's temporary one mounted writable and
|
|
# devvm's oci/ (written into that temporary folder) read-only, converts exactly
|
|
# that digest; it's deleted before base is
|
|
# created from what it wrote (outside the folder it wrote in), and the
|
|
# temporary folder goes once base has started. The conversion lock's file,
|
|
# left as a devvm that crashed leaves it, holds nothing.
|
|
exec sh -c ': > "$LOCK"'
|
|
exec sh -c 'echo musl > "$STUB_DIR/build-libc"'
|
|
exec devvm --from $PIN build base
|
|
stdout '^==> building_image_from from=node:24@sha256:a{64} image=base key=[0-9a-f]{12}$'
|
|
! stdout base_build_time
|
|
stdout '^==> converting_in_vm from=node:24@sha256:a{64} vm=devvm-oci-build$'
|
|
stdout '^==> image_ready image=base$'
|
|
! stdout oci_waiting
|
|
stub_calls
|
|
! stdout '^docker'
|
|
stdout '^gpgv --keyring '
|
|
stdout '^limactl create --tty=false --mount-none --name=devvm-oci-build --set \.images = \[\{"location":"file://.*/release-[0-9]{8}/ubuntu-26\.04-server-cloudimg-(arm64|amd64)\.img","arch":"(aarch64|x86_64)","digest":"sha256:[0-9a-f]{64}"\}\] --set \.mounts = \[\{"location":"/\S*/tmp/devvm-oci\.[0-9]+/vm","writable":true\},\{"location":"/\S*/tmp/devvm-oci\.[0-9]+/oci","writable":false\}\] --cpus=4 --memory=8 -$'
|
|
stdout '^limactl start --tty=false devvm-oci-build$'
|
|
# With HTTPS_PROXY set, the build goes through a proxy of devvm's, which
|
|
# alone has the host's proxy settings: the script reads that proxy's
|
|
# address on its input first, then runs with its arguments.
|
|
stdout '^limactl shell --workdir / devvm-oci-build sudo bash -s -- /\S*/tmp/devvm-oci\.[0-9]+/oci/build\.sh /\S*/tmp/devvm-oci\.[0-9]+/oci /\S*/tmp/devvm-oci\.[0-9]+/vm node:24@sha256:a{64} node:24@sha256:a{64} \S+ [0-9]+ /home/\S+\.guest 20G$'
|
|
stdout '^limactl delete -f devvm-oci-build\nlimactl create --tty=false --mount-none --name=devvm-img-[0-9a-f]{12} --set \.images = \[\{"location":"/\S*/tmp/devvm-oci\.[0-9]+/vm/disk\.raw","arch":"(aarch64|x86_64)","kernel":\{"location":"/\S*/tmp/devvm-oci\.[0-9]+/vm/kernel","cmdline":"root=/dev/vda rw console=hvc0"\},"initrd":\{"location":"/\S*/tmp/devvm-oci\.[0-9]+/vm/initrd"\}\}\] --cpus=4 --memory=8 -$'
|
|
cmp $STUB_DIR/create-template $DEVVM_REPO/assets/base.yaml
|
|
stdout '^limactl shell devvm-img-[0-9a-f]{12} bash -s$'
|
|
exec sh -c 'ls "$TMPDIR"'
|
|
! stdout .
|
|
# The builds' proxies are gone, with their files.
|
|
! exists $XDG_DATA_HOME/devvm/run/builds/devvm-oci-build
|
|
exec find $XDG_DATA_HOME/devvm/run/builds -name '*proxy*'
|
|
! stdout .
|
|
# Its record: what it came from, and musl from the image.
|
|
catg $IMAGES/????????????/record.json
|
|
stdout '^ "name": "base",$'
|
|
stdout '^ "from": "node:24@sha256:a{64}",$'
|
|
stdout '^ "digest": "node:24@sha256:a{64}",$'
|
|
stdout '^ "libc": "musl",$'
|
|
# Each step numbered: the conversion's five, then base's, with sudo told
|
|
# to keep the build proxy's settings and told no more.
|
|
exec sh -c 'cat "$XDG_DATA_HOME"/devvm/logs/runs/*.log'
|
|
stdout '^# \[1/12\] creating_from_ubuntu vm=devvm-oci-build$'
|
|
stdout '^# \[2/12\] configuring vm=devvm-oci-build$'
|
|
stdout '^# \[3/12\] starting vm=devvm-oci-build$'
|
|
stdout '^# \[4/12\] converting from=node:24@sha256:a{64}$'
|
|
stdout '^# \[5/12\] deleting vm=devvm-oci-build$'
|
|
stdout '^# \[6/12\] creating vm=devvm-img-'
|
|
stdout '^# \[12/12\] stopping vm=devvm-img-'
|
|
! stdout '\[13/12\]'
|
|
|
|
# Built: reused, without a lookup; a layer on it records where its base
|
|
# came from. Ubuntu's base is another image.
|
|
stub_reset
|
|
exec sh -c 'mkdir -p "$CFG/layers"; printf "#!/bin/bash\necho foo\n" > "$CFG/layers/foo.sh"'
|
|
exec devvm --from $PIN build foo
|
|
! stdout 'building_image_from .*image=base '
|
|
stdout '^==> building_image_from from=node:24@sha256:a{64} image=foo key=[0-9a-f]{12}$'
|
|
exec sh -c 'grep -lxF " \"name\": \"foo\"," "$IMAGES"/????????????/record.json | xargs cat'
|
|
stdout '^ "from": "node:24@sha256:a{64}",$'
|
|
stdout '^ "digest": "node:24@sha256:a{64}",$'
|
|
stdout '^ "libc": "musl",$'
|
|
exec devvm build base
|
|
stdout '^==> building_image image=base key=[0-9a-f]{12}$'
|
|
exec sh -c 'rm "$STUB_DIR/build-libc"'
|
|
|
|
# A new VM from it records --from; once it exists, another --from is
|
|
# ignored with a warning, and nothing is built for it.
|
|
project web
|
|
exec devvm --from $PIN --no-network-policy
|
|
stdout '^==> creating_from image=base vm=devvm-web-[0-9a-f]{14}$'
|
|
! stdout building_image
|
|
vm_name WEB web
|
|
catg $WEB_dir/state
|
|
stdout '^from=node:24@sha256:a{64}$'
|
|
stub_reset
|
|
exec devvm --from debian:13
|
|
stderr '^devvm: from_ignored vm=devvm-web-[0-9a-f]{14}$'
|
|
! stdout building_image
|
|
stub_calls
|
|
! stdout 'limactl create|docker image'
|
|
exec devvm --from $PIN
|
|
! stderr from_ignored
|
|
cd $WORK
|
|
|
|
# An image only Docker has, offline: Docker's ID, recorded; docker save
|
|
# hands it to the build VM, whose build.sh reads it for -.
|
|
stub_reset
|
|
exec sh -c 'echo sha256:dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd > "$STUB_DIR/docker-image"'
|
|
exec devvm --from myapp:dev build base
|
|
! stderr from_cached
|
|
stdout '^==> exporting_docker from=myapp:dev$'
|
|
grep '^ "myapp:dev": "sha256:d{64}"' $DATA/cache/from-digests.json
|
|
stub_calls
|
|
stdout '^docker image inspect --format \{\{\.Id\}\} myapp:dev$'
|
|
stdout '^docker info$'
|
|
stdout '^docker save -o /\S*/tmp/devvm-oci\.[0-9]+/vm/image\.tar myapp:dev$'
|
|
stdout '^limactl shell --workdir / devvm-oci-build sudo bash -s -- /\S*/tmp/devvm-oci\.[0-9]+/oci/build\.sh /\S*/tmp/devvm-oci\.[0-9]+/oci /\S*/tmp/devvm-oci\.[0-9]+/vm - myapp:dev '
|
|
exec sh -c 'ls -t "$XDG_DATA_HOME"/devvm/logs/runs/*.log | head -1 | xargs cat'
|
|
stdout '^# \[1/13\] exporting_docker from=myapp:dev$'
|
|
stdout '^# \[13/13\] stopping vm=devvm-img-'
|
|
|
|
# The cache may go at any time: the base keeps its key, from the digest
|
|
# its record names, so it stays current, and a build offline keeps it.
|
|
rm $DATA/cache
|
|
exec devvm images
|
|
stdout '^base +myapp:dev +\S+ +[0-9a-f]{12} +current '
|
|
! stdout ' stale '
|
|
stub_reset
|
|
exec devvm --from myapp:dev build base
|
|
stderr '^devvm: from_cached_unreachable from=myapp:dev host=registry-1\.docker\.io$'
|
|
! stdout building_image
|
|
|
|
# Saved by name: an export that isn't the recorded image (the tag moved
|
|
# since) is refused, before the conversion.
|
|
stub_reset
|
|
exec sh -c 'echo sha256:eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee > "$STUB_DIR/docker-saved"'
|
|
! exec devvm --from myapp:other build base
|
|
stderr '^devvm: docker_image_changed from=myapp:other$'
|
|
stub_calls
|
|
! stdout 'oci/build\.sh'
|
|
exec sh -c 'ls "$TMPDIR"'
|
|
! stdout .
|
|
exec sh -c 'rm "$STUB_DIR/docker-saved"'
|
|
|
|
# Docker not running for an image only it has: the recorded ID is kept,
|
|
# with a warning, then refused.
|
|
cp digests.json $DATA/cache/from-digests.json
|
|
exec sh -c ': > "$STUB_DIR/docker-down"'
|
|
! exec devvm --from myapp:third build base
|
|
stderr '^devvm: from_cached_unreachable from=myapp:third host=registry-1\.docker\.io$'
|
|
stderr '^devvm: docker_only from=myapp:third$'
|
|
exec sh -c 'rm "$STUB_DIR/docker-down"'
|
|
|
|
# Offline: a digest the registry gave before is kept, with a warning;
|
|
# with none, and Docker without the image, the lookup fails.
|
|
stub_reset
|
|
exec devvm --from node:24 build base
|
|
stderr '^devvm: from_cached_unreachable from=node:24 host=registry-1\.docker\.io$'
|
|
stub_calls
|
|
stdout ' sudo bash -s -- /\S*/tmp/devvm-oci\.[0-9]+/oci/build\.sh /\S*/tmp/devvm-oci\.[0-9]+/oci /\S*/vm node@sha256:a{64} node:24 '
|
|
! stdout 'docker save'
|
|
rm $STUB_DIR/docker-image
|
|
! exec devvm --from debian:13 build base
|
|
stderr '^devvm: from_unreachable from=debian:13 host=registry-1\.docker\.io$'
|
|
|
|
# A failed conversion says so, and the build VM and the temporary folder
|
|
# go.
|
|
stub_reset
|
|
exec sh -c ': > "$STUB_DIR/build-fails"'
|
|
! exec devvm rebuild --force --from $PIN base
|
|
stderr '^devvm: convert_failed from=node:24@sha256:a{64}$'
|
|
stub_calls
|
|
stdout '^limactl delete -f devvm-oci-build$'
|
|
! stdout '--name=devvm-img-'
|
|
exec sh -c 'ls "$TMPDIR"'
|
|
! stdout .
|
|
exec sh -c 'rm "$STUB_DIR/build-fails"'
|
|
|
|
# What the image's own code wrote is untrusted: a command line that could
|
|
# change Lima's config, a symlink to a host file, an unknown libc, or a
|
|
# disk that isn't raw ext4 (qcow2 can name a host file as its backing
|
|
# file) are refused, and base is never created from them.
|
|
stub_reset
|
|
exec sh -c 'printf "root=/dev/vda rw console=hvc0\"\ncopyToHost:\n- guest: /etc/hostname\n host: /tmp/x\n" > "$STUB_DIR/build-cmdline"'
|
|
! exec devvm rebuild --force --from $PIN base
|
|
stderr '^devvm: convert_bad_output file=cmdline from=node:24@sha256:a{64}$'
|
|
exec sh -c 'rm "$STUB_DIR/build-cmdline"; echo hostile > "$HOME/secret"; echo "disk.raw $HOME/secret" > "$STUB_DIR/build-symlinks"'
|
|
! exec devvm rebuild --force --from $PIN base
|
|
stderr '^devvm: convert_bad_output file=disk.raw from=node:24@sha256:a{64}$'
|
|
exec sh -c 'rm "$STUB_DIR/build-symlinks"; printf "gnu\nx\n" > "$STUB_DIR/build-libc"'
|
|
! exec devvm rebuild --force --from $PIN base
|
|
stderr '^devvm: convert_bad_output file=libc from=node:24@sha256:a{64}$'
|
|
exec sh -c 'rm "$STUB_DIR/build-libc"; : > "$STUB_DIR/build-qcow2"'
|
|
! exec devvm rebuild --force --from $PIN base
|
|
stderr '^devvm: convert_bad_output file=disk.raw from=node:24@sha256:a{64}$'
|
|
exec sh -c 'rm "$STUB_DIR/build-qcow2"'
|
|
stub_calls
|
|
! stdout '--name=devvm-img-'
|
|
exec cat $HOME/secret
|
|
stdout '^hostile$'
|
|
exec sh -c 'ls "$TMPDIR"'
|
|
! stdout .
|
|
|
|
# Ctrl+C during the conversion deletes the build VM and the temporary
|
|
# folder; nothing is left in Lima.
|
|
stub_reset
|
|
exec sh -c 'echo oci/build.sh > "$STUB_DIR/interrupt-on"; cp "$STUB_DIR/instances" "$WORK/before"'
|
|
! exec devvm rebuild --force --from $PIN base
|
|
! stderr convert_failed
|
|
stub_calls
|
|
stdout '^limactl delete -f devvm-oci-build$'
|
|
cmp $STUB_DIR/instances $WORK/before
|
|
exec sh -c 'ls "$TMPDIR"'
|
|
! stdout .
|
|
|
|
-- digests.json --
|
|
{
|
|
"digests": {
|
|
"myapp:third": "sha256:dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd",
|
|
"node:24": "node@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
|
}
|
|
}
|