From 3f979b8f49b39d78a4f8ca21bd152f7dccd64d84 Mon Sep 17 00:00:00 2001 From: Tomas Barton Date: Wed, 3 Aug 2022 13:17:30 +0200 Subject: [PATCH] Load modules config only for modules managed by puppet --- lib/facter/kmod.rb | 92 ++++++++++++++++++++++++++++------------------ 1 file changed, 57 insertions(+), 35 deletions(-) diff --git a/lib/facter/kmod.rb b/lib/facter/kmod.rb index e8e1a30..cd5c377 100644 --- a/lib/facter/kmod.rb +++ b/lib/facter/kmod.rb @@ -1,6 +1,45 @@ # frozen_string_literal: true -# @summary Return a hash of loaded kernel modules +# @summary Return a hash of loaded kernel modules explicitly declared by puppet +# e.g. using kmod::load { 'overlay': } + +def kmod_load_module_config(kmodule) + if File.exist?('/sys/module') + config = { + 'parameters' => {}, + 'used_by' => [], + } + + if File.directory?("/sys/module/#{kmodule}/parameters") + Dir.foreach("/sys/module/#{kmodule}/parameters") do |param| + next if ['.', '..'].include?(param) + + next unless File.readable?("/sys/module/#{kmodule}/parameters/#{param}") + + begin + config['parameters'][param] = File.read("/sys/module/#{kmodule}/parameters/#{param}").chomp.delete("\u0000") + rescue StandardError + # some kernel parameters are write only + # even though they have the read bit set + # so just ignore read errors + nil + end + end + end + + if File.directory?("/sys/module/#{kmodule}/holders") + Dir.foreach("/sys/module/#{kmodule}/holders") do |used| + next if ['.', '..'].include?(used) + + config['used_by'] << used + end + end + + config + else + Facter.debug('/sys/module is not found. skipping.') + end +end Facter.add(:kmods) do confine kernel: :linux @@ -8,44 +47,27 @@ kmod = {} setcode do - if File.exist?('/sys/module') - Dir.foreach('/sys/module') do |directory| - next if ['.', '..'].include?(directory) - - kmod[directory] = { - 'parameters' => {}, - 'used_by' => [], - } - - if File.directory?("/sys/module/#{directory}/parameters") - Dir.foreach("/sys/module/#{directory}/parameters") do |param| - next if ['.', '..'].include?(param) - - next unless File.readable?("/sys/module/#{directory}/parameters/#{param}") - - begin - kmod[directory]['parameters'][param] = File.read("/sys/module/#{directory}/parameters/#{param}").chomp.delete("\u0000") - rescue StandardError - # some kernel parameters are write only - # even though they have the read bit set - # so just ignore read errors - nil - end - end - end - - if File.directory?("/sys/module/#{directory}/holders") - Dir.foreach("/sys/module/#{directory}/holders") do |used| - next if ['.', '..'].include?(used) + if File.directory?('/etc/modules-load.d') + # Debian + Dir.foreach('/etc/modules-load.d') do |item| + next if ['.', '..', 'modules.conf'].include?(item) - kmod[directory]['used_by'] << used - end + if item.end_with? '.conf' + mod = File.basename(item, '.conf') + kmod[mod] = kmod_load_module_config(mod) end end + elsif File.directory?('/etc/sysconfig/modules') + # RedHat + Dir.foreach('/etc/sysconfig/modules') do |item| + next if ['.', '..'].include?(item) - kmod - else - Facter.debug('/sys/module is not found. skipping.') + if item.end_with? '.modules' + mod = File.basename(item, '.modules') + kmod[mod] = kmod_load_module_config(mod) + end + end end + kmod end end