Repository navigation
Refine VM detection logic: sanitize MAC addresses, filter non-physical adapters. - #3061
Conversation
…n-physical adapters
📝 WalkthroughWalkthroughThe 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
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related issues
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Comment |
There was a problem hiding this comment.
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
📒 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/carriersysfs 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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
…n-physical adapters (oshi#3061)
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:
!nif.isConnectorPresent(). This leverages OSHI's NetworkIF to skip interfaces that do not have a physical connector (common for virtual switches).XX:XX:XXusingLocale.ROOTvalidation to match oshi.vmmacaddr.properties keys reliably.Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.