open-nomad/client/fingerprint/bridge_linux.go

104 lines
2.6 KiB
Go
Raw Normal View History

2020-05-15 15:09:01 +00:00
package fingerprint
import (
"bufio"
"fmt"
"os"
"regexp"
"github.com/hashicorp/go-multierror"
2020-05-15 15:09:01 +00:00
"github.com/hashicorp/nomad/nomad/structs"
2020-08-24 02:09:13 +00:00
"github.com/shirou/gopsutil/host"
2020-05-15 15:09:01 +00:00
)
const bridgeKernelModuleName = "bridge"
const (
dynamicModuleRe = `%s\s+.*$`
builtinModuleRe = `.+/%s.ko$`
dependsModuleRe = `.+/%s.ko:.*$`
)
2020-05-15 15:09:01 +00:00
func (f *BridgeFingerprint) Fingerprint(req *FingerprintRequest, resp *FingerprintResponse) error {
if err := f.detect(bridgeKernelModuleName); err != nil {
2020-05-15 15:09:01 +00:00
f.logger.Warn("failed to detect bridge kernel module, bridge network mode disabled", "error", err)
return nil
}
resp.NodeResources = &structs.NodeResources{
Networks: []*structs.NetworkResource{{
Mode: "bridge",
}},
NodeNetworks: []*structs.NodeNetworkResource{{
Mode: "bridge",
Device: req.Config.BridgeNetworkName,
}},
2020-05-15 15:09:01 +00:00
}
2020-05-15 15:09:01 +00:00
resp.Detected = true
return nil
}
func (f *BridgeFingerprint) regexp(pattern, module string) *regexp.Regexp {
return regexp.MustCompile(fmt.Sprintf(pattern, module))
}
func (f *BridgeFingerprint) detect(module string) error {
// accumulate errors from every place we might find the module
var errs error
// check if the module has been dynamically loaded
dynamicPath := "/proc/modules"
if err := f.searchFile(module, dynamicPath, f.regexp(dynamicModuleRe, module)); err != nil {
errs = multierror.Append(errs, err)
} else {
return nil
}
// will need kernel info to look for builtin and unloaded modules
2020-08-24 03:24:16 +00:00
hostInfo, err := host.Info()
if err != nil {
return err
}
// check if the module is builtin to the kernel
builtinPath := fmt.Sprintf("/lib/modules/%s/modules.builtin", hostInfo.KernelVersion)
if err := f.searchFile(module, builtinPath, f.regexp(builtinModuleRe, module)); err != nil {
errs = multierror.Append(errs, err)
} else {
2020-08-24 02:09:13 +00:00
return nil
}
// check if the module is dynamic but unloaded (will have a dep entry)
dependsPath := fmt.Sprintf("/lib/modules/%s/modules.dep", hostInfo.KernelVersion)
if err := f.searchFile(module, dependsPath, f.regexp(dependsModuleRe, module)); err != nil {
errs = multierror.Append(errs, err)
} else {
2020-08-24 02:09:13 +00:00
return nil
}
return errs
2020-08-24 02:09:13 +00:00
}
func (f *BridgeFingerprint) searchFile(module, filename string, re *regexp.Regexp) error {
file, err := os.Open(filename)
2020-08-24 02:09:13 +00:00
if err != nil {
return fmt.Errorf("failed to open %s: %v", filename, err)
2020-08-24 02:09:13 +00:00
}
defer func() {
_ = file.Close()
}()
2020-08-24 02:09:13 +00:00
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if re.MatchString(scanner.Text()) {
return nil // found the module!
2020-08-24 02:09:13 +00:00
}
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("failed to scan %s: %v", filename, err)
}
2020-08-24 02:09:13 +00:00
return fmt.Errorf("module %s not in %s", module, filename)
2020-05-15 15:09:01 +00:00
}