Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions init/main_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions init/vm_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
28 changes: 28 additions & 0 deletions init/vm_linux_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
1 change: 1 addition & 0 deletions internal/image/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
55 changes: 40 additions & 15 deletions internal/image/rootdisk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
}
Expand Down
6 changes: 4 additions & 2 deletions internal/image/rootdisk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) })
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions internal/sandbox/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
Loading