How To Add Swap on CentOS 7

Hi all,

Recently we got a feedback on our tutorial -

It stated the following :

sudo swapon /swapfile swapon failed: invalid argument

Shout out to @Vault108 for pointing this out! I want to confirm we are in the process of updating the tutorial to avoid this error


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Accepted Answer

This error is created due to the usage of fallocate in the article.

The problem with fallocate is that it uses filesystem ioctls to make the allocation fast and effective, the disadvantage is that it does not physically allocate the space but swapon(2) syscall requires a real space.

To move forward with this, replace the command that uses fallocate the create the file with this:

sudo dd if=/dev/zero of=/myswap count=4096 bs=1MiB

Once you do, follow every step of the tutorial as it’s written.

Thanks again to @Vault108 for mentioning this!

Regards, KDSys