Skip to content

Commit

Permalink
provide an option to use shellbang, not symlinks, for busybox
Browse files Browse the repository at this point in the history
Not all kernels support symlinks, and not all file systems
on all kernels do. Symlinks are not as portable as we want.
For example, a simple u-root on a VFAT file system can not
be created.

It turns out there is a way to use shellbang files that is as
efficient as symlinks and much more portable.

Add an option to the busybox builder to generate shellbang files,
not symlinks.

TODO: update the tests. But this change is known to work on Plan 9.

Question: should we just move to shellbang files
only, given their more universal nature, or keep it as an option?

Signed-off-by: Ronald G. Minnich <[email protected]>
  • Loading branch information
rminnich committed Jan 3, 2021
1 parent 2280e52 commit 5de057e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
17 changes: 14 additions & 3 deletions pkg/uroot/builder/bb.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ var skip = map[string]struct{}{
//
// See bb/README.md for a detailed explanation of the implementation of busybox
// mode.
type BBBuilder struct{}
type BBBuilder struct {

// ShellBang means generate #! files instead of symlinks.
// ShellBang are more portable and just as efficient.
ShellBang bool
}

// DefaultBinaryDir implements Builder.DefaultBinaryDir.
//
Expand All @@ -41,7 +46,7 @@ func (BBBuilder) DefaultBinaryDir() string {
}

// Build is an implementation of Builder.Build for a busybox-like initramfs.
func (BBBuilder) Build(af *initramfs.Files, opts Opts) error {
func (b BBBuilder) Build(af *initramfs.Files, opts Opts) error {
// Build the busybox binary.
bbPath := filepath.Join(opts.TempDir, "bb")
if err := bb.BuildBusybox(opts.Env, opts.Packages, opts.NoStrip, bbPath); err != nil {
Expand All @@ -63,7 +68,13 @@ func (BBBuilder) Build(af *initramfs.Files, opts Opts) error {
}

// Add a symlink /bbin/{cmd} -> /bbin/bb to our initramfs.
if err := af.AddRecord(cpio.Symlink(filepath.Join(opts.BinaryDir, path.Base(pkg)), "bb")); err != nil {
// Or add a #! file if b.ShellBang is set ...
if b.ShellBang {
b := path.Base(pkg)
if err := af.AddRecord(cpio.StaticFile(filepath.Join(opts.BinaryDir, b), "#!/bbin/bb #!"+b+"\n", 0755)); err != nil {
return err
}
} else if err := af.AddRecord(cpio.Symlink(filepath.Join(opts.BinaryDir, path.Base(pkg)), "bb")); err != nil {
return err
}
}
Expand Down
4 changes: 3 additions & 1 deletion u-root.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var (
noStrip *bool
statsOutputPath *string
statsLabel *string
shellbang *bool
)

func init() {
Expand Down Expand Up @@ -78,6 +79,7 @@ func init() {
flag.Var(&extraFiles, "files", "Additional files, directories, and binaries (with their ldd dependencies) to add to archive. Can be speficified multiple times.")

noStrip = flag.Bool("no-strip", false, "Build unstripped binaries")
shellbang = flag.Bool("shellbang", false, "Use #! instead of symlinks for busybox")

statsOutputPath = flag.String("stats-output-path", "", "Write build stats to this file (JSON)")

Expand Down Expand Up @@ -263,7 +265,7 @@ func Main() error {
var b builder.Builder
switch *build {
case "bb":
b = builder.BBBuilder{}
b = builder.BBBuilder{ShellBang: *shellbang}
case "binary":
b = builder.BinaryBuilder{}
case "source":
Expand Down

0 comments on commit 5de057e

Please sign in to comment.