diff --git a/cmd/local.go b/cmd/local.go index 19687557..4835a15e 100644 --- a/cmd/local.go +++ b/cmd/local.go @@ -1,10 +1,7 @@ package cmd import ( - "fmt" "github.com/falcosecurity/driverkit/pkg/driverbuilder" - "github.com/falcosecurity/driverkit/validate" - "github.com/go-playground/validator/v10" "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/spf13/viper" @@ -15,22 +12,9 @@ import ( ) type localCmdOptions struct { - UseDKMS bool - SrcDir string `validate:"required,abs_dirpath" name:"src-dir"` - EnvMap map[string]string -} - -func (l *localCmdOptions) validate() []error { - if err := validate.V.Struct(l); err != nil { - errors := err.(validator.ValidationErrors) - errArr := []error{} - for _, e := range errors { - // Translate each error one at a time - errArr = append(errArr, fmt.Errorf(e.Translate(validate.T))) - } - return errArr - } - return nil + useDKMS bool + srcDir string + envMap map[string]string } // NewLocalCmd creates the `driverkit local` command. @@ -39,7 +23,7 @@ func NewLocalCmd(rootCommand *RootCmd, rootOpts *RootOptions, rootFlags *pflag.F localCmd := &cobra.Command{ Use: "local", Short: "Build Falco kernel modules and eBPF probes in local env with local kernel sources and gcc/clang.", - PersistentPreRunE: persistentPreRunFunc(rootCommand, rootOpts, &opts), + PersistentPreRunE: persistentPreRunFunc(rootCommand, rootOpts), Run: func(c *cobra.Command, args []string) { slog.With("processor", c.Name()).Info("driver building, it will take a few seconds") if !configOptions.DryRun { @@ -47,7 +31,7 @@ func NewLocalCmd(rootCommand *RootCmd, rootOpts *RootOptions, rootFlags *pflag.F if !b.HasOutputs() { return } - if opts.UseDKMS { + if opts.useDKMS { currentUser, err := user.Current() if err != nil { slog.With("err", err.Error()).Error("Failed to retrieve user. Exiting.") @@ -58,7 +42,7 @@ func NewLocalCmd(rootCommand *RootCmd, rootOpts *RootOptions, rootFlags *pflag.F os.Exit(1) } } - if err := driverbuilder.NewLocalBuildProcessor(viper.GetInt("timeout"), opts.UseDKMS, opts.SrcDir, opts.EnvMap).Start(b); err != nil { + if err := driverbuilder.NewLocalBuildProcessor(viper.GetInt("timeout"), opts.useDKMS, opts.srcDir, opts.envMap).Start(b); err != nil { slog.With("err", err.Error()).Error("exiting") os.Exit(1) } @@ -86,25 +70,19 @@ func NewLocalCmd(rootCommand *RootCmd, rootOpts *RootOptions, rootFlags *pflag.F flagSet.AddFlag(flag) } }) - flagSet.BoolVar(&opts.UseDKMS, "dkms", false, "Enforce usage of DKMS to build the kernel module.") - flagSet.StringVar(&opts.SrcDir, "src-dir", "", "Enforce usage of local source dir to build drivers.") - flagSet.StringToStringVar(&opts.EnvMap, "env", nil, "Env variables to be enforced during the driver build.") + flagSet.BoolVar(&opts.useDKMS, "dkms", false, "Enforce usage of DKMS to build the kernel module.") + flagSet.StringVar(&opts.srcDir, "src-dir", "", "Enforce usage of local source dir to build drivers.") + flagSet.StringToStringVar(&opts.envMap, "env", nil, "Env variables to be enforced during the driver build.") localCmd.PersistentFlags().AddFlagSet(flagSet) return localCmd } // Partially overrides rootCmd.persistentPreRunFunc setting some defaults before config init/validation stage. -func persistentPreRunFunc(rootCommand *RootCmd, rootOpts *RootOptions, localOpts *localCmdOptions) func(c *cobra.Command, args []string) error { +func persistentPreRunFunc(rootCommand *RootCmd, rootOpts *RootOptions) func(c *cobra.Command, args []string) error { return func(c *cobra.Command, args []string) error { // Default values rootOpts.Target = "local" rootOpts.Architecture = runtime.GOARCH - if errs := localOpts.validate(); errs != nil { - for _, err := range errs { - slog.With("err", err.Error()).Error("error validating local command options") - } - return fmt.Errorf("exiting for validation errors") - } return rootCommand.c.PersistentPreRunE(c, args) } } diff --git a/pkg/driverbuilder/builder/local.go b/pkg/driverbuilder/builder/local.go index 8ce0a1a5..95d7e0ab 100644 --- a/pkg/driverbuilder/builder/local.go +++ b/pkg/driverbuilder/builder/local.go @@ -55,6 +55,15 @@ func (l *LocalBuilder) TemplateData(c Config, kr kernelrelease.KernelRelease, _ BuildModule: len(c.ModuleFilePath) > 0, BuildProbe: len(c.ProbeFilePath) > 0, GCCVersion: l.GccPath, + CmakeCmd: fmt.Sprintf(cmakeCmdFmt, + c.DriverName, + c.DriverName, + c.DriverVersion, + c.DriverVersion, + c.DriverVersion, + c.DeviceName, + c.DeviceName, + c.DriverVersion), }, UseDKMS: l.UseDKMS, DownloadSrc: len(l.SrcDir) == 0, // if no srcdir is provided, download src! diff --git a/pkg/driverbuilder/builder/templates/local.sh b/pkg/driverbuilder/builder/templates/local.sh index 872c363d..f23b10a1 100644 --- a/pkg/driverbuilder/builder/templates/local.sh +++ b/pkg/driverbuilder/builder/templates/local.sh @@ -71,7 +71,8 @@ modinfo {{ .ModuleFullPath }} echo "* Building eBPF probe" if [ ! -d /sys/kernel/debug/tracing ]; then echo "* Mounting debugfs" - mount -t debugfs nodev /sys/kernel/debug + # Do not fail if this fails. + mount -t debugfs nodev /sys/kernel/debug || : fi {{ if .DownloadSrc }} diff --git a/validate/isAbsDirPath.go b/validate/isAbsDirPath.go deleted file mode 100644 index 471129ab..00000000 --- a/validate/isAbsDirPath.go +++ /dev/null @@ -1,43 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -/* -Copyright (C) 2023 The Falco Authors. -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package validate - -import ( - "fmt" - "os" - "path/filepath" - "reflect" - - "github.com/go-playground/validator/v10" -) - -func isAbsDirPath(fl validator.FieldLevel) bool { - field := fl.Field() - - switch field.Kind() { - case reflect.String: - fpath := field.String() - if !filepath.IsAbs(fpath) { - return false - } - fileInfo, err := os.Stat(fpath) - if err != nil { - return false - } - return fileInfo.IsDir() - } - - panic(fmt.Sprintf("Bad field type %T", field.Interface())) -} diff --git a/validate/validate.go b/validate/validate.go index 3b62473c..02abd945 100644 --- a/validate/validate.go +++ b/validate/validate.go @@ -50,7 +50,6 @@ func init() { V.RegisterValidation("loglevel", isLogLevel) V.RegisterValidation("filepath", isFilePath) - V.RegisterValidation("abs_dirpath", isAbsDirPath) V.RegisterValidation("sha1", isSHA1) V.RegisterValidation("target", isTargetSupported) V.RegisterValidation("architecture", isArchitectureSupported)