Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions source/kernel/kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package kernel

import (
"fmt"
"regexp"
"strconv"

"k8s.io/klog/v2"
Expand All @@ -40,14 +41,16 @@ const (

// Configuration file options
type Config struct {
KconfigFile string
ConfigOpts []string `json:"configOpts,omitempty"`
KconfigFile string
ConfigOpts []string `json:"configOpts,omitempty"`
ModuleWhiteList string
}

// newDefaultConfig returns a new config with pre-populated defaults
func newDefaultConfig() *Config {
return &Config{
KconfigFile: "",
KconfigFile: "",
ModuleWhiteList: ".*",
ConfigOpts: []string{
"NO_HZ",
"NO_HZ_IDLE",
Expand All @@ -63,7 +66,8 @@ type kernelSource struct {
features *nfdv1alpha1.Features
// legacyKconfig contains mangled kconfig values used for
// kernel.config-<flag> labels and legacy kConfig custom rules.
legacyKconfig map[string]string
legacyKconfig map[string]string
moduleWhiteList *regexp.Regexp
}

// Singleton source instance
Expand All @@ -90,6 +94,11 @@ func (s *kernelSource) SetConfig(conf source.Config) {
default:
panic(fmt.Sprintf("invalid config type: %T", conf))
}

r, err := regexp.Compile(s.config.ModuleWhiteList)
if err == nil {
s.moduleWhiteList = r
}
}

// Priority method of the LabelSource interface
Expand Down Expand Up @@ -138,14 +147,14 @@ func (s *kernelSource) Discover() error {
}

var enabledModules []string
if kmods, err := getLoadedModules(); err != nil {
if kmods, err := getLoadedModules(s); err != nil {
klog.ErrorS(err, "failed to get loaded kernel modules")
} else {
enabledModules = append(enabledModules, kmods...)
s.features.Flags[LoadedModuleFeature] = nfdv1alpha1.NewFlagFeatures(kmods...)
}

if builtinMods, err := getBuiltinModules(); err != nil {
if builtinMods, err := getBuiltinModules(s); err != nil {
klog.ErrorS(err, "failed to get builtin kernel modules")
} else {
enabledModules = append(enabledModules, builtinMods...)
Expand Down
21 changes: 17 additions & 4 deletions source/kernel/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

const kmodProcfsPath = "/proc/modules"

func getLoadedModules() ([]string, error) {
func getLoadedModules(s *kernelSource) ([]string, error) {
out, err := os.ReadFile(kmodProcfsPath)
if err != nil {
return nil, fmt.Errorf("failed to read file %s: %s", kmodProcfsPath, err.Error())
Expand All @@ -40,13 +40,20 @@ func getLoadedModules() ([]string, error) {
if len(line) == 0 {
continue
}

mod := strings.Fields(line)[0]
// Skip if module doesn't match ModuleWhiteList
if !s.moduleWhiteList.MatchString(mod) {
continue
}

// append loaded module
loadedMods = append(loadedMods, strings.Fields(line)[0])
loadedMods = append(loadedMods, mod)
}
return loadedMods, nil
}

func getBuiltinModules() ([]string, error) {
func getBuiltinModules(s *kernelSource) ([]string, error) {
kVersion, err := getVersion()
if err != nil {
return []string{}, err
Expand All @@ -67,8 +74,14 @@ func getBuiltinModules() ([]string, error) {
continue
}

mod := strings.TrimSuffix(path.Base(line), ".ko")
// Skip if module doesn't match ModuleWhiteList
if !s.moduleWhiteList.MatchString(mod) {
continue
}

// append loaded module
builtinMods = append(builtinMods, strings.TrimSuffix(path.Base(line), ".ko"))
builtinMods = append(builtinMods, mod)
}
return builtinMods, nil
}