Skip to content

Instantly share code, notes, and snippets.

@kahidna
Last active October 6, 2022 06:43
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 kahidna/141425fe3331593354928569cb8e62ff to your computer and use it in GitHub Desktop.
Save kahidna/141425fe3331593354928569cb8e62ff to your computer and use it in GitHub Desktop.
simple script to create a swap file and enable it to system
#!/bin/bash
set -e
# scipt for generate and enable swap on linux
# tested on ubuntu server 14.04.5 LTS
# where you will put swapfile, default is on /
BASEFOLDER=/
SWAPFILENAME=swapfile.img
# in megabytes
SWAPSIZE=4096
echo ""
echo "create directory swap"
sudo mkdir -vp $BASEFOLDER
echo ""
echo "create empty file for swap"
sudo dd if=/dev/zero of=$BASEFOLDER/$SWAPFILENAME bs=$SWAPSIZE count=1M
echo ""
echo "setup permission swap to 600"
sudo chmod 600 $BASEFOLDER/$SWAPFILENAME
echo ""
echo "setup empty file to swap "
sudo mkswap $BASEFOLDER/$SWAPFILENAME
echo ""
echo "activate swap"
sudo swapon $BASEFOLDER/$SWAPFILENAME
echo ""
echo "add swap file to /etc/fstab"
if [[ "$BASEFOLDER" = "/" ]]; then
#statements
echo "/$SWAPFILENAME swap swap defaults 0 0" >> /etc/fstab
else
echo "$BASEFOLDER/$SWAPFILENAME swap swap defaults 0 0" >> /etc/fstab
fi
echo ""
echo "end of script"
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment