Skip to content

Commit

Permalink
Small initramfs API improvement & Go 1.22 testing (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugelgupf authored Feb 8, 2024
2 parents 36692a5 + 5e0d7a2 commit 0fc63c9
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 27 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
name: Cross-platform builds
strategy:
matrix:
go-version: ['1.21.x']
go-version: ['1.21.x', '1.22.x']
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -35,7 +35,7 @@ jobs:
name: Build and test
strategy:
matrix:
go-version: ['1.21.x']
go-version: ['1.21.x', '1.22.x']
platform: [ubuntu-latest]

runs-on: ${{ matrix.platform }}
Expand Down Expand Up @@ -68,7 +68,7 @@ jobs:
name: Race test
strategy:
matrix:
go-version: ['1.21.x']
go-version: ['1.21.x', '1.22.x']
platform: [ubuntu-latest]

runs-on: ${{ matrix.platform }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version: '1.21.x'
go-version: '1.22.x'
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
Expand Down
10 changes: 6 additions & 4 deletions cmd/mkuimage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ func main() {
var recommendedVersions = []string{
"go1.20",
"go1.21",
"go1.22",
}

func isRecommendedVersion(v string) bool {
Expand Down Expand Up @@ -275,12 +276,13 @@ func Main(l ulog.Logger, env *golang.Environ, buildOpts *golang.BuildOpts) error

// Open the target initramfs file.
if *outputPath == "" {
if len(env.GOOS) == 0 && len(env.GOARCH) == 0 {
return fmt.Errorf("passed no path, GOOS, and GOARCH to CPIOArchiver.OpenWriter")
*outputPath, err = archiver.CreateDefault(env)
if err != nil {
return err
}
*outputPath = fmt.Sprintf("/tmp/initramfs.%s_%s.cpio", env.GOOS, env.GOARCH)
l.Printf("Output path is %v", *outputPath)
}
w, err := archiver.OpenWriter(l, *outputPath)
w, err := archiver.OpenWriter(*outputPath)
if err != nil {
return err
}
Expand Down
7 changes: 5 additions & 2 deletions uroot/initramfs/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"fmt"
"io"

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

var (
Expand All @@ -35,8 +35,11 @@ var (
// Archiver is an archive format that builds an archive using a given set of
// files.
type Archiver interface {
// CreateDefault creates a default file that can be passed as path to OpenWriter.
CreateDefault(env *golang.Environ) (string, error)

// OpenWriter opens an archive writer at `path`.
OpenWriter(l ulog.Logger, path string) (Writer, error)
OpenWriter(path string) (Writer, error)

// Reader returns a Reader that allows reading files from a file.
Reader(file io.ReaderAt) Reader
Expand Down
13 changes: 11 additions & 2 deletions uroot/initramfs/cpio.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,27 @@ import (
"io"
"os"

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

// CPIOArchiver is an implementation of Archiver for the cpio format.
type CPIOArchiver struct {
cpio.RecordFormat
}

// CreateDefault chooses /tmp/initramfs.cpio or /tmp/initramfs.GOOS_GOARCH.cpio
// if available.
func (ca CPIOArchiver) CreateDefault(env *golang.Environ) (string, error) {
if len(env.GOOS) == 0 || len(env.GOARCH) == 0 {
return "/tmp/initramfs.cpio", nil
}
return fmt.Sprintf("/tmp/initramfs.%s_%s.cpio", env.GOOS, env.GOARCH), nil
}

// OpenWriter opens `path` as the correct file type and returns an
// Writer pointing to `path`.
func (ca CPIOArchiver) OpenWriter(l ulog.Logger, path string) (Writer, error) {
func (ca CPIOArchiver) OpenWriter(path string) (Writer, error) {
if len(path) == 0 {
return nil, fmt.Errorf("path is required")
}
Expand Down
32 changes: 17 additions & 15 deletions uroot/initramfs/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
package initramfs

import (
"errors"
"fmt"
"io"
"os"

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

// DirArchiver implements Archiver for a directory.
Expand All @@ -23,23 +24,24 @@ func (da DirArchiver) Reader(io.ReaderAt) Reader {
return nil
}

// CreateDefault creates a tmpdir using os.MkdirTemp prefixed with mkuimage- or
// mkuimage-GOOS-GOARCH- if available.
func (da DirArchiver) CreateDefault(env *golang.Environ) (string, error) {
name := "mkuimage-"
if len(env.GOOS) != 0 && len(env.GOARCH) != 0 {
name = fmt.Sprintf("mkuimage-%s-%s-", env.GOOS, env.GOARCH)
}
return os.MkdirTemp("", name)
}

// OpenWriter implements Archiver.OpenWriter.
func (da DirArchiver) OpenWriter(l ulog.Logger, path string) (Writer, error) {
func (da DirArchiver) OpenWriter(path string) (Writer, error) {
if len(path) == 0 {
var err error
path, err = os.MkdirTemp("", "u-root")
if err != nil {
return nil, err
}
} else {
if _, err := os.Stat(path); os.IsExist(err) {
return nil, fmt.Errorf("path %q already exists", path)
}
if err := os.MkdirAll(path, 0o755); err != nil {
return nil, err
}
return nil, fmt.Errorf("path is required")
}
if err := os.MkdirAll(path, 0o755); err != nil && !errors.Is(err, os.ErrExist) {
return nil, err
}
l.Printf("Path is %s", path)
return dirWriter{path}, nil
}

Expand Down

0 comments on commit 0fc63c9

Please sign in to comment.