Skip to content

Refine VM detection logic: sanitize MAC addresses, filter non-physical adapters. - #3061

Merged
dbwiddis merged 1 commit into
oshi:masterfrom
asungur23:asungur23-patch-1
Jan 27, 2026
Merged

dbwiddis merged 1 commit into
oshi:masterfrom
asungur23:asungur23-patch-1

Conversation

@asungur23

@asungur23 asungur23 commented Jan 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The current DetectVM implementation incorrectly identifies physical Windows machines as Virtual Machines when Hyper-V, WSL2, or Docker Desktop is enabled. This occurs because the logic detects virtual Ethernet adapters (vEthernet) created by these services, which often utilize Microsoft OUIs, and mistakenly flags the hosting system as a VM.

Changes

This PR updates DetectVM.java to distinguish between physical and virtual network interfaces:

  1. Filter Virtual Adapters: Added a check for !nif.isConnectorPresent(). This leverages OSHI's NetworkIF to skip interfaces that do not have a physical connector (common for virtual switches).
  2. Robust MAC Parsing: improved the MAC address processing logic:
    • Sanitizes the MAC string to remove non-hex characters.
    • Extracts the OUI (first 6 hex digits) safely.
    • Formats string as XX:XX:XX using Locale.ROOT validation to match oshi.vmmacaddr.properties keys reliably.

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced virtual machine detection by adding MAC address-based identification to improve accuracy when identifying VMs.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai

coderabbitai Bot commented Jan 21, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

The DetectVM class's MAC address-based VM detection logic is enhanced to filter network interfaces for physical connectors, sanitize MAC addresses to hex digits, extract OUI identifiers, and perform lookups in a properties mapping before falling back to existing vendor/model checks.

Changes

Cohort / File(s) Summary
MAC Address-based VM Detection Enhancement
oshi-demo/src/main/java/oshi/demo/DetectVM.java
Adds network interface filtering for physical connectors, MAC address sanitization to hex format, OUI extraction from first six hex digits, and OUI-based VM lookup via properties file with formatted XX:XX:XX keys. Falls back to existing model/vendor detection if no OUI match found.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

Poem

🐰 Through virtual networks, MACs now speak clear,
OUI lookups tell us what's really here,
Filtered connectors, hex digits refined,
VM detection wisdom, cleverly designed! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and clearly summarizes the main changes: sanitizing MAC addresses and filtering non-physical adapters to refine VM detection logic.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@oshi-demo/src/main/java/oshi/demo/DetectVM.java`:
- Around line 85-97: Guard against NullPointerException by checking
nif.getMacaddr() for null before calling toUpperCase: fetch the raw MAC string
(e.g., String rawMac = nif.getMacaddr()), if rawMac is null skip this interface;
then normalize using rawMac.toUpperCase(Locale.ROOT) and sanitize to hex-only as
before. Simplify the property lookup by computing the OUI string (formatted as
"%s:%s:%s" from the first 6 hex chars) and using
vmMacAddressProps.getProperty(oui) directly, testing the returned value for null
and returning it if non-null, instead of using containsKey() followed by
getProperty().
📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bc12fba and 692e2b7.

📒 Files selected for processing (1)
  • oshi-demo/src/main/java/oshi/demo/DetectVM.java
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Codacy Static Code Analysis
🔇 Additional comments (1)
oshi-demo/src/main/java/oshi/demo/DetectVM.java (1)

81-84: The filtering logic is sound and working as documented.

isConnectorPresent() is implemented using platform-specific APIs: on Linux, it checks the /carrier sysfs file to indicate cable connectivity (which also applies to connected wireless adapters); on Windows, it checks the physical connector presence bit from the Windows API. Connected wireless adapters will not be filtered out, and the implementation has no documented issues. For the VM detection use case, filtering for connected interfaces is an appropriate approach.

Likely an incorrect or invalid review comment.

✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.

@codecov

codecov Bot commented Jan 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.39%. Comparing base (bc12fba) to head (692e2b7).
⚠️ Report is 7 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3061      +/-   ##
==========================================
+ Coverage   80.24%   85.39%   +5.14%     
==========================================
  Files          32       32              
  Lines        1458     1458              
  Branches      215      215              
==========================================
+ Hits         1170     1245      +75     
+ Misses        190      116      -74     
+ Partials       98       97       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dbwiddis
dbwiddis merged commit 9e16b82 into oshi:master Jan 27, 2026
14 checks passed
dbwiddis pushed a commit to dyorgio/oshi that referenced this pull request Jan 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants