Skip to content

Instantly share code, notes, and snippets.

@adonley
Created January 26, 2018 00:11
Show Gist options
  • Save adonley/4eeaf5310c16ed5ab81544c18d298a94 to your computer and use it in GitHub Desktop.
Save adonley/4eeaf5310c16ed5ab81544c18d298a94 to your computer and use it in GitHub Desktop.
Make swap partition on amazon EC2 instance.
#!/bin/bash
# './make_swap -s 1024' will create a 1024 MB swap
OPTIND=1;
# Size in MB
s="1024";
while getopts "s:" opt; do
case "$opt" in
s) s=$OPTARG
;;
esac
done
shift $((OPTIND-1))
[ "$1" = "--" ] && shift
if grep -Fq "/var/swap" /etc/fstab
then
echo "Swap already enabled for this machine.";
else
# 1 GB
sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=$s
sudo /sbin/mkswap /var/swap.1
sudo chmod 600 /var/swap.1
sudo /sbin/swapon /var/swap.1
echo "/var/swap.1 swap swap defaults 0 0" | sudo tee -a /etc/fstab
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment