Skip to content

Commit 8ea62ad

Browse files
FedeDPpoiana
authored andcommitted
fix(pkg/driverbuilder): fix local executor to correctly fetch KERNELDIR.
Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
1 parent a6610e6 commit 8ea62ad

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

pkg/driverbuilder/local.go

+19-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package driverbuilder
22

33
import (
44
"bufio"
5+
"bytes"
56
"context"
67
_ "embed"
78
"errors"
@@ -13,7 +14,6 @@ import (
1314
"os/exec"
1415
"os/user"
1516
"path/filepath"
16-
"strings"
1717
"time"
1818
)
1919

@@ -67,24 +67,35 @@ func (lbp *LocalBuildProcessor) Start(b *builder.Build) error {
6767
// if an unsupported target is passed.
6868
// Go on skipping automatic kernel headers download.
6969
if err == nil {
70-
slog.Info("Trying automatic kernel headers download.")
70+
slog.Info("Trying automatic kernel headers download")
7171
kernelDownloadScript, err := builder.KernelDownloadScript(realBuilder, nil, kr)
72+
// Patch kernel download script to echo KERNELDIR.
73+
// We need to capture KERNELDIR to later pass it as env variable to the build.
74+
kernelDownloadScript += "\necho $KERNELDIR"
7275
if err == nil {
7376
out, err := exec.Command("bash", "-c", kernelDownloadScript).Output()
7477
if err == nil {
75-
path := strings.TrimSuffix(string(out), "\n")
78+
// Scan all stdout line by line and
79+
// store last line as KERNELDIR path.
80+
reader := bytes.NewReader(out)
81+
scanner := bufio.NewScanner(reader)
82+
var path string
83+
for scanner.Scan() {
84+
path = scanner.Text()
85+
}
86+
slog.Info("Setting KERNELDIR env var", "path", path)
7687
// add the kerneldir path to env
7788
lbp.envMap[kernelDirEnv] = path
7889
defer func() {
7990
_ = os.RemoveAll("/tmp/kernel-download")
8091
_ = os.RemoveAll(path)
8192
}()
8293
} else {
83-
slog.Warn("Failed to download headers.", "err", err)
94+
slog.Warn("Failed to download headers", "err", err)
8495
}
8596
}
8697
} else {
87-
slog.Info("Skipping kernel headers automatic download.", "err", err)
98+
slog.Info("Skipping kernel headers automatic download", "err", err)
8899
}
89100
}
90101

@@ -137,7 +148,7 @@ func (lbp *LocalBuildProcessor) Start(b *builder.Build) error {
137148
srcProbePath := vv.GetProbeFullPath(c)
138149

139150
if len(lbp.srcDir) == 0 {
140-
slog.Info("Downloading driver sources.")
151+
slog.Info("Downloading driver sources")
141152
// Download src!
142153
libsDownloadScript, err := builder.LibsDownloadScript(c)
143154
if err != nil {
@@ -168,14 +179,14 @@ func (lbp *LocalBuildProcessor) Start(b *builder.Build) error {
168179

169180
stdout, err := cmd.StdoutPipe()
170181
if err != nil {
171-
slog.Warn("Failed to pipe stdout. Trying without piping.", "err", err)
182+
slog.Warn("Failed to pipe stdout", "err", err)
172183
_, err = cmd.CombinedOutput()
173184
} else {
174185
cmd.Stderr = cmd.Stdout // redirect stderr to stdout so that we catch it
175186
defer stdout.Close()
176187
err = cmd.Start()
177188
if err != nil {
178-
slog.Warn("Failed to execute command.", "err", err)
189+
slog.Warn("Failed to execute command", "err", err)
179190
} else {
180191
// print the output of the subprocess line by line
181192
scanner := bufio.NewScanner(stdout)

0 commit comments

Comments
 (0)