Switching to swapfile on linux

Many Linux installs use a swap partition instead of a swap file. There are some good reasons for this such as enabling sequential access for HDDs and hibernate support but a physical partition means if sizes are wrong a live USB and complicated resize procedure will be needed.

These days I find it easier to use a swapfile instead of a dedicated partition:

  1. Stop using swap partition: swapoff /dev/sdaX
  2. Delete the swap partition with gparted and grow your LVM/LUKS partition, then grow the LV holding your root filesystem
  3. Create the swapfile:
# 64GB - 2x RAM is a good starting point
dd if=/dev/zero of=/swapfile bs=1M count=65535
chmod 0600 /swapfile
mkswap /swapfile
swapon /swapfile
  1. Edit /etc/fstab:

Swapfile entry should be added/changed to:

/swapfile      none    swap    sw      0       0
  1. Reboot to test changes work reliably. Use top to check swapfile is active

Post comment