Skip to content

Commit

Permalink
new(cmd,pkg): added new local command to build local kernel using l…
Browse files Browse the repository at this point in the history
…ocal kernel sources / gccs / clang.

Signed-off-by: Federico Di Pierro <[email protected]>
  • Loading branch information
FedeDP authored and poiana committed Nov 24, 2023
1 parent 4500840 commit 78bb75a
Show file tree
Hide file tree
Showing 9 changed files with 369 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cmd/config_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/go-playground/validator/v10"
)

var validProcessors = []string{"docker", "kubernetes", "kubernetes-in-cluster"}
var validProcessors = []string{"docker", "kubernetes", "kubernetes-in-cluster", "local"}
var aliasProcessors = []string{"docker", "k8s", "k8s-ic"}
var configOptions *ConfigOptions

Expand Down
84 changes: 84 additions & 0 deletions cmd/local.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
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
return rootCommand.c.PersistentPreRunE(c, args)
}
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ func NewRootCmd() *RootCmd {
rootCmd.AddCommand(NewKubernetesCmd(rootOpts, flags))
rootCmd.AddCommand(NewKubernetesInClusterCmd(rootOpts, flags))
rootCmd.AddCommand(NewDockerCmd(rootOpts, flags))
rootCmd.AddCommand(NewLocalCmd(ret, rootOpts, flags))
rootCmd.AddCommand(NewImagesCmd(rootOpts, flags))
rootCmd.AddCommand(NewCompletionCmd())

Expand Down
2 changes: 1 addition & 1 deletion cmd/testdata/autohelp.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
level=INFO msg="specify a valid processor" processors="[docker kubernetes kubernetes-in-cluster]"
level=INFO msg="specify a valid processor" processors="[docker kubernetes kubernetes-in-cluster local]"
{{ .Desc }}

{{ .Usage }}
Expand Down
3 changes: 2 additions & 1 deletion cmd/testdata/templates/commands.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ Available Commands:
help Help about any command
images List builder images
kubernetes Build Falco kernel modules and eBPF probes against a Kubernetes cluster.
kubernetes-in-cluster Build Falco kernel modules and eBPF probes against a Kubernetes cluster inside a Kubernetes cluster.
kubernetes-in-cluster Build Falco kernel modules and eBPF probes against a Kubernetes cluster inside a Kubernetes cluster.
local Build Falco kernel modules and eBPF probes in local env with local kernel sources and gcc/clang.
9 changes: 8 additions & 1 deletion pkg/driverbuilder/builder/builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ func Script(b Builder, c Config, kr kernelrelease.KernelRelease) (string, error)
return "", err
}

var urls []string
minimumURLs := 1
if bb, ok := b.(MinimumURLsBuilder); ok {
minimumURLs = bb.MinimumURLs()
}

var urls []string
if c.KernelUrls == nil {
urls, err = b.URLs(kr)
if err != nil {
Expand Down Expand Up @@ -250,10 +250,17 @@ func (b *Build) GetBuilderImage() string {

// Factory returns a builder for the given target.
func Factory(target Type) (Builder, error) {
// Workaround for "local" target (that is not exposed to users,
// nor registered in byTarget map)".
if target.String() == "local" {
return &LocalBuilder{}, nil
}

// Driverkit builder is named "ubuntu"; there is no ubuntu-foo
if strings.HasPrefix(target.String(), "ubuntu") {
target = Type("ubuntu")
}

b, ok := byTarget[target]
if !ok {
return nil, fmt.Errorf("no builder found for target: %s", target)
Expand Down
53 changes: 53 additions & 0 deletions pkg/driverbuilder/builder/local.go
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,
},
}
}
53 changes: 53 additions & 0 deletions pkg/driverbuilder/builder/templates/local.sh
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
Loading

0 comments on commit 78bb75a

Please sign in to comment.