Skip to content

Instantly share code, notes, and snippets.

@jcharlier
Created March 27, 2018 19:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcharlier/be5512c14d3277ba923267e98efd80ab to your computer and use it in GitHub Desktop.
Save jcharlier/be5512c14d3277ba923267e98efd80ab to your computer and use it in GitHub Desktop.
Create Swapfile in Ubuntu
#!/bin/bash
regex='^[0-9]+([.][0-9]+)?$'
if [[ ! $2 =~ $regex ]]; then
echo "Usage: createSwap.sh fullPathToSwapFile swapFileSizeinGB";
echo "ex: createSwap /tmp/swapfile.swap 8"
exit 1
fi
if [[ -d $1 ]] || [[ -f $1 ]] || [[ ! $1 = /* ]]; then
echo "please supply the full path to a new swapFile"
exit 1
fi
echo "Allocating file space for $1"
sudo fallocate -l $2G $1
echo "done"
sudo chmod 600 $1
# sudo dd if=/dev/zero of=/mnt/1GB.swap bs=1024 count=1048576
sudo mkswap $1
sudo swapon $1
echo "$1 none swap sw 0 0" | sudo tee --append /etc/fstab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment