summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSong Liu <song@localhost.localdomain>2021-06-18 16:59:28 -0700
committerSong Liu <song@kernel.org>2021-06-21 15:23:08 -0700
commitffea38b6c84f96197fbb05b0768d01c2345e20a9 (patch)
tree4070a7ec1857c264dbd9216a60cc9afd08125fc4
parente1541bc6a90caa3686139524d9c1818b77ad721a (diff)
downloadohai-ffea38b6c84f96197fbb05b0768d01c2345e20a9.tar.gz
Add plugin for linux livepatch
This plugin gets "enabled" and "transition" for each livepatch in /sys/kernel/livepatch/*. Signed-off-by: Song Liu <song@kernel.org>
-rw-r--r--lib/ohai/plugins/linux/livepatch.rb38
-rw-r--r--spec/unit/plugins/linux/livepatch_spec.rb62
2 files changed, 100 insertions, 0 deletions
diff --git a/lib/ohai/plugins/linux/livepatch.rb b/lib/ohai/plugins/linux/livepatch.rb
new file mode 100644
index 00000000..a8e33169
--- /dev/null
+++ b/lib/ohai/plugins/linux/livepatch.rb
@@ -0,0 +1,38 @@
+# frozen_string_literal: true
+#
+# Author:: Song Liu <song@kernel.org>
+# Copyright:: Copyright (c) 2021 Facebook, Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+Ohai.plugin(:Livepatch) do
+ provides "livepatch"
+
+ collect_data(:linux) do
+ if file_exist?("/sys/kernel/livepatch")
+ patches = Mash.new
+ dir_glob("/sys/kernel/livepatch/*").each do |livepatch_dir|
+ dir = File.basename(livepatch_dir)
+ patches[dir] = Mash.new
+ %w{enabled transition}.each do |check|
+ if file_exist?("/sys/kernel/livepatch/#{dir}/#{check}")
+ file_open("/sys/kernel/livepatch/#{dir}/#{check}") { |f| patches[dir][check] = f.read_nonblock(1024).strip }
+ end
+ end
+ livepatch patches
+ end
+ end
+ end
+end
diff --git a/spec/unit/plugins/linux/livepatch_spec.rb b/spec/unit/plugins/linux/livepatch_spec.rb
new file mode 100644
index 00000000..b02cba96
--- /dev/null
+++ b/spec/unit/plugins/linux/livepatch_spec.rb
@@ -0,0 +1,62 @@
+#
+# Author:: Song Liu <song@kernel.org>
+# Copyright:: Copyright (c) 2021 Facebook, Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require "spec_helper"
+
+describe Ohai::System, "Linux Livepatch Plugin" do
+ PATCHES = {
+ "hotfix1" => {
+ "enabled" => "1",
+ "transition" => "0",
+ },
+ "hotfix2" => {
+ "enabled" => "0",
+ "transition" => "1",
+ },
+ }.freeze
+
+ def file_double(value)
+ tmp_double = double
+ expect(tmp_double).to receive(:read_nonblock).and_return(value)
+ tmp_double
+ end
+
+ before do
+ @plugin = get_plugin("linux/livepatch")
+ allow(@plugin).to receive(:collect_os).and_return(:linux)
+ allow(File).to receive(:exist?).with("/sys/kernel/livepatch").and_return(true)
+ allow(@plugin).to receive(:dir_glob).with("/sys/kernel/livepatch/*") do
+ PATCHES.collect { |patch, _files| "/sys/kernel/livepatch/#{patch}" }
+ end
+
+ PATCHES.each do |patch, checks|
+ allow(File).to receive(:exist?).with("/sys/kernel/livepatch/#{patch}").and_return(true)
+ checks.each do |check, value|
+ allow(File).to receive(:exist?).with("/sys/kernel/livepatch/#{patch}/#{check}").and_return(true)
+ allow(File).to receive(:open).with("/sys/kernel/livepatch/#{patch}/#{check}").and_yield(file_double(value))
+ end
+ end
+ end
+
+ it "collects all relevant data from livepatches" do
+ @plugin.run
+ PATCHES.each do |patch, checks|
+ expect(@plugin[:livepatch][patch.to_sym]).to include(checks)
+ end
+ end
+end