Skip to content

Instantly share code, notes, and snippets.

@lajosbencz
Created December 18, 2021 15:28
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 lajosbencz/a260516d064543237d932880b3cbdb8c to your computer and use it in GitHub Desktop.
Save lajosbencz/a260516d064543237d932880b3cbdb8c to your computer and use it in GitHub Desktop.
Ubuntu 20 create swap space on disk
#!/bin/bash
SIZE="$1"
FILE="/var/swap.img"
SWPS=10
CHPR=50
if [ -z "$SIZE" ]; then
echo "first argument must be size (fallocate -l)"
exit
fi
if [ ! -z "$2" ]; then
FILE="$2"
fi
if [ -f "$FILE" ]; then
echo "file ${FILE} already exist!"
exit
fi
fallocate -l "$SIZE" "$FILE"
chmod 600 "$FILE"
mkswap "$FILE"
swapon "$FILE"
echo -e "${FILE}\tnone\tswap\tsw\t0 0" >> /etc/fstab
echo -e "\nvm.swappiness=${SWPS}\nvm.vfs_cache_pressure=${CHPR}" >> /etc/sysctl.conf
sysctl "vm.swappiness=${SWPS}"
sysctl "vm.vfs_cache_pressure=${CHPR}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment