Created
November 29, 2012 19:01
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very helpful. Thanks for this recipe!