Skip to content

Commit

Permalink
Keep temporary directory in case of failure
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Koch <[email protected]>
  • Loading branch information
hugelgupf committed Feb 23, 2024
1 parent cbbc9b1 commit e63d04b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
22 changes: 15 additions & 7 deletions cmd/mkuimage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/dustin/go-humanize"
"github.com/u-root/gobusybox/src/pkg/golang"
"github.com/u-root/mkuimage/uimage"
"github.com/u-root/mkuimage/uimage/builder"
"github.com/u-root/mkuimage/uimage/uflags"
"github.com/u-root/uio/llog"
)
Expand Down Expand Up @@ -137,19 +138,20 @@ func Main(l *llog.Logger, env *golang.Environ, f *uflags.Flags) error {
v, recommendedVersions, recommendedVersions[0])
}

keepTempDir := f.KeepTempDir
if f.TempDir == "" {
var err error
f.TempDir, err = os.MkdirTemp("", "u-root")
if err != nil {
return err
}
if f.KeepTempDir {
defer func() {
defer func() {
if keepTempDir {
l.Infof("Keeping temp dir %s", f.TempDir)
}()
} else {
defer os.RemoveAll(f.TempDir)
}
} else {
os.RemoveAll(f.TempDir)
}
}()
} else if _, err := os.Stat(f.TempDir); os.IsNotExist(err) {
if err := os.MkdirAll(f.TempDir, 0o755); err != nil {
return fmt.Errorf("temporary directory %q did not exist; tried to mkdir but failed: %v", f.TempDir, err)
Expand All @@ -166,5 +168,11 @@ func Main(l *llog.Logger, env *golang.Environ, f *uflags.Flags) error {
if err != nil {
return err
}
return uimage.Create(l, append(m, more...)...)

err = uimage.Create(l, append(m, more...)...)
if errors.Is(err, builder.ErrBusyboxFailed) {
l.Errorf("Preserving temp dir due to busybox build error")
keepTempDir = true
}
return err
}
1 change: 1 addition & 0 deletions uimage/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var (

// Possible build errors.
var (
ErrBusyboxFailed = errors.New("gobusybox build failed")
ErrEnvMissing = errors.New("must specify Go build environment")
ErrTempDirMissing = errors.New("must supply temporary directory for build")
ErrNoGoFiles = errors.New("could not find package directory")
Expand Down
6 changes: 3 additions & 3 deletions uimage/builder/gbb.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ func (b *GBBBuilder) Build(l *llog.Logger, af *initramfs.Files, opts Opts) error
var errGopath *bb.ErrGopathBuild
var errGomod *bb.ErrModuleBuild
if errors.As(err, &errGopath) {
return fmt.Errorf("preserving bb generated source directory at %s due to error. To reproduce build, `cd %s` and `GO111MODULE=off GOPATH=%s go build`: %w", opts.TempDir, errGopath.CmdDir, errGopath.GOPATH, err)
return fmt.Errorf("%w: to reproduce build, `cd %s` and `GO111MODULE=off GOPATH=%s go build`: %w", ErrBusyboxFailed, errGopath.CmdDir, errGopath.GOPATH, err)
}
if errors.As(err, &errGomod) {
return fmt.Errorf("preserving bb generated source directory at %s due to error. To debug build, `cd %s` and use `go build` to build, or `go mod [why|tidy|graph]` to debug dependencies, or `go list -m all` to list all dependency versions:\n%w", opts.TempDir, errGomod.CmdDir, err)
return fmt.Errorf("%w: to debug build, `cd %s` and use `go build` to build, or `go mod [why|tidy|graph]` to debug dependencies, or `go list -m all` to list all dependency versions:\n%w", ErrBusyboxFailed, errGomod.CmdDir, err)
}
return fmt.Errorf("preserving bb generated source directory at %s due to error:\n%w", opts.TempDir, err)
return fmt.Errorf("%w: %w", ErrBusyboxFailed, err)
}

if err := af.AddFile(bbPath, "bbin/bb"); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion uimage/uimage.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ func CreateInitramfs(l *llog.Logger, opts Opts) error {
BinaryDir: cmds.TargetDir(),
}
if err := cmds.Builder.Build(l, files, bOpts); err != nil {
return fmt.Errorf("error building: %v", err)
return fmt.Errorf("error building: %w", err)
}
}

Expand Down

0 comments on commit e63d04b

Please sign in to comment.