Skip to content

Instantly share code, notes, and snippets.

@gudata
Forked from priestjim/chef-noatime-and-swap.rb
Last active February 27, 2017 16:38
Show Gist options
  • Save gudata/289f3aff571d6d6e5f63407976cce0b4 to your computer and use it in GitHub Desktop.
Save gudata/289f3aff571d6d6e5f63407976cce0b4 to your computer and use it in GitHub Desktop.
A quick (hacky) Chef recipe for adding noatime and a swap file in case a swap file does not exist
# Create and enable a swap file
have_file_swap = false
File.open('/etc/fstab',"r") do |fstab|
fstab.each do |line|
# Tokenize each fstab line with a space separator
tokens = line.split
if tokens[2] == "swap" && tokens[0] !~ /\/dev/
have_file_swap = true
next
end
end
end
# Create a regular swap file in case there is none
unless have_file_swap
swap_size = 2048
Chef::Log.info("Swapfile not found. Manually creating one of #{swap_size}M for OOM safety")
execute "creating swapfile" do
command "/bin/dd if=/dev/zero of=/swap.img bs=1M count=#{swap_size}"
action :run
creates "/swap.img"
end
execute "formatting swapfile" do
command "/sbin/mkswap -L local /swap.img"
action :run
end
mount "none" do
device "/swap.img"
fstype "swap"
options [ "sw"]
dump 0
pass 0
action :enable
end
execute "mounting swapfile" do
command "/sbin/swapon -a"
action :run
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment