Skip to content

Commit 63252df

Browse files
authored
firecracker: simplify chroot provisioning (#4868)
1 parent 28f2fe2 commit 63252df

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
diff --git a/jailer.go b/jailer.go
2+
index e261fae..a5e055d 100644
3+
--- a/jailer.go
4+
+++ b/jailer.go
5+
@@ -413,10 +413,19 @@ func LinkFilesHandler(kernelImageFileName string) Handler {
6+
}
7+
8+
// copy all drives to the root fs
9+
+ rootfsPrefix := rootfs + string(os.PathSeparator)
10+
for i, drive := range m.Cfg.Drives {
11+
hostPath := StringValue(drive.PathOnHost)
12+
- driveFileName := filepath.Base(hostPath)
13+
14+
+ // If the provided host path is already within the rootfs then just
15+
+ // update the drive path to be rootfs-relative.
16+
+ if strings.HasPrefix(hostPath, rootfsPrefix) {
17+
+ rootfsRelativePath := strings.TrimPrefix(hostPath, rootfsPrefix)
18+
+ m.Cfg.Drives[i].PathOnHost = String(rootfsRelativePath)
19+
+ continue
20+
+ }
21+
+
22+
+ driveFileName := filepath.Base(hostPath)
23+
if err := os.Link(
24+
hostPath,
25+
filepath.Join(rootfs, driveFileName),

deps.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,12 @@ def install_go_mod_dependencies(workspace_name = "buildbuddy"):
15911591
go_repository(
15921592
name = "com_github_firecracker_microvm_firecracker_go_sdk",
15931593
importpath = "github.com/firecracker-microvm/firecracker-go-sdk",
1594+
patch_args = ["-p1"],
1595+
# TODO(bduffany): when
1596+
# https://github.com/firecracker-microvm/firecracker-go-sdk/pull/510 is
1597+
# merged, cherry-pick the final revision into our fork, and remove this
1598+
# patch.
1599+
patches = ["@{}//buildpatches:com_github_firecracker_microvm_firecracker_go_sdk_jailer.patch".format(workspace_name)],
15941600
replace = "github.com/buildbuddy-io/firecracker-go-sdk",
15951601
sum = "h1:Wx6fPNZOs0SJ9NpTsLbJsItORvEM9D94k/vr8ZwIBEg=",
15961602
version = "v0.0.0-20230721-1d5c50b",

enterprise/server/remote_execution/containers/firecracker/firecracker.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,6 @@ type FirecrackerContainer struct {
361361
containerImage string // the OCI container image. ex "alpine:latest"
362362
actionWorkingDir string // the action directory with inputs / outputs
363363
containerFSPath string // the path to the container ext4 image
364-
tempDir string // path for writing disk images before the chroot is created
365364
user string // user to execute all commands as
366365

367366
rmOnce *sync.Once
@@ -1634,27 +1633,12 @@ func (c *FirecrackerContainer) create(ctx context.Context) error {
16341633
c.rmOnce = &sync.Once{}
16351634
c.rmErr = nil
16361635

1637-
var err error
1638-
c.tempDir, err = os.MkdirTemp(c.jailerRoot, "fc-container-*")
1639-
if err != nil {
1640-
return err
1641-
}
16421636
if err := os.MkdirAll(c.getChroot(), 0755); err != nil {
16431637
return status.InternalErrorf("failed to create chroot dir: %s", err)
16441638
}
16451639

16461640
scratchFSPath := c.scratchFSPath()
16471641
workspaceFSPath := c.workspaceFSPath()
1648-
1649-
// When mounting the workspace image directly as a block device (rather than
1650-
// as an NBD), the firecracker go SDK expects the disk images to be outside
1651-
// the chroot, and will move them to the chroot for us. So we place them in
1652-
// a temp dir so that the SDK doesn't complain that the chroot paths already
1653-
// exist when it tries to create them.
1654-
if !*enableNBD {
1655-
scratchFSPath = filepath.Join(c.tempDir, scratchFSName)
1656-
workspaceFSPath = filepath.Join(c.tempDir, workspaceFSName)
1657-
}
16581642
if *EnableRootfs {
16591643
if err := c.initRootfsStore(ctx); err != nil {
16601644
return status.WrapError(err, "create root image")
@@ -1670,7 +1654,6 @@ func (c *FirecrackerContainer) create(ctx context.Context) error {
16701654
if err := c.createWorkspaceImage(ctx, "" /*=workspaceDir*/, workspaceFSPath); err != nil {
16711655
return err
16721656
}
1673-
log.CtxDebugf(ctx, "Scratch and workspace disk images written to %q", c.tempDir)
16741657
log.CtxDebugf(ctx, "Using container image at %q", c.containerFSPath)
16751658
log.CtxDebugf(ctx, "getChroot() is %q", c.getChroot())
16761659
fcCfg, err := c.getConfig(ctx, c.containerFSPath, scratchFSPath, workspaceFSPath)
@@ -1969,12 +1952,6 @@ func (c *FirecrackerContainer) remove(ctx context.Context) error {
19691952
log.CtxErrorf(ctx, "Error cleaning up networking: %s", err)
19701953
lastErr = err
19711954
}
1972-
if c.tempDir != "" {
1973-
if err := os.RemoveAll(c.tempDir); err != nil {
1974-
log.CtxErrorf(ctx, "Error removing workspace fs: %s", err)
1975-
lastErr = err
1976-
}
1977-
}
19781955
if err := os.RemoveAll(filepath.Dir(c.getChroot())); err != nil {
19791956
log.CtxErrorf(ctx, "Error removing chroot: %s", err)
19801957
lastErr = err

0 commit comments

Comments
 (0)