Skip to content

Commit

Permalink
Move from ulog to llog
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Koch <[email protected]>
  • Loading branch information
hugelgupf committed Feb 13, 2024
1 parent 0d73730 commit e817c0e
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 51 deletions.
24 changes: 13 additions & 11 deletions cmd/mkuimage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"flag"
"fmt"
"log"
"log/slog"
"os"
"runtime"
"strings"
Expand All @@ -21,7 +22,7 @@ import (
"github.com/u-root/mkuimage/uroot"
"github.com/u-root/mkuimage/uroot/builder"
"github.com/u-root/mkuimage/uroot/initramfs"
"github.com/u-root/uio/ulog"
"github.com/u-root/uio/llog"
)

// multiFlag is used for flags that support multiple invocations, e.g. -files.
Expand Down Expand Up @@ -132,30 +133,31 @@ func main() {
tags := (*uflag.Strings)(&env.BuildTags)
flag.CommandLine.Var(tags, "tags", "Go build tags -- repeat the flag for multiple values")

l := llog.Default()
l.RegisterVerboseFlag(flag.CommandLine, "v", slog.LevelDebug)
flag.Parse()

l := log.New(os.Stderr, "", log.Ltime)

if usrc := os.Getenv("UROOT_SOURCE"); usrc != "" && *urootSourceDir == "" {
*urootSourceDir = usrc
}

if env.CgoEnabled {
l.Printf("Disabling CGO for u-root...")
l.Infof("Disabling CGO for u-root...")
env.CgoEnabled = false
}
l.Printf("Build environment: %s", env)
l.Infof("Build environment: %s", env)
if env.GOOS != "linux" {
l.Printf("GOOS is not linux. Did you mean to set GOOS=linux?")
l.Warnf("GOOS is not linux. Did you mean to set GOOS=linux?")
}

// Main is in a separate functions so defers run on return.
if err := Main(l, env, gbbOpts); err != nil {
l.Fatalf("Build error: %v", err)
l.Errorf("Build error: %v", err)
return
}

if stat, err := os.Stat(*outputPath); err == nil {
l.Printf("Successfully built %q (size %d bytes -- %s).", *outputPath, stat.Size(), humanize.IBytes(uint64(stat.Size())))
l.Infof("Successfully built %q (size %d bytes -- %s).", *outputPath, stat.Size(), humanize.IBytes(uint64(stat.Size())))
}
}

Expand Down Expand Up @@ -203,14 +205,14 @@ func defaultFile(env *golang.Environ) string {

// Main is a separate function so defers are run on return, which they wouldn't
// on exit.
func Main(l ulog.Logger, env *golang.Environ, buildOpts *golang.BuildOpts) error {
func Main(l *llog.Logger, env *golang.Environ, buildOpts *golang.BuildOpts) error {
v, err := env.Version()
if err != nil {
l.Printf("Could not get environment's Go version, using runtime's version: %v", err)
l.Infof("Could not get environment's Go version, using runtime's version: %v", err)
v = runtime.Version()
}
if !isRecommendedVersion(v) {
l.Printf(`WARNING: You are not using one of the recommended Go versions (have = %s, recommended = %v).
l.Warnf(`You are not using one of the recommended Go versions (have = %s, recommended = %v).
Some packages may not compile.
Go to https://golang.org/doc/install to find out how to install a newer version of Go,
or use https://godoc.org/golang.org/dl/%s to install an additional version of Go.`,
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ module github.com/u-root/mkuimage
go 1.21

require (
github.com/dustin/go-humanize v1.0.1
github.com/hugelgupf/go-shlex v0.0.0-20200702092117-c80c9d0918fa
github.com/u-root/gobusybox/src v0.0.0-20240212035024-44ff0bf359ad
github.com/u-root/u-root v0.12.0
github.com/u-root/uio v0.0.0-20240118234441-a3c409a6018e
github.com/u-root/uio v0.0.0-20240209044354-b3d14b93376a
golang.org/x/sync v0.6.0
golang.org/x/sys v0.16.0
golang.org/x/tools v0.17.0
)

require (
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/google/goterm v0.0.0-20200907032337-555d40f16ae2 // indirect
github.com/insomniacslk/dhcp v0.0.0-20231206064809-8c70d406f6d2 // indirect
github.com/josharian/native v1.1.0 // indirect
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ github.com/hugelgupf/vmtest v0.0.0-20240102225328-693afabdd27f h1:ov45/OzrJG8EKb
github.com/hugelgupf/vmtest v0.0.0-20240102225328-693afabdd27f/go.mod h1:JoDrYMZpDPYo6uH9/f6Peqms3zNNWT2XiGgioMOIGuI=
github.com/insomniacslk/dhcp v0.0.0-20231206064809-8c70d406f6d2 h1:9K06NfxkBh25x56yVhWWlKFE8YpicaSfHwoV8SFbueA=
github.com/insomniacslk/dhcp v0.0.0-20231206064809-8c70d406f6d2/go.mod h1:3A9PQ1cunSDF/1rbTq99Ts4pVnycWg+vlPkfeD2NLFI=
github.com/josharian/native v1.0.1-0.20221213033349-c1e37c09b531/go.mod h1:7X/raswPFr05uY3HiLlYeyQntB6OO7E/d2Cu7qoaN2w=
github.com/josharian/native v1.1.0 h1:uuaP0hAbW7Y4l0ZRQ6C9zfb7Mg1mbFKry/xzDAfmtLA=
github.com/josharian/native v1.1.0/go.mod h1:7X/raswPFr05uY3HiLlYeyQntB6OO7E/d2Cu7qoaN2w=
github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4=
Expand Down Expand Up @@ -47,8 +46,8 @@ github.com/u-root/gobusybox/src v0.0.0-20240212035024-44ff0bf359ad h1:lUSEFqsEuc
github.com/u-root/gobusybox/src v0.0.0-20240212035024-44ff0bf359ad/go.mod h1:vN1IwhlCo7gTDTJDUs6WCKM4/C2uiq5w0XvZCqLtb5s=
github.com/u-root/u-root v0.12.0 h1:K0AuBFriwr0w/PGS3HawiAw89e3+MU7ks80GpghAsNs=
github.com/u-root/u-root v0.12.0/go.mod h1:FYjTOh4IkIZHhjsd17lb8nYW6udgXdJhG1c0r6u0arI=
github.com/u-root/uio v0.0.0-20240118234441-a3c409a6018e h1:BA9O3BmlTmpjbvajAwzWx4Wo2TRVdpPXZEeemGQcajw=
github.com/u-root/uio v0.0.0-20240118234441-a3c409a6018e/go.mod h1:eLL9Nub3yfAho7qB0MzZizFhTU2QkLeoVsWdHtDW264=
github.com/u-root/uio v0.0.0-20240209044354-b3d14b93376a h1:BH1SOPEvehD2kVrndDnGJiUF0TrBpNs+iyYocu6h0og=
github.com/u-root/uio v0.0.0-20240209044354-b3d14b93376a/go.mod h1:P3a5rG4X7tI17Nn3aOIAYr5HbIMukwXG0urG0WuL8OA=
github.com/ulikunitz/xz v0.5.11 h1:kpFauv27b6ynzBNT/Xy+1k+fK4WswhN/6PN5WhFAGw8=
github.com/ulikunitz/xz v0.5.11/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/vishvananda/netlink v1.2.1-beta.2 h1:Llsql0lnQEbHj0I1OuKyp8otXp0r3q0mPkuhwHfStVs=
Expand All @@ -71,7 +70,6 @@ golang.org/x/sys v0.0.0-20200728102440-3e129f6d46b1/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200923182605-d9f96fdee20d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220622161953-175b2fd9d664/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
Expand Down
4 changes: 2 additions & 2 deletions uroot/builder/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/u-root/gobusybox/src/pkg/golang"
"github.com/u-root/mkuimage/uroot/initramfs"
"github.com/u-root/uio/ulog"
"github.com/u-root/uio/llog"
"golang.org/x/tools/go/packages"
)

Expand Down Expand Up @@ -49,7 +49,7 @@ func (BinaryBuilder) DefaultBinaryDir() string {
}

// Build implements Builder.Build.
func (b BinaryBuilder) Build(l ulog.Logger, af *initramfs.Files, opts Opts) error {
func (b BinaryBuilder) Build(l *llog.Logger, af *initramfs.Files, opts Opts) error {
if opts.Env == nil {
return ErrEnvMissing
}
Expand Down
6 changes: 3 additions & 3 deletions uroot/builder/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/u-root/gobusybox/src/pkg/golang"
"github.com/u-root/mkuimage/uroot/initramfs"
"github.com/u-root/uio/ulog/ulogtest"
"github.com/u-root/uio/llog"
)

func TestBinaryBuild(t *testing.T) {
Expand All @@ -26,7 +26,7 @@ func TestBinaryBuild(t *testing.T) {
}
af := initramfs.NewFiles()
var b BinaryBuilder
if err := b.Build(ulogtest.Logger{TB: t}, af, opts); err != nil {
if err := b.Build(llog.Test(t), af, opts); err != nil {
t.Fatalf("Build(%v, %v); %v != nil", af, opts, err)
}

Expand Down Expand Up @@ -83,7 +83,7 @@ func TestBinaryBuildError(t *testing.T) {
} {
af := initramfs.NewFiles()
var b BinaryBuilder
if err := b.Build(ulogtest.Logger{TB: t}, af, tt.opts); !errors.Is(err, tt.want) {
if err := b.Build(llog.Test(t), af, tt.opts); !errors.Is(err, tt.want) {
t.Errorf("Build = %v, want %v", err, tt.want)
}
}
Expand Down
4 changes: 2 additions & 2 deletions uroot/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/u-root/gobusybox/src/pkg/golang"
"github.com/u-root/mkuimage/uroot/initramfs"
"github.com/u-root/uio/ulog"
"github.com/u-root/uio/llog"
)

var (
Expand Down Expand Up @@ -62,7 +62,7 @@ type Opts struct {
type Builder interface {
// Build uses the given options to build Go packages and adds its files
// to be included in the initramfs to the given ArchiveFiles.
Build(ulog.Logger, *initramfs.Files, Opts) error
Build(*llog.Logger, *initramfs.Files, Opts) error

// DefaultBinaryDir is the initramfs' default directory for binaries
// built using this builder.
Expand Down
7 changes: 4 additions & 3 deletions uroot/builder/gbb.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ package builder
import (
"errors"
"fmt"
"log/slog"
"path"
"path/filepath"

"github.com/u-root/gobusybox/src/pkg/bb"
"github.com/u-root/mkuimage/cpio"
"github.com/u-root/mkuimage/uroot/initramfs"
"github.com/u-root/uio/ulog"
"github.com/u-root/uio/llog"
)

// Commands to skip building in bb mode.
Expand Down Expand Up @@ -47,7 +48,7 @@ func (GBBBuilder) DefaultBinaryDir() string {
}

// Build is an implementation of Builder.Build for a busybox-like initramfs.
func (b GBBBuilder) Build(l ulog.Logger, af *initramfs.Files, opts Opts) error {
func (b GBBBuilder) Build(l *llog.Logger, af *initramfs.Files, opts Opts) error {
// Build the busybox binary.
if len(opts.TempDir) == 0 {
return ErrTempDirMissing
Expand All @@ -69,7 +70,7 @@ func (b GBBBuilder) Build(l ulog.Logger, af *initramfs.Files, opts Opts) error {
BinaryPath: bbPath,
GoBuildOpts: opts.BuildOpts,
}
if err := bb.BuildBusybox(l, bopts); err != nil {
if err := bb.BuildBusybox(l.AtLevel(slog.LevelInfo), bopts); err != nil {
// Return some instructions for the user; this is printed last in the u-root tool.
//
// TODO: yeah, this isn't a good way to do error handling. The
Expand Down
6 changes: 3 additions & 3 deletions uroot/builder/gbb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/u-root/gobusybox/src/pkg/golang"
"github.com/u-root/mkuimage/cpio"
"github.com/u-root/mkuimage/uroot/initramfs"
"github.com/u-root/uio/ulog/ulogtest"
"github.com/u-root/uio/llog"
)

func TestGBBBuild(t *testing.T) {
Expand All @@ -27,7 +27,7 @@ func TestGBBBuild(t *testing.T) {
}
af := initramfs.NewFiles()
var gbb GBBBuilder
if err := gbb.Build(ulogtest.Logger{TB: t}, af, opts); err != nil {
if err := gbb.Build(llog.Test(t), af, opts); err != nil {
t.Fatalf("Build(%v, %v); %v != nil", af, opts, err)
}

Expand Down Expand Up @@ -117,7 +117,7 @@ func TestGBBBuildError(t *testing.T) {
for _, f := range tt.files {
_ = af.AddRecord(f)
}
if err := tt.gbb.Build(ulogtest.Logger{TB: t}, af, tt.opts); !errors.Is(err, tt.want) {
if err := tt.gbb.Build(llog.Test(t), af, tt.opts); !errors.Is(err, tt.want) {
t.Errorf("Build = %v, want %v", err, tt.want)
}
}
Expand Down
37 changes: 19 additions & 18 deletions uroot/uroot.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"debug/elf"
"errors"
"fmt"
"log/slog"
"os"
"path"
"path/filepath"
Expand All @@ -25,7 +26,7 @@ import (
"github.com/u-root/mkuimage/ldd"
"github.com/u-root/mkuimage/uroot/builder"
"github.com/u-root/mkuimage/uroot/initramfs"
"github.com/u-root/uio/ulog"
"github.com/u-root/uio/llog"
)

// These constants are used in DefaultRamfs.
Expand Down Expand Up @@ -247,8 +248,8 @@ func OptionsFor(mods ...Modifier) (*Opts, error) {
}

// Create creates an initramfs from the given options o.
func (o *Opts) Create(logger ulog.Logger) error {
return CreateInitramfs(logger, *o)
func (o *Opts) Create(l *llog.Logger) error {
return CreateInitramfs(l, *o)
}

// Apply modifies o with the given modifiers.
Expand Down Expand Up @@ -474,7 +475,7 @@ func WithTempDir(dir string) Modifier {
}

// Create creates an initramfs from mods specifications.
func Create(l ulog.Logger, mods ...Modifier) error {
func Create(l *llog.Logger, mods ...Modifier) error {
o, err := OptionsFor(mods...)
if err != nil {
return err
Expand All @@ -483,7 +484,7 @@ func Create(l ulog.Logger, mods ...Modifier) error {
}

// CreateInitramfs creates an initramfs built to opts' specifications.
func CreateInitramfs(logger ulog.Logger, opts Opts) error {
func CreateInitramfs(l *llog.Logger, opts Opts) error {
if _, err := os.Stat(opts.TempDir); os.IsNotExist(err) {
return fmt.Errorf("temp dir %q must exist: %w", opts.TempDir, err)
}
Expand All @@ -504,7 +505,7 @@ func CreateInitramfs(logger ulog.Logger, opts Opts) error {

// Expand commands.
for index, cmds := range opts.Commands {
paths, err := findpkg.ResolveGlobs(logger, env, lookupEnv, cmds.Packages)
paths, err := findpkg.ResolveGlobs(l.AtLevel(slog.LevelInfo), env, lookupEnv, cmds.Packages)
if err != nil {
return fmt.Errorf("%w: %w", errResolvePackage, err)
}
Expand All @@ -530,7 +531,7 @@ func CreateInitramfs(logger ulog.Logger, opts Opts) error {
TempDir: builderTmpDir,
BinaryDir: cmds.TargetDir(),
}
if err := cmds.Builder.Build(logger, files, bOpts); err != nil {
if err := cmds.Builder.Build(l, files, bOpts); err != nil {
return fmt.Errorf("error building: %v", err)
}
}
Expand All @@ -542,24 +543,24 @@ func CreateInitramfs(logger ulog.Logger, opts Opts) error {
BaseArchive: opts.BaseArchive,
UseExistingInit: opts.UseExistingInit,
}
if err := ParseExtraFiles(logger, archive.Files, opts.ExtraFiles, !opts.SkipLDD); err != nil {
if err := ParseExtraFiles(l, archive.Files, opts.ExtraFiles, !opts.SkipLDD); err != nil {
return err
}
if err := opts.addSymlinkTo(logger, archive, opts.UinitCmd, "bin/uinit"); err != nil {
if err := opts.addSymlinkTo(l, archive, opts.UinitCmd, "bin/uinit"); err != nil {
return fmt.Errorf("%w: %w", err, errUinitSymlink)
}
if len(opts.UinitArgs) > 0 {
if err := archive.AddRecord(cpio.StaticFile("etc/uinit.flags", fileflag.ArgvToFile(opts.UinitArgs), 0o444)); err != nil {
return fmt.Errorf("%w: %w", err, errUinitArgs)
}
}
if err := opts.addSymlinkTo(logger, archive, opts.InitCmd, "init"); err != nil {
if err := opts.addSymlinkTo(l, archive, opts.InitCmd, "init"); err != nil {
return fmt.Errorf("%w: %w", err, errInitSymlink)
}
if err := opts.addSymlinkTo(logger, archive, opts.DefaultShell, "bin/sh"); err != nil {
if err := opts.addSymlinkTo(l, archive, opts.DefaultShell, "bin/sh"); err != nil {
return fmt.Errorf("%w: %w", err, errDefaultshSymlink)
}
if err := opts.addSymlinkTo(logger, archive, opts.DefaultShell, "bin/defaultsh"); err != nil {
if err := opts.addSymlinkTo(l, archive, opts.DefaultShell, "bin/defaultsh"); err != nil {
return fmt.Errorf("%w: %w", err, errDefaultshSymlink)
}

Expand All @@ -579,7 +580,7 @@ var (
errUinitArgs = errors.New("could not add uinit arguments")
)

func (o *Opts) addSymlinkTo(logger ulog.Logger, archive *initramfs.Opts, command string, source string) error {
func (o *Opts) addSymlinkTo(l *llog.Logger, archive *initramfs.Opts, command string, source string) error {
if len(command) == 0 {
return nil
}
Expand All @@ -589,7 +590,7 @@ func (o *Opts) addSymlinkTo(logger ulog.Logger, archive *initramfs.Opts, command
if o.Commands != nil {
return fmt.Errorf("%w from %q to %q: %w", errSymlink, source, command, err)
}
logger.Printf("Could not create symlink from %q to %q: %v", source, command, err)
l.Errorf("Could not create symlink from %q to %q: %v", source, command, err)
return nil
}

Expand Down Expand Up @@ -639,7 +640,7 @@ func resolveCommandOrPath(cmd string, cmds []Commands) (string, error) {
// - "/home/foo" is equivalent to "/home/foo:home/foo".
//
// ParseExtraFiles will also add ldd-listed dependencies if lddDeps is true.
func ParseExtraFiles(logger ulog.Logger, archive *initramfs.Files, extraFiles []string, lddDeps bool) error {
func ParseExtraFiles(l *llog.Logger, archive *initramfs.Files, extraFiles []string, lddDeps bool) error {
var err error
// Add files from command line.
for _, file := range extraFiles {
Expand Down Expand Up @@ -694,7 +695,7 @@ func ParseExtraFiles(logger ulog.Logger, archive *initramfs.Files, extraFiles []
return nil //nolint:nilerr
}
if err = f.Close(); err != nil {
logger.Printf("WARNING: Closing ELF file %q: %v", name, err)
l.Warnf("Closing ELF file %q: %v", name, err)
}
// Pull dependencies in the case of binaries. If `path` is not
// a binary, `libs` will just be empty.
Expand All @@ -704,12 +705,12 @@ func ParseExtraFiles(logger ulog.Logger, archive *initramfs.Files, extraFiles []
}
for _, lib := range libs {
if err := archive.AddFileNoFollow(lib, lib[1:]); err != nil {
logger.Printf("WARNING: couldn't add ldd dependencies for %q: %v", lib, err)
l.Warnf("WARNING: couldn't add ldd dependencies for %q: %v", lib, err)
}
}
return nil
}); err != nil {
logger.Printf("Getting dependencies for %q: %v", src, err)
l.Warnf("Getting dependencies for %q: %v", src, err)
}
}
}
Expand Down
Loading

0 comments on commit e817c0e

Please sign in to comment.