Skip to content

Commit 923293e

Browse files
authored
fix(preflights): support for builtin kernel modules (#1737)
* fix(preflights): support for builtin kernel modules * f
1 parent ae2b5d1 commit 923293e

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

pkg/collect/host_kernel_modules.go

+61
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"bufio"
55
"bytes"
66
"encoding/json"
7+
"fmt"
8+
"io"
79
"os"
810
"os/exec"
911
"path/filepath"
@@ -157,6 +159,26 @@ type kernelModulesLoaded struct{}
157159

158160
// collect the list of modules that the kernel is aware of.
159161
func (l kernelModulesLoaded) collect() (map[string]KernelModuleInfo, error) {
162+
modules, err := l.collectProc()
163+
if err != nil {
164+
return nil, fmt.Errorf("proc: %w", err)
165+
}
166+
167+
builtin, err := l.collectBuiltin()
168+
if err != nil {
169+
return nil, fmt.Errorf("builtin: %w", err)
170+
}
171+
172+
for name, module := range builtin {
173+
if _, ok := modules[name]; !ok {
174+
modules[name] = module
175+
}
176+
}
177+
178+
return modules, nil
179+
}
180+
181+
func (l kernelModulesLoaded) collectProc() (map[string]KernelModuleInfo, error) {
160182
modules := make(map[string]KernelModuleInfo)
161183

162184
file, err := os.Open("/proc/modules")
@@ -199,6 +221,45 @@ func (l kernelModulesLoaded) collect() (map[string]KernelModuleInfo, error) {
199221
return modules, nil
200222
}
201223

224+
func (l kernelModulesLoaded) collectBuiltin() (map[string]KernelModuleInfo, error) {
225+
out, err := exec.Command("uname", "-r").Output()
226+
if err != nil {
227+
return nil, errors.Wrap(err, "failed to determine kernel release")
228+
}
229+
kernel := strings.TrimSpace(string(out))
230+
231+
file, err := os.Open(fmt.Sprintf("/usr/lib/modules/%s/modules.builtin", kernel))
232+
if err != nil {
233+
return nil, err
234+
}
235+
defer file.Close()
236+
237+
return l.parseBuiltin(file)
238+
}
239+
240+
func (l kernelModulesLoaded) parseBuiltin(r io.Reader) (map[string]KernelModuleInfo, error) {
241+
modules := make(map[string]KernelModuleInfo)
242+
243+
scanner := bufio.NewScanner(r)
244+
for scanner.Scan() {
245+
_, file := filepath.Split(scanner.Text())
246+
name := strings.TrimSuffix(file, filepath.Ext(file))
247+
248+
if name == "" {
249+
continue
250+
}
251+
252+
modules[name] = KernelModuleInfo{
253+
Status: KernelModuleLoaded,
254+
}
255+
}
256+
if err := scanner.Err(); err != nil {
257+
return nil, err
258+
}
259+
260+
return modules, nil
261+
}
262+
202263
func (c *CollectHostKernelModules) RemoteCollect(progressChan chan<- interface{}) (map[string][]byte, error) {
203264
return nil, ErrRemoteCollectorNotImplemented
204265
}

pkg/collect/host_kernel_modules_test.go

+49
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package collect
22

33
import (
44
"reflect"
5+
"strings"
56
"testing"
67

78
"github.com/pkg/errors"
@@ -164,3 +165,51 @@ func TestCollectHostKernelModules_Collect(t *testing.T) {
164165
})
165166
}
166167
}
168+
169+
func Test_parseBuiltin(t *testing.T) {
170+
tests := []struct {
171+
name string
172+
content string
173+
want map[string]KernelModuleInfo
174+
}{
175+
{
176+
name: "empty",
177+
content: "",
178+
want: map[string]KernelModuleInfo{},
179+
},
180+
{
181+
name: "basic",
182+
content: `kernel/arch/x86/events/rapl.ko
183+
kernel/arch/x86/events/amd/amd-uncore.ko
184+
kernel/arch/x86/events/intel/intel-uncore.ko
185+
kernel/arch/x86/events/intel/intel-cstate.ko`,
186+
want: map[string]KernelModuleInfo{
187+
"rapl": {
188+
Status: KernelModuleLoaded,
189+
},
190+
"amd-uncore": {
191+
Status: KernelModuleLoaded,
192+
},
193+
"intel-uncore": {
194+
Status: KernelModuleLoaded,
195+
},
196+
"intel-cstate": {
197+
Status: KernelModuleLoaded,
198+
},
199+
},
200+
},
201+
}
202+
for _, tt := range tests {
203+
t.Run(tt.name, func(t *testing.T) {
204+
l := kernelModulesLoaded{}
205+
got, err := l.parseBuiltin(strings.NewReader(tt.content))
206+
if err != nil {
207+
t.Errorf("parseBuiltin() error = %v", err)
208+
return
209+
}
210+
if !reflect.DeepEqual(got, tt.want) {
211+
t.Errorf("parseBuiltin() = %v, want %v", got, tt.want)
212+
}
213+
})
214+
}
215+
}

0 commit comments

Comments
 (0)