mirror of
https://github.com/kemko/nomad.git
synced 2026-01-08 19:35:41 +03:00
Merge pull request #8721 from code0x9/b-kernel-builtin-module
client/fingerprint: lookup kernel builtin bridge modules
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"regexp"
|
||||
|
||||
"github.com/hashicorp/nomad/nomad/structs"
|
||||
"github.com/shirou/gopsutil/host"
|
||||
)
|
||||
|
||||
const bridgeKernelModuleName = "bridge"
|
||||
@@ -35,19 +36,39 @@ func (f *BridgeFingerprint) Fingerprint(req *FingerprintRequest, resp *Fingerpri
|
||||
}
|
||||
|
||||
func (f *BridgeFingerprint) checkKMod(mod string) error {
|
||||
file, err := os.Open("/proc/modules")
|
||||
hostInfo, err := host.Info()
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not read /proc/modules: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
dynErr := f.checkKModFile(mod, "/proc/modules", fmt.Sprintf("%s\\s+.*$", mod))
|
||||
if dynErr == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
builtinErr := f.checkKModFile(mod,
|
||||
fmt.Sprintf("/lib/modules/%s/modules.builtin", hostInfo.KernelVersion),
|
||||
fmt.Sprintf(".+\\/%s.ko$", mod))
|
||||
if builtinErr == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("%v, %v", dynErr, builtinErr)
|
||||
}
|
||||
|
||||
func (f *BridgeFingerprint) checkKModFile(mod, fileName, pattern string) error {
|
||||
file, err := os.Open(fileName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not read %s: %v", fileName, err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
pattern := fmt.Sprintf("%s\\s+.*$", mod)
|
||||
for scanner.Scan() {
|
||||
if matched, err := regexp.MatchString(pattern, scanner.Text()); matched {
|
||||
return nil
|
||||
} else if err != nil {
|
||||
return fmt.Errorf("could not parse /proc/modules: %v", err)
|
||||
return fmt.Errorf("could not parse %s: %v", fileName, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user