Skip to content

Commit

Permalink
client checks kernel module in /sys/module for WSL2 bridge networking (
Browse files Browse the repository at this point in the history
  • Loading branch information
jeteve authored Jun 6, 2023
1 parent 637ddf5 commit 0d41fb6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/17306.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
client: check kernel module in `/sys/module` to help with WSL2 bridge networking
```
16 changes: 16 additions & 0 deletions client/fingerprint/bridge_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ func (f *BridgeFingerprint) detect(module string) error {
// accumulate errors from every place we might find the module
var errs error

// Check if the module is in /sys/modules
sysfsModulePath := fmt.Sprintf("/sys/module/%s", module)
if err := f.findDir(sysfsModulePath); err != nil {
errs = multierror.Append(errs, err)
} else {
return nil
}

// check if the module has been dynamically loaded
dynamicPath := "/proc/modules"
if err := f.searchFile(module, dynamicPath, f.regexp(dynamicModuleRe, module)); err != nil {
Expand Down Expand Up @@ -89,6 +97,14 @@ func (f *BridgeFingerprint) detect(module string) error {
return errs
}

func (f *BridgeFingerprint) findDir(dirname string) error {
if _, err := os.Stat(dirname); err != nil {
return fmt.Errorf("failed to find %s: %v", dirname, err)
} else {
return nil
}
}

func (f *BridgeFingerprint) searchFile(module, filename string, re *regexp.Regexp) error {
file, err := os.Open(filename)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions client/fingerprint/bridge_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ func TestBridgeFingerprint_detect(t *testing.T) {
ci.Parallel(t)

f := &BridgeFingerprint{logger: testlog.HCLogger(t)}
require.NoError(t, f.detect("ip_tables"))
require.NoError(t, f.detect("kernel")) // kernel should be there.

err := f.detect("nonexistentmodule")
require.Error(t, err)
require.Contains(t, err.Error(), "3 errors occurred")
require.Contains(t, err.Error(), "4 errors occurred")
}

func writeFile(t *testing.T, prefix, content string) string {
Expand Down

0 comments on commit 0d41fb6

Please sign in to comment.