From cd1b1de829d0e1e9176e12d454be66d308e9e151 Mon Sep 17 00:00:00 2001 From: Mahe Tardy Date: Thu, 14 Mar 2024 17:43:53 +0000 Subject: [PATCH 1/4] pkg/kernels: fix kConfigValidate naming typo Signed-off-by: Mahe Tardy --- pkg/kernels/dir.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/kernels/dir.go b/pkg/kernels/dir.go index da51b708..023ffd3f 100644 --- a/pkg/kernels/dir.go +++ b/pkg/kernels/dir.go @@ -59,7 +59,7 @@ func (kd *KernelsDir) RawConfigure(ctx context.Context, log *logrus.Logger, kern return kd.rawConfigureKernel(ctx, log, kc, kernDir) } -func kcfonfigValidate(opts []ConfigOption) error { +func kConfigValidate(opts []ConfigOption) error { var ret error // we want to check that: @@ -178,7 +178,7 @@ func (kd *KernelsDir) rawConfigureKernel( } if false { - if err := kcfonfigValidate(configOptions[:i+1]); err != nil { + if err := kConfigValidate(configOptions[:i+1]); err != nil { return fmt.Errorf("failed to validate config after applying %v: %w", opts, err) } } @@ -186,7 +186,7 @@ func (kd *KernelsDir) rawConfigureKernel( } if false { - if err := kcfonfigValidate(configOptions); err != nil { + if err := kConfigValidate(configOptions); err != nil { return fmt.Errorf("failed to validate config after scripts: %w", err) } } @@ -198,7 +198,7 @@ func (kd *KernelsDir) rawConfigureKernel( // NB: some configuration options are only available in certain // kernels. We have no way of dealing with this currently. - if err := kcfonfigValidate(configOptions); err != nil { + if err := kConfigValidate(configOptions); err != nil { log.Warnf("discrepancies in generated config: %s", err) } From 055cd5c538bd8141c0645187a6b764510b10bb2d Mon Sep 17 00:00:00 2001 From: Mahe Tardy Date: Thu, 14 Mar 2024 17:40:42 +0000 Subject: [PATCH 2/4] pkg/kernels: fix kConfigValidate broken validation kConfigValidate could not handle '--module' option which was stopping the validation by returning an error. Signed-off-by: Mahe Tardy --- pkg/kernels/dir.go | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/pkg/kernels/dir.go b/pkg/kernels/dir.go index 023ffd3f..6273c358 100644 --- a/pkg/kernels/dir.go +++ b/pkg/kernels/dir.go @@ -65,9 +65,18 @@ func kConfigValidate(opts []ConfigOption) error { // we want to check that: // - what is supposed to be enabled, is enabled // - what is supposed to be disabled, is not enabled + // - what is supposed to be configured as module, is configured as a module + + type configState string + + const ( + enabledState configState = "y" + disabledState configState = "n" + moduleState configState = "m" + ) type OptMapVal struct { - enabled bool + state configState checked bool } @@ -75,9 +84,11 @@ func kConfigValidate(opts []ConfigOption) error { for _, opt := range opts { switch opt[0] { case "--enable": - optMap[opt[1]] = OptMapVal{enabled: true} + optMap[opt[1]] = OptMapVal{state: enabledState} case "--disable": - optMap[opt[1]] = OptMapVal{enabled: false} + optMap[opt[1]] = OptMapVal{state: disabledState} + case "--module": + optMap[opt[1]] = OptMapVal{state: moduleState} default: return fmt.Errorf("Unknown option: %s", opt[0]) } @@ -90,18 +101,20 @@ func kConfigValidate(opts []ConfigOption) error { } defer kcfg.Close() - enabledRe := regexp.MustCompile(`([a-zA-Z0-9_]+)=y`) + enabledOrModuleRe := regexp.MustCompile(`([a-zA-Z0-9_]+)=(y|m)`) disabledRe := regexp.MustCompile(`# ([a-zA-Z0-9_]+) is not set`) s := bufio.NewScanner(kcfg) for s.Scan() { txt := s.Text() var opt string - optEnabled := false - if match := enabledRe.FindStringSubmatch(txt); len(match) > 0 { + var optState configState + if match := enabledOrModuleRe.FindStringSubmatch(txt); len(match) > 2 { opt = match[1] - optEnabled = true + // the regex can only match 'y' or 'm' so this should be correct + optState = configState(match[2]) } else if match := disabledRe.FindStringSubmatch(txt); len(match) > 0 { opt = match[1] + optState = disabledState } else { continue } @@ -114,10 +127,10 @@ func kConfigValidate(opts []ConfigOption) error { mapVal.checked = true optMap[opt] = mapVal - if mapVal.enabled != optEnabled { + if mapVal.state != optState { ret = errors.Join(ret, - fmt.Errorf("value %s misconfigured: expected: %t but seems to be %t based on '%s'", - opt, mapVal.enabled, optEnabled, txt)) + fmt.Errorf("value %s misconfigured: expected: %q but seems to be %q based on %q", + opt, mapVal.state, optState, txt)) } } @@ -127,9 +140,12 @@ func kConfigValidate(opts []ConfigOption) error { } for i, v := range optMap { - if v.enabled && !v.checked { + if v.state == enabledState && !v.checked { ret = errors.Join(ret, fmt.Errorf("value %s enabled but not found", i)) } + if v.state == moduleState && !v.checked { + ret = errors.Join(ret, fmt.Errorf("value %s configured as module but not found", i)) + } } return ret From 01a0421dcd3664bb283829645fec87df3bfc0b0a Mon Sep 17 00:00:00 2001 From: Mahe Tardy Date: Thu, 14 Mar 2024 16:57:51 +0000 Subject: [PATCH 3/4] pkg/kernels: correctly pass ARCH to make olddefconfig Previously, ARCH was exclusively passed at the defconfig and prepare stage and then was missing when calling olddefconfig after customizing the config. Signed-off-by: Mahe Tardy --- cmd/lvh/kernels/configure.go | 19 +++++++++++++------ pkg/kernels/dir.go | 18 +++++++++++++----- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/cmd/lvh/kernels/configure.go b/cmd/lvh/kernels/configure.go index 1cb60d6b..321c4ce9 100644 --- a/cmd/lvh/kernels/configure.go +++ b/cmd/lvh/kernels/configure.go @@ -11,9 +11,12 @@ import ( "github.com/spf13/cobra" ) -func configureCommand() *cobra.Command { - var arch string +const ( + archFlag = "arch" + archHelp = "target architecture to configure the kernel, e.g. 'amd64' or 'arm64' (default to native architecture)" +) +func configureCommand() *cobra.Command { cmd := &cobra.Command{ Use: "configure ", Short: "configure kernel", @@ -26,20 +29,20 @@ func configureCommand() *cobra.Command { } kname := args[0] - if err := kd.ConfigureKernel(context.Background(), log, kname, arch); err != nil { + if err := kd.ConfigureKernel(context.Background(), log, kname, cmd.Flag(archFlag).Value.String()); err != nil { log.Fatal(err) } }, } - cmd.Flags().StringVar(&arch, "arch", "", "target architecture to configure the kernel, e.g. 'amd64' or 'arm64' (default to native architecture)") + cmd.Flags().String(archFlag, "", archHelp) return cmd } func rawConfigureCommand() *cobra.Command { - return &cobra.Command{ + cmd := &cobra.Command{ Use: "raw_configure []", Short: "configure a kernel prepared by means other than lvh", Args: cobra.RangeArgs(1, 2), @@ -55,10 +58,14 @@ func rawConfigureCommand() *cobra.Command { if len(args) > 1 { kname = args[1] } - if err := kd.RawConfigure(context.Background(), log, kdir, kname); err != nil { + if err := kd.RawConfigure(context.Background(), log, kdir, kname, cmd.Flag(archFlag).Value.String()); err != nil { log.Fatal(err) } }, } + + cmd.Flags().String(archFlag, "", archHelp) + + return cmd } diff --git a/pkg/kernels/dir.go b/pkg/kernels/dir.go index 6273c358..636c9141 100644 --- a/pkg/kernels/dir.go +++ b/pkg/kernels/dir.go @@ -54,9 +54,9 @@ func (kd *KernelsDir) ConfigureKernel(ctx context.Context, log *logrus.Logger, k return kd.configureKernel(ctx, log, kc, targetArch) } -func (kd *KernelsDir) RawConfigure(ctx context.Context, log *logrus.Logger, kernDir, kernName string) error { +func (kd *KernelsDir) RawConfigure(ctx context.Context, log *logrus.Logger, kernDir, kernName string, targetArch string) error { kc := kd.KernelConfig(kernName) - return kd.rawConfigureKernel(ctx, log, kc, kernDir) + return kd.rawConfigureKernel(ctx, log, kc, targetArch, kernDir) } func kConfigValidate(opts []ConfigOption) error { @@ -165,7 +165,7 @@ func runAndLogMake( func (kd *KernelsDir) rawConfigureKernel( ctx context.Context, log *logrus.Logger, - kc *KernelConf, srcDir string, + kc *KernelConf, srcDir string, targetArch string, makePrepareArgs ...string, ) error { oldPath, err := os.Getwd() @@ -207,8 +207,16 @@ func (kd *KernelsDir) rawConfigureKernel( } } + crossCompilationArgs, err := arch.CrossCompileMakeArgs(targetArch) + if err != nil { + return fmt.Errorf("failed to retrieve cross compilation args: %w", err) + } + + olddefconfigMakeArgs := []string{"olddefconfig"} + olddefconfigMakeArgs = append(olddefconfigMakeArgs, crossCompilationArgs...) + // run make olddefconfig to clean up the config file, and ensure that everything is in order - if err := runAndLogMake(ctx, log, kc, "olddefconfig"); err != nil { + if err := runAndLogMake(ctx, log, kc, olddefconfigMakeArgs...); err != nil { return err } @@ -232,7 +240,7 @@ func (kd *KernelsDir) configureKernel(ctx context.Context, log *logrus.Logger, k configureMakeArgs := []string{"defconfig", "prepare"} configureMakeArgs = append(configureMakeArgs, crossCompilationArgs...) - return kd.rawConfigureKernel(ctx, log, kc, srcDir, configureMakeArgs...) + return kd.rawConfigureKernel(ctx, log, kc, srcDir, targetArch, configureMakeArgs...) } From 44fd100c90e51a6959d9b98452dfb22ca59e1da8 Mon Sep 17 00:00:00 2001 From: Mahe Tardy Date: Fri, 15 Mar 2024 09:23:11 +0000 Subject: [PATCH 4/4] pkg/kernels: fix len check on regex match to prevent panic Signed-off-by: Mahe Tardy --- pkg/kernels/dir.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/kernels/dir.go b/pkg/kernels/dir.go index 636c9141..dfcb6362 100644 --- a/pkg/kernels/dir.go +++ b/pkg/kernels/dir.go @@ -112,7 +112,7 @@ func kConfigValidate(opts []ConfigOption) error { opt = match[1] // the regex can only match 'y' or 'm' so this should be correct optState = configState(match[2]) - } else if match := disabledRe.FindStringSubmatch(txt); len(match) > 0 { + } else if match := disabledRe.FindStringSubmatch(txt); len(match) > 1 { opt = match[1] optState = disabledState } else {