diff options
author | Tim Smith <tsmith84@gmail.com> | 2016-03-03 22:38:20 -0800 |
---|---|---|
committer | Tim Smith <tsmith84@gmail.com> | 2016-03-07 10:07:12 -0800 |
commit | 0bb34fcc095c140875c1a508718724a5b6153b29 (patch) | |
tree | c770c82f775e5d321e5b7ce5fae99d0e73e1483e | |
parent | 8e724ba201e8322cd8838e668a7c3610d85ec838 (diff) | |
download | ohai-0bb34fcc095c140875c1a508718724a5b6153b29.tar.gz |
Detect guests via sysctl -n kern.vm_guest on FreeBSD
1) This gives us detection of bhyve guests
2) This works when dmidecode isn't installed, which is most of the time
3) This looks at more than just DMI data. They're also looking at
detailed CPU data to determine hyper-v and vmware.
-rw-r--r-- | lib/ohai/plugins/bsd/virtualization.rb | 27 | ||||
-rw-r--r-- | spec/unit/plugins/bsd/virtualization_spec.rb | 45 |
2 files changed, 70 insertions, 2 deletions
diff --git a/lib/ohai/plugins/bsd/virtualization.rb b/lib/ohai/plugins/bsd/virtualization.rb index 479be99f..40ebeaf6 100644 --- a/lib/ohai/plugins/bsd/virtualization.rb +++ b/lib/ohai/plugins/bsd/virtualization.rb @@ -84,6 +84,33 @@ Ohai.plugin(:Virtualization) do Ohai::Log.debug("Virtualization plugin: Guest running on KVM detected") end + # gather hypervisor of guests from sysctl kern.vm_guest + # there are a limited number of hypervisors detected here, BUT it doesn't + # require dmidecode to be installed and dmidecode isn't in freebsd out of the box + so = shell_out("sysctl -n kern.vm_guest") + case so.stdout + when /vmware/ + virtualization[:system] = "vmware" + virtualization[:role] = "guest" + virtualization[:systems][:vmware] = "guest" + Ohai::Log.debug("Virtualization plugin: Guest running on VMware detected") + when /hv/ + virtualization[:system] = "hyperv" + virtualization[:role] = "guest" + virtualization[:systems][:hyperv] = "guest" + Ohai::Log.debug("Virtualization plugin: Guest running on Hyper-V detected") + when /xen/ + virtualization[:system] = "xen" + virtualization[:role] = "guest" + virtualization[:systems][:xen] = "guest" + Ohai::Log.debug("Virtualization plugin: Guest running on Xen detected") + when /bhyve/ + virtualization[:system] = "bhyve" + virtualization[:role] = "guest" + virtualization[:systems][:bhyve] = "guest" + Ohai::Log.debug("Virtualization plugin: Guest running on bhyve detected") + end + # parse dmidecode to discover various virtualization guests if File.exist?("/usr/local/sbin/dmidecode") || File.exist?("/usr/pkg/sbin/dmidecode") guest = guest_from_dmi(shell_out("dmidecode").stdout) diff --git a/spec/unit/plugins/bsd/virtualization_spec.rb b/spec/unit/plugins/bsd/virtualization_spec.rb index 69f38415..1f2c273f 100644 --- a/spec/unit/plugins/bsd/virtualization_spec.rb +++ b/spec/unit/plugins/bsd/virtualization_spec.rb @@ -26,11 +26,12 @@ describe Ohai::System, "BSD virtualization plugin" do allow(@plugin).to receive(:shell_out).with("#{ Ohai.abs_path( "/sbin/kldstat" )}").and_return(mock_shell_out(0, "", "")) allow(@plugin).to receive(:shell_out).with("jls -nd").and_return(mock_shell_out(0, "", "")) allow(@plugin).to receive(:shell_out).with("sysctl -n hw.model").and_return(mock_shell_out(0, "", "")) + allow(@plugin).to receive(:shell_out).with("sysctl -n kern.vm_guest").and_return(mock_shell_out(0, "", "")) allow(File).to receive(:exist?).and_return false end - context "bhyve" do - it "detects we are running bhyve" do + context "when on a bhyve host" do + it "detects we are a host" do allow(File).to receive(:exist?).with("/dev/vmm").and_return true @plugin.run expect(@plugin[:virtualization][:system]).to eq("bhyve") @@ -39,6 +40,16 @@ describe Ohai::System, "BSD virtualization plugin" do end end + context "when on a bhyve guest" do + it "detects we are a guest" do + allow(@plugin).to receive(:shell_out).with("sysctl -n kern.vm_guest").and_return(mock_shell_out(0, "bhyve", "")) + @plugin.run + expect(@plugin[:virtualization][:system]).to eq("bhyve") + expect(@plugin[:virtualization][:role]).to eq("guest") + expect(@plugin[:virtualization][:systems][:bhyve]).to eq("guest") + end + end + context "jails" do it "detects we are in a jail" do allow(@plugin).to receive(:shell_out).with("sysctl -n security.jail.jailed").and_return(mock_shell_out(0, "1", "")) @@ -106,4 +117,34 @@ OUT end end end + + context "when on a xen guest" do + it "detects we are a guest" do + allow(@plugin).to receive(:shell_out).with("sysctl -n kern.vm_guest").and_return(mock_shell_out(0, "xen", "")) + @plugin.run + expect(@plugin[:virtualization][:system]).to eq("xen") + expect(@plugin[:virtualization][:role]).to eq("guest") + expect(@plugin[:virtualization][:systems][:xen]).to eq("guest") + end + end + + context "when on a vmware guest" do + it "detects we are a guest" do + allow(@plugin).to receive(:shell_out).with("sysctl -n kern.vm_guest").and_return(mock_shell_out(0, "vmware", "")) + @plugin.run + 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 on a hyper-v guest" do + it "detects we are a guest" do + allow(@plugin).to receive(:shell_out).with("sysctl -n kern.vm_guest").and_return(mock_shell_out(0, "hv", "")) + @plugin.run + expect(@plugin[:virtualization][:system]).to eq("hyperv") + expect(@plugin[:virtualization][:role]).to eq("guest") + expect(@plugin[:virtualization][:systems][:hyperv]).to eq("guest") + end + end end |