From 739040f234c356133e2cb72318f687e8c0e56b9b Mon Sep 17 00:00:00 2001 From: Donavan Miller Date: Tue, 30 Nov 2010 10:35:17 -0800 Subject: (#4754) Add support for Darwin and Parallels VM to "virtual" fact Adds support for Parallels VM guest detection with existing operating systems. Detects Parallels based on hardware vendor name and pci id. The Parallels vendor id does not seem to be listed in most pci.ids. Adds resolution for "virtual" fact in the Darwin kernel. This uses the existing Facter::Util::Macosx module to resolve system profiler data. Both vendor name and vendor id values are checked. Resolution appears to vary based on VM Host product. Signed-off-by: donavanm --- lib/facter/virtual.rb | 35 ++++++++++++++++---- spec/unit/virtual.rb | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 6 deletions(-) diff --git a/lib/facter/virtual.rb b/lib/facter/virtual.rb index a8afb60..a3b7163 100644 --- a/lib/facter/virtual.rb +++ b/lib/facter/virtual.rb @@ -1,5 +1,23 @@ require 'facter/util/virtual' +Facter.add("virtual") do + confine :kernel => "Darwin" + + setcode do + require 'facter/util/macosx' + result = "physical" + output = Facter::Util::Macosx.profiler_data("SPDisplaysDataType") + if output.is_a?(Hash) + result = "parallels" if output["spdisplays_vendor-id"] =~ /0x1ab8/ + result = "parallels" if output["spdisplays_vendor"] =~ /[Pp]arallels/ + result = "vmware" if output["spdisplays_vendor-id"] =~ /0x15ad/ + result = "vmware" if output["spdisplays_vendor"] =~ /VM[wW]are/ + end + result + end +end + + Facter.add("virtual") do confine :kernel => %w{Linux FreeBSD OpenBSD SunOS HP-UX} @@ -53,18 +71,23 @@ Facter.add("virtual") do # --- look for the vmware video card to determine if it is virtual => vmware. # --- 00:0f.0 VGA compatible controller: VMware Inc [VMware SVGA II] PCI Display Adapter result = "vmware" if p =~ /VM[wW]are/ + # --- look for pci vendor id used by Parallels video card + # --- 01:00.0 VGA compatible controller: Unknown device 1ab8:4005 + result = "parallels" if p =~ /1ab8:|[Pp]arallels/ end else output = Facter::Util::Resolution.exec('dmidecode') if not output.nil? output.each_line do |pd| - result = "vmware" if pd =~ /VMware|Parallels/ + result = "parallels" if pd =~ /Parallels/ + result = "vmware" if pd =~ /VMware/ end else output = Facter::Util::Resolution.exec('prtdiag') if not output.nil? output.each_line do |pd| - result = "vmware" if pd =~ /VMware|Parallels/ + result = "parallels" if pd =~ /Parallels/ + result = "vmware" if pd =~ /VMware/ end end end @@ -78,15 +101,15 @@ Facter.add("virtual") do result end end - + Facter.add("is_virtual") do - confine :kernel => %w{Linux FreeBSD OpenBSD SunOS HP-UX} + confine :kernel => %w{Linux FreeBSD OpenBSD SunOS HP-UX Darwin} setcode do case Facter.value(:virtual) - when "xenu", "openvzve", "vmware", "kvm", "vserver", "jail", "zone", "hpvm" + when "xenu", "openvzve", "vmware", "kvm", "vserver", "jail", "zone", "hpvm", "parallels" "true" - else + else "false" end end diff --git a/spec/unit/virtual.rb b/spec/unit/virtual.rb index a7767c9..d607960 100644 --- a/spec/unit/virtual.rb +++ b/spec/unit/virtual.rb @@ -2,6 +2,7 @@ require File.dirname(__FILE__) + '/../spec_helper' require 'facter' require 'facter/util/virtual' +require 'facter/util/macosx' describe "Virtual fact" do @@ -29,6 +30,91 @@ describe "Virtual fact" do Facter::Util::Virtual.stubs(:hpvm?).returns(true) Facter.fact(:virtual).value.should == "hpvm" end + + describe "on Darwin" do + it "should be parallels with Parallels vendor id" do + Facter.fact(:kernel).stubs(:value).returns("Darwin") + Facter::Util::Macosx.stubs(:profiler_data).returns({ "spdisplays_vendor-id" => "0x1ab8" }) + Facter.fact(:virtual).value.should == "parallels" + end + + it "should be parallels with Parallels vendor name" do + Facter.fact(:kernel).stubs(:value).returns("Darwin") + Facter::Util::Macosx.stubs(:profiler_data).returns({ "spdisplays_vendor" => "Parallels" }) + Facter.fact(:virtual).value.should == "parallels" + end + + it "should be vmware with VMWare vendor id" do + Facter.fact(:kernel).stubs(:value).returns("Darwin") + Facter::Util::Macosx.stubs(:profiler_data).returns({ "spdisplays_vendor-id" => "0x15ad" }) + Facter.fact(:virtual).value.should == "vmware" + end + + it "should be vmware with VMWare vendor name" do + Facter.fact(:kernel).stubs(:value).returns("Darwin") + Facter::Util::Macosx.stubs(:profiler_data).returns({ "spdisplays_vendor" => "VMWare" }) + Facter.fact(:virtual).value.should == "vmware" + end + end + + describe "on Linux" do + before do + Facter::Util::Virtual.stubs(:zone?).returns(false) + Facter::Util::Virtual.stubs(:openvz?).returns(false) + Facter::Util::Virtual.stubs(:vserver?).returns(false) + Facter::Util::Virtual.stubs(:xen?).returns(false) + Facter::Util::Virtual.stubs(:kvm?).returns(false) + end + + it "should be parallels with Parallels vendor id from lspci" do + Facter.fact(:kernel).stubs(:value).returns("Linux") + Facter::Util::Resolution.stubs(:exec).with('lspci').returns("01:00.0 VGA compatible controller: Unknown device 1ab8:4005") + Facter.fact(:virtual).value.should == "parallels" + end + + it "should be parallels with Parallels vendor name from lspci" do + Facter.fact(:kernel).stubs(:value).returns("Linux") + Facter::Util::Resolution.stubs(:exec).with('lspci').returns("01:00.0 VGA compatible controller: Parallels Display Adapter") + Facter.fact(:virtual).value.should == "parallels" + end + + it "should be vmware with VMware vendor name from lspci" do + Facter.fact(:kernel).stubs(:value).returns("Linux") + Facter::Util::Resolution.stubs(:exec).with('lspci').returns("00:0f.0 VGA compatible controller: VMware Inc [VMware SVGA II] PCI Display Adapter") + Facter.fact(:virtual).value.should == "vmware" + end + + it "should be vmware with VMWare vendor name from dmidecode" do + Facter.fact(:kernel).stubs(:value).returns("Linux") + Facter::Util::Resolution.stubs(:exec).with('lspci').returns(nil) + Facter::Util::Resolution.stubs(:exec).with('dmidecode').returns("On Board Device 1 Information\nType: Video\nStatus: Disabled\nDescription: VMware SVGA II") + Facter.fact(:virtual).value.should == "vmware" + end + + it "should be parallels with Parallels vendor name from dmidecode" do + Facter.fact(:kernel).stubs(:value).returns("Linux") + Facter::Util::Resolution.stubs(:exec).with('lspci').returns(nil) + Facter::Util::Resolution.stubs(:exec).with('dmidecode').returns("On Board Device Information\nType: Video\nStatus: Disabled\nDescription: Parallels Video Adapter") + Facter.fact(:virtual).value.should == "parallels" + end + + it "should be vmware with VMWare vendor name from prtdiag" do + Facter.fact(:kernel).stubs(:value).returns("Linux") + Facter::Util::Resolution.stubs(:exec).with('lspci').returns(nil) + Facter::Util::Resolution.stubs(:exec).with('dmidecode').returns(nil) + Facter::Util::Resolution.stubs(:exec).with('prtdiag').returns("System Configuration: VMware, Inc. VMware Virtual Platform") + Facter.fact(:virtual).value.should == "vmware" + end + + it "should be parallels with Parallels vendor name from prtdiag" do + Facter.fact(:kernel).stubs(:value).returns("Linux") + Facter::Util::Resolution.stubs(:exec).with('lspci').returns(nil) + Facter::Util::Resolution.stubs(:exec).with('dmidecode').returns(nil) + Facter::Util::Resolution.stubs(:exec).with('prtdiag').returns("System Configuration: Parallels Virtual Platform") + Facter.fact(:virtual).value.should == "parallels" + end + end + end describe "is_virtual fact" do @@ -85,4 +171,9 @@ describe "is_virtual fact" do Facter.fact(:is_virtual).value.should == "true" end + it "should be true when running on parallels" do + Facter.fact(:kernel).stubs(:value).returns("Darwin") + Facter.fact(:virtual).stubs(:value).returns("parallels") + Facter.fact(:is_virtual).value.should == "true" + end end -- cgit From 1985528c9e70095116f84d4f4849c149602fffec Mon Sep 17 00:00:00 2001 From: Matt Robinson Date: Tue, 30 Nov 2010 10:59:42 -0800 Subject: (#4754) Change is_virtual logic to not enumerate virtual types While looking at the patch for adding parallels to the virtual types David Schmitt noticed that it might be easier just to list the types that are NOT virtual since there's fewer of them. Paired-with: Nick Lewis --- lib/facter/virtual.rb | 3 +-- spec/unit/virtual.rb | 6 ++++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/facter/virtual.rb b/lib/facter/virtual.rb index a3b7163..8412a0a 100644 --- a/lib/facter/virtual.rb +++ b/lib/facter/virtual.rb @@ -106,8 +106,7 @@ Facter.add("is_virtual") do confine :kernel => %w{Linux FreeBSD OpenBSD SunOS HP-UX Darwin} setcode do - case Facter.value(:virtual) - when "xenu", "openvzve", "vmware", "kvm", "vserver", "jail", "zone", "hpvm", "parallels" + if Facter.value(:virtual) != "physical" && Facter.value(:virtual) != "xen0" "true" else "false" diff --git a/spec/unit/virtual.rb b/spec/unit/virtual.rb index d607960..9e8e358 100644 --- a/spec/unit/virtual.rb +++ b/spec/unit/virtual.rb @@ -135,6 +135,12 @@ describe "is_virtual fact" do Facter.fact(:is_virtual).value.should == "false" end + it "should be false when running on physical" do + Facter.fact(:kernel).stubs(:value).returns("Linux") + Facter.fact(:virtual).stubs(:value).returns("physical") + Facter.fact(:is_virtual).value.should == "false" + end + it "should be true when running on vmware" do Facter.fact(:kernel).stubs(:value).returns("Linux") Facter.fact(:virtual).stubs(:value).returns("vmware") -- cgit