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.

2 thoughts on “Increasing Linux swapfile with minimal disruption: the swapfile shuffle”

  1. I would have created a 4G swapfile2, just in case something wants more in the meantime. And 4G is the max the new config will provide, so we have the planned resource a bit earlier. And you just could have stayed with it w/o copying.

  2. Fair point. I guess it was my OCDness kicking in and wanting the swapfile to have the same name. Also no need to edit the /etc/fstab file, which obviously you'd do in your case.
    Thanks for the comment, I think I've probably only had two or three real people comment ever!

Leave a Comment