Skip to content

Instantly share code, notes, and snippets.

@andruby
Created April 28, 2010 14:02
Show Gist options
  • Save andruby/382178 to your computer and use it in GitHub Desktop.
Save andruby/382178 to your computer and use it in GitHub Desktop.
# Script to setup swap space on a Amazon AWS instance
# Takes about 1 minute per GB of swap size
# run it like this:
# ruby aws_swap.rb 4G
SWAP_LOCATION = "/mnt/swapfile"
def run(cmd)
if ARGV.join(' ').include?('-p')
puts cmd
else
%x(#{cmd})
end
end
def in_bytes(number)
if number.include?('G')
number.to_i*1024*1024
elsif number.include?('M')
number.to_i*1024
else
number
end
end
puts "Mounting /mnt"
run("sudo mount /mnt")
puts "Wrinting #{ARGV[0]} of zeroes to #{SWAP_LOCATION}"
run("sudo dd if=/dev/zero of=#{SWAP_LOCATION} bs=1024 count=#{in_bytes(ARGV[0])}")
puts "Formatting and activating swap"
run("sudo sudo mkswap #{SWAP_LOCATION}")
run("sudo swapon #{SWAP_LOCATION}")
puts run("swapon -s")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment