Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(kraft): Only accept single target in utils.BuildRootfs #1596

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions internal/cli/kraft/pkg/packager_cli_kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,18 @@ func (p *packagerCliKernel) Pack(ctx context.Context, opts *PkgOptions, args ...
return nil, fmt.Errorf("could not prepare phony target: %w", err)
}

var cmds [][]string
var envs [][]string
var cmds []string
var envs []string
if opts.Rootfs, cmds, envs, err = utils.BuildRootfs(ctx, opts.Workdir, opts.Rootfs, opts.Compress, targ); err != nil {
return nil, fmt.Errorf("could not build rootfs: %w", err)
}

if len(opts.Args) == 0 {
if cmds[0] != nil {
opts.Args = cmds[0]
}
if len(opts.Args) == 0 && cmds != nil {
opts.Args = cmds
}

if len(opts.Env) == 0 {
if envs[0] != nil {
opts.Env = envs[0]
}
if envs != nil {
opts.Env = append(opts.Env, envs...)
}

cmdShellArgs, err := shellwords.Parse(strings.Join(opts.Args, " "))
Expand Down
14 changes: 6 additions & 8 deletions internal/cli/kraft/pkg/packager_kraftfile_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,16 +283,14 @@ func (p *packagerKraftfileRuntime) Pack(ctx context.Context, opts *PkgOptions, a
return nil, fmt.Errorf("package does not convert to target")
}

var cmds [][]string
var envs [][]string
var cmds []string
var envs []string
if opts.Rootfs, cmds, envs, err = utils.BuildRootfs(ctx, opts.Workdir, opts.Rootfs, opts.Compress, targ); err != nil {
return nil, fmt.Errorf("could not build rootfs: %w", err)
}

if len(opts.Env) == 0 {
if envs[0] != nil {
opts.Env = envs[0]
}
if envs != nil {
opts.Env = append(opts.Env, envs...)
}

// If no arguments have been specified, use the ones which are default and
Expand All @@ -302,8 +300,8 @@ func (p *packagerKraftfileRuntime) Pack(ctx context.Context, opts *PkgOptions, a
opts.Args = opts.Project.Command()
} else if len(targ.Command()) > 0 {
opts.Args = targ.Command()
} else if cmds[0] != nil {
opts.Args = cmds[0]
} else if cmds != nil {
opts.Args = cmds
}
}

Expand Down
45 changes: 23 additions & 22 deletions internal/cli/kraft/pkg/packager_kraftfile_unikraft.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,35 +86,36 @@ func (p *packagerKraftfileUnikraft) Pack(ctx context.Context, opts *PkgOptions,
return nil, fmt.Errorf("nothing selected to package")
}

var cmds [][]string
var envs [][]string

// Reset the rootfs, such that it is not packaged as an initrd if it is
// already embedded inside of the kernel.
if opts.Project.KConfig().AnyYes(
"CONFIG_LIBVFSCORE_ROOTFS_EINITRD", // Deprecated
"CONFIG_LIBVFSCORE_AUTOMOUNT_EINITRD",
"CONFIG_LIBVFSCORE_AUTOMOUNT_CI_EINITRD",
) {
opts.Rootfs = ""
} else {
if opts.Rootfs, cmds, envs, err = utils.BuildRootfs(ctx, opts.Workdir, opts.Rootfs, opts.Compress, selected...); err != nil {
return nil, fmt.Errorf("could not build rootfs: %w", err)
}
}

i := 0

var result []pack.Package

for _, targ := range selected {
var cmds []string
var envs []string
rootfs := opts.Rootfs

// Reset the rootfs, such that it is not packaged as an initrd if it is
// already embedded inside of the kernel.
if opts.Project.KConfig().AnyYes(
"CONFIG_LIBVFSCORE_ROOTFS_EINITRD", // Deprecated
"CONFIG_LIBVFSCORE_AUTOMOUNT_EINITRD",
"CONFIG_LIBVFSCORE_AUTOMOUNT_CI_EINITRD",
) {
rootfs = ""
} else {
if rootfs, cmds, envs, err = utils.BuildRootfs(ctx, opts.Workdir, rootfs, opts.Compress, targ); err != nil {
return nil, fmt.Errorf("could not build rootfs: %w", err)
}
}

// See: https://github.com/golang/go/wiki/CommonMistakes#using-reference-to-loop-iterator-variable
targ := targ
baseopts := opts.packopts
name := "packaging " + targ.Name() + " (" + opts.Format + ")"

if envs[i] != nil && len(envs[i]) > 0 {
opts.Env = envs[i]
if envs != nil {
opts.Env = append(opts.Env, envs...)
}

// If no arguments have been specified, use the ones which are default and
Expand All @@ -124,8 +125,8 @@ func (p *packagerKraftfileUnikraft) Pack(ctx context.Context, opts *PkgOptions,
opts.Args = opts.Project.Command()
} else if len(targ.Command()) > 0 {
opts.Args = targ.Command()
} else if cmds[i] != nil && len(cmds[i]) > 0 {
opts.Args = cmds[i]
} else if cmds != nil {
opts.Args = cmds
}
}

Expand All @@ -149,7 +150,7 @@ func (p *packagerKraftfileUnikraft) Pack(ctx context.Context, opts *PkgOptions,
func(ctx context.Context) error {
popts := append(baseopts,
packmanager.PackArgs(cmdShellArgs...),
packmanager.PackInitrd(opts.Rootfs),
packmanager.PackInitrd(rootfs),
packmanager.PackKConfig(!opts.NoKConfig),
packmanager.PackName(opts.Name),
packmanager.PackOutput(opts.Output),
Expand Down
90 changes: 40 additions & 50 deletions internal/cli/kraft/utils/rootfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"context"
"fmt"
"path/filepath"
"slices"

"kraftkit.sh/config"
"kraftkit.sh/initrd"
Expand All @@ -21,65 +20,56 @@ import (

// BuildRootfs generates a rootfs based on the provided working directory and
// the rootfs entrypoint for the provided target(s).
func BuildRootfs(ctx context.Context, workdir, rootfs string, compress bool, targets ...target.Target) (string, [][]string, [][]string, error) {
func BuildRootfs(ctx context.Context, workdir, rootfs string, compress bool, targ target.Target) (string, []string, []string, error) {
if rootfs == "" {
return "", nil, nil, nil
}

var processes []*processtree.ProcessTreeItem
var archs []string
var cmds [][]string
var envs [][]string
var cmds []string
var envs []string

for _, targ := range targets {
arch := targ.Architecture().String()
if slices.Contains[[]string](archs, arch) {
continue
}

archs = append(archs, arch)

if !filepath.IsAbs(rootfs) {
rootfs = filepath.Join(workdir, rootfs)
}
if !filepath.IsAbs(rootfs) {
rootfs = filepath.Join(workdir, rootfs)
}

ramfs, err := initrd.New(ctx, rootfs,
initrd.WithOutput(filepath.Join(
workdir,
unikraft.BuildDir,
fmt.Sprintf(initrd.DefaultInitramfsArchFileName, arch),
)),
initrd.WithCacheDir(filepath.Join(
workdir,
unikraft.VendorDir,
"rootfs-cache",
)),
initrd.WithArchitecture(arch),
initrd.WithCompression(compress),
)
if err != nil {
return "", nil, nil, fmt.Errorf("could not initialize initramfs builder: %w", err)
}
ramfs, err := initrd.New(ctx, rootfs,
initrd.WithOutput(filepath.Join(
workdir,
unikraft.BuildDir,
fmt.Sprintf(initrd.DefaultInitramfsArchFileName, targ.Architecture().String()),
)),
initrd.WithCacheDir(filepath.Join(
workdir,
unikraft.VendorDir,
"rootfs-cache",
)),
initrd.WithArchitecture(targ.Architecture().String()),
initrd.WithCompression(compress),
)
if err != nil {
return "", nil, nil, fmt.Errorf("could not initialize initramfs builder: %w", err)
}

processes = append(processes,
processtree.NewProcessTreeItem(
"building rootfs",
arch,
func(ctx context.Context) error {
// TODO
rootfs, err = ramfs.Build(ctx)
if err != nil {
return err
}
processes = append(processes,
processtree.NewProcessTreeItem(
"building rootfs",
targ.Architecture().String(),
func(ctx context.Context) error {
rootfs, err = ramfs.Build(ctx)
if err != nil {
return err
}

cmds = append(cmds, ramfs.Args())
envs = append(envs, ramfs.Env())
// Always overwrite the existing cmds and envs, considering this will
// be the same regardless of the target.
cmds = ramfs.Args()
envs = ramfs.Env()

return nil
},
),
)
}
return nil
},
),
)

model, err := processtree.NewProcessTree(
ctx,
Expand Down
Loading