Extending your swap partition in CentOS
While I’m not a fan of SWAP memory, there are certain cases that can really save you by using swap. Would you rather get out of memory errors, or have a temporary slow down?
The CentOS/RHEL manual has a great quick tutorial on how to increase your swap space on an LVM2 logical partition. Since the default swap partition on a CentOS box is /dev/VolGroup00/LogVol01, that will be used in this example. The quick 5 steps are as follows:
- Use swapoff to disable swapping for the associated logical swap volume
- lvesize the logical volume to meet your needs
- Use mkswap to format the new swap space so it can be used properly
- Use swapon to enable the newly expanded swap volume
- Lastly, it’s always good to verify that the swap volume has grown the proper amount
Example: Extend the current swap LVM partition
swapoff -v /dev/VolGroup00/LogVol01 lvm lvresize /dev/VolGroup00/LogVol01 -L +1G mkswap /dev/VolGroup00/LogVol01 swapon -va cat /proc/swaps
Another handy trick to “create” more swap space when you aren’t using LVM or don’t have an extra partition laying around is to create a swap file yourself.
- Create a file (full of zeros) that is the desired size
- Use mkswap to format the new swap space so it can be used properly
- Use swapon to enable the newly created swap file
Example: Create a swap file of 1GB and add it to the swap pool
dd if=/dev/zero of=/opt/swap-file bs=1024 count=1000000mkswap -f /opt/swap-fileswapon /opt/swap-file