Skip to content

Commit

Permalink
fix(pkg): use correct {Module,Probe}FullPath so that we do save a `mv…
Browse files Browse the repository at this point in the history
…` in template scripts.

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
  • Loading branch information
FedeDP committed Dec 12, 2023
1 parent 558df67 commit 6a7fde7
Show file tree
Hide file tree
Showing 21 changed files with 22 additions and 44 deletions.
22 changes: 9 additions & 13 deletions pkg/driverbuilder/builder/builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,6 @@ import (
// DriverDirectory is the directory the processor uses to store the driver.
const DriverDirectory = "/tmp/driver"

// ModuleFileName is the standard file name for the kernel module.
const ModuleFileName = "module.ko"

// ProbeFileName is the standard file name for the eBPF probe.
const ProbeFileName = "probe.o"

// ModuleFullPath is the standard path for the kernel module. Builders must place the compiled module at this location.
var ModuleFullPath = path.Join(DriverDirectory, ModuleFileName)

// ProbeFullPath is the standard path for the eBPF probe. Builders must place the compiled probe at this location.
var ProbeFullPath = path.Join(DriverDirectory, "bpf", ProbeFileName)

var HeadersNotFoundErr = errors.New("kernel headers not found")

// Config contains all the configurations needed to build the kernel module or the eBPF probe.
Expand All @@ -55,6 +43,14 @@ type Config struct {
*Build
}

func (c Config) ToDriverFullPath() string {
return path.Join(DriverDirectory, "build", "driver", fmt.Sprintf("%s.ko", c.DriverName))
}

func (c Config) ToProbeFullPath() string {
return path.Join(DriverDirectory, "build", "driver", "bpf", "probe.o")
}

type commonTemplateData struct {
DriverBuildDir string
ModuleDownloadURL string
Expand Down Expand Up @@ -288,7 +284,7 @@ func (c Config) toTemplateData(b Builder, kr kernelrelease.KernelRelease) common
DriverBuildDir: DriverDirectory,
ModuleDownloadURL: fmt.Sprintf("%s/%s.tar.gz", c.DownloadBaseURL, c.DriverVersion),
ModuleDriverName: c.DriverName,
ModuleFullPath: ModuleFullPath,
ModuleFullPath: c.ToDriverFullPath(),
BuildModule: len(c.ModuleFilePath) > 0,
BuildProbe: len(c.ProbeFilePath) > 0,
GCCVersion: c.GCCVersion,
Expand Down
3 changes: 1 addition & 2 deletions pkg/driverbuilder/builder/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
_ "embed"
"fmt"
"github.com/falcosecurity/driverkit/pkg/kernelrelease"
"path"
)

// NOTE: since this is only used by local build,
Expand Down Expand Up @@ -68,7 +67,7 @@ func (l *LocalBuilder) GetModuleFullPath(c Config, kr kernelrelease.KernelReleas
// When using dkms, we will use a GLOB to match the pattern; ModuleFullPath won't be used in the templated script anyway.
return fmt.Sprintf("/var/lib/dkms/%s/%s/%s/%s/module/%s.*", c.DriverName, c.DriverVersion, kr.String(), kr.Architecture.ToNonDeb(), c.DriverName)
}
return path.Join(DriverDirectory, "build", "driver", fmt.Sprintf("%s.ko", c.DriverName))
return c.ToDriverFullPath()
}

func (l *LocalBuilder) GetDriverBuildDir() string {
Expand Down
1 change: 0 additions & 1 deletion pkg/driverbuilder/builder/templates/alinux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ cmake -DUSE_BUNDLED_DEPS=On -DCREATE_TEST_TARGETS=Off -DBUILD_LIBSCAP_GVISOR=Off
{{ if .BuildModule }}
# Build the module
make CC=/usr/bin/gcc-{{ .GCCVersion }} KERNELDIR=/tmp/kernel driver
mv driver/{{ .ModuleDriverName }}.ko {{ .ModuleFullPath }}
strip -g {{ .ModuleFullPath }}
# Print results
modinfo {{ .ModuleFullPath }}
Expand Down
1 change: 0 additions & 1 deletion pkg/driverbuilder/builder/templates/almalinux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ cmake -DUSE_BUNDLED_DEPS=On -DCREATE_TEST_TARGETS=Off -DBUILD_LIBSCAP_GVISOR=Off
{{ if .BuildModule }}
# Build the module
make CC=/usr/bin/gcc-{{ .GCCVersion }} KERNELDIR=/tmp/kernel driver
mv driver/{{ .ModuleDriverName }}.ko {{ .ModuleFullPath }}
strip -g {{ .ModuleFullPath }}
# Print results
modinfo {{ .ModuleFullPath }}
Expand Down
1 change: 0 additions & 1 deletion pkg/driverbuilder/builder/templates/amazonlinux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ cmake -DUSE_BUNDLED_DEPS=On -DCREATE_TEST_TARGETS=Off -DBUILD_LIBSCAP_GVISOR=Off
{{ if .BuildModule }}
# Build the module
make CC=/usr/bin/gcc-{{ .GCCVersion }} KERNELDIR=/tmp/kernel LD=/usr/bin/ld.bfd CROSS_COMPILE="" driver
mv driver/{{ .ModuleDriverName }}.ko {{ .ModuleFullPath }}
strip -g {{ .ModuleFullPath }}
# Print results
modinfo {{ .ModuleFullPath }}
Expand Down
1 change: 0 additions & 1 deletion pkg/driverbuilder/builder/templates/archlinux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ cmake -DUSE_BUNDLED_DEPS=On -DCREATE_TEST_TARGETS=Off -DBUILD_LIBSCAP_GVISOR=Off
{{ if .BuildModule }}
# Build the module
make CC=/usr/bin/gcc-{{ .GCCVersion }} KERNELDIR=/tmp/kernel driver
mv driver/{{ .ModuleDriverName }}.ko {{ .ModuleFullPath }}
strip -g {{ .ModuleFullPath }}
# Print results
modinfo {{ .ModuleFullPath }}
Expand Down
1 change: 0 additions & 1 deletion pkg/driverbuilder/builder/templates/centos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ cmake -DUSE_BUNDLED_DEPS=On -DCREATE_TEST_TARGETS=Off -DBUILD_LIBSCAP_GVISOR=Off
{{ if .BuildModule }}
# Build the module
make CC=/usr/bin/gcc-{{ .GCCVersion }} KERNELDIR=/tmp/kernel driver
mv driver/{{ .ModuleDriverName }}.ko {{ .ModuleFullPath }}
strip -g {{ .ModuleFullPath }}
# Print results
modinfo {{ .ModuleFullPath }}
Expand Down
1 change: 0 additions & 1 deletion pkg/driverbuilder/builder/templates/debian.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ cmake -DUSE_BUNDLED_DEPS=On -DCREATE_TEST_TARGETS=Off -DBUILD_LIBSCAP_GVISOR=Off
{{ if .BuildModule }}
# Build the module
make CC=/usr/bin/gcc-{{ .GCCVersion }} KERNELDIR=$sourcedir driver
mv driver/{{ .ModuleDriverName }}.ko {{ .ModuleFullPath }}
strip -g {{ .ModuleFullPath }}
# Print results
modinfo {{ .ModuleFullPath }}
Expand Down
1 change: 0 additions & 1 deletion pkg/driverbuilder/builder/templates/fedora.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ cmake -DUSE_BUNDLED_DEPS=On -DCREATE_TEST_TARGETS=Off -DBUILD_LIBSCAP_GVISOR=Off
{{ if .BuildModule }}
# Build the module
make CC=/usr/bin/gcc-{{ .GCCVersion }} KERNELDIR=/tmp/kernel driver
mv driver/{{ .ModuleDriverName }}.ko {{ .ModuleFullPath }}
strip -g {{ .ModuleFullPath }}
# Print results
modinfo {{ .ModuleFullPath }}
Expand Down
1 change: 0 additions & 1 deletion pkg/driverbuilder/builder/templates/flatcar.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ cmake -DUSE_BUNDLED_DEPS=On -DCREATE_TEST_TARGETS=Off -DBUILD_LIBSCAP_GVISOR=Off
{{ if .BuildModule }}
# Build the module
make CC=/usr/bin/gcc-{{ .GCCVersion }} KERNELDIR=/tmp/kernel driver
mv driver/{{ .ModuleDriverName }}.ko {{ .ModuleFullPath }}
strip -g {{ .ModuleFullPath }}
# Print results
modinfo {{ .ModuleFullPath }}
Expand Down
1 change: 0 additions & 1 deletion pkg/driverbuilder/builder/templates/opensuse.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ cmake -DUSE_BUNDLED_DEPS=On -DCREATE_TEST_TARGETS=Off -DBUILD_LIBSCAP_GVISOR=Off
{{ if .BuildModule }}
# Build the module
make CC=/usr/bin/gcc-{{ .GCCVersion }} KERNELDIR=$sourcedir driver
mv driver/{{ .ModuleDriverName }}.ko {{ .ModuleFullPath }}
strip -g {{ .ModuleFullPath }}
# Print results
modinfo {{ .ModuleFullPath }}
Expand Down
1 change: 0 additions & 1 deletion pkg/driverbuilder/builder/templates/oracle.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ cmake -DUSE_BUNDLED_DEPS=On -DCREATE_TEST_TARGETS=Off -DBUILD_LIBSCAP_GVISOR=Off
{{ if .BuildModule }}
# Build the module
make CC=/usr/bin/gcc-{{ .GCCVersion }} KERNELDIR=/tmp/kernel driver
mv driver/{{ .ModuleDriverName }}.ko {{ .ModuleFullPath }}
strip -g {{ .ModuleFullPath }}
# Print results
modinfo {{ .ModuleFullPath }}
Expand Down
1 change: 0 additions & 1 deletion pkg/driverbuilder/builder/templates/photonos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ cmake -DUSE_BUNDLED_DEPS=On -DCREATE_TEST_TARGETS=Off -DBUILD_LIBSCAP_GVISOR=Off
{{ if .BuildModule }}
# Build the module
make CC=/usr/bin/gcc-{{ .GCCVersion }} KERNELDIR=/tmp/kernel driver
mv driver/{{ .ModuleDriverName }}.ko {{ .ModuleFullPath }}
strip -g {{ .ModuleFullPath }}
# Print results
modinfo {{ .ModuleFullPath }}
Expand Down
1 change: 0 additions & 1 deletion pkg/driverbuilder/builder/templates/redhat.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ cmake -DUSE_BUNDLED_DEPS=On -DCREATE_TEST_TARGETS=Off -DBUILD_LIBSCAP_GVISOR=Off
{{ if .BuildModule }}
# Build the module
make CC=/usr/bin/gcc-{{ .GCCVersion }} KERNELDIR=/tmp/kernel driver
mv driver/{{ .ModuleDriverName }}.ko {{ .ModuleFullPath }}
strip -g {{ .ModuleFullPath }}
# Print results
modinfo {{ .ModuleFullPath }}
Expand Down
1 change: 0 additions & 1 deletion pkg/driverbuilder/builder/templates/rocky.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ cmake -DUSE_BUNDLED_DEPS=On -DCREATE_TEST_TARGETS=Off -DBUILD_LIBSCAP_GVISOR=Off
{{ if .BuildModule }}
# Build the module
make CC=/usr/bin/gcc-{{ .GCCVersion }} KERNELDIR=/tmp/kernel driver
mv driver/{{ .ModuleDriverName }}.ko {{ .ModuleFullPath }}
strip -g {{ .ModuleFullPath }}
# Print results
modinfo {{ .ModuleFullPath }}
Expand Down
3 changes: 1 addition & 2 deletions pkg/driverbuilder/builder/templates/sles.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ cmake -DUSE_BUNDLED_DEPS=On -DCREATE_TEST_TARGETS=Off -DBUILD_LIBSCAP_GVISOR=Off
{{ if .BuildModule }}
# Build the module
make CC=/usr/bin/gcc-{{ .GCCVersion }} KERNELDIR=$sourcedir driver
mv {{ .ModuleDriverName }}.ko {{ .ModuleFullPath }}
strip -g {{ .ModuleFullPath }}
# Print results
modinfo {{ .ModuleFullPath }}
Expand All @@ -60,5 +59,5 @@ modinfo {{ .ModuleFullPath }}
{{ if .BuildProbe }}
# Build the eBPF probe
make KERNELDIR=$sourcedir bpf
ls -l probe.o
ls -l driver/bpf/probe.o
{{ end }}
1 change: 0 additions & 1 deletion pkg/driverbuilder/builder/templates/ubuntu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ cmake -DUSE_BUNDLED_DEPS=On -DCREATE_TEST_TARGETS=Off -DBUILD_LIBSCAP_GVISOR=Off
{{ if .BuildModule }}
# Build the module
make CC=/usr/bin/gcc-{{ .GCCVersion }} KERNELDIR=$sourcedir driver
mv driver/{{ .ModuleDriverName }}.ko {{ .ModuleFullPath }}
strip -g {{ .ModuleFullPath }}
# Print results
modinfo {{ .ModuleFullPath }}
Expand Down
1 change: 0 additions & 1 deletion pkg/driverbuilder/builder/templates/vanilla.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ cmake -DUSE_BUNDLED_DEPS=On -DCREATE_TEST_TARGETS=Off -DBUILD_LIBSCAP_GVISOR=Off
{{ if .BuildModule }}
# Build the module
make CC=/usr/bin/gcc-{{ .GCCVersion }} KERNELDIR=/tmp/kernel driver
mv driver/{{ .ModuleDriverName }}.ko {{ .ModuleFullPath }}
strip -g {{ .ModuleFullPath }}
# Print results
modinfo {{ .ModuleFullPath }}
Expand Down
4 changes: 2 additions & 2 deletions pkg/driverbuilder/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,14 +284,14 @@ func (bp *DockerBuildProcessor) Start(b *builder.Build) error {
}

if len(b.ModuleFilePath) > 0 {
if err := copyFromContainer(ctx, cli, cdata.ID, builder.ModuleFullPath, b.ModuleFilePath); err != nil {
if err := copyFromContainer(ctx, cli, cdata.ID, c.ToDriverFullPath(), b.ModuleFilePath); err != nil {
return err
}
slog.With("path", b.ModuleFilePath).Info("kernel module available")
}

if len(b.ProbeFilePath) > 0 {
if err := copyFromContainer(ctx, cli, cdata.ID, builder.ProbeFullPath, b.ProbeFilePath); err != nil {
if err := copyFromContainer(ctx, cli, cdata.ID, c.ToProbeFullPath(), b.ProbeFilePath); err != nil {
return err
}
slog.With("path", b.ProbeFilePath).Info("eBPF probe available")
Expand Down
16 changes: 8 additions & 8 deletions pkg/driverbuilder/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ func (bp *KubernetesBuildProcessor) buildModule(b *builder.Build) error {
return err
}

if builder.ModuleFullPath != "" {
if c.ModuleFilePath != "" {
res = fmt.Sprintf("%s\n%s", "touch "+moduleLockFile, res)
res = fmt.Sprintf("%s\n%s", res, "rm "+moduleLockFile)
}
if builder.ProbeFullPath != "" {
if c.ProbeFilePath != "" {
res = fmt.Sprintf("%s\n%s", "touch "+probeLockFile, res)
res = fmt.Sprintf("%s\n%s", res, "rm "+probeLockFile)
}
Expand Down Expand Up @@ -224,10 +224,10 @@ func (bp *KubernetesBuildProcessor) buildModule(b *builder.Build) error {
return err
}
defer podClient.Delete(ctx, pod.Name, metav1.DeleteOptions{})
return bp.copyModuleAndProbeFromPodWithUID(ctx, b, namespace, string(uid))
return bp.copyModuleAndProbeFromPodWithUID(ctx, c, b, namespace, string(uid))
}

func (bp *KubernetesBuildProcessor) copyModuleAndProbeFromPodWithUID(ctx context.Context, build *builder.Build, namespace string, falcoBuilderUID string) error {
func (bp *KubernetesBuildProcessor) copyModuleAndProbeFromPodWithUID(ctx context.Context, c builder.Config, build *builder.Build, namespace string, falcoBuilderUID string) error {
namespacedClient := bp.coreV1Client.Pods(namespace)
watch, err := namespacedClient.Watch(ctx, metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", falcoBuilderUIDLabel, falcoBuilderUID),
Expand Down Expand Up @@ -255,15 +255,15 @@ func (bp *KubernetesBuildProcessor) copyModuleAndProbeFromPodWithUID(ctx context
}
if p.Status.Phase == corev1.PodRunning {
slog.With(falcoBuilderUIDLabel, falcoBuilderUID).Info("start downloading module and probe from pod")
if builder.ModuleFullPath != "" {
err = copySingleFileFromPod(build.ModuleFilePath, bp.coreV1Client, bp.clientConfig, p.Namespace, p.Name, builder.ModuleFullPath, moduleLockFile)
if c.ModuleFilePath != "" {
err = copySingleFileFromPod(c.ModuleFilePath, bp.coreV1Client, bp.clientConfig, p.Namespace, p.Name, c.ToDriverFullPath(), moduleLockFile)
if err != nil {
return err
}
slog.Info("Kernel Module extraction successful")
}
if builder.ProbeFullPath != "" {
err = copySingleFileFromPod(build.ProbeFilePath, bp.coreV1Client, bp.clientConfig, p.Namespace, p.Name, builder.ProbeFullPath, probeLockFile)
if c.ProbeFilePath != "" {
err = copySingleFileFromPod(c.ProbeFilePath, bp.coreV1Client, bp.clientConfig, p.Namespace, p.Name, c.ToProbeFullPath(), probeLockFile)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/driverbuilder/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"log/slog"
"os"
"os/exec"
"path"
"path/filepath"
"time"
)
Expand Down Expand Up @@ -85,7 +84,7 @@ func (lbp *LocalBuildProcessor) Start(b *builder.Build) error {
vv.UseDKMS = lbp.useDKMS

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

Expand Down

0 comments on commit 6a7fde7

Please sign in to comment.