@@ -47,14 +47,17 @@ import (
4747 "github.com/sylabs/singularity/v4/internal/pkg/ociplatform"
4848 "github.com/sylabs/singularity/v4/internal/pkg/remote/credential/ociauth"
4949 "github.com/sylabs/singularity/v4/internal/pkg/util/bin"
50+ fsoverlay "github.com/sylabs/singularity/v4/internal/pkg/util/fs/overlay"
51+ "github.com/sylabs/singularity/v4/internal/pkg/util/rootless"
52+ "github.com/sylabs/singularity/v4/pkg/syfs"
5053 "github.com/sylabs/singularity/v4/pkg/sylog"
5154 "golang.org/x/sync/errgroup"
5255)
5356
5457const (
5558 buildTag = "tag"
5659 bkDefaultSocket = "unix:///run/buildkit/buildkitd.sock"
57- bkLaunchTimeout = 120 * time .Second
60+ bkLaunchTimeout = 10 * time .Second
5861 bkShutdownTimeout = 10 * time .Second
5962 bkMinVersion = "v0.12.3"
6063)
@@ -161,13 +164,32 @@ func startBuildkitd(ctx context.Context, opts *Opts) (bkSocket string, cleanup f
161164 return "" , nil , err
162165 }
163166
164- bkSocket = generateSocketAddress ()
167+ bkSocket , err = generateSocketAddress ()
168+ if err != nil {
169+ return "" , nil , err
170+ }
171+
172+ args := []string {}
173+ tmpRoot := ""
174+ // Check the user .singularity dir is in a location supporting overlayfs etc. If not, use a tmpdir.
175+ if err := fsoverlay .CheckUpper (syfs .ConfigDir ()); err != nil {
176+ tmpRoot , err = os .MkdirTemp ("" , "singularity-buildkitd-" )
177+ if err != nil {
178+ sylog .Fatalf ("while creating singularity-buildkitd temporary root dir: %v" , err )
179+ }
180+ if err := fsoverlay .CheckUpper (tmpRoot ); err != nil {
181+ sylog .Fatalf ("Temporary directory does not support buildkit. Please set $TMPDIR to a local filesystem." )
182+ }
183+
184+ sylog .Warningf ("~/.singularity filesystem does not support buildkit. Using temporary directory %s. Layers will not be cached for future builds." , tmpRoot )
185+ args = append (args , "--root=" + tmpRoot )
186+ }
165187
166- // singularity-buildkitd <socket-uri> [architecture]
167- args := []string {bkSocket }
168188 if opts .ReqArch != "" {
169- args = append (args , opts .ReqArch )
189+ args = append (args , "--arch=" + opts .ReqArch )
170190 }
191+ args = append (args , "--socket=" + bkSocket )
192+
171193 cmd := exec .CommandContext (ctx , bkCmd , args ... )
172194 cmd .WaitDelay = bkShutdownTimeout
173195 cmd .Cancel = func () error {
@@ -182,8 +204,15 @@ func startBuildkitd(ctx context.Context, opts *Opts) (bkSocket string, cleanup f
182204 sylog .Errorf ("while canceling buildkit daemon process: %v" , err )
183205 }
184206 cmd .Wait ()
207+ if tmpRoot != "" {
208+ sylog .Warningf ("removing singularity-buildkitd temporary directory %s" , tmpRoot )
209+ if err := os .RemoveAll (tmpRoot ); err != nil {
210+ sylog .Errorf ("while removing singularity-buildkitd temp dir: %v" , err )
211+ }
212+ }
185213 }
186214
215+ sylog .Debugf ("starting %s %v" , bkCmd , args )
187216 if err := cmd .Start (); err != nil {
188217 return "" , nil , err
189218 }
@@ -378,15 +407,22 @@ func writeDockerTar(r io.Reader, outputFile *os.File) error {
378407 return err
379408}
380409
381- func generateSocketAddress () string {
410+ func generateSocketAddress () (string , error ) {
411+ uid , err := rootless .Getuid ()
412+ if err != nil {
413+ return "" , err
414+ }
415+
382416 socketPath := "/run/singularity-buildkitd"
417+ if uid == 0 {
418+ return "unix://" + filepath .Join (socketPath , fmt .Sprintf ("singularity-buildkitd-%d.sock" , os .Getpid ())), nil
419+ }
383420
384- // pam_systemd sets XDG_RUNTIME_DIR but not other dirs.
385421 xdgRuntimeDir := os .Getenv ("XDG_RUNTIME_DIR" )
386- if xdgRuntimeDir != "" {
387- dirs := strings .Split (xdgRuntimeDir , ":" )
388- socketPath = filepath .Join (dirs [0 ], "singularity-buildkitd" )
422+ if xdgRuntimeDir == "" {
423+ return "" , fmt .Errorf ("rootless build --oci requires XDG_RUNTIME_DIR is set" )
389424 }
390-
391- return "unix://" + filepath .Join (socketPath , fmt .Sprintf ("singularity-buildkitd-%d.sock" , os .Getpid ()))
425+ dirs := strings .Split (xdgRuntimeDir , ":" )
426+ socketPath = filepath .Join (dirs [0 ], "singularity-buildkitd" )
427+ return "unix://" + filepath .Join (socketPath , fmt .Sprintf ("singularity-buildkitd-%d.sock" , os .Getpid ())), nil
392428}
0 commit comments