Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect LXC if LXC and Docker are on the same host #1055

Merged
merged 3 commits into from
Sep 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/ohai/plugins/linux/virtualization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ def nova_exists?
virtualization[:system] = $1
virtualization[:role] = "guest"
virtualization[:systems][$1.to_sym] = "guest"
elsif File.read("/proc/1/environ") =~ /container=lxc/
Ohai::Log.debug("Plugin Virtualization: /proc/1/environ indicates lxc container. Detecting as lxc guest")
virtualization[:system] = "lxc"
virtualization[:role] = "guest"
virtualization[:systems][:lxc] = "guest"
elsif lxc_version_exists? && File.read("/proc/self/cgroup") =~ %r{\d:[^:]+:/$}
# lxc-version shouldn't be installed by default
# Even so, it is likely we are on an LXC capable host that is not being used as such
Expand Down
11 changes: 11 additions & 0 deletions spec/unit/plugins/linux/virtualization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@
2:cpu:/Charlie
1:cpuset:/Charlie
CGROUP
allow(File).to receive(:read).with("/proc/1/environ").and_return("")
expect(File).to receive(:exist?).with("/proc/self/cgroup").and_return(true)
allow(File).to receive(:read).with("/proc/self/cgroup").and_return(self_cgroup)
plugin.run
Expand All @@ -572,6 +573,7 @@
CGROUP
expect(File).to receive(:exist?).with("/proc/self/cgroup").and_return(true)
allow(File).to receive(:read).with("/proc/self/cgroup").and_return(self_cgroup)
allow(File).to receive(:read).with("/proc/1/environ").and_return("")
end

it "sets lxc host if lxc-version exists" do
Expand Down Expand Up @@ -600,6 +602,14 @@
expect(plugin[:virtualization]).to eq({ "systems" => {} })
end

it "sets lxc guest if /proc/1/environ has lxccontainer string in it" do
one_environ = "container=lxccontainer_ttys=/dev/pts/0 /dev/pts/1 /dev/pts/2 /dev/pts/3".chomp
allow(File).to receive(:read).with("/proc/1/environ").and_return(one_environ)
plugin.run
expect(plugin[:virtualization][:system]).to eq("lxc")
expect(plugin[:virtualization][:role]).to eq("guest")
end

end

it "does not set virtualization if /proc/self/cgroup isn't there" do
Expand Down Expand Up @@ -682,6 +692,7 @@
CGROUP
allow(File).to receive(:exist?).with("/proc/self/cgroup").and_return(true)
allow(File).to receive(:read).with("/proc/self/cgroup").and_return(self_cgroup)
allow(File).to receive(:read).with("/proc/1/environ").and_return("")
plugin.run
expect(plugin[:virtualization]).to eq({ "systems" => {} })
end
Expand Down