Skip to content

Commit 1fa80ea

Browse files
committed
WIP: oci: buildkit TMPDIR fallback
1 parent 5422be4 commit 1fa80ea

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

cmd/singularity-buildkitd/main.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111

1212
bkdaemon "github.com/sylabs/singularity/v4/internal/pkg/build/buildkit/daemon"
1313
"github.com/sylabs/singularity/v4/internal/pkg/buildcfg"
14+
fsoverlay "github.com/sylabs/singularity/v4/internal/pkg/util/fs/overlay"
15+
"github.com/sylabs/singularity/v4/pkg/syfs"
1416
"github.com/sylabs/singularity/v4/pkg/sylog"
1517
"github.com/sylabs/singularity/v4/pkg/util/singularityconf"
1618
)
@@ -33,10 +35,37 @@ func main() {
3335
}
3436
singularityconf.SetCurrentConfig(config)
3537

38+
if err := run(bkSocket, bkArch); err != nil {
39+
sylog.Fatalf("%s: %v", bkdaemon.DaemonName, err)
40+
}
41+
}
42+
43+
func run(bkSocket, bkArch string) error {
3644
daemonOpts := &bkdaemon.Opts{
3745
ReqArch: bkArch,
3846
}
39-
if err := bkdaemon.Run(context.Background(), daemonOpts, bkSocket); err != nil {
40-
sylog.Fatalf("%s: %v", bkdaemon.DaemonName, err)
47+
48+
// Check the user .singularity dir is in a location supporting overlayfs etc. If not, use a tmpdir.
49+
if err := fsoverlay.CheckUpper(syfs.ConfigDir()); err != nil {
50+
tmpRoot, err := os.MkdirTemp("", bkdaemon.DaemonName)
51+
if err != nil {
52+
sylog.Fatalf("%s: while creating temporary root dir: %v", bkdaemon.DaemonName, err)
53+
}
54+
defer func() {
55+
sylog.Warningf("%s: removing temporary directory %s", bkdaemon.DaemonName, tmpRoot)
56+
err := os.RemoveAll(tmpRoot)
57+
if err != nil {
58+
sylog.Errorf("%s: while removing temp dir: %v", bkdaemon.DaemonName, err)
59+
}
60+
}()
61+
62+
if err := fsoverlay.CheckUpper(tmpRoot); err != nil {
63+
sylog.Fatalf("%s: temporary directory does not support buildkit. Please set $TMPDIR to a local filesystem.", bkdaemon.DaemonName)
64+
}
65+
66+
sylog.Warningf("%s: ~/.singularity filesystem does not support buildkit. Using temporary directory %s. Layers will not be cached for future builds.", bkdaemon.DaemonName, tmpRoot)
67+
daemonOpts.TempRootDir = tmpRoot
4168
}
69+
70+
return bkdaemon.Run(context.Background(), daemonOpts, bkSocket)
4271
}

internal/pkg/build/buildkit/client/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import (
5454
const (
5555
buildTag = "tag"
5656
bkDefaultSocket = "unix:///run/buildkit/buildkitd.sock"
57-
bkLaunchTimeout = 120 * time.Second
57+
bkLaunchTimeout = 10 * time.Second
5858
bkShutdownTimeout = 10 * time.Second
5959
bkMinVersion = "v0.12.3"
6060
)

internal/pkg/build/buildkit/daemon/daemon.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ const DaemonName = "singularity-buildkitd"
8080
type Opts struct {
8181
// Requested build architecture
8282
ReqArch string
83+
// Override the location of the singularity-buildkitd root with a temporary directory (no GC)
84+
TempRootDir string
8385
}
8486

8587
type workerInitializerOpt struct {
@@ -149,7 +151,7 @@ func waitLock(ctx context.Context, lockPath string) (*flock.Flock, error) {
149151
func Run(ctx context.Context, opts *Opts, socketPath string) error {
150152
// If we need to, enter a new cgroup now, to workaround an issue with crun container cgroup creation (#1538).
151153
if err := oci.CrunNestCgroup(); err != nil {
152-
sylog.Fatalf("%s: while applying crun cgroup workaround: %v", DaemonName, err)
154+
return fmt.Errorf("%s: while applying crun cgroup workaround: %v", DaemonName, err)
153155
}
154156

155157
cfg, err := config.LoadFile(defaultConfigPath())
@@ -166,6 +168,14 @@ func Run(ctx context.Context, opts *Opts, socketPath string) error {
166168

167169
server := grpc.NewServer()
168170

171+
if opts.TempRootDir != "" {
172+
ptr := func(v bool) *bool {
173+
return &v
174+
}
175+
cfg.Root = opts.TempRootDir
176+
cfg.Workers.OCI.GC = ptr(false)
177+
}
178+
169179
// relative path does not work with nightlyone/lockfile
170180
root, err := filepath.Abs(cfg.Root)
171181
if err != nil {
@@ -181,7 +191,7 @@ func Run(ctx context.Context, opts *Opts, socketPath string) error {
181191
sylog.Debugf("%s: path for buildkitd lock file: %s", DaemonName, lockPath)
182192
lock, err := waitLock(ctx, lockPath)
183193
if err != nil {
184-
sylog.Fatalf("%s: while creating lock file: %v", DaemonName, err)
194+
return fmt.Errorf("%s: while creating lock file: %v", DaemonName, err)
185195
}
186196
defer func() {
187197
lock.Unlock()

0 commit comments

Comments
 (0)