Skip to content
Open
43 changes: 42 additions & 1 deletion packages/orchestrator/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/url"
"os"
"path/filepath"
"strings"
"testing"
"time"

Expand All @@ -18,6 +19,8 @@ import (
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/metric/noop"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"

"github.com/e2b-dev/infra/packages/orchestrator/internal/cfg"
"github.com/e2b-dev/infra/packages/orchestrator/internal/proxy"
Expand Down Expand Up @@ -120,8 +123,11 @@ func BenchmarkBaseImageLaunch(b *testing.B) {
require.NoError(b, err)
}

l, err := logger.NewDevelopmentLogger()
zc := zap.NewDevelopmentConfig()
zc.EncoderConfig.LineEnding = "\r\n"
zl, err := zc.Build(zap.WrapCore(stripWhitespace))
require.NoError(b, err)
l := logger.NewTracedLogger(zl)

sbxlogger.SetSandboxLoggerInternal(l)
// sbxlogger.SetSandboxLoggerExternal(logger)
Expand Down Expand Up @@ -394,3 +400,38 @@ func downloadKernel(b *testing.B, filename, url string) {
_, err = file.ReadFrom(response.Body)
require.NoError(b, err)
}

type whitespaceStripper struct {
wrapped zapcore.Core
}

func (w whitespaceStripper) Enabled(level zapcore.Level) bool {
return w.wrapped.Enabled(level)
}

func (w whitespaceStripper) With(fields []zapcore.Field) zapcore.Core {
return whitespaceStripper{
wrapped: w.wrapped.With(fields),
}
}

func (w whitespaceStripper) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
ce = w.wrapped.Check(ent, ce)
if ce != nil {
ce.Message = strings.TrimSpace(ce.Message)
}

return ce
}

func (w whitespaceStripper) Write(entry zapcore.Entry, fields []zapcore.Field) error {
return w.wrapped.Write(entry, fields)
}

func (w whitespaceStripper) Sync() error {
return w.wrapped.Sync()
}

func stripWhitespace(core zapcore.Core) zapcore.Core {
return &whitespaceStripper{wrapped: core}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"io"
"sort"
"strings"

containerregistry "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/tarball"
Expand All @@ -30,7 +31,7 @@ func LayerFile(filemap map[string]File) (containerregistry.Layer, error) {
for _, f := range names {
c := filemap[f]
if err := w.WriteHeader(&tar.Header{
Name: f,
Name: strings.TrimPrefix(f, "/"),
Size: int64(len(c.Bytes)),
Mode: c.Mode,
}); err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{{- /*gotype:github.com/e2b-dev/infra/packages/orchestrator/internal/template/build/core/rootfs.templateModel*/ -}}
{{ .WriteFile "etc/systemd/system/systemd-journald.service.d/override.conf" 0o644 }}
{{ .WriteFile "etc/systemd/system/systemd-networkd.service.d/override.conf" 0o644 }}

[Service]
WatchdogSec=0
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{{- /*gotype:github.com/e2b-dev/infra/packages/orchestrator/internal/template/build/core/rootfs.templateModel*/ -}}
{{ .WriteFile "/etc/systemd/system/envd.service" 0o644 }}

[Unit]
Description=Env Daemon Service
After=multi-user.target

[Service]
Type=simple
Restart=always
User=root
Group=root
Environment=GOTRACEBACK=all
LimitCORE=infinity
ExecStart=/bin/bash -l -c "/usr/bin/envd"
OOMPolicy=continue
OOMScoreAdjust=-1000
Nice=-10
Environment="GOMEMLIMIT={{ .MemoryLimit }}MiB"

[Install]
WantedBy=multi-user.target
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{- /*gotype:github.com/e2b-dev/infra/packages/orchestrator/internal/template/build/core/rootfs.templateModel*/ -}}
{{ .WriteFile "/etc/hostname" 0o644 }}

{{ .Hostname }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{- /*gotype:github.com/e2b-dev/infra/packages/orchestrator/internal/template/build/core/rootfs.templateModel*/ -}}
{{ .WriteFile "/etc/hosts" 0o644 }}

127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00:: ip6-localnet
ff00:: ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
127.0.1.1 {{ .Hostname }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{{- /*gotype:github.com/e2b-dev/infra/packages/orchestrator/internal/template/build/core/rootfs.templateModel*/ -}}
{{ .WriteFile "/etc/inittab" 0o777 }}

# Run system init
::sysinit:/etc/init.d/rcS

# Run the provision script, prefix the output with a log prefix
::wait:/bin/sh -c '{{ .ProvisionScriptPath }} 2>&1 | sed "s/^/{{ .ProvisionLogPrefix }}/"'

# Flush filesystem changes to disk
::wait:/usr/bin/busybox sync
::wait:fsfreeze --freeze /

# Report the exit code of the provisioning script
::wait:/bin/sh -c 'echo "{{ .ProvisionExitPrefix }}$(cat {{ .ProvisionResultPath }} || printf 1)"'

# Wait forever to prevent the VM from exiting until the sandbox is paused and snapshot is taken
::wait:/usr/bin/busybox sleep infinity
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{{- /*gotype:github.com/e2b-dev/infra/packages/orchestrator/internal/template/build/core/rootfs.templateModel*/ -}}
{{ .WriteFile .ProvisionScriptPath 0o777 }}
#!/bin/sh
set -eu

BUSYBOX="{{ .BusyBox }}"
RESULT_PATH="{{ .ResultPath }}"
BUSYBOX="{{ .BusyBoxPath }}"
RESULT_PATH="{{ .ProvisionResultPath }}"

echo "Starting provisioning script"

Expand Down Expand Up @@ -103,7 +105,7 @@ echo "Finished provisioning script"

# Delete itself
rm -rf /etc/init.d/rcS
rm -rf /usr/local/bin/provision.sh
rm -f {{ .ProvisionScriptPath }}

# Report successful provisioning
printf "0" > "$RESULT_PATH"
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{{- /*gotype:github.com/e2b-dev/infra/packages/orchestrator/internal/template/build/core/rootfs.templateModel*/ -}}
{{ .WriteFile "etc/init.d/rcS" 0o777 }}

#!/usr/bin/busybox ash
echo "Mounting essential filesystems"
# Ensure necessary mount points exist
mkdir -p /proc /sys /dev /tmp /run

# Mount essential filesystems
mount -t proc proc /proc
mount -t sysfs sysfs /sys
mount -t devtmpfs devtmpfs /dev
mount -t tmpfs tmpfs /tmp
mount -t tmpfs tmpfs /run

echo "System Init"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{- /*gotype:github.com/e2b-dev/infra/packages/orchestrator/internal/template/build/core/rootfs.templateModel*/ -}}
{{ .WriteFile "/etc/resolv.conf" 0o644 }}

nameserver 8.8.8.8
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{{- /*gotype:github.com/e2b-dev/infra/packages/orchestrator/internal/template/build/core/rootfs.templateModel*/ -}}
{{ .WriteFile "/etc/systemd/system/[email protected]/autologin.conf" 0o644 }}

[Service]
ExecStart=
ExecStart=-/sbin/agetty --noissue --autologin root %I 115200,38400,9600 vt102
Loading