Extend / resize SWAP partition in Centos

Hi,

If you are running low on swap memory in your centos environment; please follow these steps to extend the swap partition.

Step 1:
Since all the space in the physical disk would already be allocated to different partitions; there would not be any free space in your "Logical Volume" to give to swap partition.
You can see the volume group details by running the command > vgs




You can verify the logical volume details by running the command > lvs



You can see that there is no free space and my swap volume has 2GB space.


Step 2:
So, first logical step is to get some free space from other partition (probably from home partition as that has maximum space in the screenshot above) so that it can be allocated to swap partition

So, first we need to un-mount the home partition.
It can be done by following command > sudo umount -fl /home

Next to change the partition size, run the command >  lvreduce -L 173.88G /dev/mapper/centos-home

NOTE: Please note that the size to be given here is the new total size i.e. if old size was 60GB and you want to reduce it to 50GB so that you get 10GB free space then give 50GB here.


Once you have done this, you can run the vgs command again to verify the free space that has been obtained.



Step 3:
Now, we need to clear and turn off SWAP
This can be done by following commands i.e.
First run this command > echo "3" > /proc/sys/vm/drop_caches
Then run this command > swapoff -v /dev/centos/swap


Step 4:
Now, the grand step of increasing the swap partition
Please run this command > lvm lvresize /dev/centos/swap -L +6G

NOTE: Since I had freed 6GB space so I am adding 6GB to existing SWAP partition. This will make the new SWAP to be 2G + 6G = 8G


Running the vgs command again will show that the free space has been used up



Step 5:
Now we need to wipe off old SWAP signature and recreate the swap so that it know the new size
Use these command for it i.e.

First > mkswap /dev/centos/swap

Next >  swapon -va

Verify using command > swapon -s



Step 6:
Check the new partition details using command > lvs




Finally, Mount the home partition > sudo mount -fl /home


0 comments:

Post a Comment