Skip to content

Commit

Permalink
fix(pkg): ported local builder to use cmake.
Browse files Browse the repository at this point in the history
Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
  • Loading branch information
FedeDP committed Dec 12, 2023
1 parent 8e3366a commit 826307a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 60 deletions.
6 changes: 3 additions & 3 deletions pkg/driverbuilder/builder/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
_ "embed"
"fmt"
"github.com/falcosecurity/driverkit/pkg/kernelrelease"
"path"
)

// NOTE: since this is only used by local build,
Expand Down Expand Up @@ -63,12 +64,11 @@ func (l *LocalBuilder) TemplateData(c Config, kr kernelrelease.KernelRelease, _
}

func (l *LocalBuilder) GetModuleFullPath(c Config, kr kernelrelease.KernelRelease) string {
moduleFullPath := ModuleFullPath
if l.UseDKMS {
// When using dkms, we will use a GLOB to match the pattern; ModuleFullPath won't be used in the templated script anyway.
moduleFullPath = fmt.Sprintf("/var/lib/dkms/%s/%s/%s/%s/module/%s.*", c.DriverName, c.DriverVersion, kr.String(), kr.Architecture.ToNonDeb(), c.DriverName)
return fmt.Sprintf("/var/lib/dkms/%s/%s/%s/%s/module/%s.*", c.DriverName, c.DriverVersion, kr.String(), kr.Architecture.ToNonDeb(), c.DriverName)
}
return moduleFullPath
return path.Join(DriverDirectory, "build", "driver", fmt.Sprintf("%s.ko", c.DriverName))
}

func (l *LocalBuilder) GetDriverBuildDir() string {
Expand Down
20 changes: 11 additions & 9 deletions pkg/driverbuilder/builder/templates/local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@ 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 }}
mv /tmp/module-download/*/* {{ .DriverBuildDir }}
{{ end }}

cp /tmp/module-Makefile {{ .DriverBuildDir }}/Makefile
bash /tmp/fill-driver-config.sh {{ .DriverBuildDir }}
{{ if or .BuildProbe (and .BuildModule (not .UseDKMS)) }}
echo "* Configuring sources with cmake"
cd {{ .DriverBuildDir }}
mkdir -p build && cd build
cmake -DUSE_BUNDLED_DEPS=On -DCREATE_TEST_TARGETS=Off -DBUILD_LIBSCAP_GVISOR=Off -DBUILD_LIBSCAP_MODERN_BPF=Off -DENABLE_DRIVERS_TESTS=Off -DDRIVER_NAME={{ .ModuleDriverName }} -DBUILD_BPF=On ..
{{ end }}


{{ if .BuildModule }}
{{ if .UseDKMS }}
echo "* Building kmod with DKMS"
Expand All @@ -48,9 +53,7 @@ rm -Rf "/tmp/falco-dkms-make"
{{ else }}
echo "* Building kmod"
# Build the module
cd {{ .DriverBuildDir }}
make CC={{ .GCCVersion }}
mv {{ .ModuleDriverName }}.ko {{ .ModuleFullPath }}
make CC={{ .GCCVersion }} driver
strip -g {{ .ModuleFullPath }}
# Print results
modinfo {{ .ModuleFullPath }}
Expand All @@ -60,9 +63,8 @@ modinfo {{ .ModuleFullPath }}
{{ if .BuildProbe }}
echo "* Building eBPF probe"
# Build the eBPF probe
cd {{ .DriverBuildDir }}/bpf
make
ls -l probe.o
make bpf
ls -l driver/bpf/probe.o
{{ end }}

rm -Rf /tmp/module-download
65 changes: 17 additions & 48 deletions pkg/driverbuilder/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package driverbuilder

import (
"bufio"
"bytes"
"context"
_ "embed"
"fmt"
Expand Down Expand Up @@ -51,37 +50,7 @@ func (lbp *LocalBuildProcessor) Start(b *builder.Build) error {
}
c := b.ToConfig()

// Prepare driver config template
bufFillDriverConfig := bytes.NewBuffer(nil)
err = renderFillDriverConfig(bufFillDriverConfig, driverConfigData{DriverVersion: c.DriverVersion, DriverName: c.DriverName, DeviceName: c.DeviceName})
if err != nil {
return err
}

// Prepare makefile template
objList, err := LoadMakefileObjList(c)
if err != nil {
return err
}
bufMakefile := bytes.NewBuffer(nil)
err = renderMakefile(bufMakefile, makefileData{ModuleName: c.DriverName, ModuleBuildDir: builder.DriverDirectory, MakeObjList: objList})
if err != nil {
return err
}

// Create all local files
files := []dockerCopyFile{
{"/tmp/module-Makefile", bufMakefile.String()},
{"/tmp/fill-driver-config.sh", bufFillDriverConfig.String()},
}
for _, file := range files {
if err = os.WriteFile(file.Name, []byte(file.Body), 0o755); err != nil {
return err
}
defer os.Remove(file.Name)
}

defer os.Remove(builder.DriverDirectory)
defer os.RemoveAll(builder.DriverDirectory)

// Load gcc versions from system
var gccs []string
Expand All @@ -107,7 +76,7 @@ func (lbp *LocalBuildProcessor) Start(b *builder.Build) error {
}
} else {
// We won't use it!
gccs = []string{"gcc"}
gccs = []string{"UNUSED"}
}

// Cannot fail
Expand All @@ -116,7 +85,7 @@ func (lbp *LocalBuildProcessor) Start(b *builder.Build) error {
vv.UseDKMS = lbp.useDKMS

modulePath := vv.GetModuleFullPath(c, kr)
probePath := path.Join(vv.GetDriverBuildDir(), "bpf", builder.ProbeFileName)
probePath := path.Join(vv.GetDriverBuildDir(), "build", "driver", "bpf", builder.ProbeFileName)
for _, gcc := range gccs {
vv.GccPath = gcc

Expand Down Expand Up @@ -151,18 +120,26 @@ func (lbp *LocalBuildProcessor) Start(b *builder.Build) error {
err = cmd.Wait()
}
}
if err == nil {
break

// If we built the probe, disable its build for subsequent attempts (with other available gccs)
if c.ProbeFilePath != "" {
if _, err = os.Stat(probePath); !os.IsNotExist(err) {
if err = copyDataToLocalPath(probePath, b.ProbeFilePath); err != nil {
return err
}
slog.With("path", b.ProbeFilePath).Info("eBPF probe available")
c.ProbeFilePath = ""
}
}
// If we received an error, perhaps we must just rebuilt the kmod.

// If we received an error, perhaps we just need to try another build for the kmod.
// Check if we were able to build anything.
koFiles, err := filepath.Glob(modulePath)
if err == nil && len(koFiles) > 0 {
// Since only kmod might need to get rebuilt
// with another gcc, break here if we actually built the kmod.
break
}
if _, err = os.Stat(probePath); !os.IsNotExist(err) {
c.ProbeFilePath = ""
}
}

if len(b.ModuleFilePath) > 0 {
Expand All @@ -177,14 +154,6 @@ func (lbp *LocalBuildProcessor) Start(b *builder.Build) error {
}
slog.With("path", b.ModuleFilePath).Info("kernel module available")
}

if len(b.ProbeFilePath) > 0 {
if err = copyDataToLocalPath(probePath, b.ProbeFilePath); err != nil {
return err
}
slog.With("path", b.ProbeFilePath).Info("eBPF probe available")
}

return nil
}

Expand Down

0 comments on commit 826307a

Please sign in to comment.