Repository: falcosecurity/driverkit
Pull request #132 (2022-04-08 10:56:24)

fix: add back PROBE_VERSION allowing drivers for old falcosecurity/libs versions to be built
**What type of PR is this?**

> Uncomment one (or more) `/kind <>` lines:

/kind bug

> /kind cleanup

> /kind design

> /kind documentation

> /kind failing-test

> /kind feature

<!--
Please remove the leading whitespace before the `/kind <>` you uncommented.
-->

**Any specific area of the project related to this PR?**

> Uncomment one (or more) `/area <>` lines:

/area build

> /area cmd

> /area pkg

> /area docs

> /area tests


**What this PR does / why we need it**:

The PR reintroduces PROBE_VERSION define to allow building on old libs versions.
Bug was introduced by: 067a562f25722cdd4bd8fd84f91f412bd671a9d7.

**Which issue(s) this PR fixes**:

<!--
Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
If PR is `kind/failing-tests` or `kind/flaky-test`, please post the related issues/tests in a comment and do not use `Fixes`.
-->

Fixes #

**Special notes for your reviewer**:

**Does this PR introduce a user-facing change?**:

<!--
If no, just write "NONE" in the release-note block below.
If yes, a release note is required:
Enter your extended release note in the block below.
If the PR requires additional action from users switching to the new release, prepend the string "action required:".
For example, `action required: change the API interface of the rule engine`.
-->

```release-note
fix: allow back to build drivers for old libs versions
```


poiana: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by: *<a href="https://github.com/falcosecurity/driverkit/pull/132#" title="Author self-approved">FedeDP</a>*
To complete the [pull request process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process), please assign **fntlnz** after the PR has been reviewed.
You can assign the PR to them by writing `/assign @fntlnz` in a comment when ready.

The full list of commands accepted by this bot can be found [here](https://prow.falco.org/command-help?repo=falcosecurity%2Fdriverkit).

<details open>
Needs approval from an approver in each of these files:

- **[OWNERS](https://github.com/falcosecurity/driverkit/blob/master/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["fntlnz"]} -->
FedeDP: See https://github.com/falcosecurity/test-infra/issues/667 and https://github.com/falcosecurity/falco/issues/1964 for infos.
maxgio92: Thanks @FedeDP ! 🥳 

Patch:
diff --git a/pkg/driverbuilder/builder/rocky.go b/pkg/driverbuilder/builder/rocky.go
new file mode 100644
index 0000000..635388d
--- /dev/null
+++ b/pkg/driverbuilder/builder/rocky.go
@@ -0,0 +1,139 @@
+package builder
+
+import (
+	"bytes"
+	"fmt"
+	"text/template"
+
+	"github.com/falcosecurity/driverkit/pkg/kernelrelease"
+)
+
+// TargetTypeRocky identifies the Rocky target.
+const TargetTypeRocky Type = "rocky"
+
+func init() {
+	BuilderByTarget[TargetTypeRocky] = &rocky{}
+}
+
+// rocky is a driverkit target.
+type rocky struct {
+}
+
+// Script compiles the script to build the kernel module and/or the eBPF probe.
+func (c rocky) Script(cfg Config) (string, error) {
+	t := template.New(string(TargetTypeRocky))
+	parsed, err := t.Parse(rockyTemplate)
+	if err != nil {
+		return "", err
+	}
+
+	kr := kernelrelease.FromString(cfg.Build.KernelRelease)
+
+	// Check (and filter) existing kernels before continuing
+	urls, err := getResolvingURLs(fetchRockyKernelURLS(kr))
+	if err != nil {
+		return "", err
+	}
+
+	td := rockyTemplateData{
+		DriverBuildDir:    DriverDirectory,
+		ModuleDownloadURL: moduleDownloadURL(cfg),
+		KernelDownloadURL: urls[0],
+		GCCVersion:        rockyGccVersionFromKernelRelease(kr),
+		ModuleDriverName:  cfg.DriverName,
+		ModuleFullPath:    ModuleFullPath,
+		BuildModule:       len(cfg.Build.ModuleFilePath) > 0,
+		BuildProbe:        len(cfg.Build.ProbeFilePath) > 0,
+	}
+
+	buf := bytes.NewBuffer(nil)
+	err = parsed.Execute(buf, td)
+	if err != nil {
+		return "", err
+	}
+	return buf.String(), nil
+}
+
+func fetchRockyKernelURLS(kr kernelrelease.KernelRelease) []string {
+	rockyReleases := []string{
+		"8",
+		"8.5",
+	}
+
+	urls := []string{}
+	for _, r := range rockyReleases {
+		urls = append(urls, fmt.Sprintf(
+			"https://download.rockylinux.org/pub/rocky/%s/BaseOS/x86_64/os/Packages/k/kernel-devel-%s%s.rpm",
+			r,
+			kr.Fullversion,
+			kr.FullExtraversion,
+		))
+	}
+	return urls
+}
+
+type rockyTemplateData struct {
+	DriverBuildDir    string
+	ModuleDownloadURL string
+	KernelDownloadURL string
+	GCCVersion        string
+	ModuleDriverName  string
+	ModuleFullPath    string
+	BuildModule       bool
+	BuildProbe        bool
+}
+
+const rockyTemplate = `
+#!/bin/bash
+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 /driverkit/module-Makefile {{ .DriverBuildDir }}/Makefile
+bash /driverkit/fill-driver-config.sh {{ .DriverBuildDir }}
+
+# Fetch the kernel
+mkdir /tmp/kernel-download
+cd /tmp/kernel-download
+curl --silent -o kernel-devel.rpm -SL {{ .KernelDownloadURL }}
+rpm2cpio kernel-devel.rpm | cpio --extract --make-directories
+rm -Rf /tmp/kernel
+mkdir -p /tmp/kernel
+mv usr/src/kernels/*/* /tmp/kernel
+
+# Change current gcc
+ln -sf /usr/bin/gcc-{{ .GCCVersion }} /usr/bin/gcc
+
+{{ if .BuildModule }}
+# Build the module
+cd {{ .DriverBuildDir }}
+make KERNELDIR=/tmp/kernel
+mv {{ .ModuleDriverName }}.ko {{ .ModuleFullPath }}
+strip -g {{ .ModuleFullPath }}
+# Print results
+modinfo {{ .ModuleFullPath }}
+{{ end }}
+
+{{ if .BuildProbe }}
+# Build the eBPF probe
+cd {{ .DriverBuildDir }}/bpf
+make LLC=/usr/bin/llc-7 CLANG=/usr/bin/clang-7 CC=/usr/bin/gcc KERNELDIR=/tmp/kernel
+ls -l probe.o
+{{ end }}
+`
+
+func rockyGccVersionFromKernelRelease(kr kernelrelease.KernelRelease) string {
+	switch kr.Version {
+	case "3":
+		return "5"
+	case "2":
+		return "4.8"
+	}
+	return "8"
+}
diff --git a/pkg/driverbuilder/templates.go b/pkg/driverbuilder/templates.go
index 941709a..9ebe6e9 100644
--- a/pkg/driverbuilder/templates.go
+++ b/pkg/driverbuilder/templates.go
@@ -86,6 +86,7 @@ DRIVER_CONFIG_FILE="$DRIVER_BUILD_DIR/driver_config.h"
 cat << EOF > $DRIVER_CONFIG_FILE
 #pragma once
 
+#define PROBE_VERSION "{{ .DriverVersion }}"
 #define DRIVER_VERSION "{{ .DriverVersion }}"
 
 #define DRIVER_COMMIT "{{ .DriverVersion }}"


Test patch:
diff --git a/cmd/testdata/completion-targets.txt b/cmd/testdata/completion-targets.txt
index df7ea42..cba01a5 100644
--- a/cmd/testdata/completion-targets.txt
+++ b/cmd/testdata/completion-targets.txt
@@ -2,6 +2,7 @@ amazonlinux
 amazonlinux2
 centos
 debian
+rocky
 ubuntu-aws
 ubuntu-generic
 vanilla