Increasing/Removing SWAP patition

General support questions
Post Reply
solidblueliquid
Posts: 7
Joined: 2018/01/11 07:41:06

Increasing/Removing SWAP patition

Post by solidblueliquid » 2018/11/09 12:32:02

Hi Everyone,

I have a VPS that runs cPanel. Currently it has a physical swap patition and isn't big enough for the sites I'm running. I wondering how difficult it would be to remove the partition and then create a swap file with cPanel's script instead.

Code: Select all

[root@connected lib]# fdisk -l

Disk /dev/xvda: 128.8 GB, 128849018880 bytes, 251658240 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000ae971

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *        2048   249563135   124780544   83  Linux
/dev/xvda2       249563136   251658239     1047552   82  Linux swap / Solaris

Disk /dev/loop0: 4294 MB, 4294967296 bytes, 8388608 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

[root@connected lib]# free -m
              total        used        free      shared  buff/cache   available
Mem:           4777        1059        2752          12         965        3427
Swap:          1022         655         367
[root@connected lib]#

Code: Select all

[root@connected lib]# cat /proc/swaps
Filename                                Type            Size    Used    Priority
/dev/xvda2                              partition       1047548 670060  -1
[root@connected lib]#

User avatar
TrevorH
Forum Moderator
Posts: 23841
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Re: Increasing/Removing SWAP patition

Post by TrevorH » 2018/11/09 14:01:33

I'm not sure that's the correct solution looking at those numbers. I'd try using sysctl vm.swappiness=3 (or some similarly low number) to stop it swapping so much. You appear to have plenty of RAM there but your filesystem cache had grown quite large. If running that command helps (best to reboot, run the command then leave it and see if it behaves better) then you can set it in /etc/sysctl.d/name-the-file-how-you-want.conf so it takes effect at boot time from then on.
CentOS 5 died in March 2017 - migrate NOW!
Full time Geek, part time moderator. Use the FAQ Luke

solidblueliquid
Posts: 7
Joined: 2018/01/11 07:41:06

Re: Increasing/Removing SWAP patition

Post by solidblueliquid » 2018/11/09 14:05:06

I'm just configuring the way cPanel advises, they suggest 4gb swap

User avatar
TrevorH
Forum Moderator
Posts: 23841
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Re: Increasing/Removing SWAP patition

Post by TrevorH » 2018/11/09 15:58:10

That's ridiculous. If you use even 1/4 of that 4GB swap on a system with only 5GB RAM then you'll be swapping so much that the system will grind to a halt.

Besides, the way you have your system set up makes it very tricky to change it now. The majority of your 128GB disk is set up as a single linux filesystem, not using LVM. To expand your current swap partition you would need to reduce the size of the filesystem on /dev/xvda1 - which if it's using xfs (the default in CentOS 7, is impossible without a reinstallation/reformat). If it's using ext4 then the filesystem can be shrunk but you will need to boot from alternate boot media to do so as you cannot shrink a mounted ext4 filesystem and you cannot umount your / filesystem while the system is running. Once the filesystem is resized to be smaller than it is now then you would need to use fdisk to delete the current /dev/xvda1 partition and then recreate it starting on the exact same sector number that it starts on now but with a smaller ending sector. It is extremely important to do those steps in the right order - first resize the filesystem and then resize the partition. If you do that in the opposite order then you will damage the filesystem. Then you'd need to use swapoff to stop using the current swap partition and also delete and redefine that too, then mkswap it to make it usable. If the swap partition is referenced in /etc/fstab by uuid or label then that would need to be changed before you rebooted.

My suggestion was based on looking at the numbers you posted which show that you are actually using a little more than 1GB RAM. Then there is nearly 3.5GB used for filesystem cache and that's where the majority of your memory is being used. That's something that can be tuned and the swappiness parameter is the way to do that. The file /usr/share/doc/kernel-doc-3.10.0/Documentation/sysctl/vm.txt (part of the kernel-doc package) has this to say about it:
swappiness

This control is used to define how aggressive the kernel will swap
memory pages. Higher values will increase agressiveness, lower values
decrease the amount of swap. A value of 0 instructs the kernel not to
initiate swap until the amount of free and file-backed pages is less
than the high water mark in a zone.

The default value is 60
In my tests I don't think 60 is the default on RHEL/CentOS kernels, it appears to be 30. You can reduce how much swapping goes on by reducing the size of the number and in my tests, 3 appeared to be the magic number where it stopped using so much. Yours may be different.
CentOS 5 died in March 2017 - migrate NOW!
Full time Geek, part time moderator. Use the FAQ Luke

Post Reply