The function attempts to identify which Virtual Machine (VM) based on common VM signatures in MAC address and computer model. @return A string indicating the machine's virtualization info if it can be determined, or an emptry string otherwise.
()
| 84 | * determined, or an emptry string otherwise. |
| 85 | */ |
| 86 | public static String identifyVM() { |
| 87 | SystemInfo si = new SystemInfo(); |
| 88 | HardwareAbstractionLayer hw = si.getHardware(); |
| 89 | // Check CPU Vendor |
| 90 | String vendor = hw.getProcessor().getProcessorIdentifier().getVendor().trim(); |
| 91 | if (vmVendor.containsKey(vendor)) { |
| 92 | return vmVendor.get(vendor); |
| 93 | } |
| 94 | |
| 95 | // Try well known MAC addresses |
| 96 | List<NetworkIF> nifs = hw.getNetworkIFs(); |
| 97 | for (NetworkIF nif : nifs) { |
| 98 | String mac = nif.getMacaddr().toUpperCase(); |
| 99 | String oui = mac.length() > 7 ? mac.substring(0, 8) : mac; |
| 100 | if (vmMacAddressProps.containsKey(oui)) { |
| 101 | return vmMacAddressProps.getProperty(oui); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Try well known models |
| 106 | String model = hw.getComputerSystem().getModel(); |
| 107 | for (String vm : vmModelArray) { |
| 108 | if (model.contains(vm)) { |
| 109 | return vm; |
| 110 | } |
| 111 | } |
| 112 | String manufacturer = hw.getComputerSystem().getManufacturer(); |
| 113 | if ("Microsoft Corporation".equals(manufacturer) && "Virtual Machine".equals(model)) { |
| 114 | return "Microsoft Hyper-V"; |
| 115 | } |
| 116 | |
| 117 | // Couldn't find VM, return empty string |
| 118 | return ""; |
| 119 | } |
| 120 | } |
no test coverage detected