diff options
author | Tim Smith <tsmith@chef.io> | 2018-04-18 12:53:02 -0700 |
---|---|---|
committer | Tim Smith <tsmith@chef.io> | 2018-06-11 12:40:59 -0700 |
commit | 50a993b567284ba43a34a67d6924b40a8932706f (patch) | |
tree | 9617f11d8c9bfdbf2301f9651452c93b5a3c76a7 | |
parent | a729bcede36227960ee6460c47ac90b8243d3a7d (diff) | |
download | chef-50a993b567284ba43a34a67d6924b40a8932706f.tar.gz |
Add kernel_module resource from the kernel_module cookbook
Copied from the 1.1.1 release.
Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r-- | lib/chef/resource/kernel_module.rb | 100 | ||||
-rw-r--r-- | lib/chef/resources.rb | 1 |
2 files changed, 101 insertions, 0 deletions
diff --git a/lib/chef/resource/kernel_module.rb b/lib/chef/resource/kernel_module.rb new file mode 100644 index 0000000000..9b1836e266 --- /dev/null +++ b/lib/chef/resource/kernel_module.rb @@ -0,0 +1,100 @@ +# +# Resource:: kernel_module +# +# The MIT License (MIT) +# +# Copyright 2016-2018, Shopify Inc. +# Copyright 22018, Chef Software, Inc. + +require "chef/resource" + +class Chef + class Resource + class KernelModule < Chef::Resource + resource_name :kernel_module + provides(:kernel_module) { true } + + property :modname, String, name_property: true, identity: true + property :load_dir, String, default: "/etc/modules-load.d" + property :unload_dir, String, default: "/etc/modprobe.d" + + # Load kernel module, and ensure it loads on reboot + action :install do + declare_resource(:directory, new_resource.load_dir) do + recursive true + end + + declare_resource(:file, "#{new_resource.load_dir}/#{new_resource.modname}.conf") do + content "#{new_resource.modname}\n" + notifies :run, "execute[update initramfs]" + end + + declare_resource(:execute, "update initramfs") do + command initramfs_command + action :nothing + end + + new_resource.run_action(:load) + end + + # Unload module and remove module config, so it doesn't load on reboot. + action :uninstall do + declare_resource(:file, "#{new_resource.load_dir}/#{new_resource.modname}.conf") do + action :delete + notifies :run, "execute[update initramfs]" + end + + declare_resource(:file, "#{new_resource.unload_dir}/blacklist_#{new_resource.modname}.conf") do + action :delete + notifies :run, "execute[update initramfs]" + end + + declare_resource(:execute, "update initramfs") do + command initramfs_command + action :nothing + end + + new_resource.run_action(:unload) + end + + # Blacklist kernel module + action :blacklist do + declare_resource(:file, "#{new_resource.unload_dir}/blacklist_#{new_resource.modname}.conf") do + content "blacklist #{new_resource.modname}" + notifies :run, "execute[update initramfs]" + end + + declare_resource(:execute, "update initramfs") do + command initramfs_command + action :nothing + end + + new_resource.run_action(:unload) + end + + # Load kernel module + action :load do + declare_resource(:execute, "modprobe #{new_resource.modname}") do + not_if "cat /proc/modules | grep ^#{new_resource.modname}" + end + end + + # Unload kernel module + action :unload do + declare_resource(:execute, "modprobe -r #{new_resource.modname}") do + only_if "cat /proc/modules | grep ^#{new_resource.modname}" + end + end + + action_class do + def initramfs_command + if platform_family?("debian") + "update-initramfs -u" + else + "dracut -f" + end + end + end + end + end +end diff --git a/lib/chef/resources.rb b/lib/chef/resources.rb index c001e56055..d942518b21 100644 --- a/lib/chef/resources.rb +++ b/lib/chef/resources.rb @@ -49,6 +49,7 @@ require "chef/resource/homebrew_cask" require "chef/resource/homebrew_package" require "chef/resource/homebrew_tap" require "chef/resource/ifconfig" +require "chef/resource/kernel_module" require "chef/resource/ksh" require "chef/resource/launchd" require "chef/resource/link" |