From 0aa60c87a7b228c2165c7f05987fa6a858cdae56 Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Thu, 29 Oct 2015 23:04:26 -0700 Subject: Detect vbox/vmware on Windows and speed up runs Fetch WMI data within Ruby vs. shelling out to Powershell. This should be WAY faster. --- lib/ohai/plugins/windows/virtualization.rb | 40 ++++++++++++++++++------------ 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/lib/ohai/plugins/windows/virtualization.rb b/lib/ohai/plugins/windows/virtualization.rb index 6090fd3a..b10c747c 100644 --- a/lib/ohai/plugins/windows/virtualization.rb +++ b/lib/ohai/plugins/windows/virtualization.rb @@ -1,6 +1,8 @@ # # Author:: Pavel Yudin () +# Author:: Tim Smith () # Copyright:: Copyright (c) 2015 Pavel Yudin +# Copyright:: Copyright (c) 2015 Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,29 +18,35 @@ # limitations under the License. # -require 'ohai/util/file_helper' - -include Ohai::Util::FileHelper +require 'wmi-lite/wmi' Ohai.plugin(:Virtualization) do - provides "virtualization" - - def powershell_exists? - which('powershell.exe') - end + provides 'virtualization' collect_data(:windows) do virtualization Mash.new unless virtualization virtualization[:systems] = Mash.new unless virtualization[:systems] - # Detect Parallels virtual machine from BIOS information - if powershell_exists? - so = shell_out('powershell.exe "Get-WmiObject -Class Win32_BIOS"') - if so.stdout =~ /Parallels Software International Inc./ - virtualization[:system] = 'parallels' - virtualization[:role] = 'guest' - virtualization[:systems][:parallels] = 'guest' - end + # Grab BIOS data from WMI to determine vendor information + wmi = WmiLite::Wmi.new + bios = wmi.instances_of('Win32_BIOS') + + case bios[0]['manufacturer'] + when 'innotek GmbH' + virtualization[:system] = 'vbox' + virtualization[:role] = 'guest' + virtualization[:systems][:vbox] = 'guest' + when 'Parallels Software International Inc.' + virtualization[:system] = 'parallels' + virtualization[:role] = 'guest' + virtualization[:systems][:parallels] = 'guest' + end + + # vmware fusion detection + if bios[0]['serialnumber'] == /VMware/ + virtualization[:system] = 'vmware' + virtualization[:role] = 'guest' + virtualization[:systems][:vmware] = 'guest' end end end -- cgit v1.2.1 From 21ca40a87fd3cc46ba464f2c1ee307fb542b8f85 Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Thu, 29 Oct 2015 23:48:35 -0700 Subject: Only load wmi on Windows --- lib/ohai/plugins/windows/virtualization.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ohai/plugins/windows/virtualization.rb b/lib/ohai/plugins/windows/virtualization.rb index b10c747c..790f0c66 100644 --- a/lib/ohai/plugins/windows/virtualization.rb +++ b/lib/ohai/plugins/windows/virtualization.rb @@ -18,12 +18,12 @@ # limitations under the License. # -require 'wmi-lite/wmi' - Ohai.plugin(:Virtualization) do provides 'virtualization' collect_data(:windows) do + require 'wmi-lite/wmi' + virtualization Mash.new unless virtualization virtualization[:systems] = Mash.new unless virtualization[:systems] -- cgit v1.2.1 From 065259a390e0c3170362f8b7e1d936341ebb98b3 Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Mon, 23 Nov 2015 22:40:07 -0800 Subject: Fix Vmware detection and detect KVM --- lib/ohai/plugins/windows/virtualization.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/ohai/plugins/windows/virtualization.rb b/lib/ohai/plugins/windows/virtualization.rb index 790f0c66..100900e3 100644 --- a/lib/ohai/plugins/windows/virtualization.rb +++ b/lib/ohai/plugins/windows/virtualization.rb @@ -40,10 +40,14 @@ Ohai.plugin(:Virtualization) do virtualization[:system] = 'parallels' virtualization[:role] = 'guest' virtualization[:systems][:parallels] = 'guest' + when 'Bochs' + virtualization[:system] = "kvm" + virtualization[:role] = "guest" + virtualization[:systems][:kvm] = "guest" end # vmware fusion detection - if bios[0]['serialnumber'] == /VMware/ + if bios[0]['serialnumber'] =~ /VMware/ virtualization[:system] = 'vmware' virtualization[:role] = 'guest' virtualization[:systems][:vmware] = 'guest' -- cgit v1.2.1 From 132d09369cb415ef18d7cdf135d6586efae11a4c Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Mon, 23 Nov 2015 23:24:21 -0800 Subject: Virtualization specs for windows --- spec/unit/plugins/windows/virtualization_spec.rb | 214 +++++++++++++++++++---- 1 file changed, 182 insertions(+), 32 deletions(-) diff --git a/spec/unit/plugins/windows/virtualization_spec.rb b/spec/unit/plugins/windows/virtualization_spec.rb index 213aba13..2e4fd41f 100644 --- a/spec/unit/plugins/windows/virtualization_spec.rb +++ b/spec/unit/plugins/windows/virtualization_spec.rb @@ -1,6 +1,8 @@ # # Author:: Pavel Yudin () +# Author:: Tim Smith () # Copyright:: Copyright (c) 2015 Pavel Yudin +# Copyright:: Copyright (c) 2015 Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,47 +20,195 @@ require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper.rb') -describe Ohai::System, "Windows virtualization platform" do - let(:plugin) { get_plugin("windows/virtualization")} +describe Ohai::System, 'Windows virtualization platform' do + let(:plugin) { get_plugin('windows/virtualization') } before(:each) do allow(plugin).to receive(:collect_os).and_return(:windows) - allow(plugin).to receive(:powershell_exists?).and_return(false) end - describe "when we are checking for parallels" do - it "should set parallels guest if powershell exists and it's output contains 'Parallels Software International Inc.'" do - allow(plugin).to receive(:powershell_exists?).and_return(true) - bios=<<-BIOS -SMBIOSBIOSVersion : 10.2.0 (28956) rev 0 -Manufacturer : Parallels Software International Inc. -Name : Default System BIOS -SerialNumber : Parallels-92 05 B4 56 97 11 4F FA B1 95 1A FF 8E F9 DD CE -Version : PRLS - 1 - BIOS - shellout = double("shellout") - allow(shellout).to receive(:stdout).and_return(bios) - allow(plugin).to receive(:shell_out).with('powershell.exe "Get-WmiObject -Class Win32_BIOS"').and_return(shellout) + context 'when running on vmware' do + it 'system is vmware' do + allow_any_instance_of(WmiLite::Wmi).to receive(:instances_of).with('Win32_BIOS').and_return([{ 'bioscharacteristics' => [4, 7, 8, 9, 10, 11, 12, 14, 15, 16, 19, 26, 27, 28, 29, 30, 32, 39, 40, 41, 42, 50, 57, 58], + 'biosversion' => ['INTEL - 6040000', 'PhoenixBIOS 4.0 Release 6.0 '], + 'buildnumber' => nil, + 'caption' => 'PhoenixBIOS 4.0 Release 6.0 ', + 'codeset' => nil, 'currentlanguage' => nil, + 'description' => 'PhoenixBIOS 4.0 Release 6.0 ', + 'identificationcode' => nil, + 'installablelanguages' => nil, + 'installdate' => nil, + 'languageedition' => nil, + 'listoflanguages' => nil, + 'manufacturer' => 'Phoenix Technologies LTD', + 'name' => 'PhoenixBIOS 4.0 Release 6.0 ', + 'othertargetos' => nil, + 'primarybios' => true, + 'releasedate' => '20130731000000.000000+000', + 'serialnumber' => 'VMware-56 4d 65 24 ac cf ec 72-fa 29 b2 7d 8f df b2 7a', + 'smbiosbiosversion' => '6.00', + 'smbiosmajorversion' => 2, + 'smbiosminorversion' => 4, + 'smbiospresent' => true, + 'softwareelementid' => 'PhoenixBIOS 4.0 Release 6.0 ', + 'softwareelementstate' => 3, + 'status' => 'OK', + 'targetoperatingsystem' => 0, + 'version' => 'INTEL - 6040000' + }]) plugin.run - expect(plugin[:virtualization][:system]).to eq("parallels") - expect(plugin[:virtualization][:role]).to eq("guest") - expect(plugin[:virtualization][:systems][:parallels]).to eq("guest") + expect(plugin[:virtualization][:system]).to eq('vmware') + expect(plugin[:virtualization][:role]).to eq('guest') + expect(plugin[:virtualization][:systems][:vmware]).to eq('guest') end + end + + context 'when running on parallels desktop' do + it 'system is parallels' do + allow_any_instance_of(WmiLite::Wmi).to receive(:instances_of).with('Win32_BIOS').and_return([{ 'bioscharacteristics' => [4, 7, 9, 10, 15, 24, 25, 27, 28, 29, 30, 32, 42, 44, 48, 49, 51, 64, 65, 67], + 'biosversion' => ['PRLS - 1'], + 'buildnumber' => nil, + 'caption' => 'Default System BIOS', + 'codeset' => nil, + 'currentlanguage' => nil, + 'description' => 'Default System BIOS', + 'identificationcode' => nil, + 'installablelanguages' => nil, + 'installdate' => nil, + 'languageedition' => nil, + 'listoflanguages' => nil, + 'manufacturer' => 'Parallels Software International Inc.', + 'name' => 'Default System BIOS', + 'othertargetos' => nil, + 'primarybios' => true, + 'releasedate' => '20151005000000.000000+000', + 'serialnumber' => 'Parallels-82 75 A0 A0 9B B4 47 7C 87 A9 D9 E1 2B 90 4B 1F', + 'smbiosbiosversion' => '11.0.2 (31348)', + 'smbiosmajorversion' => 2, + 'smbiosminorversion' => 7, + 'smbiospresent' => true, + 'softwareelementid' => 'Default System BIOS', + 'softwareelementstate' => 3, + 'status' => 'OK', + 'targetoperatingsystem' => 0, + 'version' => 'PRLS - 1' + }]) + plugin.run + expect(plugin[:virtualization][:system]).to eq('parallels') + expect(plugin[:virtualization][:role]).to eq('guest') + expect(plugin[:virtualization][:systems][:parallels]).to eq('guest') + end + end + + context 'when running on kvm' do + it 'system is kvm' do + allow_any_instance_of(WmiLite::Wmi).to receive(:instances_of).with('Win32_BIOS').and_return([{ 'bioscharacteristics' => [3, 42, 48, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79], + 'biosversion' => ['BOCHS - 1'], + 'buildnumber' => nil, + 'caption' => 'Default System BIOS', + 'codeset' => nil, + 'currentlanguage' => nil, + 'description' => 'Default System BIOS', + 'identificationcode' => nil, + 'installablelanguages' => nil, + 'installdate' => nil, + 'languageedition' => nil, + 'listoflanguages' => nil, + 'manufacturer' => 'Bochs', + 'name' => 'Default System BIOS', + 'othertargetos' => nil, + 'primarybios' => true, + 'releasedate' => '20110101******.******+***', + 'serialnumber' => nil, + 'smbiosbiosversion' => 'Bochs', + 'smbiosmajorversion' => 2, + 'smbiosminorversion' => 4, + 'smbiospresent' => true, + 'softwareelementid' => 'Default System BIOS', + 'softwareelementstate' => 3, + 'status' => 'OK', + 'targetoperatingsystem' => 0, 'version' => 'BOCHS -1' + }]) + plugin.run + expect(plugin[:virtualization][:system]).to eq('kvm') + expect(plugin[:virtualization][:role]).to eq('guest') + expect(plugin[:virtualization][:systems][:kvm]).to eq('guest') + end + end + + context 'when running on virtualbox' do + it 'system is vbox' do + allow_any_instance_of(WmiLite::Wmi).to receive(:instances_of).with('Win32_BIOS').and_return([{ 'bioscharacteristics' => [4, 7, 15, 16, 27, 30, 32], + 'biosversion' => ['VBOX - 1'], + 'buildnumber' => nil, + 'caption' => 'Default System BIOS', + 'codeset' => nil, + 'currentlanguage' => nil, + 'description' => 'Default System BIOS', + 'identificationcode' => nil, + 'installablelanguages' => nil, + 'installdate' => nil, + 'languageedition' => nil, + 'listoflanguages' => nil, + 'manufacturer' => 'innotek GmbH', + 'name' => 'Default System BIOS', + 'othertargetos' => nil, + 'primarybios' => true, + 'releasedate' => '20061201000000.000000+000', + 'serialnumber' => '0', + 'smbiosbiosversion' => 'VirtualBox', + 'smbiosmajorversion' => 2, + 'smbiosminorversion' => 5, + 'smbiospresent' => true, + 'softwareelementid' => 'Default System BIOS', + 'softwareelementstate' => 3, + 'status' => 'OK', + 'targetoperatingsystem' => 0, + 'version' => 'VBOX - 1' + }]) + plugin.run + expect(plugin[:virtualization][:system]).to eq('vbox') + expect(plugin[:virtualization][:role]).to eq('guest') + expect(plugin[:virtualization][:systems][:vbox]).to eq('guest') + end + end - it "should not set parallels guest if powershell exists and it's output not contain 'Parallels Software International Inc.'" do - allow(plugin).to receive(:ioreg_exists?).and_return(true) - bios=<<-BIOS -SMBIOSBIOSVersion : 4.6.5 -Manufacturer      : American Megatrends Inc. -Name              : BIOS Date: 10/23/12 15:38:23 Ver: 04.06.05 -SerialNumber      : 334281-001 -Version           : Dealin - 1072009 - BIOS - shellout = double("shellout") - allow(shellout).to receive(:stdout).and_return(bios) - allow(plugin).to receive(:shell_out).with('powershell.exe "Get-WmiObject -Class Win32_BIOS"').and_return(shellout) + context 'when running on a hardware system' do + it 'does not set virtualization attributes' do + allow_any_instance_of(WmiLite::Wmi).to receive(:instances_of).with('Win32_BIOS').and_return([{ 'bioscharacteristics' => [7, 11, 12, 15, 16, 17, 19, 23, 24, 25, 26, 27, 28, 29, 32, 33, 40, 42, 43], + 'biosversion' => ['DELL - 1072009', 'A10', 'American Megatrends - 4028D'], + 'buildnumber' => nil, + 'caption' => 'A10', + 'codeset' => nil, + 'currentlanguage' => nil, + 'description' => 'A10', + 'embeddedcontrollermajorversion' => 255, + 'embeddedcontrollerminorversion' => 255, + 'identificationcode' => nil, + 'installablelanguages' => nil, + 'installdate' => nil, + 'languageedition' => nil, + 'listoflanguages' => nil, + 'manufacturer' => 'Dell Inc.', + 'name' => 'A10', + 'othertargetos' => nil, + 'primarybios' => true, + 'releasedate' => '20130513000000.000000+000', + 'serialnumber' => '87GBNY1', + 'smbiosbiosversion' => 'A10', + 'smbiosmajorversion' => 2, + 'smbiosminorversion' => 7, + 'smbiospresent' => true, + 'softwareelementid' => 'A10', + 'softwareelementstate' => 3, + 'status' => 'OK', + 'systembiosmajorversion' => 4, + 'systembiosminorversion' => 6, + 'targetoperatingsystem' => 0, + 'version' => 'DELL - 1072009' + }]) plugin.run - expect(plugin[:virtualization]).to eq({'systems' => {}}) + expect(plugin[:virtualization]).to eq('systems' => {}) end end end -- cgit v1.2.1