bash -c 'read blocks blocksize < <(stat -c "%b %B" "$1"); dd if=/dev/zero bs=${blocksize} count=${blocks} of="$1" conv=notrunc status=progress' - /FILE
SWAP_FILE="/swapfile"
# allocate file
sudo fallocate -l 512M "$SWAP_FILE"
# fill file with zeros
sudo bash -c 'read blocks blocksize < <(stat -c "%b %B" "$1"); dd if=/dev/zero bs=${blocksize} count=${blocks} of="$1" conv=notrunc status=progress' - "$SWAP_FILE"
sudo chmod 600 "$SWAP_FILE"
# Create
sudo mkswap "$SWAP_FILE"
# Activate
sudo swapon "$SWAP_FILE"
# Add swap load to fstab file
echo "$SWAP_FILE swap swap defaults 0 0" | sudo tee -a /etc/fstab
# Check
sudo swapon --show