summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2015-11-25 10:00:57 -0800
committerTim Smith <tsmith@chef.io>2015-11-25 10:00:57 -0800
commit4d467a0a1d733f60744f640cec7ce7e84f8eac2c (patch)
tree84e4b035e7e2d0a697286eb680f90d9f75e977e5
parentf1e35bf1d2352bcec0d8b244fdb575ea031a1ee4 (diff)
parent132d09369cb415ef18d7cdf135d6586efae11a4c (diff)
downloadohai-4d467a0a1d733f60744f640cec7ce7e84f8eac2c.tar.gz
Merge pull request #642 from tas50/windows_virtualization
Detect vbox/vmware/kvm on Windows and speed up runs
-rw-r--r--lib/ohai/plugins/windows/virtualization.rb46
-rw-r--r--spec/unit/plugins/windows/virtualization_spec.rb214
2 files changed, 211 insertions, 49 deletions
diff --git a/lib/ohai/plugins/windows/virtualization.rb b/lib/ohai/plugins/windows/virtualization.rb
index 6090fd3a..100900e3 100644
--- a/lib/ohai/plugins/windows/virtualization.rb
+++ b/lib/ohai/plugins/windows/virtualization.rb
@@ -1,6 +1,8 @@
#
# Author:: Pavel Yudin (<pyudin@parallels.com>)
+# Author:: Tim Smith (<tsmith@chef.io>)
# 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,39 @@
# limitations under the License.
#
-require 'ohai/util/file_helper'
-
-include Ohai::Util::FileHelper
-
Ohai.plugin(:Virtualization) do
- provides "virtualization"
-
- def powershell_exists?
- which('powershell.exe')
- end
+ provides 'virtualization'
collect_data(:windows) do
+ require 'wmi-lite/wmi'
+
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'
+ when 'Bochs'
+ virtualization[:system] = "kvm"
+ virtualization[:role] = "guest"
+ virtualization[:systems][:kvm] = "guest"
+ end
+
+ # vmware fusion detection
+ if bios[0]['serialnumber'] =~ /VMware/
+ virtualization[:system] = 'vmware'
+ virtualization[:role] = 'guest'
+ virtualization[:systems][:vmware] = 'guest'
end
end
end
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 (<pyudin@parallels.com>)
+# Author:: Tim Smith (<tsmith@chef.io>)
# 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