[PATCH/facter 1/2] Fix #4352 - Support for detecting virtuals (jails) on FreeBSD

3 views
Skip to first unread message

Jiri Kubicek

unread,
Jul 25, 2010, 6:50:35 PM7/25/10
to puppe...@googlegroups.com
There was no support for detecting FreeBSD jails as a virtual in facter. This patch detects jail by getting "security.jail.jailed" kernel state via sysctl.

Signed-off-by: Jiri Kubicek <jiri.k...@kraxnet.cz>
---
lib/facter/util/virtual.rb | 3 +++
lib/facter/virtual.rb | 6 +++++-
spec/unit/util/virtual.rb | 10 ++++++++++
spec/unit/virtual.rb | 6 ++++++
4 files changed, 24 insertions(+), 1 deletions(-)

diff --git a/lib/facter/util/virtual.rb b/lib/facter/util/virtual.rb
index 80f4e2c..900375f 100644
--- a/lib/facter/util/virtual.rb
+++ b/lib/facter/util/virtual.rb
@@ -57,5 +57,8 @@ module Facter::Util::Virtual
"kvm"
end

+ def self.jail?
+ Facter::Util::Resolution.exec("/sbin/sysctl -n security.jail.jailed") == "1"
+ end

end
diff --git a/lib/facter/virtual.rb b/lib/facter/virtual.rb
index c6d0f22..c14a715 100644
--- a/lib/facter/virtual.rb
+++ b/lib/facter/virtual.rb
@@ -38,6 +38,10 @@ Facter.add("virtual") do
result = Facter::Util::Virtual.kvm_type()
end

+ if Facter.value(:kernel)=="FreeBSD"
+ result = "jail" if Facter::Util::Virtual.jail?
+ end
+
if result == "physical"
output = Facter::Util::Resolution.exec('lspci')
if not output.nil?
@@ -76,7 +80,7 @@ Facter.add("is_virtual") do

setcode do
case Facter.value(:virtual)
- when "xenu", "openvzve", "vmware", "kvm", "vserver"
+ when "xenu", "openvzve", "vmware", "kvm", "vserver", "jail"
true
else
false
diff --git a/spec/unit/util/virtual.rb b/spec/unit/util/virtual.rb
index de339b8..f1ccf1e 100644
--- a/spec/unit/util/virtual.rb
+++ b/spec/unit/util/virtual.rb
@@ -100,4 +100,14 @@ describe Facter::Util::Virtual do
Facter::Util::Virtual.should be_kvm
end

+ it "should identify FreeBSD jail when in jail" do
+ Facter::Util::Resolution.stubs(:exec).with("/sbin/sysctl -n security.jail.jailed").returns("1")
+ Facter::Util::Virtual.should be_jail
+ end
+
+ it "should not identify FreeBSD jail when not in jail" do
+ Facter::Util::Resolution.stubs(:exec).with("/sbin/sysctl -n security.jail.jailed").returns("0")
+ Facter::Util::Virtual.should_not be_jail
+ end
+
end
diff --git a/spec/unit/virtual.rb b/spec/unit/virtual.rb
index 7dbd146..311001d 100644
--- a/spec/unit/virtual.rb
+++ b/spec/unit/virtual.rb
@@ -55,4 +55,10 @@ describe "is_virtual fact" do
Facter.fact(:is_virtual).value.should == true
end

+ it "should be true when running in jail" do
+ Facter.fact(:kernel).stubs(:value).returns("FreeBSD")
+ Facter.fact(:virtual).stubs(:value).returns("jail")
+ Facter.fact(:is_virtual).value.should == true
+ end
+
end
--
1.6.4.3

Jiri Kubicek

unread,
Jul 25, 2010, 6:50:36 PM7/25/10
to puppe...@googlegroups.com
There was no support for detecting FreeBSD running in KVM as a virtual in facter. This patch detects KVM by getting "hw.model" kernel state via sysctl. Jails running in KVM are also correctly detected as "jail" not "kvm".

Signed-off-by: Jiri Kubicek <jiri.k...@kraxnet.cz>
---

lib/facter/util/virtual.rb | 9 +++++----
spec/unit/util/virtual.rb | 6 ++++++
spec/unit/virtual.rb | 7 +++++++
3 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/lib/facter/util/virtual.rb b/lib/facter/util/virtual.rb
index 900375f..2d18c33 100644
--- a/lib/facter/util/virtual.rb
+++ b/lib/facter/util/virtual.rb
@@ -43,11 +43,12 @@ module Facter::Util::Virtual
end

def self.kvm?
- if FileTest.exists?("/proc/cpuinfo")
- txt = File.read("/proc/cpuinfo")
- return true if txt =~ /QEMU Virtual CPU/
+ txt = if FileTest.exists?("/proc/cpuinfo")
+ File.read("/proc/cpuinfo")
+ elsif Facter.value(:kernel)=="FreeBSD"
+ Facter::Util::Resolution.exec("/sbin/sysctl -n hw.model")
end
- return false
+ (txt =~ /QEMU Virtual CPU/) ? true : false
end

def self.kvm_type
diff --git a/spec/unit/util/virtual.rb b/spec/unit/util/virtual.rb
index f1ccf1e..5b59cf9 100644
--- a/spec/unit/util/virtual.rb
+++ b/spec/unit/util/virtual.rb
@@ -100,6 +100,12 @@ describe Facter::Util::Virtual do


Facter::Util::Virtual.should be_kvm
end

+ it "should detect kvm on FreeBSD" do


+ Facter.fact(:kernel).stubs(:value).returns("FreeBSD")

+ Facter::Util::Resolution.stubs(:exec).with("/sbin/sysctl -n hw.model").returns("QEMU Virtual CPU version 0.12.4")
+ Facter::Util::Virtual.should be_kvm
+ end


+
it "should identify FreeBSD jail when in jail" do

Facter::Util::Resolution.stubs(:exec).with("/sbin/sysctl -n security.jail.jailed").returns("1")

Facter::Util::Virtual.should be_jail
diff --git a/spec/unit/virtual.rb b/spec/unit/virtual.rb
index 311001d..8ee843b 100644
--- a/spec/unit/virtual.rb
+++ b/spec/unit/virtual.rb
@@ -17,6 +17,13 @@ describe "Virtual fact" do
Facter.fact(:virtual).value.should == "zone"
end

+ it "should be jail on FreeBSD when a jail in kvm" do


+ Facter.fact(:kernel).stubs(:value).returns("FreeBSD")

+ Facter::Util::Virtual.stubs(:jail?).returns(true)
+ Facter::Util::Virtual.stubs(:kvm?).returns(true)
+ Facter.fact(:virtual).value.should == "jail"
+ end
+
end

describe "is_virtual fact" do
--
1.6.4.3

Paul Nasrat

unread,
Jul 26, 2010, 2:44:25 AM7/26/10
to puppe...@googlegroups.com
On 25 July 2010 23:50, Jiri Kubicek <jiri.k...@kraxnet.cz> wrote:
> There was no support for detecting FreeBSD jails as a virtual in facter. This patch detects jail by getting "security.jail.jailed" kernel state via sysctl.

Thanks, on a scan looks good, but I'll try test later today.

Paul

> --
> You received this message because you are subscribed to the Google Groups "Puppet Developers" group.
> To post to this group, send email to puppe...@googlegroups.com.
> To unsubscribe from this group, send email to puppet-dev+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/puppet-dev?hl=en.
>
>

Paul Nasrat

unread,
Jul 26, 2010, 2:45:10 AM7/26/10
to puppe...@googlegroups.com
On 25 July 2010 23:50, Jiri Kubicek <jiri.k...@kraxnet.cz> wrote:
> There was no support for detecting FreeBSD running in KVM as a virtual in facter. This patch detects KVM by getting "hw.model" kernel state via sysctl. Jails running in KVM are also correctly detected as "jail" not "kvm".

Looks good, as with other patch once I've had a chance to test will merge.

Thanks

Paul

Reply all
Reply to author
Forward
0 new messages