diff options
author | Tim Smith <tsmith@chef.io> | 2016-03-08 20:18:07 -0800 |
---|---|---|
committer | Tim Smith <tsmith@chef.io> | 2016-03-08 20:18:07 -0800 |
commit | 9c722f6d6343c1cd79ff1b9d46078d338d63fda8 (patch) | |
tree | 456fa346f23daabb261b78583329fb7f281b3613 | |
parent | 7fa49d96d38319b7d8284fe38b86bb48985b1f10 (diff) | |
parent | 0bb34fcc095c140875c1a508718724a5b6153b29 (diff) | |
download | ohai-9c722f6d6343c1cd79ff1b9d46078d338d63fda8.tar.gz |
Merge pull request #756 from tas50/freebsd_virtualization
Improve FreeBSD guest virtualization detection
-rw-r--r-- | lib/ohai/plugins/bsd/virtualization.rb | 49 | ||||
-rw-r--r-- | spec/unit/plugins/bsd/virtualization_spec.rb | 56 |
2 files changed, 101 insertions, 4 deletions
diff --git a/lib/ohai/plugins/bsd/virtualization.rb b/lib/ohai/plugins/bsd/virtualization.rb index c8bfe359..40ebeaf6 100644 --- a/lib/ohai/plugins/bsd/virtualization.rb +++ b/lib/ohai/plugins/bsd/virtualization.rb @@ -1,6 +1,7 @@ # # Author:: Bryan McLellan (btm@loftninjas.org) # Copyright:: Copyright (c) 2009 Bryan McLellan +# Copyright:: Copyright (c) 2015-2016 Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -34,13 +35,18 @@ Ohai.plugin(:Virtualization) do virtualization[:system] = "jail" virtualization[:role] = "guest" virtualization[:systems][:jail] = "guest" + Ohai::Log.debug("Virtualization plugin: Guest running in FreeBSD jail detected") end - so = shell_out("jls -n") + # run jls to get a list of running jails + # -n: name=value 1 line per jail format + # -d: list the dying jails as well as active jails + so = shell_out("jls -nd") if (so.stdout || "").lines.count >= 1 virtualization[:system] = "jail" virtualization[:role] = "host" virtualization[:systems][:jail] = "host" + Ohai::Log.debug("Virtualization plugin: Host running FreeBSD jails detected") end # detect from modules @@ -51,20 +57,58 @@ Ohai.plugin(:Virtualization) do virtualization[:system] = "vbox" virtualization[:role] = "host" virtualization[:systems][:vbox] = "host" + Ohai::Log.debug("Virtualization plugin: Guest running on VirtualBox detected") when /vboxguest/ virtualization[:system] = "vbox" virtualization[:role] = "guest" virtualization[:systems][:vbox] = "guest" + Ohai::Log.debug("Virtualization plugin: Host running VirtualBox detected") end end - # Detect KVM/QEMU from cpu, report as KVM + # Detect bhyve by presence of /dev/vmm + if File.exist?("/dev/vmm") + virtualization[:system] = "bhyve" + virtualization[:role] = "host" + virtualization[:systems][:bhyve] = "host" + Ohai::Log.debug("Virtualization plugin: Host running bhyve detected") + end + + # Detect KVM/QEMU paravirt guests from cpu, report as KVM # hw.model: QEMU Virtual CPU version 0.9.1 so = shell_out("sysctl -n hw.model") if so.stdout.split($/)[0] =~ /QEMU Virtual CPU|Common KVM processor|Common 32-bit KVM processor/ virtualization[:system] = "kvm" virtualization[:role] = "guest" virtualization[:systems][:kvm] = "guest" + 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 @@ -74,6 +118,7 @@ Ohai.plugin(:Virtualization) do virtualization[:system] = guest virtualization[:role] = "guest" virtualization[:systems][guest.to_sym] = "guest" + Ohai::Log.debug("Virtualization plugin: Guest running on #{guest} detected") end end end diff --git a/spec/unit/plugins/bsd/virtualization_spec.rb b/spec/unit/plugins/bsd/virtualization_spec.rb index 0df9136e..1f2c273f 100644 --- a/spec/unit/plugins/bsd/virtualization_spec.rb +++ b/spec/unit/plugins/bsd/virtualization_spec.rb @@ -24,8 +24,30 @@ describe Ohai::System, "BSD virtualization plugin" do allow(@plugin).to receive(:collect_os).and_return(:freebsd) allow(@plugin).to receive(:shell_out).with("sysctl -n security.jail.jailed").and_return(mock_shell_out(0, "0", "")) 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 -n").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 "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") + expect(@plugin[:virtualization][:role]).to eq("host") + expect(@plugin[:virtualization][:systems][:bhyve]).to eq("host") + 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 @@ -40,7 +62,7 @@ describe Ohai::System, "BSD virtualization plugin" do it "detects we are hosting jails" do # from http://www.freebsd.org/doc/handbook/jails-application.html @jails = "JID IP Address Hostname Path\n 3 192.168.3.17 ns.example.org /home/j/ns\n 2 192.168.3.18 mail.example.org /home/j/mail\n 1 62.123.43.14 www.example.org /home/j/www" - allow(@plugin).to receive(:shell_out).with("jls -n").and_return(mock_shell_out(0, @jails, "")) + allow(@plugin).to receive(:shell_out).with("jls -nd").and_return(mock_shell_out(0, @jails, "")) @plugin.run expect(@plugin[:virtualization][:system]).to eq("jail") expect(@plugin[:virtualization][:role]).to eq("host") @@ -95,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 |