Skip to content

Instantly share code, notes, and snippets.

@priestjim
Created November 29, 2012 19:01
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save priestjim/4171136 to your computer and use it in GitHub Desktop.
Save priestjim/4171136 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
# Enable noatime on local ext3/4 & xfs filesystems
fstab = File.open('/etc/fstab',"r")
newlines = Array.new
needremount = Array.new
ihaveswap = false
fstab.each do |line|
# Tokenize each fstab line with a space separator
tokens = line.split
if tokens[2] == "swap"
newlines << tokens.join("\t")
ihaveswap = true
next
elsif ((! %w{ ext3 ext4 xfs }.include?(tokens[2])) || (%w{ ext3 ext4 xfs tmpfs nfs auto }.include?(tokens[2]) && tokens[3].match(/noatime/)))
newlines << tokens.join("\t")
next
end
needremount << tokens[0]
tokens[3] << ",noatime"
newlines << tokens.join("\t")
end
fstab.close
newlines = newlines.compact.join("\n")
fstab = File.open("/etc/fstab", "w")
fstab.puts(newlines)
fstab.close
# Remount each mount point that got updated
needremount.each do |mountpoint|
Chef::Log.info('Remounting #mountpoint')
execute "mountpoint remout" do
command "/bin/mount -o remount \"#{mountpoint}\""
action :run
timeout 30
end
end
# Create a regular swap file in case there is none
if (ihaveswap == false)
Chef::Log.info("Swapfile not found. Manually creating one of 512M for OOM safety")
execute "creating swapfile" do
command "/bin/dd if=/dev/zero of=/swap.img bs=1M count=512"
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
@joe1chen
Copy link

Very helpful. Thanks for this recipe!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment