diff --git a/pkg/driverbuilder/builder/templates/alinux_kernel.sh b/pkg/driverbuilder/builder/templates/alinux_kernel.sh index db4b2fac..6719f273 100644 --- a/pkg/driverbuilder/builder/templates/alinux_kernel.sh +++ b/pkg/driverbuilder/builder/templates/alinux_kernel.sh @@ -32,4 +32,4 @@ mkdir -p /tmp/kernel mv usr/src/kernels/*/* /tmp/kernel # exit value -echo /tmp/kernel \ No newline at end of file +export KERNELDIR=/tmp/kernel \ No newline at end of file diff --git a/pkg/driverbuilder/builder/templates/almalinux_kernel.sh b/pkg/driverbuilder/builder/templates/almalinux_kernel.sh index db4b2fac..6719f273 100644 --- a/pkg/driverbuilder/builder/templates/almalinux_kernel.sh +++ b/pkg/driverbuilder/builder/templates/almalinux_kernel.sh @@ -32,4 +32,4 @@ mkdir -p /tmp/kernel mv usr/src/kernels/*/* /tmp/kernel # exit value -echo /tmp/kernel \ No newline at end of file +export KERNELDIR=/tmp/kernel \ No newline at end of file diff --git a/pkg/driverbuilder/builder/templates/amazonlinux_kernel.sh b/pkg/driverbuilder/builder/templates/amazonlinux_kernel.sh index e6524acc..47e5a72d 100644 --- a/pkg/driverbuilder/builder/templates/amazonlinux_kernel.sh +++ b/pkg/driverbuilder/builder/templates/amazonlinux_kernel.sh @@ -35,4 +35,4 @@ mkdir -p /tmp/kernel mv usr/src/kernels/*/* /tmp/kernel # exit value -echo /tmp/kernel \ No newline at end of file +export KERNELDIR=/tmp/kernel \ No newline at end of file diff --git a/pkg/driverbuilder/builder/templates/archlinux_kernel.sh b/pkg/driverbuilder/builder/templates/archlinux_kernel.sh index a44182d6..422c6454 100644 --- a/pkg/driverbuilder/builder/templates/archlinux_kernel.sh +++ b/pkg/driverbuilder/builder/templates/archlinux_kernel.sh @@ -32,4 +32,4 @@ mkdir -p /tmp/kernel mv usr/lib/modules/*/build/* /tmp/kernel # exit value -echo /tmp/kernel \ No newline at end of file +export KERNELDIR=/tmp/kernel \ No newline at end of file diff --git a/pkg/driverbuilder/builder/templates/centos_kernel.sh b/pkg/driverbuilder/builder/templates/centos_kernel.sh index db4b2fac..6719f273 100644 --- a/pkg/driverbuilder/builder/templates/centos_kernel.sh +++ b/pkg/driverbuilder/builder/templates/centos_kernel.sh @@ -32,4 +32,4 @@ mkdir -p /tmp/kernel mv usr/src/kernels/*/* /tmp/kernel # exit value -echo /tmp/kernel \ No newline at end of file +export KERNELDIR=/tmp/kernel \ No newline at end of file diff --git a/pkg/driverbuilder/builder/templates/debian_kernel.sh b/pkg/driverbuilder/builder/templates/debian_kernel.sh index ae500de0..953dcad9 100644 --- a/pkg/driverbuilder/builder/templates/debian_kernel.sh +++ b/pkg/driverbuilder/builder/templates/debian_kernel.sh @@ -38,4 +38,4 @@ sourcedir=$(find . -type d -name "{{ .KernelHeadersPattern }}" | head -n 1 | xar sed -i 's/\/usr\/src/../g' $sourcedir/Makefile # exit value -echo $sourcedir +export KERNELDIR=$sourcedir diff --git a/pkg/driverbuilder/builder/templates/fedora_kernel.sh b/pkg/driverbuilder/builder/templates/fedora_kernel.sh index db4b2fac..6719f273 100644 --- a/pkg/driverbuilder/builder/templates/fedora_kernel.sh +++ b/pkg/driverbuilder/builder/templates/fedora_kernel.sh @@ -32,4 +32,4 @@ mkdir -p /tmp/kernel mv usr/src/kernels/*/* /tmp/kernel # exit value -echo /tmp/kernel \ No newline at end of file +export KERNELDIR=/tmp/kernel \ No newline at end of file diff --git a/pkg/driverbuilder/builder/templates/flatcar_kernel.sh b/pkg/driverbuilder/builder/templates/flatcar_kernel.sh index 3b3a7718..e97e3a79 100644 --- a/pkg/driverbuilder/builder/templates/flatcar_kernel.sh +++ b/pkg/driverbuilder/builder/templates/flatcar_kernel.sh @@ -39,4 +39,4 @@ make KCONFIG_CONFIG=/tmp/kernel.config oldconfig make KCONFIG_CONFIG=/tmp/kernel.config modules_prepare # exit value -echo /tmp/kernel \ No newline at end of file +export KERNELDIR=/tmp/kernel \ No newline at end of file diff --git a/pkg/driverbuilder/builder/templates/opensuse_kernel.sh b/pkg/driverbuilder/builder/templates/opensuse_kernel.sh index 05752ca0..ca852f34 100644 --- a/pkg/driverbuilder/builder/templates/opensuse_kernel.sh +++ b/pkg/driverbuilder/builder/templates/opensuse_kernel.sh @@ -34,4 +34,4 @@ cd /tmp/kernel-download/usr/src sourcedir="$(find . -type d -name "linux-*-obj" | head -n 1 | xargs readlink -f)/*/default" # exit value -echo $sourcedir \ No newline at end of file +export KERNELDIR=$sourcedir \ No newline at end of file diff --git a/pkg/driverbuilder/builder/templates/oracle_kernel.sh b/pkg/driverbuilder/builder/templates/oracle_kernel.sh index db4b2fac..6719f273 100644 --- a/pkg/driverbuilder/builder/templates/oracle_kernel.sh +++ b/pkg/driverbuilder/builder/templates/oracle_kernel.sh @@ -32,4 +32,4 @@ mkdir -p /tmp/kernel mv usr/src/kernels/*/* /tmp/kernel # exit value -echo /tmp/kernel \ No newline at end of file +export KERNELDIR=/tmp/kernel \ No newline at end of file diff --git a/pkg/driverbuilder/builder/templates/photonos_kernel.sh b/pkg/driverbuilder/builder/templates/photonos_kernel.sh index f3433833..9c325b4f 100644 --- a/pkg/driverbuilder/builder/templates/photonos_kernel.sh +++ b/pkg/driverbuilder/builder/templates/photonos_kernel.sh @@ -35,4 +35,4 @@ mkdir -p /tmp/kernel mv usr/src/linux-*headers-*/* /tmp/kernel # exit value -echo /tmp/kernel \ No newline at end of file +export KERNELDIR=/tmp/kernel \ No newline at end of file diff --git a/pkg/driverbuilder/builder/templates/redhat_kernel.sh b/pkg/driverbuilder/builder/templates/redhat_kernel.sh index 68eb804a..f9829c7a 100644 --- a/pkg/driverbuilder/builder/templates/redhat_kernel.sh +++ b/pkg/driverbuilder/builder/templates/redhat_kernel.sh @@ -34,4 +34,4 @@ mkdir -p /tmp/kernel mv usr/src/kernels/*/* /tmp/kernel # exit value -echo /tmp/kernel \ No newline at end of file +export KERNELDIR=/tmp/kernel \ No newline at end of file diff --git a/pkg/driverbuilder/builder/templates/rocky_kernel.sh b/pkg/driverbuilder/builder/templates/rocky_kernel.sh index db4b2fac..6719f273 100644 --- a/pkg/driverbuilder/builder/templates/rocky_kernel.sh +++ b/pkg/driverbuilder/builder/templates/rocky_kernel.sh @@ -32,4 +32,4 @@ mkdir -p /tmp/kernel mv usr/src/kernels/*/* /tmp/kernel # exit value -echo /tmp/kernel \ No newline at end of file +export KERNELDIR=/tmp/kernel \ No newline at end of file diff --git a/pkg/driverbuilder/builder/templates/sles_kernel.sh b/pkg/driverbuilder/builder/templates/sles_kernel.sh index 145dee68..65012d07 100644 --- a/pkg/driverbuilder/builder/templates/sles_kernel.sh +++ b/pkg/driverbuilder/builder/templates/sles_kernel.sh @@ -35,4 +35,4 @@ done sourcedir="$(find . -type d -name "linux-*-obj" | head -n 1 | xargs readlink -f)/*/default" # exit value -echo $sourcedir +export KERNELDIR=$sourcedir diff --git a/pkg/driverbuilder/builder/templates/ubuntu_kernel.sh b/pkg/driverbuilder/builder/templates/ubuntu_kernel.sh index 60eb4238..28fa68cb 100644 --- a/pkg/driverbuilder/builder/templates/ubuntu_kernel.sh +++ b/pkg/driverbuilder/builder/templates/ubuntu_kernel.sh @@ -34,4 +34,4 @@ cd /tmp/kernel-download/usr/src/ sourcedir=$(find . -type d -name "{{ .KernelHeadersPattern }}" | head -n 1 | xargs readlink -f) # exit value -echo $sourcedir \ No newline at end of file +export KERNELDIR=$sourcedir \ No newline at end of file diff --git a/pkg/driverbuilder/builder/templates/vanilla_kernel.sh b/pkg/driverbuilder/builder/templates/vanilla_kernel.sh index ce1d824c..57cda3ee 100644 --- a/pkg/driverbuilder/builder/templates/vanilla_kernel.sh +++ b/pkg/driverbuilder/builder/templates/vanilla_kernel.sh @@ -47,4 +47,4 @@ make KCONFIG_CONFIG=/tmp/kernel.config prepare make KCONFIG_CONFIG=/tmp/kernel.config modules_prepare # exit value -echo /tmp/kernel \ No newline at end of file +export KERNELDIR=/tmp/kernel \ No newline at end of file diff --git a/pkg/driverbuilder/docker.go b/pkg/driverbuilder/docker.go index 12804bd5..d3dec409 100644 --- a/pkg/driverbuilder/docker.go +++ b/pkg/driverbuilder/docker.go @@ -233,6 +233,13 @@ func (bp *DockerBuildProcessor) Start(b *builder.Build) error { return err } + // We make all 3 scripts executables, + // then: + // * download libs at required version + // * download and extract headers + // * each download-headers script will export KERNELDIR variable internally + // * we source download-headers.sh so that KERNELDIR is then visible to driverkit.sh + // * we finally make the actual build of the drivers runCmd := ` #!/bin/bash @@ -242,7 +249,8 @@ chmod +x /driverkit/download-headers.sh chmod +x /driverkit/driverkit.sh /driverkit/download-libs.sh -KERNELDIR=$(/driverkit/download-headers.sh) /driverkit/driverkit.sh +. /driverkit/download-headers.sh +/driverkit/driverkit.sh ` files := []dockerCopyFile{ diff --git a/pkg/driverbuilder/kubernetes.go b/pkg/driverbuilder/kubernetes.go index ab723f09..793d2a7e 100644 --- a/pkg/driverbuilder/kubernetes.go +++ b/pkg/driverbuilder/kubernetes.go @@ -113,9 +113,9 @@ func (bp *KubernetesBuildProcessor) buildModule(b *builder.Build) error { } // We run a script that downloads libs, - // download and extracts kernelURLs saving its output to KERNELDIR env variable, + // then downloads and extracts kernelURLs exporting KERNELDIR env variable, // then finally runs the build script. - res = fmt.Sprintf("%s\nexport KERNELDIR=$(%s)\n%s", libsDownloadScript, kernelDownloadScript, res) + res = fmt.Sprintf("%s\n%s\n%s", libsDownloadScript, kernelDownloadScript, res) if c.ModuleFilePath != "" { res = fmt.Sprintf("%s\n%s", "touch "+moduleLockFile, res) diff --git a/pkg/driverbuilder/local.go b/pkg/driverbuilder/local.go index 280ad7e2..6ab34133 100644 --- a/pkg/driverbuilder/local.go +++ b/pkg/driverbuilder/local.go @@ -2,6 +2,7 @@ package driverbuilder import ( "bufio" + "bytes" "context" _ "embed" "errors" @@ -13,7 +14,6 @@ import ( "os/exec" "os/user" "path/filepath" - "strings" "time" ) @@ -67,12 +67,23 @@ func (lbp *LocalBuildProcessor) Start(b *builder.Build) error { // if an unsupported target is passed. // Go on skipping automatic kernel headers download. if err == nil { - slog.Info("Trying automatic kernel headers download.") + slog.Info("Trying automatic kernel headers download") kernelDownloadScript, err := builder.KernelDownloadScript(realBuilder, nil, kr) + // Patch kernel download script to echo KERNELDIR. + // We need to capture KERNELDIR to later pass it as env variable to the build. + kernelDownloadScript += "\necho $KERNELDIR" if err == nil { out, err := exec.Command("bash", "-c", kernelDownloadScript).Output() if err == nil { - path := strings.TrimSuffix(string(out), "\n") + // Scan all stdout line by line and + // store last line as KERNELDIR path. + reader := bytes.NewReader(out) + scanner := bufio.NewScanner(reader) + var path string + for scanner.Scan() { + path = scanner.Text() + } + slog.Info("Setting KERNELDIR env var", "path", path) // add the kerneldir path to env lbp.envMap[kernelDirEnv] = path defer func() { @@ -80,11 +91,11 @@ func (lbp *LocalBuildProcessor) Start(b *builder.Build) error { _ = os.RemoveAll(path) }() } else { - slog.Warn("Failed to download headers.", "err", err) + slog.Warn("Failed to download headers", "err", err) } } } else { - slog.Info("Skipping kernel headers automatic download.", "err", err) + slog.Info("Skipping kernel headers automatic download", "err", err) } } @@ -137,7 +148,7 @@ func (lbp *LocalBuildProcessor) Start(b *builder.Build) error { srcProbePath := vv.GetProbeFullPath(c) if len(lbp.srcDir) == 0 { - slog.Info("Downloading driver sources.") + slog.Info("Downloading driver sources") // Download src! libsDownloadScript, err := builder.LibsDownloadScript(c) if err != nil { @@ -168,14 +179,14 @@ func (lbp *LocalBuildProcessor) Start(b *builder.Build) error { stdout, err := cmd.StdoutPipe() if err != nil { - slog.Warn("Failed to pipe stdout. Trying without piping.", "err", err) + slog.Warn("Failed to pipe stdout", "err", err) _, err = cmd.CombinedOutput() } else { cmd.Stderr = cmd.Stdout // redirect stderr to stdout so that we catch it defer stdout.Close() err = cmd.Start() if err != nil { - slog.Warn("Failed to execute command.", "err", err) + slog.Warn("Failed to execute command", "err", err) } else { // print the output of the subprocess line by line scanner := bufio.NewScanner(stdout)