Created
April 28, 2010 14:02
-
-
Save andruby/382178 to your computer and use it in GitHub Desktop.
This file contains 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
# 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