generated from falcosecurity/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new(cmd,pkg): added new
local
command to build local kernel using l…
…ocal kernel sources / gccs / clang. Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
- Loading branch information
Showing
9 changed files
with
365 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"github.com/falcosecurity/driverkit/pkg/driverbuilder" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
"github.com/spf13/viper" | ||
"golang.org/x/sys/unix" | ||
"log/slog" | ||
"os" | ||
"runtime" | ||
"strings" | ||
) | ||
|
||
// NewLocalCmd creates the `driverkit local` command. | ||
func NewLocalCmd(rootCommand *RootCmd, rootOpts *RootOptions, rootFlags *pflag.FlagSet) *cobra.Command { | ||
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), | ||
Run: func(c *cobra.Command, args []string) { | ||
slog.With("processor", c.Name()).Info("driver building, it will take a few seconds") | ||
if !configOptions.DryRun { | ||
b := rootOpts.ToBuild() | ||
if !b.HasOutputs() { | ||
return | ||
} | ||
if err := driverbuilder.NewLocalBuildProcessor(viper.GetInt("timeout")).Start(b); err != nil { | ||
slog.With("err", err.Error()).Error("exiting") | ||
os.Exit(1) | ||
} | ||
} | ||
}, | ||
} | ||
// Add root flags, but not the ones unneeded | ||
unusedFlagsSet := map[string]struct{}{ | ||
"architecture": {}, | ||
"kernelrelease": {}, | ||
"kernelversion": {}, | ||
"target": {}, | ||
"kernelurls": {}, | ||
"builderrepo": {}, | ||
"builderimage": {}, | ||
"gccversion": {}, | ||
"kernelconfigdata": {}, | ||
"proxy": {}, | ||
"registry-name": {}, | ||
"registry-password": {}, | ||
"registry-plain-http": {}, | ||
"registry-user": {}, | ||
} | ||
flagSet := pflag.NewFlagSet("local", pflag.ExitOnError) | ||
rootFlags.VisitAll(func(flag *pflag.Flag) { | ||
if _, ok := unusedFlagsSet[flag.Name]; !ok { | ||
flagSet.AddFlag(flag) | ||
} | ||
}) | ||
localCmd.PersistentFlags().AddFlagSet(flagSet) | ||
return localCmd | ||
} | ||
|
||
// Partially overrides rootCmd.persistentPreRunFunc setting some defaults before config init/validation stage. | ||
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" | ||
u := unix.Utsname{} | ||
if err := unix.Uname(&u); err != nil { | ||
slog.Error("failed to retrieve default uname values", "err", err) | ||
// this only affects logs! | ||
rootOpts.KernelRelease = "1.0.0" | ||
rootOpts.KernelVersion = "1" | ||
} else { | ||
rootOpts.KernelRelease = string(bytes.Trim(u.Release[:], "\x00")) | ||
kv := string(bytes.Trim(u.Version[:], "\x00")) | ||
kv = strings.Trim(kv, "#") | ||
kv = strings.Split(kv, " ")[0] | ||
rootOpts.KernelVersion = kv | ||
} | ||
rootOpts.Architecture = runtime.GOARCH | ||
fn := persistentValidateFunc(rootCommand, rootOpts) | ||
return fn(c, args) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package builder | ||
|
||
import ( | ||
_ "embed" | ||
"fmt" | ||
"github.com/falcosecurity/driverkit/pkg/kernelrelease" | ||
) | ||
|
||
// NOTE: since this is only used by local build, | ||
// it is not exposed in `target` array, | ||
// so no init() function to register it is present. | ||
|
||
//go:embed templates/local.sh | ||
var localTemplate string | ||
|
||
type LocalBuilder struct { | ||
GccPath string | ||
} | ||
|
||
func (l *LocalBuilder) Name() string { | ||
return "local" | ||
} | ||
|
||
func (l *LocalBuilder) TemplateScript() string { | ||
return localTemplate | ||
} | ||
|
||
func (l *LocalBuilder) URLs(kr kernelrelease.KernelRelease) ([]string, error) { | ||
return nil, nil | ||
} | ||
|
||
func (l *LocalBuilder) MinimumURLs() int { | ||
// We don't need any url | ||
return 0 | ||
} | ||
|
||
type localTemplateData struct { | ||
commonTemplateData | ||
} | ||
|
||
func (l *LocalBuilder) TemplateData(c Config, _ kernelrelease.KernelRelease, _ []string) interface{} { | ||
return localTemplateData{ | ||
commonTemplateData: commonTemplateData{ | ||
DriverBuildDir: DriverDirectory, | ||
ModuleDownloadURL: fmt.Sprintf("%s/%s.tar.gz", c.DownloadBaseURL, c.DriverVersion), | ||
ModuleDriverName: c.DriverName, | ||
ModuleFullPath: ModuleFullPath, | ||
BuildModule: len(c.ModuleFilePath) > 0, | ||
BuildProbe: len(c.ProbeFilePath) > 0, | ||
GCCVersion: l.GccPath, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#!/bin/bash | ||
# 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. | ||
# | ||
# Simple script that desperately tries to load the kernel instrumentation by | ||
# looking for it in a bunch of ways. Convenient when running Falco inside | ||
# a container or in other weird environments. | ||
# | ||
set -xeuo pipefail | ||
|
||
rm -Rf {{ .DriverBuildDir }} | ||
mkdir {{ .DriverBuildDir }} | ||
rm -Rf /tmp/module-download | ||
mkdir -p /tmp/module-download | ||
|
||
curl --silent -SL {{ .ModuleDownloadURL }} | tar -xzf - -C /tmp/module-download | ||
mv /tmp/module-download/*/driver/* {{ .DriverBuildDir }} | ||
|
||
cp /tmp/module-Makefile {{ .DriverBuildDir }}/Makefile | ||
bash /tmp/fill-driver-config.sh {{ .DriverBuildDir }} | ||
|
||
{{ if .BuildModule }} | ||
# Build the module | ||
cd {{ .DriverBuildDir }} | ||
make CC={{ .GCCVersion }} | ||
mv {{ .ModuleDriverName }}.ko {{ .ModuleFullPath }} | ||
strip -g {{ .ModuleFullPath }} | ||
# Print results | ||
modinfo {{ .ModuleFullPath }} | ||
{{ end }} | ||
|
||
{{ if .BuildProbe }} | ||
# Build the eBPF probe | ||
cd {{ .DriverBuildDir }}/bpf | ||
make | ||
ls -l probe.o | ||
{{ end }} | ||
|
||
rm -Rf /tmp/module-download |
Oops, something went wrong.