v0.1.0 cbb8ad6

images.go RawHistoryFolder

1// SPDX-License-Identifier: MIT
2
3package vm
4
5import (
6	"context"
7	"crypto/sha256"
8	"encoding/hex"
9	"fmt"
10	"io"
11	"os"
12	"path/filepath"
13
14	"bnema.dev/ermite-runner/internal/cmdrun"
15	"bnema.dev/ermite-runner/internal/sandbox"
16)
17
18// ensureExecDisk builds (or reuses) the small read-only ext4 disk that carries
19// the runner's own executable: the guest root during boot, and /opt/ermite
20// afterwards. It is named after the executable's hash, so a runner upgrade
21// builds a new one and no image rebuild is needed; older ones are removed.
22// mkfs.ext4 -d needs no root.
23func ensureExecDisk(ctx context.Context, run cmdrun.Runner, exe, stateDir string) (string, error) {
24	sum, size, err := hashFile(exe)
25	if err != nil {
26		return "", fmt.Errorf("hash executable: %w", err)
27	}
28	cache := filepath.Join(stateDir, "cache")
29	if err := os.MkdirAll(cache, 0o755); err != nil {
30		return "", err
31	}
32	name := "execdisk-" + sum[:16] + ".ext4"
33	dest := filepath.Join(cache, name)
34	if _, err := os.Stat(dest); err == nil {
35		return dest, nil
36	}
37	// Drop disks of other executable versions.
38	if olds, _ := filepath.Glob(filepath.Join(cache, "execdisk-*")); len(olds) > 0 {
39		for _, o := range olds {
40			_ = os.Remove(o)
41		}
42	}
43
44	tmp, err := os.MkdirTemp(cache, "execroot-")
45	if err != nil {
46		return "", err
47	}
48	defer func() { _ = os.RemoveAll(tmp) }()
49	for _, d := range []string{"proc", "sys", "dev", "mnt/base", "mnt/scratch", "mnt/newroot"} {
50		if err := os.MkdirAll(filepath.Join(tmp, d), 0o755); err != nil {
51			return "", err
52		}
53	}
54	if err := copyFile(exe, filepath.Join(tmp, sandbox.GuestExecName)); err != nil {
55		return "", fmt.Errorf("copy executable: %w", err)
56	}
57	if err := os.Chmod(filepath.Join(tmp, sandbox.GuestExecName), 0o755); err != nil {
58		return "", err
59	}
60
61	part := dest + ".part"
62	_ = os.Remove(part)
63	f, err := os.OpenFile(part, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o644)
64	if err != nil {
65		return "", err
66	}
67	// The executable, ext4 overhead and some room; the disk is read-only.
68	mib := size/(1<<20)*115/100 + 16
69	if err := f.Truncate(mib << 20); err != nil {
70		_ = f.Close()
71		return "", err
72	}
73	_ = f.Close()
74	if _, err := run.Run(ctx, nil, "mkfs.ext4", "-q", "-F", "-L", "ermite-exec",
75		"-O", "^has_journal", "-E", "root_owner=0:0", "-d", tmp, part); err != nil {
76		_ = os.Remove(part)
77		return "", fmt.Errorf("build exec disk: %w", err)
78	}
79	if err := os.Rename(part, dest); err != nil {
80		return "", err
81	}
82	return dest, nil
83}
84
85func hashFile(path string) (sum string, size int64, err error) {
86	f, err := os.Open(path)
87	if err != nil {
88		return "", 0, err
89	}
90	defer func() { _ = f.Close() }()
91	h := sha256.New()
92	n, err := io.Copy(h, f)
93	if err != nil {
94		return "", 0, err
95	}
96	return hex.EncodeToString(h.Sum(nil)), n, nil
97}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 4

Tags 11

↑↓ moveenter openesc close