mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 21:36:40 -10:00
fix(guest): a dockerd restart without an init system stops only dockerd
With no init system (smolvm, Docker's containers), a launch that changed Docker's proxy settings killed whatever process /run/docker.pid named. /run outlives a stop there, so that could be another process that took the pid. The restart now kills the pid only when /proc/PID/comm is dockerd, as services.sh checks, and deletes the pid file and /run/docker before dockerd starts, as a boot would. Tests: e2e smolvm, on the Alpine docker-layer VM with network policy (the base whose /etc/conf.d/docker the restart reads): a stale pid file naming another process and a left-over run folder, then a launch that restarts Docker's daemon; the other process lives, the run folder is new and dockerd runs. It fails on the code before (the other process gone). Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
4 files changed
+27
-4
No files matched your search
@@ -361,11 +361,13 @@ if command -v dockerd >/dev/null; then
|
||||
elif [[ -e /run/openrc/softlevel ]]; then sudo rc-service docker restart >/dev/null
|
||||
else
|
||||
# No init system (smolvm): stopped, then started as assets/container/services.sh starts it, with
|
||||
# Docker's settings read (devvm's block) and the proxy variables sudo keeps.
|
||||
# Docker's settings read (devvm's block) and the proxy variables sudo keeps. A pid file whose
|
||||
# process isn't dockerd (a pid reused since) names nothing to stop, and Docker's run folder goes.
|
||||
sudo sh -c 'pid=$(cat /run/docker.pid 2>/dev/null || true)
|
||||
if [ -n "$pid" ] && kill "$pid" 2>/dev/null; then
|
||||
if [ -n "$pid" ] && [ "$(cat "/proc/$pid/comm" 2>/dev/null)" = dockerd ] && kill "$pid" 2>/dev/null; then
|
||||
i=0; while kill -0 "$pid" 2>/dev/null && [ "$i" -lt 30 ]; do sleep 1; i=$((i + 1)); done
|
||||
fi
|
||||
rm -rf /run/docker.pid /run/docker
|
||||
[ ! -f /etc/conf.d/docker ] || . /etc/conf.d/docker
|
||||
mkdir -p /var/log
|
||||
nohup dockerd -p /run/docker.pid >/var/log/dockerd.log 2>&1 </dev/null &'
|
||||
|
||||
@@ -226,7 +226,7 @@ The sections so far:
|
||||
- `tools`: first, the shared tools resolve to upstream's newest versions, with devvm's pinned mise (downloaded once), each download with its checksum; the rest of the run doesn't check again (`DEVVM_NO_TOOL_CHECK`). Then a node project's VM: node, npm, pnpm, bun and yarn at the locked versions, each a read-only EROFS image, the share read-only even for root, `npm i -g` into the VM's home, a non-login shell over SSH, an `.nvmrc`'s own version, the tools mounted at boot without devvm, and a second node VM that builds nothing.
|
||||
- `network`: a claude+codex VM with `--network-policy`, a fake token in devvm's environment and a fake `~/.claude.json` and Codex login in a home of its own. Its proxy runs and the self-check passed; its password was in no host process's arguments while devvm launched it (`ps`, sampled), and the guest has it; `ssh devvm-net` reaches it; direct connections fail, even as root, to an address and to other ports; an allowed host works and others, or other ports, are blocked; DNS fails at once; the host's loopback services are out of reach, directly or through the proxy; the proxy refuses requests without the VM's password; the token is nowhere in the VM, which has devvm's CA, its own placeholder and logins' copies with placeholders only; the proxy puts the token in for its host (for the VM's own placeholder only), an MCP server's header and Codex's key for theirs, over TLS the VM verifies; the host's login files are unchanged. `devvm secret set github --stdin` saves a fake token (in the Keychain on macOS, under the run's service, found through a link to `~/Library/Keychains` in the VM's home) and `secret get` reads it back; `secret use github` binds it to the running proxy at once, and on the next launch the VM has its own placeholder in `GH_TOKEN`, never the token, which the proxy puts in for `api.github.com`; `secret unuse` and `rm` take the binding and the token away. `TestE2ESelfCheck` in `internal/netpolicy` runs the self-check on the VM: it passes, then, once the VM is started outside devvm, it fails and stops the VM. `devvm stop` stops the VM and its proxy at once. `devvm network off` makes its next start direct, without a proxy; `on` while it runs shows it `unenforced` until `devvm restart` starts it confined; `off` while it runs confined keeps it on its proxy until it next starts.
|
||||
- `nix`: the nix layer. A copy of `assets/layers/nix.sh` as a layer of the run's own, built behind a company proxy of the run's own, reaches Nix's releases through it and leaves no proxy settings for `nix-daemon` (and not the company proxy's password) anywhere on its image's disk. Then a folder with `package.json`, `flake.nix` and `.envrc` gets a node+nix VM with network policy: Nix and direnv at their pins, a store that's root's, `sandbox` on with no fallback, `trusted-users` root alone and an untrusted user's `--option substituters` ignored, `nix-daemon` with the VM's own proxy in devvm's drop-in alone, `nix run nixpkgs#hello` from Nix's cache, a build in Nix's sandbox, `sudo nix` and `nix` over ssh, the flake's dev shell (nixpkgs at a pinned revision) loaded by direnv in an interactive shell, its `node` ahead of the shared node prompt after prompt and gone outside the folder, nix-direnv's cache in the VM's home with no `.direnv` in the folder, a fixed-output download from a host that isn't allowed refused with `403` and named by `devvm-why`, and after `devvm network off` and `devvm restart`, no drop-in and no proxy for the daemon.
|
||||
- `smolvm` (`--backend smolvm`): the [smolvm backend](../backends/smolvm.md) on smolvm's own machines, which on macOS share yours: the run looks only at machines with its sandbox's label, deletes only those it made, and checks every other machine is as it was. base and a fixture layer built as packs, a rebuild, `images rm`, and a failed script's machine kept stopped; a project VM with its size, the folder read-write and files the VM makes yours, the tools read-only even for root, its hostname, `TestE2EExecQuoting` in `internal/backend/smolvm` (arguments, folder, input, the VM's user and login environment), no Docker (no docker layer), a listed port on the host's loopback, a server on the VM's loopback alone reaching it, an unlisted port not forwarded, smolvm publishing none of the ports, a port added to a running VM reaching this machine at once and gone once removed, the VM never restarted, a new size applied on restart and new ports at once, a listed port a host program has on every address left to it until it's freed, stop, start and rm; a `--ssh-agent` VM using a throwaway agent, and none once turned off and restarted; ssh for editors' emulation ([its checks](../internals/design.md#ssh-for-editors-on-smolvm)): pinned host keys of each VM's own, its own key alone, no forwarding out even past a hostile sshd, `devvm host` and `code`, a VM `--from alpine:3` reached over ssh, a `--clone` VM's push and fetch; network policy's emulation, on a docker VM (no agent's login read): the VM behind its proxy and decider, the proxy's password in no host process's arguments while devvm launched it and in the guest's environment from its proxy file and Docker's daemon, Docker pulling through the proxy, its allow list and closed DNS, allowed and blocked hosts, direct connections failing even as root (to the host's loopback too), DNS failing at once and every probe query (TXT, ANY, AAAA, HTTPS, EDNS0, under `devvm.invalid`) refused in smolvm's decision log with no flow beyond the host's loopback (nettop, macOS), `devvm.invalid` as the log's control, UDP and ICMP getting nowhere, the decider's token nowhere in the VM or a process's arguments, `devvm network log`, `network allow` and `rm` at once, an allow list widened while it runs making it `unenforced`, nothing out with the proxy gone or a decider that answers garbage or nothing, no start without a decider, a start with someone else's decider stopped, and `network off` and `on`, where the direct, DNS and UDP probes get out (their controls); a docker VM `--from alpine:3` with network policy, its Docker restarted by devvm's setup and pulling through the proxy; a node VM's uncompressed tool images; a nix VM with network policy, whose `nix-daemon` devvm starts with the VM's proxy in `/etc/conf.d/nix-daemon`, running `nix run nixpkgs#hello` through the proxy and a build in Nix's sandbox, then after `devvm network off` and `devvm restart` a daemon without the proxy.
|
||||
- `smolvm` (`--backend smolvm`): the [smolvm backend](../backends/smolvm.md) on smolvm's own machines, which on macOS share yours: the run looks only at machines with its sandbox's label, deletes only those it made, and checks every other machine is as it was. base and a fixture layer built as packs, a rebuild, `images rm`, and a failed script's machine kept stopped; a project VM with its size, the folder read-write and files the VM makes yours, the tools read-only even for root, its hostname, `TestE2EExecQuoting` in `internal/backend/smolvm` (arguments, folder, input, the VM's user and login environment), no Docker (no docker layer), a listed port on the host's loopback, a server on the VM's loopback alone reaching it, an unlisted port not forwarded, smolvm publishing none of the ports, a port added to a running VM reaching this machine at once and gone once removed, the VM never restarted, a new size applied on restart and new ports at once, a listed port a host program has on every address left to it until it's freed, stop, start and rm; a `--ssh-agent` VM using a throwaway agent, and none once turned off and restarted; ssh for editors' emulation ([its checks](../internals/design.md#ssh-for-editors-on-smolvm)): pinned host keys of each VM's own, its own key alone, no forwarding out even past a hostile sshd, `devvm host` and `code`, a VM `--from alpine:3` reached over ssh, a `--clone` VM's push and fetch; network policy's emulation, on a docker VM (no agent's login read): the VM behind its proxy and decider, the proxy's password in no host process's arguments while devvm launched it and in the guest's environment from its proxy file and Docker's daemon, Docker pulling through the proxy, its allow list and closed DNS, allowed and blocked hosts, direct connections failing even as root (to the host's loopback too), DNS failing at once and every probe query (TXT, ANY, AAAA, HTTPS, EDNS0, under `devvm.invalid`) refused in smolvm's decision log with no flow beyond the host's loopback (nettop, macOS), `devvm.invalid` as the log's control, UDP and ICMP getting nowhere, the decider's token nowhere in the VM or a process's arguments, `devvm network log`, `network allow` and `rm` at once, an allow list widened while it runs making it `unenforced`, nothing out with the proxy gone or a decider that answers garbage or nothing, no start without a decider, a start with someone else's decider stopped, and `network off` and `on`, where the direct, DNS and UDP probes get out (their controls); a docker VM `--from alpine:3` with network policy, its Docker restarted by devvm's setup and pulling through the proxy, and a restart over a stale pid file stopping no process but Docker's daemon; a node VM's uncompressed tool images; a nix VM with network policy, whose `nix-daemon` devvm starts with the VM's proxy in `/etc/conf.d/nix-daemon`, running `nix run nixpkgs#hello` through the proxy and a build in Nix's sandbox, then after `devvm network off` and `devvm restart` a daemon without the proxy.
|
||||
- `docker` (`--backend docker`): the [Docker backend](../backends/docker.md), with state under `/tmp/devvm-go-e2e-docker`, on an engine where only what has its sandbox's label is touched, and what hasn't must be as it was at the end. On a Linux engine of the machine's own (on Linux; on macOS in the docker host VM below, from a `devvm` built here for it, its results counted here): doctor, base and a fixture layer built by committing containers, the layer's build reaching the internet and the host's server on every address but not its loopback-only one, a rebuild, `images rm`, a failed script's container kept stopped; a project VM with its CPUs and memory, the folder read-write and what the VM writes yours (uid and gid), its home a volume of its own, the tools' volume read-only even for root, its hostname, no Docker, `devvm size`'s `disk_used`, stop and start keeping its disk; a disk size and the nix layer refused in one line, nothing made; the docker layer ([its checks](../internals/design.md#the-docker-layer-on-docker)): on the Linux engine (Sysbox installed in the docker host VM, pinned) under Sysbox, on its own network, its engine's volumes counted in `devvm size`'s `disk_used`, and refused for a VM that exists; refused with network policy, and on Docker Desktop; ssh for editors', ports' and agent forwarding's emulations ([their checks](../internals/design.md#ssh-for-editors-on-docker)): no port published and no sshd listening, pinned host keys of each VM's own, its own key alone, no forwarding out even past a hostile sshd, `devvm host` and `code`, `devvm _docker-connect` refusing another devvm's container, a VM started outside devvm without ssh, every port and listed ports through devvm's forwarder, never a port of this machine's programs (on its `127.0.0.1`, `::1` or every address) or another VM's, the agent in ssh sessions alone (a throwaway one standing for yours, devvm's, none), a `--clone` VM's push and fetch; an agent VM in a home of its own whose added and removed folders make its container again from a commit, its disk and home kept, the commit before deleted, rm deleting its commit and volumes; a node VM whose tools are linked from Docker's tools volume, read-only even for root. Then on Docker Desktop: doctor naming it and its file sharing, a build refused in one line with nothing made, base and the shared tools copied from the Linux engine's run (Desktop can't build yet; the same paths give them the same label), and the project, agent and node VMs' checks again, with `TestE2EExecQuoting` in `internal/backend/docker`; at the end everything with the sandbox's label is deleted.
|
||||
- `from`: run with a `docker` that isn't running, so nothing comes from Docker. A node+python+docker+nix+claude VM from `node:24` (Debian 12, in a home of its own) boots, with its base keyed by the registry's digest, its node, python and claude at the locked versions, the docker layer's Docker from Docker's Debian repository, and the nix layer's Nix under systemd running `nix run nixpkgs#hello` and a build in Nix's sandbox; a node+docker+nix VM from `alpine:3` runs node and pnpm from musl tool images, the docker layer's Docker on OpenRC, with no crashed services, and the nix layer's Nix as an OpenRC service, with `nix run nixpkgs#hello` and a sandboxed build, then after `devvm network on` and `devvm restart` a download only its daemon makes from a host that isn't allowed refused with the proxy's `403`; base builds from `ubuntu:26.04`; and the build VM and its temporary folders are gone.
|
||||
|
||||
|
||||
@@ -98,7 +98,7 @@ A machine runs its image as a container, and nothing starts at boot. After each
|
||||
|
||||
- It makes the loop devices that the shared tools are mounted with, because `/dev` has none, and then mounts the tools.
|
||||
- It starts the VM's [sshd](#ssh-for-editors) with new host keys, logging to `/var/log/devvm-sshd.log`.
|
||||
- In a VM with the [docker layer](../using/images.md#built-in-layers), it starts Docker's daemon in the background, logging to `/var/log/dockerd.log`. The layer's script also starts the daemon when you add the layer to a running VM. devvm's setup restarts it the same way when a launch changes the VM's [network policy](../safety/network.md) settings, which on Alpine the daemon reads from `/etc/conf.d/docker`.
|
||||
- In a VM with the [docker layer](../using/images.md#built-in-layers), it starts Docker's daemon in the background, logging to `/var/log/dockerd.log`, with its pid in `/run/docker.pid`. The layer's script also starts the daemon when you add the layer to a running VM. devvm's setup restarts it the same way when a launch changes the VM's [network policy](../safety/network.md) settings, which on Alpine the daemon reads from `/etc/conf.d/docker`. `/run` outlives a stop here, so the pid file can name a process that isn't Docker's daemon, which took the pid since. The start counts the daemon as running only when that process is named `dockerd`, and the restart stops only such a process. Both delete the pid file and Docker's run folder, `/run/docker`, before the daemon starts, as a boot would.
|
||||
- In a VM with the [nix layer](images.md#the-nix-layer), it starts Nix's daemon in the background, logging to `/var/log/nix-daemon.log`, with its pid in `/run/nix-daemon.pid`. The daemon reads its settings from `/etc/conf.d/nix-daemon`, which devvm's setup writes with the VM's proxy when network policy is on and removes when it's off, and the setup restarts the daemon when that file changes. The layer's script also starts the daemon when you add the layer to a running VM.
|
||||
|
||||
devvm's commands in a VM, such as its setup and your shell, run as the VM's user with the environment a login would give: `HOME`, `USER`, and `/etc/environment`, which smolvm doesn't read.
|
||||
|
||||
@@ -3201,6 +3201,25 @@ smolnet_fail_closed() {
|
||||
! env -u SMOLVM_INTERCEPTOR_TOKEN SSH_AUTH_SOCK=/dev/null/e2e-no-agent SMOLVM_USAGE=0 smolvm machine start --name "$1" >"$work/e" 2>&1 &&
|
||||
[[ $(smol_status "$1") != running ]] && grep -qi interceptor "$work/e"
|
||||
}
|
||||
# smolnet_dockerd_stale VM DIR: Docker's pid file names another process (dockerd gone, its pid taken)
|
||||
# and its run folder is left over, when a launch restarts Docker's daemon (devvm's block in its
|
||||
# settings, /etc/conf.d/docker, gone, so the launch writes it again: Alpine's, which has that file):
|
||||
# the other process lives, and dockerd starts again, its run folder new.
|
||||
smolnet_dockerd_stale() { local out
|
||||
out=$(SMOLVM_USAGE=0 smolvm machine exec --name "$1" -- sh -c '
|
||||
pid=$(cat /run/docker.pid) && kill "$pid" && i=0
|
||||
while kill -0 "$pid" 2>/dev/null && [ "$i" -lt 30 ]; do sleep 1; i=$((i + 1)); done
|
||||
nohup sleep 900 >/dev/null 2>&1 </dev/null & echo $! > /run/docker.pid
|
||||
mkdir -p /run/docker && touch /run/docker/e2e-stale
|
||||
sed -i "/^# devvm-proxy begin/,/^# devvm-proxy end/d" /etc/conf.d/docker
|
||||
cat /run/docker.pid' </dev/null) || return 1
|
||||
(cd "$2" && printf 'exit\n' | "${U[@]}" "$D" >"$work/last.log" 2>&1) || { tail -5 "$work/last.log"; return 1; }
|
||||
SMOLVM_USAGE=0 smolvm machine exec --name "$1" -- sh -c '
|
||||
echo "the other process: $(cat /proc/'"$out"'/comm 2>/dev/null || echo gone); pid file: $(cat /run/docker.pid)"
|
||||
[ "$(cat /proc/'"$out"'/comm 2>/dev/null)" = sleep ] && kill '"$out"' && [ ! -e /run/docker/e2e-stale ] || exit 1
|
||||
i=0; while ! docker info >/dev/null 2>&1 && [ "$i" -lt 30 ]; do sleep 1; i=$((i + 1)); done
|
||||
[ "$(cat "/proc/$(cat /run/docker.pid)/comm")" = dockerd ] && docker info >/dev/null' </dev/null
|
||||
}
|
||||
# smolnet_widened VM DIR: its allow list widened while it runs (smolvm machine update): devvm shows
|
||||
# it unenforced, and its next launch stops it; the one after starts it confined again.
|
||||
smolnet_widened() {
|
||||
@@ -3353,6 +3372,8 @@ check "a VM --from alpine:3 with the docker layer and network policy is made, an
|
||||
{ tail -5 '$work/last.log'; false; }"
|
||||
check "... its Docker, restarted by devvm, pulls through the proxy" in_smol "$va" \
|
||||
'for i in $(seq 30); do sudo docker info >/dev/null 2>&1 && break; sleep 1; done; sudo docker run --rm hello-world | grep -q Hello'
|
||||
check "... a launch restarting it stops no other process its stale pid file names, and clears its run folder" \
|
||||
smolnet_dockerd_stale "$va" "$na"
|
||||
(cd "$na" && quiet "${U[@]}" "$D" rm --force --no-export)
|
||||
kill "$listener" 2>/dev/null; [[ -z ${udp_listener:-} ]] || kill "$udp_listener" 2>/dev/null; listener="" udp_listener=""
|
||||
rm -f "$work/listener.auth"
|
||||
|
||||
Reference in new issue
Block a user