Increase swap disk disk on CentOS 7 from 2 GB to 4 GB - how to update /etc/fstab

eugenevdm.host

Well-Known Member
Oct 21, 2019
51
6
8
Cape Town
cPanel Access Level
DataCenter Provider
I'm troubleshooting our server falling over last night, and when I looked, I saw swap space was depleted and cPHulk taking up all the CPU.

TL;DR Any ideas how to quickly increase size of dm-1 partition?

To isolate, I'm first going to increase swap space from 2 GB (server has 8GB RAM) to 4 GB as running out of swap sounds like a big no-no to me.

I found two types of methods on the internet on how to increase the swap space on a CentOS Linux server, but I'm having problems doing the /etc/fstab part and / or understanding the second method.

The first method seems pretty straightforward, for example the one at Linuxize "How to Add Swap Space on CentOS 7".
In summary:

Check first what's going on (I have 2 GB swap space):

swapon --show
Create swap. At this point the guide says it can be done with fallocate -l 4G /swapfile too, but this failed later so used dd method instead
dd if=/dev/zero of=/swapfile bs=1024 count=4194304
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
(this failed with swapon failed: Invalid argument if fallocate was used)

cat /etc/fstab
...
/swapfile swap swap defaults 0 0
So that all works. But now I have this:

Bash:
# swapon --show
NAME      TYPE      SIZE USED PRIO
/dev/dm-1 partition   2G 1.8G   -2
/swapfile file        4G   0B   -3
Two swap files!

The other type of guide seems nice and complicated. Stuff about increasing that disk partition.

Questions:

1. Will I be okay with two swap files (I prefer one)?
2. Any quick steps to rather just increase dm-1?

I've spend a ton of time on this and waking up at 3AM wasn't nice. I'm in a bad mood already and also haven't had coffee.
 

andrew.n

Well-Known Member
Jun 9, 2020
458
118
43
EU
cPanel Access Level
Root Administrator
Actually the 2 swap files are added together so instead of 2GB which was the old one you now have 6GB which wouldn't hurt. You can check it with "free -m' command.