summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric G. Wolfe <eric.wolfe@gmail.com>2014-03-06 11:58:25 -0500
committeradamedx <adamed@opscode.com>2014-03-31 09:48:27 -0700
commita736eaab5c2fea98d3a0930af499774dab966ea3 (patch)
treec3e58de2b8e19751b01101720400b84ee8649537
parent5b183e382f28e9b95e486a58176de1eb24657c89 (diff)
downloadohai-a736eaab5c2fea98d3a0930af499774dab966ea3.tar.gz
Check specifically for /lxc/ cgroup
* If cgroup is named lxc, with a container name (hex/alpha/digit/dashes), then its an lxc guest. * If cgroup is named arbitrarily, then its not necessarily an lxc guest. * If cgroup capabilities exist, and the cgroup is root (/), then its an lxc host.
-rw-r--r--lib/ohai/plugins/linux/virtualization.rb14
-rw-r--r--spec/unit/plugins/linux/virtualization_spec.rb37
2 files changed, 36 insertions, 15 deletions
diff --git a/lib/ohai/plugins/linux/virtualization.rb b/lib/ohai/plugins/linux/virtualization.rb
index 92b8e257..16b0c7bf 100644
--- a/lib/ohai/plugins/linux/virtualization.rb
+++ b/lib/ohai/plugins/linux/virtualization.rb
@@ -128,12 +128,16 @@ Ohai.plugin(:Virtualization) do
end
# Detect LXC/Docker
- # Pattern will vary by platform, and init system.
- # Generally, it will look like this inside a container:
+ #
+ # /proc/self/cgroup will look like this inside a docker container:
# <index #>:<subsystem>:/lxc/<hexadecimal container id>
#
- # Cgroup name, could be arbitrary and named 'Charlie', instead of 'lxc' however.
- # <index #>:<subsystem>:/<cgroup name>/<hexadecimal container id>
+ # /proc/self/cgroup could have a name including alpha/digit/dashes
+ # <index #>:<subsystem>:/lxc/<named container id>
+ #
+ # /proc/self/cgroup could have a non-lxc cgroup name indicating other uses
+ # of cgroups. This is probably not LXC/Docker.
+ # <index #>:<subsystem>:/Charlie
#
# A host which supports cgroups, and has capacity to host lxc containers,
# will show the subsystems and root (/) namespace.
@@ -142,7 +146,7 @@ Ohai.plugin(:Virtualization) do
# Full notes, https://tickets.opscode.com/browse/OHAI-551
# Kernel docs, https://www.kernel.org/doc/Documentation/cgroups
if File.exists?("/proc/self/cgroup")
- if File.read("/proc/self/cgroup") =~ %r{^\d+:.+:/.+/[\w\d]+$}
+ if File.read("/proc/self/cgroup") =~ %r{^\d+:.+:/lxc/.+$}
virtualization[:system] = "lxc"
virtualization[:role] = "guest"
elsif File.read("/proc/self/cgroup") =~ %r{\d:.+:/$}
diff --git a/spec/unit/plugins/linux/virtualization_spec.rb b/spec/unit/plugins/linux/virtualization_spec.rb
index b2941362..35a8a7bd 100644
--- a/spec/unit/plugins/linux/virtualization_spec.rb
+++ b/spec/unit/plugins/linux/virtualization_spec.rb
@@ -264,7 +264,7 @@ VBOX
end
describe "when we are checking for lxc" do
- it "should set lxc guest if /proc/self/cgroup exist and there are /lxc/ mounts" do
+ it "should set lxc guest if /proc/self/cgroup exist and there are /lxc/<hexadecimal> mounts" do
self_cgroup=<<-CGROUP
8:blkio:/lxc/baa660ed81bc81d262ac6e19486142aeec5fce2043e2a173eb2505c6fbed89bc
7:net_cls:/lxc/baa660ed81bc81d262ac6e19486142aeec5fce2043e2a173eb2505c6fbed89bc
@@ -282,16 +282,16 @@ CGROUP
@plugin[:virtualization][:role].should == "guest"
end
- it "should set lxc guest if /proc/self/cgroup exist and the cgroup is named Charlie" do
+ it "should set lxc guest if /proc/self/cgroup exist and there are /lxc/<name> mounts" do
self_cgroup=<<-CGROUP
-8:blkio:/Charlie/baa660ed81bc81d262ac6e19486142aeec5fce2043e2a173eb2505c6fbed89bc
-7:net_cls:/Charlie/baa660ed81bc81d262ac6e19486142aeec5fce2043e2a173eb2505c6fbed89bc
-6:freezer:/Charlie/baa660ed81bc81d262ac6e19486142aeec5fce2043e2a173eb2505c6fbed89bc
-5:devices:/Charlie/baa660ed81bc81d262ac6e19486142aeec5fce2043e2a173eb2505c6fbed89bc
-4:memory:/Charlie/baa660ed81bc81d262ac6e19486142aeec5fce2043e2a173eb2505c6fbed89bc
-3:cpuacct:/Charlie/baa660ed81bc81d262ac6e19486142aeec5fce2043e2a173eb2505c6fbed89bc
-2:cpu:/Charlie/baa660ed81bc81d262ac6e19486142aeec5fce2043e2a173eb2505c6fbed89bc
-1:cpuset:/
+8:blkio:/lxc/vanilla
+7:net_cls:/lxc/vanilla
+6:freezer:/lxc/vanilla
+5:devices:/lxc/vanilla
+4:memory:/lxc/vanilla
+3:cpuacct:/lxc/vanilla
+2:cpu:/lxc/vanilla
+1:cpuset:/lxc/vanilla
CGROUP
File.should_receive(:exists?).with("/proc/self/cgroup").and_return(true)
File.stub(:read).with("/proc/self/cgroup").and_return(self_cgroup)
@@ -300,6 +300,23 @@ CGROUP
@plugin[:virtualization][:role].should == "guest"
end
+ it "should set not set anyting if /proc/self/cgroup exist and the cgroup is named arbitrarily, it isn't necessarily lxc." do
+ self_cgroup=<<-CGROUP
+8:blkio:/Charlie
+7:net_cls:/Charlie
+6:freezer:/Charlie
+5:devices:/Charlie
+4:memory:/Charlie
+3:cpuacct:/Charlie
+2:cpu:/Charlie
+1:cpuset:/Charlie
+CGROUP
+ File.should_receive(:exists?).with("/proc/self/cgroup").and_return(true)
+ File.stub(:read).with("/proc/self/cgroup").and_return(self_cgroup)
+ @plugin.run
+ @plugin[:virtualization].should == {}
+ end
+
it "should set lxc host if /proc/self/cgroup only has / mounts" do
self_cgroup=<<-CGROUP
8:blkio:/