Files
devvm/internal/vm/dockerlayer.go
T
w3cjandClaude Opus 5.5 7259ad13a1 feat(docker): the docker layer on Docker VMs with Sysbox
Docker's engine runs inside a Docker VM only under Sysbox, on a Linux engine of this machine's: its own user namespace, its own network with nothing else on it, volumes of its own for Docker's and containerd's files, dockerd started after each start. Made with the layer or given it in place (its container made again from a commit); a failed add in place makes the VM as it was. Elsewhere refused in one line (docker_layer_no_sysbox, docker_layer_needs_vm); devvm never runs a container --privileged.

No network policy for it at any step (net_docker_layer): root in it controls its own network. A bridge routing to this machine's loopback (Docker's userland proxy off), or one whose setting can't be read, is refused at creation and each start (docker_route_localnet, docker_bridge_unreadable).

Set-id files of root's, or root's group's, in the project folder are named apart as running as root (setid_root_files); setid_files says the rest run as their owner. Root in any Docker VM writes the folder as the host's root.

Reviewed with the security-audit skill; e2e on the Linux engine with Sysbox 0.7.1 (135/0) and Docker Desktop (115/0, the refusal).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-29 03:17:36 -07:00

92 lines
3.2 KiB
Go

package vm
import (
"context"
"github.com/w3cj/devvm/internal/backend"
"github.com/w3cj/devvm/internal/image"
"github.com/w3cj/devvm/internal/msg"
"github.com/w3cj/devvm/internal/netpolicy"
"github.com/w3cj/devvm/internal/state"
)
// dockerLayers is how VMs run the docker layer on a backend that makes
// VMs for it (backend.DockerLayerVM), new ones and those it's added to in
// place alike: the runtime (KeyDockerLayer), and setting it on a VM that
// exists. Such a VM gets no network policy.
type dockerLayers struct{ *Env }
// choice is the runtime a VM with the docker layer runs it under on a
// backend that makes VMs for it (dl), and the feature that needs
// (docker-layer, refused by Require while unreviewed); the backend's
// refusal where it can't run it.
func (e dockerLayers) choice(dl backend.DockerLayerVM, vm string) (string, []backend.Feature, error) {
rt, err := dl.DockerLayer(vm)
if err != nil {
return "", nil, err
}
return rt, []backend.Feature{backend.FeatureDockerLayer}, nil
}
// say says which runtime runs a VM's docker layer; nothing for "".
func (e dockerLayers) say(b backend.Backend, mode, vm string) {
if mode != "" {
e.Info(msg.DockerLayerRuntime(b.Name(), mode, vm))
}
}
// inPlace checks the docker layer added to a VM in place (when added has
// it) on a backend that makes VMs for it: its runtime (choice), or the
// backend's refusal; refused too for a VM with network policy, before
// anything changes. It returns the runtime ("" otherwise) and what adding
// the layers needs of the backend.
func (e dockerLayers) inPlace(b backend.Backend, st *state.VM, added string) (string, []backend.Feature, error) {
dl, made := b.(backend.DockerLayerVM)
if !made || !image.HasLayer(added, image.DockerLayer) {
return "", backend.ImageFeatures(added), nil
}
mode, need, err := e.choice(dl, st.Name())
if err == nil && netpolicy.On(st) {
err = msg.NetDockerLayer(b.Name(), st.Name())
}
return mode, need, err
}
// set makes a stopped VM one for the docker layer under runtime mode,
// recorded (KeyDockerLayer), its backend's container made again at its
// next start; nothing for "".
func (e dockerLayers) set(ctx context.Context, b backend.Backend, st *state.VM, mode string) error {
dl, made := b.(backend.DockerLayerVM)
if mode == "" || !made {
return nil
}
if err := dl.SetDockerLayer(ctx, st.Name(), true); err != nil {
return err
}
if err := st.Set(KeyDockerLayer, mode); err != nil {
return err
}
e.say(b, mode, st.Name())
return nil
}
// unset makes a VM that set made one for the docker layer under mode one
// as it was, once the layer's script failed: stopped, its backend's
// container made again without it, its state's KeyDockerLayer emptied.
// Nothing for "". It runs on when ctx is cancelled (an interrupted
// script), which would leave the VM without network policy for good.
func (e dockerLayers) unset(ctx context.Context, b backend.Backend, st *state.VM, mode string) error {
dl, made := b.(backend.DockerLayerVM)
if mode == "" || !made {
return nil
}
ctx = context.WithoutCancel(ctx)
if err := b.Stop(ctx, st.Name()); err != nil {
return err
}
if err := dl.SetDockerLayer(ctx, st.Name(), false); err != nil {
return err
}
return st.Set(KeyDockerLayer, "")
}