Last active
September 29, 2020 20:26
-
-
Save midweste/8153eabaadd6371caad92caaf3444f4c to your computer and use it in GitHub Desktop.
Create and activate a swapfile
This file contains hidden or 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
#!/bin/sh | |
# Create and activate swapfile | |
NOW=$(date '+%Y%m%d%H%M%S') | |
SWAPSIZEGB=6 | |
SWAPFILENAME="/swap.${NOW}.img" | |
if [ "$(id -u)" != "0" ]; then | |
echo "This script must be run as root (sudo)" 1>&2 | |
exit 1 | |
fi | |
if [ -f "${SWAPFILENAME}" ];then | |
echo "${SWAPFILENAME} already exists" | |
exit 2 | |
fi | |
if ! [ ${SWAPSIZEGB} -eq ${SWAPSIZEGB} ] 2> /dev/null ;then | |
echo "SWAPSIZEGB must be an integer" | |
exit 3 | |
fi | |
SWAPINBYTES=$(expr ${SWAPSIZEGB} \* 1048596) | |
echo "creating swap space" | |
dd if=/dev/zero of="${SWAPFILENAME}" bs=1024 count=${SWAPINBYTES} | |
chmod 0600 "${SWAPFILENAME}" | |
mkswap "${SWAPFILENAME}" | |
swapon "${SWAPFILENAME}" | |
cp /etc/fstab "/etc/fstab.${NOW}.bak" | |
echo "${SWAPFILENAME} none swap sw 0 0" | sudo tee -a /etc/fstab | |
echo "${SWAPSIZEGB} GB swap file created at ${SWAPFILENAME}" | |
free |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment