From f9ea4a0e4e3e9c9e0f5036b072554357f485b48e Mon Sep 17 00:00:00 2001 From: ephraimduncan Date: Thu, 5 Nov 2026 21:07:49 +0000 Subject: [PATCH] fix(vm): give jobs the environment of the image In VM mode, jobs did not get the ENV of the image config. Thus a VM had no RUNNER_TOOL_CACHE or ImageOS, and actions/setup-node, setup-python, and setup-go downloaded the tools that the image has in /opt/hostedtoolcache. On a nested VM, setup-node took 2 to 3 minutes for each job. The root disk now holds the image environment in /etc/runplane/environ. runplane-init sets it after it mounts the root, as Docker does in container mode. --- init/main_linux.go | 3 ++ init/vm_linux.go | 19 +++++++++++ init/vm_linux_test.go | 28 +++++++++++++++ internal/image/integration_test.go | 1 + internal/image/rootdisk.go | 55 ++++++++++++++++++++++-------- internal/image/rootdisk_test.go | 6 ++-- internal/sandbox/sandbox.go | 6 ++++ 7 files changed, 101 insertions(+), 17 deletions(-) create mode 100644 init/vm_linux_test.go diff --git a/init/main_linux.go b/init/main_linux.go index d6d48e8..b06f5c2 100644 --- a/init/main_linux.go +++ b/init/main_linux.go @@ -67,6 +67,9 @@ func main() { log.Error("set up the VM", "err", err) powerOff(true, 1) } + if err := setImageEnv(sandbox.ImageEnvFile); err != nil { + log.Warn("set the environment of the image", "err", err) + } } else if _, err := os.Stat(filepath.Dir(controlSocket)); err != nil { log.Error("no VM and no control socket folder: is this a runplane sandbox?", "err", err) os.Exit(1) diff --git a/init/vm_linux.go b/init/vm_linux.go index 3b6e6be..c077caf 100644 --- a/init/vm_linux.go +++ b/init/vm_linux.go @@ -112,6 +112,25 @@ func setupVM() error { return unix.Setrlimit(unix.RLIMIT_NOFILE, &unix.Rlimit{Cur: 65536, Max: 65536}) } +// setImageEnv sets the environment of the image config from file, as +// Docker does for the first process of a container. Without it, jobs do not +// get variables such as RUNNER_TOOL_CACHE, and setup actions download the +// tools that the image has. +func setImageEnv(file string) error { + b, err := os.ReadFile(file) + if err != nil { + return err + } + for kv := range strings.SplitSeq(string(b), "\x00") { + if k, v, ok := strings.Cut(kv, "="); ok && k != "" { + if err := os.Setenv(k, v); err != nil { + return err + } + } + } + return nil +} + // pivot makes root the new root mount and drops the old root. func pivot(root string) error { if err := unix.Chdir(root); err != nil { diff --git a/init/vm_linux_test.go b/init/vm_linux_test.go new file mode 100644 index 0000000..e5d09bc --- /dev/null +++ b/init/vm_linux_test.go @@ -0,0 +1,28 @@ +//go:build linux + +package main + +import ( + "os" + "path/filepath" + "testing" +) + +func TestSetImageEnv(t *testing.T) { + // t.Setenv puts the old values back after the test. + t.Setenv("RUNNER_TOOL_CACHE", "") + t.Setenv("GOFLAGS", "") + file := filepath.Join(t.TempDir(), "environ") + err := os.WriteFile(file, []byte("RUNNER_TOOL_CACHE=/opt/hostedtoolcache\x00GOFLAGS=-tags=vm\x00"), 0o644) + if err != nil { + t.Fatal(err) + } + if err := setImageEnv(file); err != nil { + t.Fatal(err) + } + for k, want := range map[string]string{"RUNNER_TOOL_CACHE": "/opt/hostedtoolcache", "GOFLAGS": "-tags=vm"} { + if got := os.Getenv(k); got != want { + t.Errorf("%s = %q, want %q", k, got, want) + } + } +} diff --git a/internal/image/integration_test.go b/internal/image/integration_test.go index 0c0e45f..1ca0e77 100644 --- a/internal/image/integration_test.go +++ b/internal/image/integration_test.go @@ -109,6 +109,7 @@ func TestDockerBuildsBuiltinRootDisk(t *testing.T) { assertExt4(t, img.Path) debugfs(t, img.Path, "ls -l /sbin/", "runplane-init") debugfs(t, img.Path, "stat /.dockerenv", "File not found") + debugfs(t, img.Path, "cat /etc/runplane/environ", "RUNNER_TOOL_CACHE=/opt/hostedtoolcache\x00") } func vmManager(t *testing.T, docker *client.Client) *Manager { diff --git a/internal/image/rootdisk.go b/internal/image/rootdisk.go index 31902e5..58ca54b 100644 --- a/internal/image/rootdisk.go +++ b/internal/image/rootdisk.go @@ -23,6 +23,8 @@ import ( "github.com/google/go-containerregistry/pkg/v1/remote" "github.com/moby/moby/api/types/container" "github.com/moby/moby/client" + + "github.com/runplane/runplane/internal/sandbox" ) const initPath = "sbin/runplane-init" @@ -65,6 +67,10 @@ func (m *Manager) ensureRootDisk(ctx context.Context, src source, r *reporter) ( if err != nil { return Image{}, fmt.Errorf("digest of %s: %w", src.ref, err) } + cfg, err := img.ConfigFile() + if err != nil { + return Image{}, fmt.Errorf("config of %s: %w", src.ref, err) + } out := Image{Ref: src.ref, Digest: digest.String()} if out.Path, err = m.rootDiskPath(out.Digest); err != nil { return Image{}, err @@ -86,7 +92,7 @@ func (m *Manager) ensureRootDisk(ctx context.Context, src source, r *reporter) ( r.status(StatusConverting) out.Size, err = writeExt4(out.Path, func(w io.Writer) error { - return rootfs(w, mutate.Extract(layersImage{img, layers}), init.bin) + return rootfs(w, mutate.Extract(layersImage{img, layers}), init.bin, cfg.Config.Env) }) if err != nil { return Image{}, err @@ -102,11 +108,15 @@ func (m *Manager) buildRootDisk(ctx context.Context, src source, init []byte, r if err := m.build(ctx, src.builtin, tag); err != nil { return Image{}, err } - out, err := m.inspect(ctx, tag) + info, err := m.cfg.Docker.ImageInspect(ctx, tag) if err != nil { - return Image{}, err + return Image{}, fmt.Errorf("inspect %s: %w", tag, err) + } + var env []string + if info.Config != nil { + env = info.Config.Env } - size := out.Size + out := Image{Ref: tag, Digest: info.ID} if out.Path, err = m.rootDiskPath(out.Digest); err != nil { return Image{}, err } @@ -134,10 +144,10 @@ func (m *Manager) buildRootDisk(ctx context.Context, src source, init []byte, r var read int64 counted := &counter{ctx: ctx, r: files, add: func(n int64) { read += n - r.bytes(StatusConverting, read, size) + r.bytes(StatusConverting, read, info.Size) }} out.Size, err = writeExt4(out.Path, func(w io.Writer) error { - return rootfs(w, readCloser{counted, files}, init) + return rootfs(w, readCloser{counted, files}, init, env) }) if err != nil { return Image{}, err @@ -271,10 +281,11 @@ func (i layersImage) Layers() ([]v1.Layer, error) { return out, nil } -// rootfs copies the tar of a flat file system from src to w, and adds -// runplane-init and the mount points. The ext4 writer cannot write through -// a symlink, so rootfs follows symlinks such as /sbin -> usr/sbin. -func rootfs(w io.Writer, src io.ReadCloser, init []byte) error { +// rootfs copies the tar of a flat file system from src to w, and adds the +// mount points, runplane-init, and the image environment env. The ext4 +// writer cannot write through a symlink, so rootfs follows symlinks such as +// /sbin -> usr/sbin. +func rootfs(w io.Writer, src io.ReadCloser, init []byte, env []string) error { defer src.Close() tr := tar.NewReader(src) tw := tar.NewWriter(w) @@ -312,12 +323,26 @@ func rootfs(w io.Writer, src io.ReadCloser, init []byte) error { return err } } - hdr := &tar.Header{Typeflag: tar.TypeReg, Name: resolve(initPath, links), Mode: 0o755, Size: int64(len(init))} - if err := tw.WriteHeader(hdr); err != nil { - return err + var environ []byte + for _, kv := range env { + environ = append(append(environ, kv...), 0) } - if _, err := tw.Write(init); err != nil { - return err + files := []struct { + path string + mode int64 + data []byte + }{ + {initPath, 0o755, init}, + {clean(sandbox.ImageEnvFile), 0o644, environ}, + } + for _, f := range files { + hdr := &tar.Header{Typeflag: tar.TypeReg, Name: resolve(f.path, links), Mode: f.mode, Size: int64(len(f.data))} + if err := tw.WriteHeader(hdr); err != nil { + return err + } + if _, err := tw.Write(f.data); err != nil { + return err + } } return tw.Close() } diff --git a/internal/image/rootdisk_test.go b/internal/image/rootdisk_test.go index df8dd02..b377152 100644 --- a/internal/image/rootdisk_test.go +++ b/internal/image/rootdisk_test.go @@ -84,6 +84,7 @@ func TestEnsureRootDisk(t *testing.T) { debugfs(t, got.Path, "stat /tmp", "Mode: 01777") debugfs(t, got.Path, "stat /mnt/scratch", "Type: directory") debugfs(t, got.Path, "stat /proc", "Mode: 0711") + debugfs(t, got.Path, "cat /etc/runplane/environ", "PATH=/usr/sbin:/usr/bin\x00GOFLAGS=-tags=vm\x00") steps = nil again, err := m.Ensure(ctx, ref, func(p Progress) { steps = append(steps, p) }) @@ -186,8 +187,8 @@ func TestWriteExt4LeavesNothingOnError(t *testing.T) { } } -// testImage has a base layer with /sbin -> usr/sbin, and an upper layer -// that removes /etc/gone and changes /etc/kept. +// testImage has a base layer with /sbin -> usr/sbin, an upper layer that +// removes /etc/gone and changes /etc/kept, and two variables in its config. func testImage(t *testing.T, arch string) v1.Image { t.Helper() base := tarLayer(t, @@ -213,6 +214,7 @@ func testImage(t *testing.T, arch string) v1.Image { } cfg = cfg.DeepCopy() cfg.OS, cfg.Architecture = "linux", arch + cfg.Config.Env = []string{"PATH=/usr/sbin:/usr/bin", "GOFLAGS=-tags=vm"} img, err = mutate.ConfigFile(img, cfg) if err != nil { t.Fatal(err) diff --git a/internal/sandbox/sandbox.go b/internal/sandbox/sandbox.go index 95c7876..5386642 100644 --- a/internal/sandbox/sandbox.go +++ b/internal/sandbox/sandbox.go @@ -35,6 +35,12 @@ func (s State) Done() bool { return s == StateGone || s == StateFailed } // Drivers change only objects with this label. const LabelSandbox = "dev.runplane.sandbox" +// ImageEnvFile holds the environment of the image config in a VM root disk, +// as "KEY=value" entries that each end with a NUL byte. Docker gives this +// environment to the first process of a container. In a VM, runplane-init +// reads it from this file. +const ImageEnvFile = "/etc/runplane/environ" + type Spec struct { ID string `json:"id"` PoolID string `json:"pool_id"`