Skip to content

Commit b5f3df0

Browse files
authored
Merge branch 'main' into zero-fetch
2 parents 00dc390 + 1ce9c2f commit b5f3df0

File tree

3 files changed

+86
-51
lines changed

3 files changed

+86
-51
lines changed

packages/orchestrator/internal/sandbox/fc/client.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/firecracker-microvm/firecracker-go-sdk"
99
"github.com/go-openapi/strfmt"
1010

11-
"github.com/e2b-dev/infra/packages/orchestrator/internal/sandbox/socket"
1211
"github.com/e2b-dev/infra/packages/orchestrator/internal/sandbox/template"
1312
"github.com/e2b-dev/infra/packages/orchestrator/internal/sandbox/uffd/memory"
1413
"github.com/e2b-dev/infra/packages/shared/pkg/fc/client"
@@ -43,13 +42,6 @@ func (c *apiClient) loadSnapshot(
4342
ctx, span := tracer.Start(ctx, "load-snapshot")
4443
defer span.End()
4544

46-
err := socket.Wait(ctx, uffdSocketPath)
47-
if err != nil {
48-
return fmt.Errorf("error waiting for uffd socket: %w", err)
49-
}
50-
51-
telemetry.ReportEvent(ctx, "uffd socket ready")
52-
5345
backendType := models.MemoryBackendBackendTypeUffd
5446
backend := &models.MemoryBackend{
5547
BackendPath: &uffdSocketPath,
@@ -70,7 +62,7 @@ func (c *apiClient) loadSnapshot(
7062
},
7163
}
7264

73-
_, err = c.client.Operations.LoadSnapshot(&snapshotConfig)
65+
_, err := c.client.Operations.LoadSnapshot(&snapshotConfig)
7466
if err != nil {
7567
return fmt.Errorf("error loading snapshot: %w", err)
7668
}

packages/orchestrator/internal/sandbox/fc/process.go

Lines changed: 83 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ import (
1616
"go.opentelemetry.io/otel/trace"
1717
"go.uber.org/zap"
1818
"go.uber.org/zap/zapio"
19+
"golang.org/x/sync/errgroup"
1920

2021
"github.com/e2b-dev/infra/packages/orchestrator/internal/cfg"
2122
"github.com/e2b-dev/infra/packages/orchestrator/internal/sandbox/network"
23+
"github.com/e2b-dev/infra/packages/orchestrator/internal/sandbox/rootfs"
2224
"github.com/e2b-dev/infra/packages/orchestrator/internal/sandbox/socket"
2325
"github.com/e2b-dev/infra/packages/orchestrator/internal/sandbox/template"
2426
"github.com/e2b-dev/infra/packages/shared/pkg/logger"
@@ -62,11 +64,11 @@ type Process struct {
6264
config cfg.BuilderConfig
6365
firecrackerSocketPath string
6466

65-
slot *network.Slot
66-
providerRootfsPath string
67-
rootfsPath string
68-
kernelPath string
69-
files *storage.SandboxFiles
67+
slot *network.Slot
68+
rootfsProvider rootfs.Provider
69+
rootfsPath string
70+
kernelPath string
71+
files *storage.SandboxFiles
7072

7173
Exit *utils.ErrorOnce
7274

@@ -80,7 +82,7 @@ func NewProcess(
8082
slot *network.Slot,
8183
files *storage.SandboxFiles,
8284
versions Config,
83-
rootfsProviderPath string,
85+
rootfsProvider rootfs.Provider,
8486
rootfsPaths RootfsPaths,
8587
) (*Process, error) {
8688
ctx, childSpan := tracer.Start(ctx, "initialize-fc", trace.WithAttributes(
@@ -129,7 +131,7 @@ func NewProcess(
129131
firecrackerSocketPath: files.SandboxFirecrackerSocketPath(),
130132
config: config,
131133
client: newApiClient(files.SandboxFirecrackerSocketPath()),
132-
providerRootfsPath: rootfsProviderPath,
134+
rootfsProvider: rootfsProvider,
133135
files: files,
134136
slot: slot,
135137

@@ -161,12 +163,7 @@ func (p *Process) configure(
161163
}
162164
p.cmd.Stderr = io.MultiWriter(stderrWriters...)
163165

164-
err := utils.SymlinkForce("/dev/null", p.files.SandboxCacheRootfsLinkPath(p.config.StorageConfig))
165-
if err != nil {
166-
return fmt.Errorf("error symlinking rootfs: %w", err)
167-
}
168-
169-
err = p.cmd.Start()
166+
err := p.cmd.Start()
170167
if err != nil {
171168
return fmt.Errorf("error starting fc process: %w", err)
172169
}
@@ -227,7 +224,13 @@ func (p *Process) Create(
227224
ctx, childSpan := tracer.Start(ctx, "create-fc")
228225
defer childSpan.End()
229226

230-
err := p.configure(
227+
// Symlink /dev/null to the rootfs link path, so we can start the FC process without the rootfs and then symlink the real rootfs.
228+
err := utils.SymlinkForce("/dev/null", p.files.SandboxCacheRootfsLinkPath(p.config.StorageConfig))
229+
if err != nil {
230+
return fmt.Errorf("error symlinking rootfs: %w", err)
231+
}
232+
233+
err = p.configure(
231234
ctx,
232235
sbxMetadata,
233236
options.Stdout,
@@ -290,10 +293,21 @@ func (p *Process) Create(
290293
telemetry.ReportEvent(ctx, "set fc boot source config")
291294

292295
// Rootfs
293-
err = utils.SymlinkForce(p.providerRootfsPath, p.files.SandboxCacheRootfsLinkPath(p.config.StorageConfig))
296+
rootfsPath, err := p.rootfsProvider.Path()
294297
if err != nil {
295-
return fmt.Errorf("error symlinking rootfs: %w", err)
298+
fcStopErr := p.Stop(ctx)
299+
300+
return errors.Join(fmt.Errorf("error getting rootfs path: %w", err), fcStopErr)
296301
}
302+
telemetry.ReportEvent(ctx, "got rootfs path")
303+
304+
err = utils.SymlinkForce(rootfsPath, p.files.SandboxCacheRootfsLinkPath(p.config.StorageConfig))
305+
if err != nil {
306+
fcStopErr := p.Stop(ctx)
307+
308+
return errors.Join(fmt.Errorf("error symlinking rootfs: %w", err), fcStopErr)
309+
}
310+
telemetry.ReportEvent(ctx, "symlinked rootfs")
297311

298312
err = p.client.setRootfsDrive(ctx, p.rootfsPath, options.IoEngine)
299313
if err != nil {
@@ -351,24 +365,64 @@ func (p *Process) Resume(
351365
ctx, span := tracer.Start(ctx, "resume-fc")
352366
defer span.End()
353367

354-
err := p.configure(
355-
ctx,
356-
sbxMetadata,
357-
nil,
358-
nil,
359-
)
368+
// Symlink /dev/null to the rootfs link path, so we can start the FC process without the rootfs and then symlink the real rootfs.
369+
err := utils.SymlinkForce("/dev/null", p.files.SandboxCacheRootfsLinkPath(p.config.StorageConfig))
360370
if err != nil {
361-
fcStopErr := p.Stop(ctx)
362-
363-
return errors.Join(fmt.Errorf("error starting fc process: %w", err), fcStopErr)
371+
return fmt.Errorf("error symlinking rootfs: %w", err)
364372
}
365373

366-
err = utils.SymlinkForce(p.providerRootfsPath, p.files.SandboxCacheRootfsLinkPath(p.config.StorageConfig))
374+
// create errgroup with context that handled socket wait + rootfs symlink
375+
eg, egCtx := errgroup.WithContext(ctx)
376+
377+
eg.Go(func() error {
378+
err := p.configure(
379+
egCtx,
380+
sbxMetadata,
381+
nil,
382+
nil,
383+
)
384+
if err != nil {
385+
return fmt.Errorf("error starting fc process: %w", err)
386+
}
387+
388+
telemetry.ReportEvent(egCtx, "configured fc")
389+
390+
return nil
391+
})
392+
393+
eg.Go(func() error {
394+
err := socket.Wait(egCtx, uffdSocketPath)
395+
if err != nil {
396+
return fmt.Errorf("error waiting for uffd socket: %w", err)
397+
}
398+
399+
telemetry.ReportEvent(egCtx, "uffd socket ready")
400+
401+
return nil
402+
})
403+
404+
eg.Go(func() error {
405+
rootfsPath, err := p.rootfsProvider.Path()
406+
if err != nil {
407+
return fmt.Errorf("error getting rootfs path: %w", err)
408+
}
409+
410+
err = utils.SymlinkForce(rootfsPath, p.files.SandboxCacheRootfsLinkPath(p.config.StorageConfig))
411+
if err != nil {
412+
return fmt.Errorf("error symlinking rootfs: %w", err)
413+
}
414+
415+
telemetry.ReportEvent(egCtx, "symlinked rootfs")
416+
417+
return nil
418+
})
419+
420+
err = eg.Wait()
367421
if err != nil {
368-
return fmt.Errorf("error symlinking rootfs: %w", err)
369-
}
422+
fcStopErr := p.Stop(ctx)
370423

371-
telemetry.ReportEvent(ctx, "symlinked rootfs")
424+
return errors.Join(fmt.Errorf("error waiting for uffd socket or symlinking rootfs: %w", err), fcStopErr)
425+
}
372426

373427
err = p.client.loadSnapshot(
374428
ctx,

packages/orchestrator/internal/sandbox/sandbox.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,6 @@ func (f *Factory) CreateSandbox(
249249
}
250250

251251
// / ==== END of resources initialization ====
252-
rootfsPath, err := rootfsProvider.Path()
253-
if err != nil {
254-
return nil, fmt.Errorf("failed to get rootfs path: %w", err)
255-
}
256252
ips := <-ipsCh
257253
if ips.err != nil {
258254
return nil, fmt.Errorf("failed to get network slot: %w", ips.err)
@@ -265,7 +261,7 @@ func (f *Factory) CreateSandbox(
265261
ips.slot,
266262
sandboxFiles,
267263
config.FirecrackerConfig,
268-
rootfsPath,
264+
rootfsProvider,
269265
fc.ConstantRootfsPaths,
270266
)
271267
if err != nil {
@@ -455,13 +451,6 @@ func (f *Factory) ResumeSandbox(
455451
cancelUffdStartCtx(fmt.Errorf("uffd process exited: %w", errors.Join(uffdWaitErr, context.Cause(uffdStartCtx))))
456452
}()
457453

458-
rootfsPath, err := rootfsOverlay.Path()
459-
if err != nil {
460-
return nil, fmt.Errorf("failed to get rootfs path: %w", err)
461-
}
462-
463-
telemetry.ReportEvent(ctx, "got rootfs path")
464-
465454
ips := <-ipsCh
466455
if ips.err != nil {
467456
return nil, fmt.Errorf("failed to get network slot: %w", ips.err)
@@ -484,7 +473,7 @@ func (f *Factory) ResumeSandbox(
484473
sandboxFiles,
485474
// The versions need to base exactly the same as the paused sandbox template because of the FC compatibility.
486475
config.FirecrackerConfig,
487-
rootfsPath,
476+
rootfsOverlay,
488477
fc.RootfsPaths{
489478
TemplateVersion: meta.Version,
490479
TemplateID: config.BaseTemplateID,

0 commit comments

Comments
 (0)