diff options
-rw-r--r-- | lib/facter/util/virtual.rb | 17 | ||||
-rw-r--r-- | lib/facter/virtual.rb | 6 | ||||
-rw-r--r-- | spec/unit/util/virtual.rb | 7 | ||||
-rw-r--r-- | spec/unit/virtual.rb | 6 |
4 files changed, 35 insertions, 1 deletions
diff --git a/lib/facter/util/virtual.rb b/lib/facter/util/virtual.rb index 0c3fb73..ca7c367 100644 --- a/lib/facter/util/virtual.rb +++ b/lib/facter/util/virtual.rb @@ -40,4 +40,21 @@ module Facter::Util::Virtual FileTest.exists?(f) end end + + def self.kvm? + if FileTest.exists?("/proc/cpuinfo") + txt = File.read("/proc/cpuinfo") + return true if txt =~ /QEMU Virtual CPU/ + end + return false + end + + def self.kvm_type + # TODO Tell the difference between kvm and qemu + # Can't work out a way to do this at the moment that doesn't + # require a special binary + "kvm" + end + + end diff --git a/lib/facter/virtual.rb b/lib/facter/virtual.rb index 78413a9..3f02003 100644 --- a/lib/facter/virtual.rb +++ b/lib/facter/virtual.rb @@ -34,6 +34,10 @@ Facter.add("virtual") do end end + if Facter::Util::Virtual.kvm? + result = Facter::Util::Virtual.kvm_type() + end + if result == "physical" output = Facter::Util::Resolution.exec('lspci') if not output.nil? @@ -72,7 +76,7 @@ Facter.add("is_virtual") do setcode do case Facter.value(:virtual) - when "xenu", "openvzve", "vmware" + when "xenu", "openvzve", "vmware", "kvm" true else false diff --git a/spec/unit/util/virtual.rb b/spec/unit/util/virtual.rb index 1f1c0f8..de339b8 100644 --- a/spec/unit/util/virtual.rb +++ b/spec/unit/util/virtual.rb @@ -93,4 +93,11 @@ describe Facter::Util::Virtual do FileTest.expects(:exists?).with("/proc/xen").returns(false) Facter::Util::Virtual.should_not be_xen end + + it "should detect kvm" do + FileTest.stubs(:exists?).with("/proc/cpuinfo").returns(true) + File.stubs(:read).with("/proc/cpuinfo").returns("model name : QEMU Virtual CPU version 0.9.1\n") + Facter::Util::Virtual.should be_kvm + end + end diff --git a/spec/unit/virtual.rb b/spec/unit/virtual.rb index cc72ffc..fe9988e 100644 --- a/spec/unit/virtual.rb +++ b/spec/unit/virtual.rb @@ -48,4 +48,10 @@ describe "is_virtual fact" do Facter.fact(:virtual).stubs(:value).returns("openvzve") Facter.fact(:is_virtual).value.should == true end + + it "should be true when running on kvm" do + Facter.fact(:virtual).stubs(:value).returns("kvm") + Facter.fact(:is_virtual).value.should == true + end + end |