Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

feat: daemonless box runner helpers

ogsamrat9af90f5e3c3a
diff --git a/apps/desktop/box/go/cmd/boxd/main.go b/apps/desktop/box/go/cmd/boxd/main.go
index 9f1067b3..3632ad14 100644
--- a/apps/desktop/box/go/cmd/boxd/main.go
+++ b/apps/desktop/box/go/cmd/boxd/main.go
@@ -16,6 +16,9 @@
// BOXD_PATH folders put in front of PATH (the package-manager wrappers)
// BOXD_ENV_FILE KEY=VALUE lines read before each command (host-written)
// BOXD_SHELL shell for commands (default /bin/bash, else /bin/sh)
+// BOXD_PORT_FILE where to write the port it listens on (BOXD_LISTEN may end in :0)
+// BOXD_IDLE_EXIT minutes with no request after which it exits (0: never; the
+// bubblewrap and WSL boxes, which have no engine to keep them)
package main
import (
@@ -57,6 +60,8 @@ type config struct {
pathPre string
envFile string
shell string
+ portFile string
+ idle time.Duration
baseEnv []string
}
@@ -125,10 +130,10 @@ func main() {
fmt.Println(version)
return
}
- // PID 1 with no init in front (`unshare --pid --fork`, a runtime without
- // --init): be a tiny init that reaps orphans and run the daemon as its
- // child, so a reaper never takes a command's exit status from the
- // daemon's own Wait.
+ // PID 1 with no init in front (WSL containers have no --init; `unshare
+ // --pid --fork` in the WSL distro): be a tiny init that reaps orphans and
+ // run the daemon as its child, so a reaper never takes a command's exit
+ // status from the daemon's own Wait.
if os.Getpid() == 1 && os.Getenv("BOXD_INIT_CHILD") == "" {
os.Exit(runInit())
}
@@ -149,6 +154,16 @@ func main() {
if err != nil {
log.Fatalf("listen: %v", err)
}
+ if cfg.portFile != "" {
+ port := ln.Addr().(*net.TCPAddr).Port
+ tmp := cfg.portFile + ".tmp"
+ if err := os.WriteFile(tmp, []byte(strconv.Itoa(port)+"\n"), 0o644); err == nil {
+ _ = os.Rename(tmp, cfg.portFile)
+ }
+ }
+ if cfg.idle > 0 {
+ go idleExit(cfg.idle, ln)
+ }
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT)
go func() {
@@ -170,7 +185,32 @@ func main() {
if tcp, ok := conn.(*net.TCPConn); ok {
_ = tcp.SetNoDelay(true)
}
- go serve(cfg, conn)
+ activity.Add(1)
+ lastActive.Store(time.Now().UnixNano())
+ go func() {
+ defer func() { activity.Add(-1); lastActive.Store(time.Now().UnixNano()) }()
+ serve(cfg, conn)
+ }()
+ }
+}
+
+// Open connections, and when the last one ended (BOXD_IDLE_EXIT).
+var (
+ activity atomic.Int64
+ lastActive atomic.Int64
+)
+
+// idleExit ends boxd (and with it the box) once no request came for `idle`
+// and none is running (a server in the foreground holds its connection).
+func idleExit(idle time.Duration, ln net.Listener) {
+ lastActive.Store(time.Now().UnixNano())
+ for {
+ time.Sleep(idle / 20)
+ if activity.Load() == 0 && time.Since(time.Unix(0, lastActive.Load())) > idle {
+ log.Printf("idle for %v: exiting", idle)
+ _ = ln.Close()
+ os.Exit(0)
+ }
}
}
@@ -196,6 +236,8 @@ func loadConfig() config {
pathPre: os.Getenv("BOXD_PATH"),
envFile: os.Getenv("BOXD_ENV_FILE"),
shell: os.Getenv("BOXD_SHELL"),
+ portFile: os.Getenv("BOXD_PORT_FILE"),
+ idle: time.Duration(atoi("BOXD_IDLE_EXIT")) * time.Minute,
}
if cfg.shell == "" {
cfg.shell = "/bin/bash"
@@ -209,7 +251,7 @@ func loadConfig() config {
}
cfg.baseEnv = append(cfg.baseEnv, kv)
}
- for _, k := range []string{"BOXD_TOKEN", "BOXD_LISTEN", "BOXD_UID", "BOXD_GID", "BOXD_HOME", "BOXD_ROOT", "BOXD_PATH", "BOXD_ENV_FILE", "BOXD_SHELL"} {
+ for _, k := range []string{"BOXD_TOKEN", "BOXD_LISTEN", "BOXD_UID", "BOXD_GID", "BOXD_HOME", "BOXD_ROOT", "BOXD_PATH", "BOXD_ENV_FILE", "BOXD_SHELL", "BOXD_PORT_FILE", "BOXD_IDLE_EXIT"} {
_ = os.Unsetenv(k)
}
if cfg.token == "" {
diff --git a/apps/desktop/box/go/cmd/boxrun/init.go b/apps/desktop/box/go/cmd/boxrun/init.go
new file mode 100644
index 00000000..2e4d043e
--- /dev/null
+++ b/apps/desktop/box/go/cmd/boxrun/init.go
@@ -0,0 +1,217 @@
+//go:build linux
+
+package main
+
+import (
+ "encoding/json"
+ "errors"
+ "fmt"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "strings"
+ "syscall"
+ "time"
+)
+
+// mountOverlay mounts lower+upper at merged: the kernel's overlayfs (in a
+// user namespace: 5.11+, with userxattr), else fuse-overlayfs when given.
+// Returns "kernel" or "fuse".
+func mountOverlay(lower, upper, work, merged, fuse string) (string, error) {
+ base := fmt.Sprintf("lowerdir=%s,upperdir=%s,workdir=%s", lower, upper, work)
+ var errs []string
+ for _, opts := range []string{base + ",userxattr", base} {
+ err := syscall.Mount("overlay", merged, "overlay", 0, opts)
+ if err == nil {
+ return "kernel", nil
+ }
+ errs = append(errs, err.Error())
+ }
+ if fuse != "" {
+ if _, err := os.Stat("/dev/fuse"); err == nil {
+ cmd := exec.Command(fuse, "-o", base, merged)
+ out, err := cmd.CombinedOutput()
+ if err == nil {
+ return "fuse", nil
+ }
+ errs = append(errs, "fuse-overlayfs: "+strings.TrimSpace(string(out)))
+ } else {
+ errs = append(errs, "no /dev/fuse")
+ }
+ }
+ return "", errors.New("overlay: " + strings.Join(errs, "; "))
+}
+
+func ensureDir(p string) error {
+ if fi, err := os.Lstat(p); err == nil {
+ if fi.IsDir() {
+ return nil
+ }
+ return fmt.Errorf("%s is not a folder", p)
+ }
+ return os.MkdirAll(p, 0o755)
+}
+
+func ensureFile(p string) error {
+ if fi, err := os.Lstat(p); err == nil {
+ if fi.Mode().IsRegular() {
+ return nil
+ }
+ return fmt.Errorf("%s is not a file", p)
+ }
+ if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
+ return err
+ }
+ f, err := os.OpenFile(p, os.O_CREATE|os.O_WRONLY, 0o644)
+ if err != nil {
+ return err
+ }
+ return f.Close()
+}
+
+func bind(src, dst string, ro bool, rec bool) error {
+ flags := uintptr(syscall.MS_BIND)
+ if rec {
+ flags |= syscall.MS_REC
+ }
+ if err := syscall.Mount(src, dst, "", flags, ""); err != nil {
+ return fmt.Errorf("bind %s: %w", dst, err)
+ }
+ if ro {
+ // Read-only needs a remount of the bind; flags locked by the parent namespace must be kept.
+ var st syscall.Statfs_t
+ extra := uintptr(0)
+ if syscall.Statfs(dst, &st) == nil {
+ extra = uintptr(st.Flags) & (syscall.MS_NOSUID | syscall.MS_NODEV | syscall.MS_NOEXEC | syscall.MS_NOATIME | syscall.MS_RELATIME)
+ }
+ if err := syscall.Mount("", dst, "", syscall.MS_BIND|syscall.MS_REMOUNT|syscall.MS_RDONLY|extra, ""); err != nil {
+ return fmt.Errorf("read-only %s: %w", dst, err)
+ }
+ }
+ return nil
+}
+
+// stageInit runs inside the namespaces as root: overlay root, the project
+// and run folders, /proc /dev /sys, DNS, then the root switch and boxd.
+func stageInit(args []string) error {
+ if len(args) < 1 {
+ return errors.New("init: no spec")
+ }
+ data, err := os.ReadFile(args[0])
+ if err != nil {
+ return err
+ }
+ var sp spec
+ if err := json.Unmarshal(data, &sp); err != nil {
+ return err
+ }
+ t0 := time.Now()
+ if sp.Root {
+ // unshare(1) as real root: nothing may leak back to the distro's mounts.
+ _ = syscall.Mount("", "/", "", syscall.MS_REC|syscall.MS_PRIVATE, "")
+ }
+ if sp.Hostname != "" {
+ _ = syscall.Sethostname([]byte(sp.Hostname))
+ }
+ m := sp.Merged
+ if err := ensureDir(m); err != nil {
+ return err
+ }
+ overlay, err := mountOverlay(sp.Lower, sp.Upper, sp.Work, m, sp.Fuse)
+ if err != nil {
+ return err
+ }
+ in := func(p string) string { return filepath.Join(m, p) }
+ // The command folders boxd and the launcher use. They are made once, in
+ // the upper layer, before boxd's first index, so they are no change.
+ for _, d := range []string{"proc", "dev", "sys", "tmp", "run", "opt/omnirush/bin"} {
+ if err := ensureDir(in(d)); err != nil {
+ return err
+ }
+ }
+ _ = os.Chmod(in("tmp"), 0o1777)
+ if err := syscall.Mount("proc", in("proc"), "proc", syscall.MS_NOSUID|syscall.MS_NODEV|syscall.MS_NOEXEC, ""); err != nil {
+ // Some kernels refuse a fresh proc while the host's is partly covered: bind the namespace's own.
+ if err2 := bind("/proc", in("proc"), false, true); err2 != nil {
+ return fmt.Errorf("proc: %v; %v", err, err2)
+ }
+ }
+ if err := bind("/dev", in("dev"), false, true); err != nil {
+ return err
+ }
+ if fi, err := os.Stat(in("dev/shm")); err == nil && fi.IsDir() {
+ _ = syscall.Mount("tmpfs", in("dev/shm"), "tmpfs", syscall.MS_NOSUID|syscall.MS_NODEV, "mode=1777")
+ }
+ if err := bind("/sys", in("sys"), true, true); err != nil {
+ // /sys is only read by a few tools: go on without it.
+ fmt.Fprintf(os.Stderr, "boxrun: %v\n", err)
+ }
+ if err := ensureFile(in("opt/omnirush/bin/boxd")); err != nil {
+ return err
+ }
+ if err := bind(sp.Boxd, in("opt/omnirush/bin/boxd"), true, false); err != nil {
+ return err
+ }
+ for _, f := range []struct{ src, dst string }{{sp.Resolv, "etc/resolv.conf"}, {sp.Hosts, "etc/hosts"}} {
+ if f.src == "" {
+ continue
+ }
+ if _, err := os.Stat(f.src); err != nil {
+ continue
+ }
+ target := in(f.dst)
+ if fi, err := os.Lstat(target); err == nil && fi.Mode()&os.ModeSymlink != 0 {
+ continue // the image links it somewhere: leave it
+ }
+ if ensureFile(target) == nil {
+ _ = bind(f.src, target, true, false)
+ }
+ }
+ for _, b := range sp.Binds {
+ target := in(b.Dst)
+ fi, err := os.Stat(b.Src)
+ if err != nil {
+ return fmt.Errorf("bind source %s: %w", b.Dst, err)
+ }
+ if fi.IsDir() {
+ err = ensureDir(target)
+ } else {
+ err = ensureFile(target)
+ }
+ if err != nil {
+ return err
+ }
+ if err := bind(b.Src, target, b.RO, true); err != nil {
+ return err
+ }
+ }
+ if sp.AptRoot {
+ // One uid is mapped (the user's, as root): apt cannot drop to _apt.
+ conf := in("etc/apt/apt.conf.d")
+ if fi, err := os.Stat(conf); err == nil && fi.IsDir() {
+ file := filepath.Join(conf, "00omnirush-box")
+ if _, err := os.Stat(file); err != nil {
+ _ = os.WriteFile(file, []byte("APT::Sandbox::User \"root\";\n"), 0o644)
+ }
+ }
+ }
+ if sp.InitOut != "" {
+ info, _ := json.Marshal(map[string]any{"overlay": overlay, "ms": time.Since(t0).Milliseconds()})
+ _ = os.WriteFile(sp.InitOut, info, 0o600)
+ }
+ // Switch root: pivot_root(".", ".") stacks the old root on the new one;
+ // detaching it leaves only the box.
+ if err := os.Chdir(m); err != nil {
+ return err
+ }
+ if err := syscall.PivotRoot(".", "."); err != nil {
+ return fmt.Errorf("pivot_root: %w", err)
+ }
+ if err := syscall.Unmount(".", syscall.MNT_DETACH); err != nil {
+ return fmt.Errorf("detach old root: %w", err)
+ }
+ if err := os.Chdir("/"); err != nil {
+ return err
+ }
+ return syscall.Exec("/opt/omnirush/bin/boxd", []string{"boxd"}, sp.Env)
+}
diff --git a/apps/desktop/box/go/cmd/boxrun/main.go b/apps/desktop/box/go/cmd/boxrun/main.go
new file mode 100644
index 00000000..53817697
--- /dev/null
+++ b/apps/desktop/box/go/cmd/boxrun/main.go
@@ -0,0 +1,1154 @@
+//go:build linux
+
+// boxrun runs an OmniRush box with no container engine: the pinned image is
+// pulled and unpacked by boxrun itself (no daemon, no root), and a project's
+// box is that root filesystem under a per-project overlay upper layer, in
+// its own user, mount, PID, IPC and UTS namespaces made by bubblewrap
+// (unprivileged), or by unshare(1) when boxrun runs as root (inside the
+// OmniRush WSL distro). The network is the host's, so installs work and
+// boxd (the same helper the container boxes run) listens on the host's
+// loopback. The upper layer stays on disk, so installs survive restarts.
+//
+// boxrun probe [--bwrap PATH] [--fuse-overlayfs PATH] [--dir DIR] can boxes run here, and how
+// boxrun pull --store DIR --ref REF [--arch A] download + unpack an image
+// boxrun import --store DIR --file TAR [--ref REF] [--arch A] unpack `docker save` / a rootfs tar
+// boxrun image --store DIR --ref REF [--arch A] the unpacked image, if any
+// boxrun rmimage --store DIR --id ID delete an unpacked image
+// boxrun up --box DIR --store DIR --image ID --boxd FILE ... start (or reuse) a project's box
+// boxrun stop --box DIR stop it (the upper layer stays)
+// boxrun rm --box DIR stop it and delete its layer
+// boxrun status --box DIR running or not, port
+// boxrun enter --box DIR -- CMD... run CMD inside (an interactive shell)
+//
+// Every command prints one JSON line on stdout.
+package main
+
+import (
+ "context"
+ "encoding/json"
+ "errors"
+ "flag"
+ "fmt"
+ "net"
+ "os"
+ "os/exec"
+ "os/signal"
+ "path/filepath"
+ "runtime"
+ "strconv"
+ "strings"
+ "syscall"
+ "time"
+
+ "omnirush.ai/box/oci"
+)
+
+var version = "dev"
+
+func main() {
+ if len(os.Args) < 2 {
+ fmt.Fprintln(os.Stderr, "usage: boxrun probe|pull|import|image|up|stop|rm|status|enter|--version")
+ os.Exit(2)
+ }
+ cmd, args := os.Args[1], os.Args[2:]
+ var err error
+ switch cmd {
+ case "--version", "version":
+ fmt.Println(version)
+ return
+ case "probe":
+ err = cmdProbe(args)
+ case "pull":
+ err = cmdPull(args)
+ case "import":
+ err = cmdImport(args)
+ case "image":
+ err = cmdImage(args)
+ case "rmimage":
+ err = cmdRmImage(args)
+ case "up":
+ err = cmdUp(args)
+ case "stop":
+ err = cmdStop(args, false)
+ case "rm":
+ err = cmdStop(args, true)
+ case "status":
+ err = cmdStatus(args)
+ case "enter":
+ err = cmdEnter(args)
+ case "init":
+ // Inside the namespaces (internal): overlay, mounts, switch root, exec boxd.
+ err = stageInit(args)
+ case "probe-inner":
+ probeInner(args)
+ return
+ case "rmtree":
+ // Inside the namespaces (internal): delete what the user's own uid cannot (subuid owners).
+ err = rmtreeInner(args)
+ default:
+ err = fmt.Errorf("unknown command %q", cmd)
+ }
+ if err != nil {
+ emit(map[string]any{"ok": false, "error": err.Error()})
+ os.Exit(1)
+ }
+}
+
+func emit(v any) {
+ data, _ := json.Marshal(v)
+ fmt.Println(string(data))
+}
+
+type multi []string
+
+func (m *multi) String() string { return strings.Join(*m, ",") }
+func (m *multi) Set(v string) error { *m = append(*m, v); return nil }
+
+func newFlags(name string) *flag.FlagSet {
+ fs := flag.NewFlagSet(name, flag.ContinueOnError)
+ fs.SetOutput(os.Stderr)
+ return fs
+}
+
+// --- images -----------------------------------------------------------------
+
+func cmdPull(args []string) error {
+ fs := newFlags("pull")
+ store := fs.String("store", "", "image store folder")
+ refS := fs.String("ref", "", "image reference")
+ arch := fs.String("arch", runtime.GOARCH, "architecture")
+ timeout := fs.Duration("timeout", 60*time.Minute, "give up after")
+ if err := fs.Parse(args); err != nil {
+ return err
+ }
+ if *store == "" || *refS == "" {
+ return errors.New("--store and --ref are needed")
+ }
+ if file, ok := archiveRef(*refS); ok {
+ // docker-archive:<tar> / oci-archive:<tar>: a local image (a host kit, a box image not published yet).
+ start := time.Now()
+ s := oci.Store{Dir: *store}
+ if img, id := s.FindArchive(*refS); img != nil {
+ emit(map[string]any{"ok": true, "id": id, "digest": img.Digest, "config": img.Config, "arch": img.Arch, "ref": img.Ref, "ms": time.Since(start).Milliseconds()})
+ return nil
+ }
+ img, id, err := s.Import(file, "", *arch, oci.ApplyOptions{Chown: os.Geteuid() == 0})
+ if err != nil {
+ return err
+ }
+ _ = s.RememberArchive(*refS, id)
+ emit(map[string]any{"ok": true, "id": id, "digest": img.Digest, "config": img.Config, "arch": img.Arch, "ref": img.Ref, "ms": time.Since(start).Milliseconds()})
+ return nil
+ }
+ ref, err := oci.ParseRef(*refS)
+ if err != nil {
+ return err
+ }
+ ctx, cancel := context.WithTimeout(context.Background(), *timeout)
+ defer cancel()
+ start := time.Now()
+ s := oci.Store{Dir: *store}
+ img, id, err := s.Pull(ctx, oci.NewClient(), ref, *arch, oci.ApplyOptions{Chown: os.Geteuid() == 0}, func(i, n int, digest string, size int64) {
+ fmt.Fprintf(os.Stderr, "layer %d/%d %s %d bytes\n", i, n, digest, size)
+ })
+ if err != nil {
+ return err
+ }
+ emit(map[string]any{"ok": true, "id": id, "digest": img.Digest, "manifest": img.Manifest, "config": img.Config, "arch": img.Arch, "ref": img.Ref, "ms": time.Since(start).Milliseconds()})
+ return nil
+}
+
+func cmdImport(args []string) error {
+ fs := newFlags("import")
+ store := fs.String("store", "", "image store folder")
+ file := fs.String("file", "", "archive")
+ refS := fs.String("ref", "", "reference to file it under")
+ arch := fs.String("arch", runtime.GOARCH, "architecture")
+ if err := fs.Parse(args); err != nil {
+ return err
+ }
+ if *store == "" || *file == "" {
+ return errors.New("--store and --file are needed")
+ }
+ start := time.Now()
+ img, id, err := (oci.Store{Dir: *store}).Import(*file, *refS, *arch, oci.ApplyOptions{Chown: os.Geteuid() == 0})
+ if err != nil {
+ return err
+ }
+ emit(map[string]any{"ok": true, "id": id, "digest": img.Digest, "config": img.Config, "arch": img.Arch, "ref": img.Ref, "ms": time.Since(start).Milliseconds()})
+ return nil
+}
+
+func cmdImage(args []string) error {
+ fs := newFlags("image")
+ store := fs.String("store", "", "image store folder")
+ refS := fs.String("ref", "", "image reference")
+ id := fs.String("id", "", "image id")
+ arch := fs.String("arch", runtime.GOARCH, "architecture")
+ if err := fs.Parse(args); err != nil {
+ return err
+ }
+ s := oci.Store{Dir: *store}
+ var img *oci.Image
+ found := *id
+ if found != "" {
+ img = s.Lookup(found)
+ } else if _, ok := archiveRef(*refS); ok {
+ img, found = s.FindArchive(*refS)
+ } else {
+ ref, err := oci.ParseRef(*refS)
+ if err != nil {
+ return err
+ }
+ img, found = s.Find(ref, *arch)
+ }
+ if img == nil {
+ emit(map[string]any{"ok": true, "present": false})
+ return nil
+ }
+ emit(map[string]any{"ok": true, "present": true, "id": found, "digest": img.Digest, "config": img.Config, "arch": img.Arch, "ref": img.Ref})
+ return nil
+}
+
+// archiveRef reads docker-archive:<file> and oci-archive:<file>.
+func archiveRef(ref string) (string, bool) {
+ for _, p := range []string{"docker-archive:", "oci-archive:"} {
+ if strings.HasPrefix(ref, p) && len(ref) > len(p) {
+ return ref[len(p):], true
+ }
+ }
+ return "", false
+}
+
+func cmdRmImage(args []string) error {
+ fs := newFlags("rmimage")
+ store := fs.String("store", "", "image store folder")
+ id := fs.String("id", "", "image id")
+ if err := fs.Parse(args); err != nil {
+ return err
+ }
+ if *store == "" || *id == "" || strings.ContainsAny(*id, "/\\.") {
+ return errors.New("--store and a plain --id are needed")
+ }
+ if err := removeTree(filepath.Join(*store, "images", *id)); err != nil {
+ return err
+ }
+ emit(map[string]any{"ok": true, "removed": *id})
+ return nil
+}
+
+// --- probe -------------------------------------------------------------------
+
+func readSysctl(name string) *int {
+ data, err := os.ReadFile("/proc/sys/" + strings.ReplaceAll(name, ".", "/"))
+ if err != nil {
+ return nil
+ }
+ n, err := strconv.Atoi(strings.TrimSpace(string(data)))
+ if err != nil {
+ return nil
+ }
+ return &n
+}
+
+func kernelRelease() string {
+ var u syscall.Utsname
+ if syscall.Uname(&u) != nil {
+ return ""
+ }
+ b := make([]byte, 0, len(u.Release))
+ for _, c := range u.Release {
+ if c == 0 {
+ break
+ }
+ b = append(b, byte(c))
+ }
+ return string(b)
+}
+
+// kernelAtLeast compares "6.8.0-139-generic" with major.minor.
+func kernelAtLeast(release string, major, minor int) bool {
+ num := func(s string) int {
+ n := 0
+ for _, c := range s {
+ if c < '0' || c > '9' {
+ break
+ }
+ n = n*10 + int(c-'0')
+ }
+ return n
+ }
+ parts := strings.SplitN(release, ".", 3)
+ if len(parts) < 2 {
+ return false
+ }
+ ma, mi := num(parts[0]), num(parts[1])
+ return ma > major || (ma == major && mi >= minor)
+}
+
+func findBwrap(asked string) string {
+ if asked != "" {
+ if _, err := os.Stat(asked); err == nil {
+ return asked
+ }
+ }
+ if p, err := exec.LookPath("bwrap"); err == nil {
+ return p
+ }
+ for _, p := range []string{"/usr/bin/bwrap", "/usr/local/bin/bwrap"} {
+ if _, err := os.Stat(p); err == nil {
+ return p
+ }
+ }
+ return ""
+}
+
+func findTool(asked, name string) string {
+ if asked != "" {
+ if _, err := os.Stat(asked); err == nil {
+ return asked
+ }
+ return ""
+ }
+ if p, err := exec.LookPath(name); err == nil {
+ return p
+ }
+ for _, d := range []string{"/usr/bin", "/usr/sbin", "/bin", "/sbin", "/usr/local/bin"} {
+ p := filepath.Join(d, name)
+ if _, err := os.Stat(p); err == nil {
+ return p
+ }
+ }
+ return ""
+}
+
+func bwrapVersion(bin string) string {
+ out, err := exec.Command(bin, "--version").Output()
+ if err != nil {
+ return ""
+ }
+ return strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(string(out)), "bubblewrap"))
+}
+
+// subIDs is the user's subordinate id range from /etc/subuid or /etc/subgid.
+func subIDs(file string, name string, id int) (start, count int) {
+ data, err := os.ReadFile(file)
+ if err != nil {
+ return 0, 0
+ }
+ for _, line := range strings.Split(string(data), "\n") {
+ f := strings.Split(strings.TrimSpace(line), ":")
+ if len(f) != 3 || (f[0] != name && f[0] != strconv.Itoa(id)) {
+ continue
+ }
+ s, e1 := strconv.Atoi(f[1])
+ c, e2 := strconv.Atoi(f[2])
+ if e1 == nil && e2 == nil && c > 0 {
+ return s, c
+ }
+ }
+ return 0, 0
+}
+
+func userName() string {
+ if u := os.Getenv("USER"); u != "" {
+ return u
+ }
+ data, _ := os.ReadFile("/etc/passwd")
+ uid := strconv.Itoa(os.Getuid())
+ for _, line := range strings.Split(string(data), "\n") {
+ f := strings.Split(line, ":")
+ if len(f) > 2 && f[2] == uid {
+ return f[0]
+ }
+ }
+ return ""
+}
+
+// idMap is how box uids map to this machine's: "single" (the user is root
+// inside, nothing else is mapped) or "subuid" (65536 ids from /etc/subuid
+// through newuidmap, so apt/dpkg can own files as other users).
+type idMap struct {
+ Mode string
+ UIDStart, UIDCount int
+ GIDStart, GIDCount int
+ NewUIDMap, NewGIDMap string
+}
+
+func detectIDMap(asked string) idMap {
+ if asked == "single" || os.Geteuid() == 0 {
+ return idMap{Mode: "single"}
+ }
+ nu, ng := findTool("", "newuidmap"), findTool("", "newgidmap")
+ us, uc := subIDs("/etc/subuid", userName(), os.Getuid())
+ gs, gc := subIDs("/etc/subgid", userName(), os.Getuid())
+ if nu == "" || ng == "" || uc < 65536 || gc < 65536 {
+ return idMap{Mode: "single"}
+ }
+ return idMap{Mode: "subuid", UIDStart: us, UIDCount: 65536, GIDStart: gs, GIDCount: 65536, NewUIDMap: nu, NewGIDMap: ng}
+}
+
+type probeResult struct {
+ OK bool `json:"ok"`
+ Launcher string `json:"launcher,omitempty"`
+ Bwrap string `json:"bwrap,omitempty"`
+ BwrapVer string `json:"bwrap_version,omitempty"`
+ Overlay string `json:"overlay,omitempty"`
+ IDMap string `json:"idmap,omitempty"`
+ Kernel string `json:"kernel"`
+ AppArmor *int `json:"apparmor_restrict_unprivileged_userns"`
+ UsernsClone *int `json:"unprivileged_userns_clone"`
+ MaxUserNS *int `json:"max_user_namespaces"`
+ Root bool `json:"root"`
+ Reason string `json:"reason,omitempty"`
+ Code string `json:"code,omitempty"`
+ Detail string `json:"detail,omitempty"`
+ Ms int64 `json:"ms"`
+}
+
+func cmdProbe(args []string) error {
+ fs := newFlags("probe")
+ bw := fs.String("bwrap", "", "bwrap to use")
+ fuse := fs.String("fuse-overlayfs", "", "fuse-overlayfs to use")
+ dir := fs.String("dir", os.TempDir(), "a folder on the disk boxes will use")
+ if err := fs.Parse(args); err != nil {
+ return err
+ }
+ emit(probe(*bw, *fuse, *dir))
+ return nil
+}
+
+func probe(bwAsked, fuseAsked, dir string) probeResult {
+ start := time.Now()
+ r := probeResult{Kernel: kernelRelease(), AppArmor: readSysctl("kernel.apparmor_restrict_unprivileged_userns"), UsernsClone: readSysctl("kernel.unprivileged_userns_clone"), MaxUserNS: readSysctl("user.max_user_namespaces"), Root: os.Geteuid() == 0}
+ defer func() { r.Ms = time.Since(start).Milliseconds() }()
+ fail := func(code, reason, detail string) probeResult {
+ r.OK, r.Code, r.Reason, r.Detail = false, code, reason, strings.TrimSpace(detail)
+ r.Ms = time.Since(start).Milliseconds()
+ return r
+ }
+ tmp, err := os.MkdirTemp(dir, ".boxrun-probe-")
+ if err != nil {
+ return fail("disk", "cannot write the box folder", err.Error())
+ }
+ defer removeTree(tmp)
+ for _, d := range []string{"lower", "upper", "work", "merged"} {
+ _ = os.MkdirAll(filepath.Join(tmp, d), 0o755)
+ }
+ _ = os.WriteFile(filepath.Join(tmp, "lower", "f"), []byte("x"), 0o644)
+ self, _ := os.Executable()
+ fuseBin := findTool(fuseAsked, "fuse-overlayfs")
+ var cmd *exec.Cmd
+ if r.Root {
+ un := findTool("", "unshare")
+ if un == "" {
+ return fail("no_unshare", "unshare (util-linux) is missing", "")
+ }
+ r.Launcher = "unshare"
+ cmd = exec.Command(un, "--mount", "--propagation", "private", "--", self, "probe-inner", tmp, fuseBin)
+ } else {
+ bin := findBwrap(bwAsked)
+ if bin == "" {
+ return fail("no_bwrap", "bubblewrap (bwrap) is not installed", "")
+ }
+ r.Launcher, r.Bwrap, r.BwrapVer = "bwrap", bin, bwrapVersion(bin)
+ bargs := []string{"--unshare-user", "--uid", "0", "--gid", "0", "--unshare-pid", "--cap-add", "ALL",
+ "--ro-bind", "/", "/", "--dev", "/dev", "--proc", "/proc", "--bind", tmp, tmp}
+ if fuseBin != "" {
+ bargs = append(bargs, "--dev-bind-try", "/dev/fuse", "/dev/fuse")
+ }
+ bargs = append(bargs, "--", self, "probe-inner", tmp, fuseBin)
+ cmd = exec.Command(bin, bargs...)
+ }
+ cmd.Env = []string{"PATH=/usr/sbin:/usr/bin:/sbin:/bin"}
+ out, err := cmd.CombinedOutput()
+ text := string(out)
+ var inner struct {
+ Overlay string `json:"overlay"`
+ Error string `json:"error"`
+ UID int `json:"uid"`
+ }
+ jsonLine := ""
+ for _, line := range strings.Split(text, "\n") {
+ if strings.HasPrefix(strings.TrimSpace(line), "{") {
+ jsonLine = line
+ }
+ }
+ if err != nil || jsonLine == "" || json.Unmarshal([]byte(jsonLine), &inner) != nil {
+ return classify(r, text, fail)
+ }
+ if inner.Overlay == "none" || inner.Overlay == "" {
+ if !kernelAtLeast(r.Kernel, 5, 11) {
+ return fail("overlay", fmt.Sprintf("this kernel (%s) cannot mount overlayfs without root (5.11+ can) and fuse-overlayfs is not installed", r.Kernel), inner.Error)
+ }
+ return fail("overlay", "overlayfs does not mount in a user namespace here and fuse-overlayfs is not installed", inner.Error)
+ }
+ r.OK = true
+ r.Overlay = inner.Overlay
+ r.IDMap = detectIDMap("auto").Mode
+ r.Ms = time.Since(start).Milliseconds()
+ return r
+}
+
+// classify turns a failed namespace start into one line for the user.
+func classify(r probeResult, text string, fail func(code, reason, detail string) probeResult) probeResult {
+ low := strings.ToLower(text)
+ switch {
+ case r.AppArmor != nil && *r.AppArmor == 1:
+ return fail("apparmor", "AppArmor blocks unprivileged user namespaces (kernel.apparmor_restrict_unprivileged_userns=1, Ubuntu 23.10+)", text)
+ case r.UsernsClone != nil && *r.UsernsClone == 0:
+ return fail("userns_off", "unprivileged user namespaces are off (kernel.unprivileged_userns_clone=0)", text)
+ case r.MaxUserNS != nil && *r.MaxUserNS == 0:
+ return fail("userns_off", "user namespaces are off (user.max_user_namespaces=0)", text)
+ case strings.Contains(low, "setting up uid map") || strings.Contains(low, "uid_map") || strings.Contains(low, "operation not permitted") || strings.Contains(low, "permission denied"):
+ return fail("userns_denied", "this system does not let unprivileged users create namespaces (a container or a hardened kernel)", text)
+ }
+ return fail("start", "bubblewrap could not start a namespace", text)
+}
+
+// probeInner runs inside the probe's namespaces: can overlayfs mount here?
+func probeInner(args []string) {
+ out := map[string]any{"uid": os.Getuid()}
+ if len(args) < 1 {
+ out["error"] = "no dir"
+ emit(out)
+ return
+ }
+ dir := args[0]
+ fuse := ""
+ if len(args) > 1 {
+ fuse = args[1]
+ }
+ merged := filepath.Join(dir, "merged")
+ mode, err := mountOverlay(filepath.Join(dir, "lower"), filepath.Join(dir, "upper"), filepath.Join(dir, "work"), merged, fuse)
+ if err != nil {
+ out["overlay"] = "none"
+ out["error"] = err.Error()
+ emit(out)
+ return
+ }
+ // A write lands in the upper layer, a delete leaves a whiteout.
+ _ = os.WriteFile(filepath.Join(merged, "g"), []byte("y"), 0o644)
+ _ = os.Remove(filepath.Join(merged, "f"))
+ _, errUp := os.Stat(filepath.Join(dir, "upper", "g"))
+ _ = syscall.Unmount(merged, syscall.MNT_DETACH)
+ if errUp != nil {
+ out["overlay"] = "none"
+ out["error"] = "upper layer not written"
+ } else {
+ out["overlay"] = mode
+ }
+ emit(out)
+}
+
+// --- the box -----------------------------------------------------------------
+
+// spec is what `init` does inside the namespaces (written to <box>/spec.json).
+type spec struct {
+ Lower string `json:"lower"`
+ Upper string `json:"upper"`
+ Work string `json:"work"`
+ Merged string `json:"merged"`
+ Fuse string `json:"fuse,omitempty"`
+ Boxd string `json:"boxd"`
+ Resolv string `json:"resolv,omitempty"`
+ Hosts string `json:"hosts,omitempty"`
+ Binds []bindSpec `json:"binds"`
+ Env []string `json:"env"`
+ Hostname string `json:"hostname"`
+ InitOut string `json:"init_out"`
+ Root bool `json:"root"` // unshare as real root (WSL distro): mount /dev and /sys from the host
+ AptRoot bool `json:"apt_root"`
+}
+
+type bindSpec struct {
+ Src string `json:"src"`
+ Dst string `json:"dst"`
+ RO bool `json:"ro,omitempty"`
+}
+
+type state struct {
+ Pid int `json:"pid"`
+ Port int `json:"port"`
+ Launcher string `json:"launcher"`
+ IDMap string `json:"idmap"`
+ Overlay string `json:"overlay"`
+ Image string `json:"image"`
+ Started int64 `json:"started"`
+ Version string `json:"version"`
+}
+
+func readState(box string) *state {
+ data, err := os.ReadFile(filepath.Join(box, "state.json"))
+ if err != nil {
+ return nil
+ }
+ var s state
+ if json.Unmarshal(data, &s) != nil {
+ return nil
+ }
+ return &s
+}
+
+func writeJSONFile(file string, v any) error {
+ data, _ := json.MarshalIndent(v, "", " ")
+ tmp := file + ".tmp"
+ if err := os.WriteFile(tmp, append(data, '\n'), 0o600); err != nil {
+ return err
+ }
+ return os.Rename(tmp, file)
+}
+
+// alive: the pid runs and is our launcher (not a reused pid).
+func alive(s *state) bool {
+ if s == nil || s.Pid <= 0 {
+ return false
+ }
+ if syscall.Kill(s.Pid, 0) != nil {
+ return false
+ }
+ cmdline, err := os.ReadFile(fmt.Sprintf("/proc/%d/cmdline", s.Pid))
+ if err != nil {
+ return false
+ }
+ c := string(cmdline)
+ return strings.Contains(c, "bwrap") || strings.Contains(c, "unshare") || strings.Contains(c, "boxrun")
+}
+
+func portOpen(port int) bool {
+ if port <= 0 {
+ return false
+ }
+ conn, err := net.DialTimeout("tcp", "127.0.0.1:"+strconv.Itoa(port), 300*time.Millisecond)
+ if err != nil {
+ return false
+ }
+ conn.Close()
+ return true
+}
+
+func parseBind(v string) (bindSpec, error) {
+ i := strings.LastIndex(v, "=/")
+ if i <= 0 {
+ return bindSpec{}, fmt.Errorf("bad bind %q (SRC=DST)", v)
+ }
+ src, _ := filepath.Abs(v[:i])
+ return bindSpec{Src: src, Dst: filepath.Clean(v[i+1:])}, nil
+}
+
+func resolvFile() string {
+ p, err := filepath.EvalSymlinks("/etc/resolv.conf")
+ if err != nil {
+ return ""
+ }
+ // systemd-resolved's stub (127.0.0.53) works: the network is the host's.
+ return p
+}
+
+func cmdUp(args []string) error {
+ fs := newFlags("up")
+ box := fs.String("box", "", "the box's folder (upper layer, state)")
+ store := fs.String("store", "", "image store folder")
+ image := fs.String("image", "", "image id (boxrun image)")
+ boxd := fs.String("boxd", "", "boxd binary")
+ portFile := fs.String("port-file", "", "host path of the file boxd writes its port to")
+ launcher := fs.String("launcher", "auto", "auto|bwrap|unshare")
+ bwAsked := fs.String("bwrap", "", "bwrap to use")
+ fuseAsked := fs.String("fuse-overlayfs", "", "fuse-overlayfs to use")
+ idmapAsked := fs.String("idmap", "auto", "auto|single|subuid")
+ foreground := fs.Bool("foreground", false, "stay attached (the WSL keeper): exit when stdin closes")
+ timeout := fs.Duration("timeout", 30*time.Second, "wait this long for boxd")
+ var binds, roBinds, envs multi
+ fs.Var(&binds, "bind", "SRC=DST (repeat)")
+ fs.Var(&roBinds, "ro-bind", "SRC=DST read-only (repeat)")
+ fs.Var(&envs, "env", "K=V for boxd (repeat)")
+ if err := fs.Parse(args); err != nil {
+ return err
+ }
+ if *box == "" || *store == "" || *image == "" || *boxd == "" || *portFile == "" {
+ return errors.New("--box, --store, --image, --boxd and --port-file are needed")
+ }
+ for _, p := range []*string{box, store, boxd, portFile} {
+ *p, _ = filepath.Abs(*p)
+ }
+ start := time.Now()
+ if s := readState(*box); alive(s) {
+ if port := readPort(*portFile); port > 0 && portOpen(port) {
+ emit(map[string]any{"ok": true, "reused": true, "pid": s.Pid, "port": port, "launcher": s.Launcher, "idmap": s.IDMap, "overlay": s.Overlay, "ms": time.Since(start).Milliseconds()})
+ if *foreground {
+ keep(nil)
+ }
+ return nil
+ }
+ killTree(s.Pid)
+ }
+ st := oci.Store{Dir: *store}
+ img := st.Lookup(*image)
+ if img == nil {
+ return fmt.Errorf("image %s is not unpacked", *image)
+ }
+ for _, d := range []string{"upper", "work"} {
+ if err := os.MkdirAll(filepath.Join(*box, d), 0o755); err != nil {
+ return err
+ }
+ }
+ _ = os.Remove(*portFile)
+ root := os.Geteuid() == 0
+ mode := *launcher
+ if mode == "auto" {
+ mode = "bwrap"
+ if root {
+ mode = "unshare"
+ }
+ }
+ ids := detectIDMap(*idmapAsked)
+ if *idmapAsked == "subuid" && ids.Mode != "subuid" {
+ return errors.New("no subordinate ids for this user (newuidmap, /etc/subuid)")
+ }
+ var bs []bindSpec
+ for i, v := range append(append([]string{}, binds...), roBinds...) {
+ b, err := parseBind(v)
+ if err != nil {
+ return err
+ }
+ b.RO = i >= len(binds)
+ bs = append(bs, b)
+ }
+ self, _ := os.Executable()
+ fuseBin := findTool(*fuseAsked, "fuse-overlayfs")
+ env := append([]string{}, img.Env...)
+ if !hasKey(env, "PATH") {
+ env = append(env, "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin")
+ }
+ env = append(env, envs...)
+ sp := spec{Hostname: "omnirush-box", Env: env, AptRoot: ids.Mode == "single" && !root}
+ var cmd *exec.Cmd
+ if mode == "unshare" {
+ un := findTool("", "unshare")
+ if un == "" {
+ return errors.New("unshare (util-linux) is missing")
+ }
+ sp.Lower, sp.Upper, sp.Work = st.RootFS(*image), filepath.Join(*box, "upper"), filepath.Join(*box, "work")
+ sp.Merged = filepath.Join(*box, "merged")
+ _ = os.MkdirAll(sp.Merged, 0o755)
+ sp.Fuse, sp.Boxd, sp.Resolv, sp.Hosts = fuseBin, *boxd, resolvFile(), "/etc/hosts"
+ sp.Binds, sp.InitOut, sp.Root = bs, filepath.Join(*box, "init.json"), true
+ if err := writeJSONFile(filepath.Join(*box, "spec.json"), sp); err != nil {
+ return err
+ }
+ cmd = exec.Command(un, "--mount", "--pid", "--fork", "--ipc", "--uts", "--propagation", "private", "--kill-child", "--", self, "init", filepath.Join(*box, "spec.json"))
+ } else {
+ bin := findBwrap(*bwAsked)
+ if bin == "" {
+ return errors.New("bubblewrap (bwrap) is not installed")
+ }
+ sp.Lower, sp.Upper, sp.Work, sp.Merged = "/.omni/lower", "/.omni/box/upper", "/.omni/box/work", "/.omni/merged"
+ sp.Boxd, sp.InitOut = "/.omni/boxd", "/.omni/box/init.json"
+ bargs := []string{"--unshare-user", "--uid", "0", "--gid", "0", "--unshare-pid", "--unshare-ipc", "--unshare-uts", "--unshare-cgroup-try",
+ "--hostname", "omnirush-box", "--cap-add", "ALL", "--new-session",
+ "--dev", "/dev", "--proc", "/proc", "--ro-bind", "/sys", "/sys",
+ "--dir", "/.omni/merged",
+ "--ro-bind", st.RootFS(*image), "/.omni/lower",
+ "--bind", *box, "/.omni/box",
+ "--ro-bind", self, "/.omni/boxrun",
+ "--ro-bind", *boxd, "/.omni/boxd",
+ }
+ if ids.Mode == "subuid" {
+ bargs = append(bargs, "--userns-block-fd", "4", "--info-fd", "3")
+ }
+ if r := resolvFile(); r != "" {
+ bargs = append(bargs, "--ro-bind-try", r, "/.omni/resolv.conf")
+ sp.Resolv = "/.omni/resolv.conf"
+ }
+ bargs = append(bargs, "--ro-bind-try", "/etc/hosts", "/.omni/hosts")
+ sp.Hosts = "/.omni/hosts"
+ if fuseBin != "" {
+ bargs = append(bargs, "--dev-bind-try", "/dev/fuse", "/dev/fuse", "--ro-bind", fuseBin, "/.omni/fuse-overlayfs")
+ sp.Fuse = "/.omni/fuse-overlayfs"
+ }
+ for i, b := range bs {
+ staged := fmt.Sprintf("/.omni/b/%d", i)
+ flagName := "--bind"
+ if b.RO {
+ flagName = "--ro-bind"
+ }
+ bargs = append(bargs, flagName, b.Src, staged)
+ sp.Binds = append(sp.Binds, bindSpec{Src: staged, Dst: b.Dst, RO: b.RO})
+ }
+ if err := writeJSONFile(filepath.Join(*box, "spec.json"), sp); err != nil {
+ return err
+ }
+ bargs = append(bargs, "--chdir", "/", "--", "/.omni/boxrun", "init", "/.omni/box/spec.json")
+ cmd = exec.Command(bin, bargs...)
+ }
+ cmd.Env = []string{"PATH=/usr/sbin:/usr/bin:/sbin:/bin"}
+ logFile, err := os.OpenFile(filepath.Join(*box, "boxd.log"), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o600)
+ if err != nil {
+ return err
+ }
+ defer logFile.Close()
+ cmd.Stdout, cmd.Stderr = logFile, logFile
+ cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
+ var infoR, blockW *os.File
+ if mode == "bwrap" && ids.Mode == "subuid" {
+ ir, iw, err := os.Pipe()
+ if err != nil {
+ return err
+ }
+ br, bw, err := os.Pipe()
+ if err != nil {
+ return err
+ }
+ cmd.ExtraFiles = []*os.File{iw, br}
+ infoR, blockW = ir, bw
+ defer ir.Close()
+ defer bw.Close()
+ defer iw.Close()
+ defer br.Close()
+ }
+ _ = os.Remove(filepath.Join(*box, "init.json"))
+ if err := cmd.Start(); err != nil {
+ return err
+ }
+ if infoR != nil {
+ cmd.ExtraFiles[0].Close()
+ cmd.ExtraFiles[1].Close()
+ if err := mapIDs(infoR, blockW, ids); err != nil {
+ killTree(cmd.Process.Pid)
+ return fmt.Errorf("subordinate ids: %w", err)
+ }
+ }
+ exited := make(chan error, 1)
+ go func() { exited <- cmd.Wait() }()
+ deadline := time.Now().Add(*timeout)
+ port := 0
+ for time.Now().Before(deadline) {
+ if port = readPort(*portFile); port > 0 && portOpen(port) {
+ break
+ }
+ port = 0
+ select {
+ case err := <-exited:
+ return fmt.Errorf("the box exited at start (%v): %s", err, tail(filepath.Join(*box, "boxd.log"), 600))
+ case <-time.After(15 * time.Millisecond):
+ }
+ }
+ if port == 0 {
+ killTree(cmd.Process.Pid)
+ return fmt.Errorf("boxd did not answer in %v: %s", *timeout, tail(filepath.Join(*box, "boxd.log"), 600))
+ }
+ var initInfo struct {
+ Overlay string `json:"overlay"`
+ }
+ if data, err := os.ReadFile(filepath.Join(*box, "init.json")); err == nil {
+ _ = json.Unmarshal(data, &initInfo)
+ }
+ s := state{Pid: cmd.Process.Pid, Port: port, Launcher: mode, IDMap: ids.Mode, Overlay: initInfo.Overlay, Image: *image, Started: time.Now().Unix(), Version: version}
+ _ = writeJSONFile(filepath.Join(*box, "state.json"), s)
+ emit(map[string]any{"ok": true, "reused": false, "pid": s.Pid, "port": port, "launcher": mode, "idmap": ids.Mode, "overlay": s.Overlay, "ms": time.Since(start).Milliseconds()})
+ if *foreground {
+ keep(func() { killTree(cmd.Process.Pid) })
+ return nil
+ }
+ _ = cmd.Process.Release()
+ return nil
+}
+
+// keep holds the WSL keeper attached until stdin closes or a signal comes
+// (wsl.exe ended): the distro stays up while a session uses its box.
+func keep(onEnd func()) {
+ sigs := make(chan os.Signal, 1)
+ signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT, syscall.SIGHUP)
+ done := make(chan struct{})
+ go func() {
+ buf := make([]byte, 256)
+ for {
+ if _, err := os.Stdin.Read(buf); err != nil {
+ close(done)
+ return
+ }
+ }
+ }()
+ select {
+ case <-done:
+ case <-sigs:
+ }
+ // The box itself keeps running until boxd's idle limit: another
+ // session may still use it. Only a keeper's own box with no other
+ // keeper would be stopped here, which `onEnd` leaves to boxd's idle exit.
+ _ = onEnd
+}
+
+func hasKey(env []string, key string) bool {
+ for _, kv := range env {
+ if strings.HasPrefix(kv, key+"=") {
+ return true
+ }
+ }
+ return false
+}
+
+func readPort(file string) int {
+ data, err := os.ReadFile(file)
+ if err != nil {
+ return 0
+ }
+ n, _ := strconv.Atoi(strings.TrimSpace(string(data)))
+ return n
+}
+
+func tail(file string, max int) string {
+ data, _ := os.ReadFile(file)
+ s := strings.TrimSpace(string(data))
+ if len(s) > max {
+ s = s[len(s)-max:]
+ }
+ return strings.ReplaceAll(s, "\n", " | ")
+}
+
+// mapIDs runs newuidmap/newgidmap for bwrap's child, then lets it go on.
+func mapIDs(info *os.File, block *os.File, ids idMap) error {
+ dec := json.NewDecoder(info)
+ var msg struct {
+ Pid int `json:"child-pid"`
+ }
+ done := make(chan error, 1)
+ go func() { done <- dec.Decode(&msg) }()
+ select {
+ case err := <-done:
+ if err != nil {
+ return err
+ }
+ case <-time.After(10 * time.Second):
+ return errors.New("bwrap did not report its child")
+ }
+ pid := strconv.Itoa(msg.Pid)
+ uid, gid := strconv.Itoa(os.Getuid()), strconv.Itoa(os.Getgid())
+ if out, err := exec.Command(ids.NewUIDMap, pid, "0", uid, "1", "1", strconv.Itoa(ids.UIDStart), strconv.Itoa(ids.UIDCount-1)).CombinedOutput(); err != nil {
+ return fmt.Errorf("newuidmap: %v %s", err, strings.TrimSpace(string(out)))
+ }
+ if out, err := exec.Command(ids.NewGIDMap, pid, "0", gid, "1", "1", strconv.Itoa(ids.GIDStart), strconv.Itoa(ids.GIDCount-1)).CombinedOutput(); err != nil {
+ return fmt.Errorf("newgidmap: %v %s", err, strings.TrimSpace(string(out)))
+ }
+ _, err := block.Write([]byte{1})
+ return err
+}
+
+// killTree kills a process and everything below it, deepest first.
+func killTree(pid int) {
+ children := map[int][]int{}
+ entries, _ := os.ReadDir("/proc")
+ for _, e := range entries {
+ p, err := strconv.Atoi(e.Name())
+ if err != nil {
+ continue
+ }
+ data, err := os.ReadFile("/proc/" + e.Name() + "/stat")
+ if err != nil {
+ continue
+ }
+ s := string(data)
+ i := strings.LastIndex(s, ")")
+ if i < 0 {
+ continue
+ }
+ f := strings.Fields(s[i+1:])
+ if len(f) < 2 {
+ continue
+ }
+ pp, _ := strconv.Atoi(f[1])
+ children[pp] = append(children[pp], p)
+ }
+ var order []int
+ var visit func(int)
+ visit = func(p int) {
+ for _, c := range children[p] {
+ visit(c)
+ }
+ order = append(order, p)
+ }
+ visit(pid)
+ for _, p := range order {
+ _ = syscall.Kill(p, syscall.SIGKILL)
+ }
+ for i := 0; i < 100 && syscall.Kill(pid, 0) == nil; i++ {
+ var ws syscall.WaitStatus
+ _, _ = syscall.Wait4(pid, &ws, syscall.WNOHANG, nil)
+ time.Sleep(10 * time.Millisecond)
+ }
+}
+
+func cmdStop(args []string, remove bool) error {
+ fs := newFlags("stop")
+ box := fs.String("box", "", "the box's folder")
+ bwAsked := fs.String("bwrap", "", "bwrap to use")
+ if err := fs.Parse(args); err != nil {
+ return err
+ }
+ if *box == "" {
+ return errors.New("--box is needed")
+ }
+ *box, _ = filepath.Abs(*box)
+ s := readState(*box)
+ wasRunning := alive(s)
+ if wasRunning {
+ killTree(s.Pid)
+ }
+ _ = os.Remove(filepath.Join(*box, "state.json"))
+ if remove {
+ if err := removeTree(*box); err != nil {
+ // Files owned by subordinate ids: delete them from inside a namespace that maps them.
+ if err2 := rmtreeViaNamespace(*box, *bwAsked); err2 != nil {
+ return fmt.Errorf("%v; %v", err, err2)
+ }
+ if err := removeTree(*box); err != nil {
+ return err
+ }
+ }
+ }
+ emit(map[string]any{"ok": true, "was_running": wasRunning, "removed": remove})
+ return nil
+}
+
+func rmtreeViaNamespace(box, bwAsked string) error {
+ ids := detectIDMap("auto")
+ bin := findBwrap(bwAsked)
+ if ids.Mode != "subuid" || bin == "" {
+ return errors.New("cannot remove files owned by other ids")
+ }
+ self, _ := os.Executable()
+ cmd := exec.Command(bin, "--unshare-user", "--uid", "0", "--gid", "0", "--cap-add", "ALL", "--userns-block-fd", "4", "--info-fd", "3",
+ "--ro-bind", "/", "/", "--dev", "/dev", "--proc", "/proc", "--bind", box, box, "--", self, "rmtree", box)
+ ir, iw, _ := os.Pipe()
+ br, bw, _ := os.Pipe()
+ cmd.ExtraFiles = []*os.File{iw, br}
+ if err := cmd.Start(); err != nil {
+ return err
+ }
+ iw.Close()
+ br.Close()
+ if err := mapIDs(ir, bw, ids); err != nil {
+ killTree(cmd.Process.Pid)
+ return err
+ }
+ ir.Close()
+ bw.Close()
+ return cmd.Wait()
+}
+
+func rmtreeInner(args []string) error {
+ if len(args) < 1 {
+ return errors.New("no folder")
+ }
+ entries, _ := os.ReadDir(args[0])
+ for _, e := range entries {
+ if err := removeTree(filepath.Join(args[0], e.Name())); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
+func removeTree(p string) error {
+ _ = filepath.Walk(p, func(q string, fi os.FileInfo, err error) error {
+ if err == nil && fi.IsDir() && fi.Mode().Perm()&0o700 != 0o700 {
+ _ = os.Chmod(q, fi.Mode().Perm()|0o700)
+ }
+ return nil
+ })
+ return os.RemoveAll(p)
+}
+
+func cmdStatus(args []string) error {
+ fs := newFlags("status")
+ box := fs.String("box", "", "the box's folder")
+ portFile := fs.String("port-file", "", "boxd's port file")
+ if err := fs.Parse(args); err != nil {
+ return err
+ }
+ s := readState(*box)
+ running := alive(s)
+ port := 0
+ if running && *portFile != "" {
+ port = readPort(*portFile)
+ }
+ out := map[string]any{"ok": true, "running": running, "port": port, "upper_bytes": dirBytes(filepath.Join(*box, "upper"))}
+ if s != nil {
+ out["pid"], out["launcher"], out["idmap"], out["overlay"], out["image"] = s.Pid, s.Launcher, s.IDMap, s.Overlay, s.Image
+ }
+ emit(out)
+ return nil
+}
+
+func dirBytes(dir string) int64 {
+ var n int64
+ _ = filepath.Walk(dir, func(_ string, fi os.FileInfo, err error) error {
+ if err == nil && fi.Mode().IsRegular() {
+ n += fi.Size()
+ }
+ return nil
+ })
+ return n
+}
+
+// cmdEnter runs a command inside a running box (an interactive shell for
+// `omnirush sandbox shell`) with nsenter: the box's user (when bwrap made
+// one), mount and PID namespaces, and its root.
+func cmdEnter(args []string) error {
+ fs := newFlags("enter")
+ box := fs.String("box", "", "the box's folder")
+ wd := fs.String("wd", "/", "working folder inside")
+ if err := fs.Parse(args); err != nil {
+ return err
+ }
+ rest := fs.Args()
+ if len(rest) == 0 {
+ rest = []string{"/bin/bash", "-l"}
+ }
+ s := readState(*box)
+ if !alive(s) {
+ return errors.New("the box is not running")
+ }
+ target := innermost(s.Pid)
+ ns := findTool("", "nsenter")
+ if ns == "" {
+ return errors.New("nsenter (util-linux) is missing")
+ }
+ nargs := []string{"-t", strconv.Itoa(target), "-m", "-p", "-u", "-i"}
+ if s.Launcher == "bwrap" {
+ nargs = append(nargs, "-U", "--preserve-credentials=no")
+ }
+ nargs = append(nargs, "-r", "-w"+*wd, "--")
+ nargs = append(nargs, rest...)
+ // --preserve-credentials=no is not an option of every nsenter: drop it.
+ clean := []string{}
+ for _, a := range nargs {
+ if a != "--preserve-credentials=no" {
+ clean = append(clean, a)
+ }
+ }
+ return syscall.Exec(ns, append([]string{"nsenter"}, clean...), os.Environ())
+}
+
+// innermost is the deepest descendant (boxd runs at the bottom of bwrap's chain).
+func innermost(pid int) int {
+ for i := 0; i < 8; i++ {
+ data, err := os.ReadFile(fmt.Sprintf("/proc/%d/task/%d/children", pid, pid))
+ if err != nil {
+ return pid
+ }
+ f := strings.Fields(string(data))
+ if len(f) == 0 {
+ return pid
+ }
+ next, _ := strconv.Atoi(f[0])
+ if next <= 0 {
+ return pid
+ }
+ pid = next
+ if comm, _ := os.ReadFile(fmt.Sprintf("/proc/%d/comm", pid)); strings.TrimSpace(string(comm)) == "boxd" {
+ return pid
+ }
+ }
+ return pid
+}
diff --git a/apps/desktop/box/go/cmd/boxsh/main.go b/apps/desktop/box/go/cmd/boxsh/main.go
index c5774730..d25fc8f6 100644
--- a/apps/desktop/box/go/cmd/boxsh/main.go
+++ b/apps/desktop/box/go/cmd/boxsh/main.go
@@ -37,7 +37,9 @@ type Record struct {
Root string `json:"root"`
CRoot string `json:"croot"`
Container string `json:"container"`
- Runtime string `json:"runtime"`
+ // Backend runs the box (docker, podman, bwrap, apple, wslc, wsl, ...).
+ Backend string `json:"backend"`
+ Runtime string `json:"runtime"`
// RuntimeArgs come before the runtime's own (`-e docker` through wsl.exe).
RuntimeArgs []string `json:"runtime_args"`
Host string `json:"host"`
@@ -89,6 +91,11 @@ func main() {
start := time.Now()
code, timing, err := runRemote(rec, req)
if err != nil {
+ if rec.Runtime == "" && rec.Backend != "" {
+ // A backend with no `exec` of its own (bubblewrap, the WSL distro): boxd is the only way in.
+ fmt.Fprintf(os.Stderr, "omnirush sandbox: the sandbox is not reachable (%v); `omnirush sandbox status` says why\n", err)
+ os.Exit(125)
+ }
// boxd is not reachable (restarting, crashed): run it with the runtime's exec instead.
code = runtimeExec(rec, req)
timing.Fallback = true
diff --git a/apps/desktop/box/go/oci/layer.go b/apps/desktop/box/go/oci/layer.go
new file mode 100644
index 00000000..41fdc12c
--- /dev/null
+++ b/apps/desktop/box/go/oci/layer.go
@@ -0,0 +1,293 @@
+//go:build linux
+
+package oci
+
+import (
+ "archive/tar"
+ "bufio"
+ "compress/gzip"
+ "errors"
+ "fmt"
+ "io"
+ "os"
+ "path"
+ "path/filepath"
+ "sort"
+ "strings"
+ "syscall"
+ "time"
+)
+
+// ApplyOptions tune unpacking.
+type ApplyOptions struct {
+ // Chown sets owners from the tar (only possible as root: the WSL
+ // distro). Unprivileged, every file is the user's, which the box's user
+ // namespace shows as root.
+ Chown bool
+}
+
+// ApplyStats counts what one layer did.
+type ApplyStats struct {
+ Entries int
+ Whiteouts int
+ Skipped int // devices and the like (cannot be made unprivileged)
+ Bytes int64
+}
+
+type dirMeta struct {
+ path string
+ mode os.FileMode
+ mtime time.Time
+}
+
+// Decompress wraps a layer stream: gzip by magic bytes, else plain tar.
+func Decompress(r io.Reader) (io.Reader, func() error, error) {
+ br := bufio.NewReaderSize(r, 64<<10)
+ head, err := br.Peek(2)
+ if err != nil && !errors.Is(err, io.EOF) {
+ return nil, nil, err
+ }
+ if len(head) == 2 && head[0] == 0x1f && head[1] == 0x8b {
+ zr, err := gzip.NewReader(br)
+ if err != nil {
+ return nil, nil, err
+ }
+ zr.Multistream(true)
+ return zr, zr.Close, nil
+ }
+ if len(head) >= 2 && head[0] == 0x28 && head[1] == 0xb5 {
+ return nil, nil, errors.New("zstd image layers are not supported yet")
+ }
+ return br, func() error { return nil }, nil
+}
+
+// safeJoin resolves name under root without leaving it (no "..", and no
+// symlink in a parent pointing outside: parents are resolved inside root).
+func safeJoin(root, name string) (string, error) {
+ clean := path.Clean("/" + strings.TrimPrefix(name, "./"))
+ if clean == "/" {
+ return root, nil
+ }
+ parts := strings.Split(strings.TrimPrefix(clean, "/"), "/")
+ cur := root
+ for i, part := range parts {
+ if part == "" || part == "." || part == ".." {
+ return "", fmt.Errorf("bad path %q", name)
+ }
+ next := filepath.Join(cur, part)
+ if i == len(parts)-1 {
+ return next, nil
+ }
+ // Parents: follow symlinks, but resolved inside root.
+ for hops := 0; hops < 40; hops++ {
+ fi, err := os.Lstat(next)
+ if err != nil || fi.Mode()&os.ModeSymlink == 0 {
+ break
+ }
+ target, err := os.Readlink(next)
+ if err != nil {
+ break
+ }
+ var resolved string
+ if strings.HasPrefix(target, "/") {
+ resolved = path.Clean(target)
+ } else {
+ rel, _ := filepath.Rel(root, filepath.Dir(next))
+ resolved = path.Clean("/" + filepath.ToSlash(rel) + "/" + target)
+ }
+ next = filepath.Join(root, filepath.FromSlash(resolved))
+ }
+ cur = next
+ }
+ return cur, nil
+}
+
+func removeAllForce(p string) error {
+ _ = filepath.Walk(p, func(q string, fi os.FileInfo, err error) error {
+ if err == nil && fi.IsDir() && fi.Mode().Perm()&0o700 != 0o700 {
+ _ = os.Chmod(q, fi.Mode().Perm()|0o700)
+ }
+ return nil
+ })
+ return os.RemoveAll(p)
+}
+
+// Unpacker applies layers in order onto one root; folder modes and times
+// are set by Finish, after the last layer (a 0555 folder of one layer must
+// still take the files of the next).
+type Unpacker struct {
+ Root string
+ Opt ApplyOptions
+ dirs map[string]dirMeta
+}
+
+// Apply unpacks one layer (a tar stream, gzip or plain) with the OCI
+// whiteout rules: `.wh.<name>` removes name, `.wh..wh..opq` empties the
+// folder of what lower layers put there.
+func (u *Unpacker) Apply(layer io.Reader) (ApplyStats, error) {
+ root, opt := u.Root, u.Opt
+ if u.dirs == nil {
+ u.dirs = map[string]dirMeta{}
+ }
+ var st ApplyStats
+ r, closeFn, err := Decompress(layer)
+ if err != nil {
+ return st, err
+ }
+ tr := tar.NewReader(r)
+ // Folders this layer made opaque: their lower contents are dropped once
+ // (entries of this layer under them stay).
+ opaqueDone := map[string]bool{}
+ thisLayer := map[string]bool{}
+ for {
+ hdr, err := tr.Next()
+ if errors.Is(err, io.EOF) {
+ break
+ }
+ if err != nil {
+ return st, err
+ }
+ st.Entries++
+ name := path.Clean("/" + strings.TrimPrefix(hdr.Name, "./"))
+ base := path.Base(name)
+ dir := path.Dir(name)
+ if strings.HasPrefix(base, ".wh.") {
+ st.Whiteouts++
+ parent, err := safeJoin(root, dir)
+ if err != nil {
+ return st, err
+ }
+ if base == ".wh..wh..opq" {
+ if !opaqueDone[dir] {
+ opaqueDone[dir] = true
+ entries, _ := os.ReadDir(parent)
+ for _, e := range entries {
+ if !thisLayer[path.Join(dir, e.Name())] {
+ _ = removeAllForce(filepath.Join(parent, e.Name()))
+ }
+ }
+ }
+ continue
+ }
+ _ = removeAllForce(filepath.Join(parent, strings.TrimPrefix(base, ".wh.")))
+ continue
+ }
+ if name == "/" {
+ continue
+ }
+ target, err := safeJoin(root, name)
+ if err != nil {
+ return st, err
+ }
+ if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil {
+ return st, err
+ }
+ thisLayer[name] = true
+ existing, lerr := os.Lstat(target)
+ if lerr == nil {
+ // A folder stays a folder (only its metadata changes); anything else is replaced.
+ if !(existing.IsDir() && hdr.Typeflag == tar.TypeDir) {
+ if err := removeAllForce(target); err != nil {
+ return st, err
+ }
+ }
+ }
+ mode := os.FileMode(hdr.Mode & 0o777)
+ special := os.FileMode(0)
+ if hdr.Mode&0o4000 != 0 {
+ special |= os.ModeSetuid
+ }
+ if hdr.Mode&0o2000 != 0 {
+ special |= os.ModeSetgid
+ }
+ if hdr.Mode&0o1000 != 0 {
+ special |= os.ModeSticky
+ }
+ switch hdr.Typeflag {
+ case tar.TypeDir:
+ if lerr != nil || !existing.IsDir() {
+ if err := os.Mkdir(target, 0o755); err != nil && !os.IsExist(err) {
+ return st, err
+ }
+ }
+ u.dirs[target] = dirMeta{target, mode | special, hdr.ModTime}
+ case tar.TypeReg, tar.TypeRegA:
+ f, err := os.OpenFile(target, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o600)
+ if err != nil {
+ return st, err
+ }
+ n, err := io.Copy(f, tr)
+ st.Bytes += n
+ if cerr := f.Close(); err == nil {
+ err = cerr
+ }
+ if err != nil {
+ return st, err
+ }
+ if err := os.Chmod(target, mode|special); err != nil {
+ return st, err
+ }
+ case tar.TypeSymlink:
+ if err := os.Symlink(hdr.Linkname, target); err != nil {
+ return st, err
+ }
+ case tar.TypeLink:
+ src, err := safeJoin(root, hdr.Linkname)
+ if err != nil {
+ return st, err
+ }
+ if err := os.Link(src, target); err != nil {
+ return st, fmt.Errorf("hard link %s: %w", hdr.Name, err)
+ }
+ case tar.TypeFifo:
+ if err := syscall.Mkfifo(target, uint32(mode)); err != nil {
+ return st, err
+ }
+ case tar.TypeChar, tar.TypeBlock:
+ if opt.Chown {
+ devType := uint32(syscall.S_IFCHR)
+ if hdr.Typeflag == tar.TypeBlock {
+ devType = syscall.S_IFBLK
+ }
+ dev := (hdr.Devmajor << 8) | (hdr.Devminor & 0xff) | ((hdr.Devminor &^ 0xff) << 12)
+ if err := syscall.Mknod(target, devType|uint32(mode), int(dev)); err == nil {
+ break
+ }
+ }
+ st.Skipped++
+ delete(thisLayer, name)
+ continue
+ default:
+ st.Skipped++
+ delete(thisLayer, name)
+ continue
+ }
+ if opt.Chown {
+ _ = os.Lchown(target, hdr.Uid, hdr.Gid)
+ if hdr.Typeflag == tar.TypeReg || hdr.Typeflag == tar.TypeRegA {
+ // chown drops setuid bits: put them back.
+ _ = os.Chmod(target, mode|special)
+ }
+ }
+ if hdr.Typeflag != tar.TypeSymlink && hdr.Typeflag != tar.TypeDir && hdr.Typeflag != tar.TypeLink {
+ _ = os.Chtimes(target, hdr.ModTime, hdr.ModTime)
+ }
+ }
+ return st, closeFn()
+}
+
+// Finish sets folder modes and times, deepest first.
+func (u *Unpacker) Finish() {
+ dirs := make([]dirMeta, 0, len(u.dirs))
+ for _, d := range u.dirs {
+ dirs = append(dirs, d)
+ }
+ sort.Slice(dirs, func(i, j int) bool { return len(dirs[i].path) > len(dirs[j].path) })
+ for _, d := range dirs {
+ if fi, err := os.Lstat(d.path); err != nil || !fi.IsDir() {
+ continue
+ }
+ _ = os.Chmod(d.path, d.mode)
+ _ = os.Chtimes(d.path, d.mtime, d.mtime)
+ }
+}
diff --git a/apps/desktop/box/go/oci/oci_test.go b/apps/desktop/box/go/oci/oci_test.go
new file mode 100644
index 00000000..56b3d685
--- /dev/null
+++ b/apps/desktop/box/go/oci/oci_test.go
@@ -0,0 +1,344 @@
+//go:build linux
+
+package oci
+
+import (
+ "archive/tar"
+ "bytes"
+ "compress/gzip"
+ "context"
+ "crypto/sha256"
+ "encoding/hex"
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+)
+
+type entry struct {
+ name, body, link string
+ typ byte
+ mode int64
+}
+
+func tarBytes(t *testing.T, entries []entry, gz bool) []byte {
+ t.Helper()
+ var buf bytes.Buffer
+ var w = &buf
+ var zw *gzip.Writer
+ tw := tar.NewWriter(w)
+ if gz {
+ zw = gzip.NewWriter(w)
+ tw = tar.NewWriter(zw)
+ }
+ for _, e := range entries {
+ mode := e.mode
+ if mode == 0 {
+ mode = 0o644
+ if e.typ == tar.TypeDir {
+ mode = 0o755
+ }
+ }
+ hdr := &tar.Header{Name: e.name, Typeflag: e.typ, Mode: mode, Size: int64(len(e.body)), Linkname: e.link}
+ if e.typ != tar.TypeReg {
+ hdr.Size = 0
+ }
+ if err := tw.WriteHeader(hdr); err != nil {
+ t.Fatal(err)
+ }
+ if e.typ == tar.TypeReg {
+ tw.Write([]byte(e.body))
+ }
+ }
+ tw.Close()
+ if zw != nil {
+ zw.Close()
+ }
+ return buf.Bytes()
+}
+
+func digestOf(b []byte) string {
+ s := sha256.Sum256(b)
+ return "sha256:" + hex.EncodeToString(s[:])
+}
+
+func TestParseRef(t *testing.T) {
+ cases := map[string]string{
+ "ubuntu": "docker.io/library/ubuntu:latest",
+ "ubuntu:24.04": "docker.io/library/ubuntu:24.04",
+ "ghcr.io/omnirush-ai/box-base:2026.10.1": "ghcr.io/omnirush-ai/box-base:2026.10.1",
+ "localhost:5000/x/y:t": "localhost:5000/x/y:t",
+ "127.0.0.1:5000/omnirush-ai/box-base:1": "127.0.0.1:5000/omnirush-ai/box-base:1",
+ }
+ for in, want := range cases {
+ r, err := ParseRef(in)
+ if err != nil || r.String() != want {
+ t.Errorf("%s -> %v %v, want %s", in, r.String(), err, want)
+ }
+ }
+ d := "sha256:" + strings.Repeat("a", 64)
+ r, err := ParseRef("ghcr.io/o/b:1@" + d)
+ if err != nil || r.Digest != d || r.Reference() != d || r.Tag != "1" {
+ t.Fatalf("pinned: %+v %v", r, err)
+ }
+ if _, err := ParseRef("ghcr.io/O/B"); err == nil {
+ t.Fatal("upper case accepted")
+ }
+ if !(Ref{Registry: "localhost:5000"}).Plain() || (Ref{Registry: "ghcr.io"}).Plain() {
+ t.Fatal("plain http only on loopback")
+ }
+}
+
+func TestParseChallenge(t *testing.T) {
+ s, p := ParseChallenge(`Bearer realm="https://ghcr.io/token",service="ghcr.io",scope="repository:o/b:pull"`)
+ if s != "bearer" || p["realm"] != "https://ghcr.io/token" || p["service"] != "ghcr.io" || p["scope"] != "repository:o/b:pull" {
+ t.Fatalf("%s %v", s, p)
+ }
+}
+
+func TestApplyWhiteoutsAndModes(t *testing.T) {
+ root := t.TempDir()
+ u := &Unpacker{Root: root}
+ l1 := tarBytes(t, []entry{
+ {name: "etc/", typ: tar.TypeDir},
+ {name: "etc/a", typ: tar.TypeReg, body: "a"},
+ {name: "etc/b", typ: tar.TypeReg, body: "b"},
+ {name: "opt/", typ: tar.TypeDir},
+ {name: "opt/old", typ: tar.TypeReg, body: "x"},
+ {name: "ro/", typ: tar.TypeDir, mode: 0o555},
+ {name: "ro/f", typ: tar.TypeReg, body: "f"},
+ {name: "bin/", typ: tar.TypeDir},
+ {name: "bin/sudo", typ: tar.TypeReg, body: "s", mode: 0o4755},
+ {name: "bin/link", typ: tar.TypeSymlink, link: "sudo"},
+ {name: "bin/hard", typ: tar.TypeLink, link: "bin/sudo"},
+ {name: "dev/", typ: tar.TypeDir},
+ {name: "dev/null", typ: tar.TypeChar},
+ }, true)
+ if _, err := u.Apply(bytes.NewReader(l1)); err != nil {
+ t.Fatal(err)
+ }
+ l2 := tarBytes(t, []entry{
+ {name: "etc/.wh.a", typ: tar.TypeReg},
+ {name: "opt/.wh..wh..opq", typ: tar.TypeReg},
+ {name: "opt/new", typ: tar.TypeReg, body: "n"},
+ {name: "ro/g", typ: tar.TypeReg, body: "g"},
+ }, false)
+ st, err := u.Apply(bytes.NewReader(l2))
+ if err != nil {
+ t.Fatal(err)
+ }
+ u.Finish()
+ if st.Whiteouts != 2 {
+ t.Fatalf("whiteouts %d", st.Whiteouts)
+ }
+ if _, err := os.Stat(filepath.Join(root, "etc/a")); !os.IsNotExist(err) {
+ t.Fatal("etc/a not removed")
+ }
+ if _, err := os.Stat(filepath.Join(root, "etc/b")); err != nil {
+ t.Fatal("etc/b removed")
+ }
+ if _, err := os.Stat(filepath.Join(root, "opt/old")); !os.IsNotExist(err) {
+ t.Fatal("opaque folder kept a lower file")
+ }
+ if _, err := os.Stat(filepath.Join(root, "opt/new")); err != nil {
+ t.Fatal("opaque folder lost its own file")
+ }
+ if _, err := os.Stat(filepath.Join(root, "ro/g")); err != nil {
+ t.Fatal("a 0555 folder of a lower layer refused a file")
+ }
+ fi, _ := os.Stat(filepath.Join(root, "ro"))
+ if fi.Mode().Perm() != 0o555 {
+ t.Fatalf("ro mode %v", fi.Mode())
+ }
+ fi, _ = os.Stat(filepath.Join(root, "bin/sudo"))
+ if fi.Mode()&os.ModeSetuid == 0 {
+ t.Fatal("setuid bit lost")
+ }
+ if l, _ := os.Readlink(filepath.Join(root, "bin/link")); l != "sudo" {
+ t.Fatal("symlink")
+ }
+ a, _ := os.Stat(filepath.Join(root, "bin/hard"))
+ if !os.SameFile(a, fi) {
+ t.Fatal("hard link")
+ }
+ os.Chmod(filepath.Join(root, "ro"), 0o755)
+}
+
+func TestApplyRefusesEscape(t *testing.T) {
+ root := t.TempDir()
+ outside := t.TempDir()
+ u := &Unpacker{Root: root}
+ l := tarBytes(t, []entry{
+ {name: "evil", typ: tar.TypeSymlink, link: outside},
+ {name: "evil/pwned", typ: tar.TypeReg, body: "x"},
+ }, false)
+ if _, err := u.Apply(bytes.NewReader(l)); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := os.Stat(filepath.Join(outside, "pwned")); err == nil {
+ t.Fatal("a symlinked parent wrote outside the root")
+ }
+ if _, err := os.Stat(filepath.Join(root, outside, "pwned")); err != nil {
+ t.Fatal("the file should land inside the root")
+ }
+ l2 := tarBytes(t, []entry{{name: "../x", typ: tar.TypeReg, body: "x"}}, false)
+ (&Unpacker{Root: root}).Apply(bytes.NewReader(l2))
+ if _, err := os.Stat(filepath.Join(filepath.Dir(root), "x")); err == nil {
+ t.Fatal("../ escaped")
+ }
+}
+
+// fakeRegistry serves one multi-arch image behind a bearer token.
+func fakeRegistry(t *testing.T, layers [][]byte) (*httptest.Server, string) {
+ t.Helper()
+ blobs := map[string][]byte{}
+ var descs []Descriptor
+ for _, l := range layers {
+ d := digestOf(l)
+ blobs[d] = l
+ descs = append(descs, Descriptor{MediaType: "application/vnd.oci.image.layer.v1.tar+gzip", Digest: d, Size: int64(len(l))})
+ }
+ cfg, _ := json.Marshal(map[string]any{"architecture": "amd64", "os": "linux", "config": map[string]any{"Env": []string{"PATH=/usr/bin:/bin", "HOME=/root"}, "WorkingDir": "/root"}})
+ blobs[digestOf(cfg)] = cfg
+ man, _ := json.Marshal(map[string]any{"schemaVersion": 2, "mediaType": MediaOCIManifest, "config": Descriptor{MediaType: "application/vnd.oci.image.config.v1+json", Digest: digestOf(cfg), Size: int64(len(cfg))}, "layers": descs})
+ armMan := []byte(`{"schemaVersion":2}`)
+ idx, _ := json.Marshal(map[string]any{"schemaVersion": 2, "mediaType": MediaOCIIndex, "manifests": []Descriptor{
+ {MediaType: MediaOCIManifest, Digest: digestOf(armMan), Size: int64(len(armMan)), Platform: &Platform{OS: "linux", Architecture: "arm64", Variant: "v8"}},
+ {MediaType: MediaOCIManifest, Digest: digestOf(man), Size: int64(len(man)), Platform: &Platform{OS: "linux", Architecture: "amd64"}},
+ }})
+ manifests := map[string][]byte{"1": idx, digestOf(idx): idx, digestOf(man): man, digestOf(armMan): armMan}
+ var srv *httptest.Server
+ srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if r.URL.Path == "/token" {
+ if r.URL.Query().Get("scope") != "repository:o/box:pull" {
+ w.WriteHeader(400)
+ return
+ }
+ w.Write([]byte(`{"token":"t0k"}`))
+ return
+ }
+ if r.Header.Get("Authorization") != "Bearer t0k" {
+ w.Header().Set("WWW-Authenticate", `Bearer realm="`+srv.URL+`/token",service="test"`)
+ w.WriteHeader(401)
+ return
+ }
+ parts := strings.Split(r.URL.Path, "/")
+ last := parts[len(parts)-1]
+ switch parts[len(parts)-2] {
+ case "manifests":
+ if body, ok := manifests[last]; ok {
+ var probe map[string]any
+ json.Unmarshal(body, &probe)
+ w.Header().Set("Content-Type", probe["mediaType"].(string))
+ w.Write(body)
+ return
+ }
+ case "blobs":
+ if body, ok := blobs[last]; ok {
+ w.Write(body)
+ return
+ }
+ }
+ w.WriteHeader(404)
+ }))
+ return srv, digestOf(idx)
+}
+
+func TestPullIndexWithToken(t *testing.T) {
+ l1 := tarBytes(t, []entry{{name: "etc/", typ: tar.TypeDir}, {name: "etc/os-release", typ: tar.TypeReg, body: "ID=test\n"}}, true)
+ l2 := tarBytes(t, []entry{{name: "etc/.wh.os-release", typ: tar.TypeReg}, {name: "etc/hi", typ: tar.TypeReg, body: "hi"}}, true)
+ srv, idxDigest := fakeRegistry(t, [][]byte{l1, l2})
+ defer srv.Close()
+ host := strings.TrimPrefix(srv.URL, "http://")
+ host = strings.Replace(host, "127.0.0.1", "localhost", 1)
+ ref, err := ParseRef(host + "/o/box:1")
+ if err != nil {
+ t.Fatal(err)
+ }
+ ref.Registry = strings.TrimPrefix(srv.URL, "http://")
+ store := Store{Dir: t.TempDir()}
+ c := NewClient()
+ img, id, err := store.Pull(context.Background(), c, ref, "x64", ApplyOptions{}, nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if img.Digest != idxDigest || img.Arch != "amd64" || len(img.Env) != 2 || img.WorkDir != "/root" {
+ t.Fatalf("%+v", img)
+ }
+ root := store.RootFS(id)
+ if b, _ := os.ReadFile(filepath.Join(root, "etc/hi")); string(b) != "hi" {
+ t.Fatal("layer 2 missing")
+ }
+ if _, err := os.Stat(filepath.Join(root, "etc/os-release")); !os.IsNotExist(err) {
+ t.Fatal("whiteout across layers")
+ }
+ // Second pull: found by tag without the network.
+ srv.Close()
+ img2, id2, err := store.Pull(context.Background(), c, ref, "amd64", ApplyOptions{}, nil)
+ if err != nil || id2 != id || img2.Digest != idxDigest {
+ t.Fatalf("cached: %v %s", err, id2)
+ }
+ // Pinned by digest.
+ pinned := ref
+ pinned.Digest = idxDigest
+ if got, _ := store.Find(pinned, "amd64"); got == nil {
+ t.Fatal("not found by digest")
+ }
+}
+
+func TestPullRejectsBadBlob(t *testing.T) {
+ l1 := tarBytes(t, []entry{{name: "a", typ: tar.TypeReg, body: "a"}}, true)
+ srv, _ := fakeRegistry(t, [][]byte{l1})
+ defer srv.Close()
+ ref, _ := ParseRef("localhost:1/o/box:1")
+ ref.Registry = strings.TrimPrefix(srv.URL, "http://")
+ ref.Digest = "sha256:" + strings.Repeat("0", 64)
+ _, _, err := (Store{Dir: t.TempDir()}).Pull(context.Background(), NewClient(), ref, "amd64", ApplyOptions{}, nil)
+ if err == nil {
+ t.Fatal("a wrong pinned digest was accepted")
+ }
+}
+
+func TestImportDockerSave(t *testing.T) {
+ layer := tarBytes(t, []entry{{name: "usr/", typ: tar.TypeDir}, {name: "usr/x", typ: tar.TypeReg, body: "x"}}, false)
+ cfg := []byte(`{"architecture":"arm64","os":"linux","config":{"Env":["A=1"]}}`)
+ man := []byte(`[{"Config":"cfg.json","RepoTags":["omnirush-ai/box-base:t"],"Layers":["l1/layer.tar"]}]`)
+ archive := tarBytes(t, []entry{
+ {name: "manifest.json", typ: tar.TypeReg, body: string(man)},
+ {name: "cfg.json", typ: tar.TypeReg, body: string(cfg)},
+ {name: "l1/layer.tar", typ: tar.TypeReg, body: string(layer)},
+ }, false)
+ file := filepath.Join(t.TempDir(), "img.tar")
+ os.WriteFile(file, archive, 0o644)
+ store := Store{Dir: t.TempDir()}
+ img, id, err := store.Import(file, "", "amd64", ApplyOptions{})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if img.Digest != digestOf(cfg) || img.Arch != "arm64" || img.Env[0] != "A=1" {
+ t.Fatalf("%+v", img)
+ }
+ if b, _ := os.ReadFile(filepath.Join(store.RootFS(id), "usr/x")); string(b) != "x" {
+ t.Fatal("layer not applied")
+ }
+}
+
+func TestImportRootfsTar(t *testing.T) {
+ archive := tarBytes(t, []entry{{name: "./", typ: tar.TypeDir}, {name: "./etc/", typ: tar.TypeDir}, {name: "./etc/wsl.conf", typ: tar.TypeReg, body: "[interop]\n"}}, true)
+ file := filepath.Join(t.TempDir(), "rootfs.tar.gz")
+ os.WriteFile(file, archive, 0o644)
+ store := Store{Dir: t.TempDir()}
+ img, id, err := store.Import(file, "omnirush/wsl:1", "amd64", ApplyOptions{})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if img.Digest != digestOf(archive) || img.Ref != "omnirush/wsl:1" {
+ t.Fatalf("%+v", img)
+ }
+ if _, err := os.Stat(filepath.Join(store.RootFS(id), "etc/wsl.conf")); err != nil {
+ t.Fatal(err)
+ }
+}
diff --git a/apps/desktop/box/go/oci/ref.go b/apps/desktop/box/go/oci/ref.go
new file mode 100644
index 00000000..3dcc37b8
--- /dev/null
+++ b/apps/desktop/box/go/oci/ref.go
@@ -0,0 +1,116 @@
+// Package oci pulls a pinned OCI/Docker image from a registry (anonymous or
+// with a token) and unpacks its layers into one root folder, with no daemon
+// and no root: what the bubblewrap box (and the WSL distro's boxes) start
+// from. Only what the box images use is supported: gzip or plain tar layers,
+// image indexes and manifest lists, schema-2 manifests.
+package oci
+
+import (
+ "errors"
+ "fmt"
+ "strings"
+)
+
+// Ref is a parsed image reference.
+type Ref struct {
+ Registry string // host[:port] as the API is reached (registry-1.docker.io for Docker Hub)
+ Repo string // library/ubuntu, omnirush-ai/box-base
+ Tag string
+ Digest string // sha256:..., when pinned
+}
+
+// String is the reference as written (registry/repo[:tag][@digest]).
+func (r Ref) String() string {
+ host := r.Registry
+ if host == "registry-1.docker.io" {
+ host = "docker.io"
+ }
+ s := host + "/" + r.Repo
+ if r.Tag != "" {
+ s += ":" + r.Tag
+ }
+ if r.Digest != "" {
+ s += "@" + r.Digest
+ }
+ return s
+}
+
+// Reference is what the manifest is fetched by: the digest when pinned.
+func (r Ref) Reference() string {
+ if r.Digest != "" {
+ return r.Digest
+ }
+ if r.Tag != "" {
+ return r.Tag
+ }
+ return "latest"
+}
+
+// Plain reports whether the registry is reached over plain HTTP: loopback
+// only (a local registry for tests), as Docker does by default.
+func (r Ref) Plain() bool {
+ host := r.Registry
+ if i := strings.LastIndex(host, ":"); i > 0 && !strings.HasSuffix(host, "]") {
+ host = host[:i]
+ }
+ return host == "localhost" || host == "127.0.0.1" || host == "[::1]"
+}
+
+// ParseRef reads docker-style references: ubuntu, ubuntu:24.04,
+// ghcr.io/omnirush-ai/box-base:2026.10.1@sha256:..., localhost:5000/x/y.
+func ParseRef(s string) (Ref, error) {
+ s = strings.TrimSpace(s)
+ if s == "" {
+ return Ref{}, errors.New("empty image reference")
+ }
+ var r Ref
+ if i := strings.Index(s, "@"); i >= 0 {
+ r.Digest = s[i+1:]
+ s = s[:i]
+ if !ValidDigest(r.Digest) {
+ return Ref{}, fmt.Errorf("bad digest %q", r.Digest)
+ }
+ }
+ first := s
+ rest := ""
+ if i := strings.Index(s, "/"); i >= 0 {
+ first, rest = s[:i], s[i+1:]
+ }
+ if rest != "" && (strings.ContainsAny(first, ".:") || first == "localhost") {
+ r.Registry = first
+ s = rest
+ } else {
+ r.Registry = "registry-1.docker.io"
+ }
+ if r.Registry == "docker.io" || r.Registry == "index.docker.io" {
+ r.Registry = "registry-1.docker.io"
+ }
+ if i := strings.LastIndex(s, ":"); i >= 0 && !strings.Contains(s[i:], "/") {
+ r.Tag = s[i+1:]
+ s = s[:i]
+ }
+ if r.Registry == "registry-1.docker.io" && !strings.Contains(s, "/") {
+ s = "library/" + s
+ }
+ if s == "" || strings.ContainsAny(s, " \t@") || s != strings.ToLower(s) {
+ return Ref{}, fmt.Errorf("bad repository %q", s)
+ }
+ r.Repo = s
+ if r.Tag == "" && r.Digest == "" {
+ r.Tag = "latest"
+ }
+ return r, nil
+}
+
+// ValidDigest reports a sha256 digest (the only algorithm the boxes use).
+func ValidDigest(d string) bool {
+ if !strings.HasPrefix(d, "sha256:") || len(d) != 71 {
+ return false
+ }
+ for _, c := range d[7:] {
+ if !(c >= '0' && c <= '9' || c >= 'a' && c <= 'f') {
+ return false
+ }
+ }
+ return true
+}
diff --git a/apps/desktop/box/go/oci/registry.go b/apps/desktop/box/go/oci/registry.go
new file mode 100644
index 00000000..f32a9202
--- /dev/null
+++ b/apps/desktop/box/go/oci/registry.go
@@ -0,0 +1,460 @@
+package oci
+
+import (
+ "context"
+ "crypto/sha256"
+ "encoding/hex"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "io"
+ "net/http"
+ "net/url"
+ "os"
+ "strings"
+ "sync"
+ "time"
+)
+
+// Media types the boxes use.
+const (
+ MediaOCIIndex = "application/vnd.oci.image.index.v1+json"
+ MediaDockerList = "application/vnd.docker.distribution.manifest.list.v2+json"
+ MediaOCIManifest = "application/vnd.oci.image.manifest.v1+json"
+ MediaDockerSchema2 = "application/vnd.docker.distribution.manifest.v2+json"
+)
+
+// Descriptor points at a blob or manifest.
+type Descriptor struct {
+ MediaType string `json:"mediaType"`
+ Digest string `json:"digest"`
+ Size int64 `json:"size"`
+ Platform *Platform `json:"platform,omitempty"`
+}
+
+// Platform of an image (linux/amd64, linux/arm64/v8).
+type Platform struct {
+ OS string `json:"os"`
+ Architecture string `json:"architecture"`
+ Variant string `json:"variant,omitempty"`
+}
+
+type manifest struct {
+ SchemaVersion int `json:"schemaVersion"`
+ MediaType string `json:"mediaType"`
+ Config Descriptor `json:"config"`
+ Layers []Descriptor `json:"layers"`
+ Manifests []Descriptor `json:"manifests"`
+}
+
+// Config is the part of an image config a box needs.
+type Config struct {
+ Architecture string `json:"architecture"`
+ OS string `json:"os"`
+ Config struct {
+ Env []string `json:"Env"`
+ WorkingDir string `json:"WorkingDir"`
+ User string `json:"User"`
+ Entrypoint []string `json:"Entrypoint"`
+ Cmd []string `json:"Cmd"`
+ Labels map[string]string `json:"Labels"`
+ } `json:"config"`
+}
+
+// Image is a resolved image: what to download, and what it was resolved from.
+type Image struct {
+ Ref string `json:"ref"`
+ Digest string `json:"digest"` // the pinned / top-level digest (index or manifest)
+ Manifest string `json:"manifest"` // the platform manifest's digest
+ Config string `json:"config"` // the config blob's digest (the image id)
+ Arch string `json:"arch"`
+ Layers []Descriptor `json:"layers"`
+ Env []string `json:"env"`
+ WorkDir string `json:"workdir,omitempty"`
+ User string `json:"user,omitempty"`
+ Labels map[string]string `json:"labels,omitempty"`
+}
+
+// Client talks to registries (anonymous; OMNIRUSH_BOX_REGISTRY_AUTH=user:token
+// for a private one).
+type Client struct {
+ HTTP *http.Client
+ Auth string
+ UserAgent string
+
+ mu sync.Mutex
+ tokens map[string]string
+}
+
+// NewClient with the defaults the CLI uses.
+func NewClient() *Client {
+ return &Client{
+ HTTP: &http.Client{Timeout: 0, Transport: &http.Transport{Proxy: http.ProxyFromEnvironment, ResponseHeaderTimeout: 60 * time.Second, IdleConnTimeout: 30 * time.Second}},
+ Auth: os.Getenv("OMNIRUSH_BOX_REGISTRY_AUTH"),
+ UserAgent: "omnirush-boxrun",
+ }
+}
+
+func (c *Client) base(ref Ref) string {
+ scheme := "https"
+ if ref.Plain() {
+ scheme = "http"
+ }
+ return scheme + "://" + ref.Registry + "/v2/" + ref.Repo
+}
+
+// do sends a GET, answering a Bearer challenge once.
+func (c *Client) do(ctx context.Context, ref Ref, u string, accept []string) (*http.Response, error) {
+ for attempt := 0; attempt < 2; attempt++ {
+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
+ if err != nil {
+ return nil, err
+ }
+ if len(accept) > 0 {
+ req.Header.Set("Accept", strings.Join(accept, ", "))
+ }
+ req.Header.Set("User-Agent", c.UserAgent)
+ c.mu.Lock()
+ tok := c.tokens[ref.Registry+"/"+ref.Repo]
+ c.mu.Unlock()
+ if tok != "" {
+ req.Header.Set("Authorization", "Bearer "+tok)
+ }
+ resp, err := c.HTTP.Do(req)
+ if err != nil {
+ return nil, err
+ }
+ if resp.StatusCode == http.StatusUnauthorized && attempt == 0 {
+ challenge := resp.Header.Get("WWW-Authenticate")
+ resp.Body.Close()
+ tok, err := c.token(ctx, ref, challenge)
+ if err != nil {
+ return nil, err
+ }
+ c.mu.Lock()
+ if c.tokens == nil {
+ c.tokens = map[string]string{}
+ }
+ c.tokens[ref.Registry+"/"+ref.Repo] = tok
+ c.mu.Unlock()
+ continue
+ }
+ if resp.StatusCode != http.StatusOK {
+ body, _ := io.ReadAll(io.LimitReader(resp.Body, 512))
+ resp.Body.Close()
+ return nil, fmt.Errorf("%s: HTTP %d %s", redactURL(u), resp.StatusCode, strings.TrimSpace(string(body)))
+ }
+ return resp, nil
+ }
+ return nil, errors.New("registry refused the token")
+}
+
+func redactURL(u string) string {
+ if p, err := url.Parse(u); err == nil {
+ p.RawQuery = ""
+ return p.String()
+ }
+ return u
+}
+
+// ParseChallenge reads `Bearer realm="...",service="...",scope="..."`.
+func ParseChallenge(h string) (scheme string, params map[string]string) {
+ params = map[string]string{}
+ h = strings.TrimSpace(h)
+ if i := strings.IndexByte(h, ' '); i > 0 {
+ scheme, h = strings.ToLower(h[:i]), h[i+1:]
+ } else {
+ return strings.ToLower(h), params
+ }
+ for h != "" {
+ h = strings.TrimLeft(h, " ,")
+ eq := strings.IndexByte(h, '=')
+ if eq <= 0 {
+ break
+ }
+ key := strings.ToLower(strings.TrimSpace(h[:eq]))
+ h = h[eq+1:]
+ var val string
+ if strings.HasPrefix(h, `"`) {
+ end := strings.IndexByte(h[1:], '"')
+ if end < 0 {
+ val, h = h[1:], ""
+ } else {
+ val, h = h[1:1+end], h[2+end:]
+ }
+ } else {
+ end := strings.IndexByte(h, ',')
+ if end < 0 {
+ val, h = h, ""
+ } else {
+ val, h = h[:end], h[end:]
+ }
+ }
+ params[key] = val
+ }
+ return scheme, params
+}
+
+func (c *Client) token(ctx context.Context, ref Ref, challenge string) (string, error) {
+ scheme, p := ParseChallenge(challenge)
+ if scheme != "bearer" || p["realm"] == "" {
+ return "", fmt.Errorf("registry wants %q auth", scheme)
+ }
+ u, err := url.Parse(p["realm"])
+ if err != nil {
+ return "", err
+ }
+ if u.Scheme != "https" && !ref.Plain() {
+ return "", errors.New("registry token realm is not https")
+ }
+ q := u.Query()
+ if p["service"] != "" {
+ q.Set("service", p["service"])
+ }
+ scope := p["scope"]
+ if scope == "" {
+ scope = "repository:" + ref.Repo + ":pull"
+ }
+ q.Set("scope", scope)
+ u.RawQuery = q.Encode()
+ req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
+ if err != nil {
+ return "", err
+ }
+ req.Header.Set("User-Agent", c.UserAgent)
+ if c.Auth != "" {
+ if user, pass, ok := strings.Cut(c.Auth, ":"); ok {
+ req.SetBasicAuth(user, pass)
+ }
+ }
+ resp, err := c.HTTP.Do(req)
+ if err != nil {
+ return "", err
+ }
+ defer resp.Body.Close()
+ if resp.StatusCode != http.StatusOK {
+ return "", fmt.Errorf("registry token: HTTP %d", resp.StatusCode)
+ }
+ var body struct {
+ Token string `json:"token"`
+ AccessToken string `json:"access_token"`
+ }
+ if err := json.NewDecoder(io.LimitReader(resp.Body, 1<<20)).Decode(&body); err != nil {
+ return "", err
+ }
+ if body.Token != "" {
+ return body.Token, nil
+ }
+ if body.AccessToken != "" {
+ return body.AccessToken, nil
+ }
+ return "", errors.New("registry token: empty")
+}
+
+func (c *Client) getManifest(ctx context.Context, ref Ref, reference string) ([]byte, string, string, error) {
+ resp, err := c.do(ctx, ref, c.base(ref)+"/manifests/"+reference, []string{MediaOCIIndex, MediaDockerList, MediaOCIManifest, MediaDockerSchema2})
+ if err != nil {
+ return nil, "", "", err
+ }
+ defer resp.Body.Close()
+ body, err := io.ReadAll(io.LimitReader(resp.Body, 4<<20))
+ if err != nil {
+ return nil, "", "", err
+ }
+ sum := sha256.Sum256(body)
+ digest := "sha256:" + hex.EncodeToString(sum[:])
+ if strings.HasPrefix(reference, "sha256:") && reference != digest {
+ return nil, "", "", fmt.Errorf("manifest digest mismatch: asked %s, got %s", reference, digest)
+ }
+ mt := resp.Header.Get("Content-Type")
+ if i := strings.IndexByte(mt, ';'); i >= 0 {
+ mt = mt[:i]
+ }
+ return body, digest, strings.TrimSpace(mt), nil
+}
+
+// ArchOf maps Node/Go arch names to OCI ones.
+func ArchOf(arch string) string {
+ switch arch {
+ case "x64", "x86_64", "amd64":
+ return "amd64"
+ case "arm64", "aarch64":
+ return "arm64"
+ }
+ return arch
+}
+
+// PickPlatform chooses a manifest of an index for linux/<arch>.
+func PickPlatform(list []Descriptor, arch string) (Descriptor, error) {
+ arch = ArchOf(arch)
+ for _, d := range list {
+ if d.Platform == nil || d.Platform.OS != "linux" || d.Platform.Architecture != arch {
+ continue
+ }
+ if arch == "arm64" && d.Platform.Variant != "" && d.Platform.Variant != "v8" {
+ continue
+ }
+ return d, nil
+ }
+ have := []string{}
+ for _, d := range list {
+ if d.Platform != nil {
+ have = append(have, d.Platform.OS+"/"+d.Platform.Architecture)
+ }
+ }
+ return Descriptor{}, fmt.Errorf("no linux/%s image (has %s)", arch, strings.Join(have, ", "))
+}
+
+// Resolve reads the manifests and config of ref for linux/arch.
+func (c *Client) Resolve(ctx context.Context, ref Ref, arch string) (*Image, error) {
+ body, digest, mt, err := c.getManifest(ctx, ref, ref.Reference())
+ if err != nil {
+ return nil, err
+ }
+ var m manifest
+ if err := json.Unmarshal(body, &m); err != nil {
+ return nil, fmt.Errorf("manifest: %w", err)
+ }
+ if mt == "" {
+ mt = m.MediaType
+ }
+ img := &Image{Ref: ref.String(), Digest: digest, Manifest: digest}
+ if mt == MediaOCIIndex || mt == MediaDockerList || len(m.Manifests) > 0 {
+ pick, err := PickPlatform(m.Manifests, arch)
+ if err != nil {
+ return nil, err
+ }
+ body, mdigest, _, err := c.getManifest(ctx, ref, pick.Digest)
+ if err != nil {
+ return nil, err
+ }
+ m = manifest{}
+ if err := json.Unmarshal(body, &m); err != nil {
+ return nil, fmt.Errorf("manifest: %w", err)
+ }
+ img.Manifest = mdigest
+ }
+ if m.SchemaVersion != 2 || m.Config.Digest == "" {
+ return nil, errors.New("unsupported manifest (schema 2 only)")
+ }
+ cfgBytes, err := c.blobBytes(ctx, ref, m.Config.Digest, 8<<20)
+ if err != nil {
+ return nil, fmt.Errorf("config: %w", err)
+ }
+ if err := fillConfig(img, cfgBytes, m.Config.Digest); err != nil {
+ return nil, err
+ }
+ img.Layers = m.Layers
+ for _, l := range img.Layers {
+ if _, err := LayerCompression(l.MediaType); err != nil {
+ return nil, err
+ }
+ }
+ return img, nil
+}
+
+func fillConfig(img *Image, cfgBytes []byte, digest string) error {
+ var cfg Config
+ if err := json.Unmarshal(cfgBytes, &cfg); err != nil {
+ return fmt.Errorf("config: %w", err)
+ }
+ if cfg.OS != "" && cfg.OS != "linux" {
+ return fmt.Errorf("image is for %s, not linux", cfg.OS)
+ }
+ img.Config = digest
+ img.Arch = cfg.Architecture
+ img.Env = cfg.Config.Env
+ img.WorkDir = cfg.Config.WorkingDir
+ img.User = cfg.Config.User
+ img.Labels = cfg.Config.Labels
+ return nil
+}
+
+// LayerCompression says how a layer is stored: "gzip" or "" (plain tar).
+func LayerCompression(mediaType string) (string, error) {
+ switch {
+ case strings.HasSuffix(mediaType, "+gzip") || strings.HasSuffix(mediaType, ".tar.gzip"):
+ return "gzip", nil
+ case strings.HasSuffix(mediaType, "+zstd"):
+ return "", errors.New("zstd image layers are not supported yet")
+ case strings.HasSuffix(mediaType, ".tar") || strings.HasSuffix(mediaType, "layer.v1.tar") || mediaType == "":
+ return "", nil
+ case strings.Contains(mediaType, "foreign") || strings.Contains(mediaType, "nondistributable"):
+ return "", fmt.Errorf("foreign layer %s", mediaType)
+ }
+ return "", fmt.Errorf("unknown layer type %s", mediaType)
+}
+
+func (c *Client) blobBytes(ctx context.Context, ref Ref, digest string, max int64) ([]byte, error) {
+ rc, err := c.OpenBlob(ctx, ref, digest)
+ if err != nil {
+ return nil, err
+ }
+ defer rc.Close()
+ data, err := io.ReadAll(io.LimitReader(rc, max))
+ if err != nil {
+ return nil, err
+ }
+ return data, rc.Close()
+}
+
+// OpenBlob streams a blob; Close reports a digest mismatch once it was read to the end.
+func (c *Client) OpenBlob(ctx context.Context, ref Ref, digest string) (io.ReadCloser, error) {
+ if !ValidDigest(digest) {
+ return nil, fmt.Errorf("bad digest %q", digest)
+ }
+ resp, err := c.do(ctx, ref, c.base(ref)+"/blobs/"+digest, nil)
+ if err != nil {
+ return nil, err
+ }
+ return NewVerifier(resp.Body, digest), nil
+}
+
+// Verifier hashes what is read; Close (or a read to EOF) checks the digest.
+type Verifier struct {
+ r io.ReadCloser
+ want string
+ h hashWriter
+ eof bool
+ closed bool
+}
+
+type hashWriter interface {
+ io.Writer
+ Sum([]byte) []byte
+}
+
+// NewVerifier wraps r.
+func NewVerifier(r io.ReadCloser, digest string) *Verifier {
+ return &Verifier{r: r, want: digest, h: sha256.New()}
+}
+
+func (v *Verifier) Read(p []byte) (int, error) {
+ n, err := v.r.Read(p)
+ if n > 0 {
+ v.h.Write(p[:n])
+ }
+ if errors.Is(err, io.EOF) {
+ v.eof = true
+ if got := "sha256:" + hex.EncodeToString(v.h.Sum(nil)); got != v.want {
+ return n, fmt.Errorf("digest mismatch: want %s, got %s", v.want, got)
+ }
+ }
+ return n, err
+}
+
+// Close checks the digest when the blob was read to the end.
+func (v *Verifier) Close() error {
+ if v.closed {
+ return nil
+ }
+ v.closed = true
+ err := v.r.Close()
+ if !v.eof {
+ return err
+ }
+ if got := "sha256:" + hex.EncodeToString(v.h.Sum(nil)); got != v.want {
+ return fmt.Errorf("digest mismatch: want %s, got %s", v.want, got)
+ }
+ return err
+}
diff --git a/apps/desktop/box/go/oci/store.go b/apps/desktop/box/go/oci/store.go
new file mode 100644
index 00000000..96298da2
--- /dev/null
+++ b/apps/desktop/box/go/oci/store.go
@@ -0,0 +1,470 @@
+//go:build linux
+
+package oci
+
+import (
+ "archive/tar"
+ "context"
+ "crypto/rand"
+ "crypto/sha256"
+ "encoding/hex"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "io"
+ "os"
+ "path"
+ "path/filepath"
+ goruntime "runtime"
+ "strings"
+ "syscall"
+ "time"
+)
+
+// Store keeps unpacked images: <dir>/images/<id>/{rootfs/,image.json}.
+type Store struct {
+ Dir string
+}
+
+// ImageID names an unpacked image: the digest it was pinned by, per arch.
+func ImageID(digest, arch string) string {
+ return strings.TrimPrefix(digest, "sha256:")[:32] + "-" + ArchOf(arch)
+}
+
+// Paths of an image.
+func (s Store) imageDir(id string) string { return filepath.Join(s.Dir, "images", id) }
+
+// RootFS of an unpacked image.
+func (s Store) RootFS(id string) string { return filepath.Join(s.imageDir(id), "rootfs") }
+
+// Lookup returns an unpacked image's record, or nil.
+func (s Store) Lookup(id string) *Image {
+ data, err := os.ReadFile(filepath.Join(s.imageDir(id), "image.json"))
+ if err != nil {
+ return nil
+ }
+ var img Image
+ if json.Unmarshal(data, &img) != nil {
+ return nil
+ }
+ if _, err := os.Stat(s.RootFS(id)); err != nil {
+ return nil
+ }
+ return &img
+}
+
+// Find returns the unpacked image for a reference (by digest when pinned,
+// else by the last tag pulled) for arch, or nil.
+func (s Store) Find(ref Ref, arch string) (*Image, string) {
+ if ref.Digest != "" {
+ id := ImageID(ref.Digest, arch)
+ if img := s.Lookup(id); img != nil {
+ return img, id
+ }
+ return nil, ""
+ }
+ data, err := os.ReadFile(s.tagFile(ref, arch))
+ if err != nil {
+ return nil, ""
+ }
+ id := strings.TrimSpace(string(data))
+ if img := s.Lookup(id); img != nil {
+ return img, id
+ }
+ return nil, ""
+}
+
+func (s Store) tagFile(ref Ref, arch string) string {
+ sum := sha256.Sum256([]byte(ref.Registry + "/" + ref.Repo + ":" + ref.Tag + "|" + ArchOf(arch)))
+ return filepath.Join(s.Dir, "tags", hex.EncodeToString(sum[:12]))
+}
+
+// lock takes an exclusive lock for an image id (one unpack at a time).
+func (s Store) lock(id string) (func(), error) {
+ dir := filepath.Join(s.Dir, "locks")
+ if err := os.MkdirAll(dir, 0o700); err != nil {
+ return nil, err
+ }
+ f, err := os.OpenFile(filepath.Join(dir, id+".lock"), os.O_CREATE|os.O_RDWR, 0o600)
+ if err != nil {
+ return nil, err
+ }
+ if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX); err != nil {
+ f.Close()
+ return nil, err
+ }
+ return func() { _ = syscall.Flock(int(f.Fd()), syscall.LOCK_UN); f.Close() }, nil
+}
+
+func (s Store) tmpDir() (string, error) {
+ b := make([]byte, 6)
+ _, _ = rand.Read(b)
+ dir := filepath.Join(s.Dir, "tmp", hex.EncodeToString(b))
+ return dir, os.MkdirAll(dir, 0o700)
+}
+
+// Progress hears each layer as it is unpacked.
+type Progress func(layer int, of int, digest string, bytes int64)
+
+// commit writes image.json into the unpacked folder and moves it in place.
+func (s Store) commit(tmp, id string, img *Image) error {
+ data, _ := json.MarshalIndent(img, "", " ")
+ if err := os.WriteFile(filepath.Join(tmp, "image.json"), append(data, '\n'), 0o644); err != nil {
+ return err
+ }
+ dest := s.imageDir(id)
+ if err := os.MkdirAll(filepath.Dir(dest), 0o755); err != nil {
+ return err
+ }
+ if _, err := os.Stat(dest); err == nil {
+ // Another unpack of the same image won: keep it.
+ go removeAllForce(tmp)
+ return nil
+ }
+ return os.Rename(tmp, dest)
+}
+
+// Pull resolves ref for linux/arch, downloads and unpacks it (each layer
+// verified against its digest) unless already there. Returns the image and its id.
+func (s Store) Pull(ctx context.Context, c *Client, ref Ref, arch string, opt ApplyOptions, progress Progress) (*Image, string, error) {
+ if img, id := s.Find(ref, arch); img != nil {
+ return img, id, nil
+ }
+ img, err := c.Resolve(ctx, ref, arch)
+ if err != nil {
+ return nil, "", err
+ }
+ top := img.Digest
+ if ref.Digest != "" {
+ top = ref.Digest
+ }
+ id := ImageID(top, arch)
+ unlock, err := s.lock(id)
+ if err != nil {
+ return nil, "", err
+ }
+ defer unlock()
+ if got := s.Lookup(id); got != nil {
+ _ = s.rememberTag(ref, arch, id)
+ return got, id, nil
+ }
+ tmp, err := s.tmpDir()
+ if err != nil {
+ return nil, "", err
+ }
+ ok := false
+ defer func() {
+ if !ok {
+ _ = removeAllForce(tmp)
+ }
+ }()
+ u := &Unpacker{Root: filepath.Join(tmp, "rootfs"), Opt: opt}
+ if err := os.MkdirAll(u.Root, 0o755); err != nil {
+ return nil, "", err
+ }
+ for i, layer := range img.Layers {
+ var lastErr error
+ for attempt := 0; attempt < 3; attempt++ {
+ if attempt > 0 {
+ time.Sleep(time.Duration(attempt*2) * time.Second)
+ }
+ lastErr = s.applyBlob(ctx, c, ref, u, layer.Digest)
+ if lastErr == nil || ctx.Err() != nil {
+ break
+ }
+ }
+ if lastErr != nil {
+ return nil, "", fmt.Errorf("layer %d/%d: %w", i+1, len(img.Layers), lastErr)
+ }
+ if progress != nil {
+ progress(i+1, len(img.Layers), layer.Digest, layer.Size)
+ }
+ }
+ u.Finish()
+ img.Digest = top
+ if err := s.commit(tmp, id, img); err != nil {
+ return nil, "", err
+ }
+ ok = true
+ _ = s.rememberTag(ref, arch, id)
+ return img, id, nil
+}
+
+// applyBlob streams one layer into the unpacker. A retry after a failure
+// half-way re-applies the whole layer, which is idempotent.
+func (s Store) applyBlob(ctx context.Context, c *Client, ref Ref, u *Unpacker, digest string) error {
+ rc, err := c.OpenBlob(ctx, ref, digest)
+ if err != nil {
+ return err
+ }
+ defer rc.Close()
+ if _, err := u.Apply(rc); err != nil {
+ return err
+ }
+ // Read to the end so the digest is checked.
+ if _, err := io.Copy(io.Discard, rc); err != nil {
+ return err
+ }
+ return rc.Close()
+}
+
+func (s Store) rememberTag(ref Ref, arch, id string) error {
+ if ref.Digest != "" || ref.Tag == "" {
+ return nil
+ }
+ file := s.tagFile(ref, arch)
+ if err := os.MkdirAll(filepath.Dir(file), 0o700); err != nil {
+ return err
+ }
+ return os.WriteFile(file, []byte(id+"\n"), 0o600)
+}
+
+// Import unpacks an image from a local archive: `docker save` output (with
+// or without an OCI layout inside), an OCI layout tar, or a plain root
+// filesystem tar (gzip or not; the WSL distro's). `as` is the reference to
+// file it under (a rootfs tar has no digest: its sha256 is used).
+func (s Store) Import(file, as, arch string, opt ApplyOptions) (*Image, string, error) {
+ tmp, err := s.tmpDir()
+ if err != nil {
+ return nil, "", err
+ }
+ ok := false
+ defer func() {
+ if !ok {
+ _ = removeAllForce(tmp)
+ }
+ }()
+ u := &Unpacker{Root: filepath.Join(tmp, "rootfs"), Opt: opt}
+ if err := os.MkdirAll(u.Root, 0o755); err != nil {
+ return nil, "", err
+ }
+ img, err := importArchive(file, u)
+ if err != nil {
+ return nil, "", err
+ }
+ u.Finish()
+ if as != "" {
+ img.Ref = as
+ }
+ if img.Arch == "" {
+ img.Arch = ArchOf(arch)
+ }
+ id := ImageID(img.Digest, img.Arch)
+ if got := s.Lookup(id); got != nil {
+ return got, id, nil
+ }
+ if err := s.commit(tmp, id, img); err != nil {
+ return nil, "", err
+ }
+ ok = true
+ if as != "" {
+ if ref, err := ParseRef(as); err == nil {
+ _ = s.rememberTag(ref, img.Arch, id)
+ }
+ }
+ return img, id, nil
+}
+
+// archiveIndex lists the members of an archive (name -> offset is not
+// kept: members are read by re-scanning, which is fine for a one-time import).
+func readMember(file, name string, max int64) ([]byte, error) {
+ f, err := os.Open(file)
+ if err != nil {
+ return nil, err
+ }
+ defer f.Close()
+ r, closeFn, err := Decompress(f)
+ if err != nil {
+ return nil, err
+ }
+ defer closeFn()
+ tr := tar.NewReader(r)
+ want := path.Clean(name)
+ for {
+ hdr, err := tr.Next()
+ if errors.Is(err, io.EOF) {
+ return nil, os.ErrNotExist
+ }
+ if err != nil {
+ return nil, err
+ }
+ if path.Clean(strings.TrimPrefix(hdr.Name, "./")) == want {
+ return io.ReadAll(io.LimitReader(tr, max))
+ }
+ }
+}
+
+// applyMember streams one member of the archive (a layer) into the unpacker.
+func applyMember(file, name, digest string, u *Unpacker) error {
+ f, err := os.Open(file)
+ if err != nil {
+ return err
+ }
+ defer f.Close()
+ outer, closeFn, err := Decompress(f)
+ if err != nil {
+ return err
+ }
+ defer closeFn()
+ tr := tar.NewReader(outer)
+ want := path.Clean(name)
+ for {
+ hdr, err := tr.Next()
+ if errors.Is(err, io.EOF) {
+ return fmt.Errorf("%s: not in the archive", name)
+ }
+ if err != nil {
+ return err
+ }
+ if path.Clean(strings.TrimPrefix(hdr.Name, "./")) != want {
+ continue
+ }
+ var r io.Reader = tr
+ var v *Verifier
+ if digest != "" {
+ v = NewVerifier(io.NopCloser(tr), digest)
+ r = v
+ }
+ if _, err := u.Apply(r); err != nil {
+ return err
+ }
+ if v != nil {
+ if _, err := io.Copy(io.Discard, v); err != nil {
+ return err
+ }
+ return v.Close()
+ }
+ return nil
+ }
+}
+
+func importArchive(file string, u *Unpacker) (*Image, error) {
+ // An OCI layout (index.json), as `docker save` writes since 25.0.
+ if data, err := readMember(file, "index.json", 4<<20); err == nil {
+ var idx manifest
+ if json.Unmarshal(data, &idx) == nil && len(idx.Manifests) > 0 {
+ return importLayout(file, idx, u)
+ }
+ }
+ // The older `docker save` format (manifest.json).
+ if data, err := readMember(file, "manifest.json", 4<<20); err == nil {
+ var list []struct {
+ Config string `json:"Config"`
+ RepoTags []string `json:"RepoTags"`
+ Layers []string `json:"Layers"`
+ }
+ if json.Unmarshal(data, &list) == nil && len(list) > 0 && list[0].Config != "" {
+ entry := list[0]
+ cfg, err := readMember(file, entry.Config, 8<<20)
+ if err != nil {
+ return nil, err
+ }
+ sum := sha256.Sum256(cfg)
+ img := &Image{Digest: "sha256:" + hex.EncodeToString(sum[:])}
+ if len(entry.RepoTags) > 0 {
+ img.Ref = entry.RepoTags[0]
+ }
+ if err := fillConfig(img, cfg, img.Digest); err != nil {
+ return nil, err
+ }
+ img.Manifest = img.Digest
+ for _, layer := range entry.Layers {
+ if err := applyMember(file, layer, "", u); err != nil {
+ return nil, err
+ }
+ img.Layers = append(img.Layers, Descriptor{Digest: "", MediaType: "application/vnd.docker.image.rootfs.diff.tar"})
+ }
+ return img, nil
+ }
+ }
+ // A plain root filesystem tar: one layer, its own sha256 as the id.
+ f, err := os.Open(file)
+ if err != nil {
+ return nil, err
+ }
+ h := sha256.New()
+ if _, err := u.Apply(io.TeeReader(f, h)); err != nil {
+ f.Close()
+ return nil, err
+ }
+ _, _ = io.Copy(h, f)
+ f.Close()
+ digest := "sha256:" + hex.EncodeToString(h.Sum(nil))
+ return &Image{Digest: digest, Manifest: digest, Config: digest, Env: []string{"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"}}, nil
+}
+
+func importLayout(file string, idx manifest, u *Unpacker) (*Image, error) {
+ blob := func(d string) string { return "blobs/sha256/" + strings.TrimPrefix(d, "sha256:") }
+ top := idx.Manifests[0]
+ img := &Image{Digest: top.Digest}
+ data, err := readMember(file, blob(top.Digest), 4<<20)
+ if err != nil {
+ return nil, err
+ }
+ var m manifest
+ if err := json.Unmarshal(data, &m); err != nil {
+ return nil, err
+ }
+ if len(m.Manifests) > 0 {
+ pick, err := PickPlatform(m.Manifests, hostArch())
+ if err != nil {
+ pick = m.Manifests[0]
+ }
+ if data, err = readMember(file, blob(pick.Digest), 4<<20); err != nil {
+ return nil, err
+ }
+ m = manifest{}
+ if err := json.Unmarshal(data, &m); err != nil {
+ return nil, err
+ }
+ img.Manifest = pick.Digest
+ } else {
+ img.Manifest = top.Digest
+ }
+ cfg, err := readMember(file, blob(m.Config.Digest), 8<<20)
+ if err != nil {
+ return nil, err
+ }
+ if err := fillConfig(img, cfg, m.Config.Digest); err != nil {
+ return nil, err
+ }
+ for _, layer := range m.Layers {
+ if _, err := LayerCompression(layer.MediaType); err != nil {
+ return nil, err
+ }
+ if err := applyMember(file, blob(layer.Digest), layer.Digest, u); err != nil {
+ return nil, err
+ }
+ }
+ img.Layers = m.Layers
+ return img, nil
+}
+
+func hostArch() string { return goruntime.GOARCH }
+
+// FindArchive returns the image a local archive reference was unpacked as.
+func (s Store) FindArchive(ref string) (*Image, string) {
+ data, err := os.ReadFile(s.archiveFile(ref))
+ if err != nil {
+ return nil, ""
+ }
+ id := strings.TrimSpace(string(data))
+ if img := s.Lookup(id); img != nil {
+ return img, id
+ }
+ return nil, ""
+}
+
+// RememberArchive files an archive reference under the image it unpacked to.
+func (s Store) RememberArchive(ref, id string) error {
+ file := s.archiveFile(ref)
+ if err := os.MkdirAll(filepath.Dir(file), 0o700); err != nil {
+ return err
+ }
+ return os.WriteFile(file, []byte(id+"\n"), 0o600)
+}
+
+func (s Store) archiveFile(ref string) string {
+ sum := sha256.Sum256([]byte(ref))
+ return filepath.Join(s.Dir, "tags", "archive-"+hex.EncodeToString(sum[:12]))
+}