summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith84@gmail.com>2016-02-29 21:39:09 -0800
committerTim Smith <tsmith84@gmail.com>2016-02-29 21:39:09 -0800
commitc705a8773c2acf6864777a1d21a21de1a08285dd (patch)
tree55a4aef7b6b9c26021f5b5196103fe9c1891d520
parentac9f5a2ae4ada9f2e6e821418ed3a9039bda9a91 (diff)
downloadohai-c705a8773c2acf6864777a1d21a21de1a08285dd.tar.gz
Replace unreliable host KVM detection with better guest detection
This will detect hvm KVM guests
-rw-r--r--lib/ohai/plugins/linux/virtualization.rb15
-rw-r--r--spec/unit/plugins/linux/virtualization_spec.rb9
2 files changed, 13 insertions, 11 deletions
diff --git a/lib/ohai/plugins/linux/virtualization.rb b/lib/ohai/plugins/linux/virtualization.rb
index d681bc5d..674b892d 100644
--- a/lib/ohai/plugins/linux/virtualization.rb
+++ b/lib/ohai/plugins/linux/virtualization.rb
@@ -64,14 +64,10 @@ Ohai.plugin(:Virtualization) do
# - Additional edge cases likely should not change the above assumptions
# but rather be additive - btm
- # Detect from kernel module
+ # Detect Virtualbox from kernel module
if File.exist?("/proc/modules")
modules = File.read("/proc/modules")
- if modules =~ /^kvm/
- virtualization[:system] = "kvm"
- virtualization[:role] = "host"
- virtualization[:systems][:kvm] = "host"
- elsif modules =~ /^vboxdrv/
+ if modules =~ /^vboxdrv/
virtualization[:system] = "vbox"
virtualization[:role] = "host"
virtualization[:systems][:vbox] = "host"
@@ -95,6 +91,13 @@ Ohai.plugin(:Virtualization) do
end
end
+ # Detect KVM guests via /sys/ data
+ if File.exist?("/sys/devices/virtual/misc/kvm")
+ virtualization[:system] = "kvm"
+ virtualization[:role] = "guest"
+ virtualization[:systems][:kvm] = "guest"
+ end
+
# Detect OpenVZ / Virtuozzo.
# http://wiki.openvz.org/BC_proc_entries
if File.exist?("/proc/bc/0")
diff --git a/spec/unit/plugins/linux/virtualization_spec.rb b/spec/unit/plugins/linux/virtualization_spec.rb
index 91afd507..ff80f665 100644
--- a/spec/unit/plugins/linux/virtualization_spec.rb
+++ b/spec/unit/plugins/linux/virtualization_spec.rb
@@ -77,13 +77,12 @@ describe Ohai::System, "Linux virtualization platform" do
end
describe "when we are checking for kvm" do
- it "sets kvm host if /proc/modules contains kvm" do
- expect(File).to receive(:exist?).with("/proc/modules").and_return(true)
- allow(File).to receive(:read).with("/proc/modules").and_return("kvm 165872 1 kvm_intel")
+ it "sets kvm guest if /sys/devices/virtual/misc/kvm exists" do
+ expect(File).to receive(:exist?).with("/sys/devices/virtual/misc/kvm").and_return(true)
plugin.run
expect(plugin[:virtualization][:system]).to eq("kvm")
- expect(plugin[:virtualization][:role]).to eq("host")
- expect(plugin[:virtualization][:systems][:kvm]).to eq("host")
+ expect(plugin[:virtualization][:role]).to eq("guest")
+ expect(plugin[:virtualization][:systems][:kvm]).to eq("guest")
end
it "sets kvm guest if /proc/cpuinfo contains QEMU Virtual CPU" do