Recipes
The default.rb
recipe installs and configures zram
on a system. It then creates the systemd
service components.
The uninstall.rb
recipe will uninstall zram
and all the components; however, if a cookbook or various run_list
still calls the default recipe, it will get reinstalled.
#
# Cookbook:: zram
# Recipe:: default
#
org = node['org_namespace']
# Set swap size in MB. By default it sets it to 10% of the total memory.
node.default[org]['zram']['swap_size_mb'] = (node['memory']['total'].to_i / node[org]['zram']['percent_of_memory']) / 1024
# Set number of devices equal to the number of cores.
node.default[org]['zram']['num_devices'] = node['cpu']['cores']
# Set the size of each device based on the full size devided by the number of devices.
(1..node[org]['zram']['num_devices'].to_i).each do |n|
node.default[org]['zram']['disk_size']["zram#{n - 1}"] = node[org]['zram']['swap_size_mb'].to_i / node[org]['zram']['num_devices'].to_i
end
template 'modules-load_zram.conf' do
path '/etc/modules-load.d/zram.conf'
source 'modules-load_zram.conf.erb'
user 'root'
group 'root'
mode '0640'
action :create
end
template 'modeprobe_zram.conf' do
path '/etc/modprobe.d/zram.conf'
source 'modeprobe_zram.conf.erb'
user 'root'
group 'root'
mode '0640'
action :create
notifies :stop, 'systemd_unit[zram.service]', :immediately if ::File.exist?('/etc/systemd/system/zram.service')
notifies :unload, 'kernel_module[zram]', :immediately unless node[org]['test_kitchen'] || !shell_out('modinfo zram')
end
template '99-zram.rules' do
path '/etc/udev/rules.d/99-zram.rules'
source '99-zram.rules.erb'
user 'root'
group 'root'
mode '0640'
action :create
notifies :stop, 'systemd_unit[zram.service]', :immediately if ::File.exist?('/etc/systemd/system/zram.service')
notifies :unload, 'kernel_module[zram]', :immediately unless node[org]['test_kitchen'] || !shell_out('modinfo zram')
end
# Loading zram modules.
# Not loading it in test_kitchen on the chance the host system already has it enabled.
unless node[org]['test_kitchen']
kernel_module 'zram' do
action :load
end
end
# Creating zram service.
systemd_unit 'zram.service' do
content({ Unit: {
Description: 'zram',
After: 'multi-user.target',
},
Service: {
Type: 'oneshot',
RemainAfterExit: 'true',
ExecStartPre: node[org]['zram']['disk_size'].keys.collect { |k| "/sbin/mkswap /dev/#{k}" },
ExecStart: node[org]['zram']['disk_size'].keys.collect { |k| "/sbin/swapon -p 100 /dev/#{k}" },
ExecStop: node[org]['zram']['disk_size'].keys.collect { |k| "/sbin/swapoff /dev/#{k}" },
},
Install: {
WantedBy: 'multi-user.target',
} })
if node[org]['test_kitchen']
action [:create, :enable]
else
action [:create, :enable, :start]
end
end
# Setting sysctl params.
node[org]['zram']['sysctl']['params'].each do |k, v|
sysctl k do
value v
end
end
#
# Cookbook:: zram
# Recipe:: uninstall
#
# Copyright:: 2021, The Authors, All Rights Reserved.
org = node['org_namespace']
# Set number of devices equal to the number of cores.
node.default[org]['zram']['num_devices'] = node['cpu']['cores']
# Set the size of each device based on the full size divided by the number of devices.
(1..node[org]['zram']['num_devices'].to_i).each do |n|
swap_file "/dev/zram#{n - 1}" do
action :remove
end
end
unless node[org]['test_kitchen']
kernel_module 'zram' do
action :unload
end
end
%w(
/etc/modules-load.d/zram.conf
/etc/modprobe.d/zram.conf
/etc/udev/rules.d/99-zram.rules
).each do |cleanup_files|
file cleanup_files do
action :delete
end
end
# Creating zram service.
systemd_unit 'zram.service' do
action [:stop, :delete]
end