Increasing Linux swapfile with minimal disruption: the swapfile shuffle

Had a server which needed a bit more swap, but the current swap was at 100%, and it was marginal whether there was enough RAM to allow me to delete the current 2G swap and replace it with 4G. So I came up with the "swapfile shuffle".

 # Lets see what we've got
swapon --show
NAME      TYPE SIZE USED PRIO
/swapfile file   2G   2G   -2

# Make a new temp swapfile and turn it on.
fallocate -l 2G /swapfile2
chmod 600 /swapfile2
mkswap /swapfile2
swapon /swapfile2

# Check progress
 htop -> 4G of active swap. Memory looks OK. 
 
# Looking good, so now we turn off the original swapfile, resize it and then turn it on
swapoff /swapfile
fallocate -l 4G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile

# Check again. 
htop -> Whoo, now have 6Gb swap

# Now can disable the temp swapfile 
swapoff /swapfile2 
rm /swapfile2

All good. Happy server.

Leave a Comment