-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
new(docker,pkg/driverbuilder): use cmake
instead of makefile template to build kmod and bpf
#302
Changes from all commits
a2cab38
ecf2951
c63a417
2dedbfb
054333e
e6eab8d
cb2c45b
13171b6
2d58a7c
784f575
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,19 +31,25 @@ 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) | ||
const ( | ||
DriverDirectory = "/tmp/driver" | ||
cmakeCmdFmt = `cmake -Wno-dev \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the cmake command being run. Btw it spots some unused CMake variable to keep compatibility with old driver versions, eg: |
||
-DUSE_BUNDLED_DEPS=On \ | ||
-DCREATE_TEST_TARGETS=Off \ | ||
-DBUILD_LIBSCAP_GVISOR=Off \ | ||
-DBUILD_LIBSCAP_MODERN_BPF=Off \ | ||
-DENABLE_DRIVERS_TESTS=Off \ | ||
-DDRIVER_NAME=%s \ | ||
-DPROBE_NAME=%s \ | ||
-DBUILD_BPF=On \ | ||
-DDRIVER_VERSION=%s \ | ||
-DPROBE_VERSION=%s \ | ||
-DGIT_COMMIT=%s \ | ||
-DDRIVER_DEVICE_NAME=%s \ | ||
-DPROBE_DEVICE_NAME=%s \ | ||
.. && \ | ||
sed -i s/'DRIVER_COMMIT ""'/'DRIVER_COMMIT "%s"'/g driver/src/driver_config.h` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the end, we also manually set the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could save all of this if we used |
||
) | ||
|
||
var HeadersNotFoundErr = errors.New("kernel headers not found") | ||
|
||
|
@@ -55,6 +61,14 @@ type Config struct { | |
*Build | ||
} | ||
|
||
func (c Config) ToDriverFullPath() string { | ||
return path.Join(DriverDirectory, "build", "driver", fmt.Sprintf("%s.ko", c.DriverName)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We expect the built kmod with cmake to be under |
||
} | ||
|
||
func (c Config) ToProbeFullPath() string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We expect the built probe with cmake to be under |
||
return path.Join(DriverDirectory, "build", "driver", "bpf", "probe.o") | ||
} | ||
|
||
type commonTemplateData struct { | ||
DriverBuildDir string | ||
ModuleDownloadURL string | ||
|
@@ -63,6 +77,7 @@ type commonTemplateData struct { | |
BuildModule bool | ||
BuildProbe bool | ||
GCCVersion string | ||
CmakeCmd string | ||
} | ||
|
||
// Builder represents a builder capable of generating a script for a driverkit target. | ||
|
@@ -293,10 +308,19 @@ 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, | ||
CmakeCmd: fmt.Sprintf(cmakeCmdFmt, | ||
c.DriverName, | ||
c.DriverName, | ||
c.DriverVersion, | ||
c.DriverVersion, | ||
c.DriverVersion, | ||
c.DeviceName, | ||
c.DeviceName, | ||
c.DriverVersion), | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,10 +28,7 @@ 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 }} | ||
mv /tmp/module-download/*/* {{ .DriverBuildDir }} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Scripts are a bit different now:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually really like how much this all simplifies the code, and I think the trade offs are worth it. |
||
|
||
# Fetch the kernel | ||
mkdir /tmp/kernel-download | ||
|
@@ -42,19 +39,20 @@ rm -Rf /tmp/kernel | |
mkdir -p /tmp/kernel | ||
mv usr/src/kernels/*/* /tmp/kernel | ||
|
||
cd {{ .DriverBuildDir }} | ||
mkdir -p build && cd build | ||
{{ .CmakeCmd }} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we always use the same CmakeCmd, make it easily customizable (at least during development) by using a single templated variable for it. |
||
|
||
{{ if .BuildModule }} | ||
# Build the module | ||
cd {{ .DriverBuildDir }} | ||
make CC=/usr/bin/gcc-{{ .GCCVersion }} KERNELDIR=/tmp/kernel | ||
mv {{ .ModuleDriverName }}.ko {{ .ModuleFullPath }} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need the |
||
make CC=/usr/bin/gcc-{{ .GCCVersion }} KERNELDIR=/tmp/kernel driver | ||
strip -g {{ .ModuleFullPath }} | ||
# Print results | ||
modinfo {{ .ModuleFullPath }} | ||
{{ end }} | ||
|
||
{{ if .BuildProbe }} | ||
# Build the eBPF probe | ||
cd {{ .DriverBuildDir }}/bpf | ||
make KERNELDIR=/tmp/kernel | ||
ls -l probe.o | ||
make KERNELDIR=/tmp/kernel bpf | ||
ls -l driver/bpf/probe.o | ||
{{ end }} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can now run integration tests against multiple driver versions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This way, we can ensure backward compatibility with driver versions for changes.