Skip to content

Commit

Permalink
Fix #531: Docker changed their cgroups format
Browse files Browse the repository at this point in the history
Take into account new cgroup format for docker.
Tested with docker 1.6.2 on Debian sid with kernel 4.0.5
  • Loading branch information
jbfavre authored and thommay committed Aug 13, 2015
1 parent e62882a commit f3152b4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/ohai/plugins/linux/virtualization.rb
Expand Up @@ -190,7 +190,9 @@ def docker_exists?
# 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+:[^:]+:/(lxc|docker)/.+$}
cgroup_content = File.read("/proc/self/cgroup")
if cgroup_content =~ %r{^\d+:[^:]+:/(lxc|docker)/.+$} ||
cgroup_content =~ %r{^\d+:[^:]+:/[^/]+/(lxc|docker)-.+$}
virtualization[:system] = $1
virtualization[:role] = "guest"
virtualization[:systems][$1.to_sym] = "guest"
Expand Down
21 changes: 21 additions & 0 deletions spec/unit/plugins/linux/virtualization_spec.rb
Expand Up @@ -513,6 +513,27 @@
expect(plugin[:virtualization][:systems][:docker]).to eq("guest")
end

# Relevant at least starting docker 1.6.2, kernel 4.0.5 & systemd 224-1.
# Doi not exactly know which software/version really matters here.
it "should set docker guest if /proc/self/cgroup exists and there are /system.slice/docker-<hexadecimal> mounts (systemd managed cgroup)" do
self_cgroup=<<-CGROUP
8:devices:/system.slice/docker-47341c91be8d491cb3b8a475ad5b4aef6e79bf728cbb351c384e4a6c410f172f.scope
7:cpuset:/system.slice/docker-47341c91be8d491cb3b8a475ad5b4aef6e79bf728cbb351c384e4a6c410f172f.scope
6:blkio:/system.slice/docker-47341c91be8d491cb3b8a475ad5b4aef6e79bf728cbb351c384e4a6c410f172f.scope
5:freezer:/system.slice/docker-47341c91be8d491cb3b8a475ad5b4aef6e79bf728cbb351c384e4a6c410f172f.scope
4:net_cls:/
3:memory:/system.slice/docker-47341c91be8d491cb3b8a475ad5b4aef6e79bf728cbb351c384e4a6c410f172f.scope
2:cpu,cpuacct:/system.slice/docker-47341c91be8d491cb3b8a475ad5b4aef6e79bf728cbb351c384e4a6c410f172f.scope
1:name=systemd:/system.slice/docker-47341c91be8d491cb3b8a475ad5b4aef6e79bf728cbb351c384e4a6c410f172f.scope
CGROUP
allow(File).to receive(:exists?).with("/proc/self/cgroup").and_return(true)
allow(File).to receive(:read).with("/proc/self/cgroup").and_return(self_cgroup)
plugin.run
expect(plugin[:virtualization][:system]).to eq("docker")
expect(plugin[:virtualization][:role]).to eq("guest")
expect(plugin[:virtualization][:systems][:docker]).to eq("guest")
end

it "sets not set anything if /proc/self/cgroup exist and the cgroup is named arbitrarily, it isn't necessarily lxc." do
self_cgroup=<<-CGROUP
8:blkio:/Charlie
Expand Down

0 comments on commit f3152b4

Please sign in to comment.