Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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