Question

sudo swapon /swapfile error

Following tutorial: https://www.digitalocean.com/community/tutorials/additional-recommended-steps-for-new-centos-7-servers

When I get to the swapon command it fiels.

$ sudo swapon /swapfile swapon: /swapfile: swapon failed: Invalid argument

Docs should be fixed to tell people to use: sudo dd if=/dev/zero of=/swapfile count=4096 bs=1MiB instead of fallocate. Anyway another tutorial also says its bad idea to use swapfile for SSD systems

Subscribe
Share

Had the same problem. Found the answer here: https://unix.stackexchange.com/questions/294600/i-cant-enable-swap-space-on-centos-7

TLDR;

On CentOS and Red Hat, replace the command that uses fallocate the create the file with this:

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

Then follow every other step.


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

fallocate works fine on other OSes (in fact it’s literally recommended in the mkswap man page), so I’m guessing the reason for this is because the root partition is using xfs on the CentOS 7 images.

I’d also agree with you that if you’re up to adding 4 gigabytes of swap, you’re way better off upgrading the droplet :)

Sí si funciona, debes hacer un reboot. Mira:

1.- Creamos el swapfile: sudo fallocate -l 4G /swapfile

2.- Verificamos el espacio reservado en swapfile: ls -lh /swapfile [root@159 ~]# ls -lh /swapfile -rw-r–r–. 1 root root 4.0G Mar 18 03:36 /swapfile

3.- Enable Swap y con permisos restringidos: chmod 600 /swapfile

4.- verificamos que sea así: ls -lh /swapfile [root@159 ~]# ls -lh /swapfile -rw-------. 1 root root 4.0G Mar 18 03:36 /swapfile

5.- Configuramos el archivo de intercambio: mkswap /swapfile [root@159 ~]# mkswap /swapfile Setting up swapspace version 1, size = 4194300 KiB no label, UUID=e83cdb0e-97f6-4f74-b71d-56c077387b14

6.- mkswap /swapfile [root@159 ~]# swapon /swapfile swapon: /swapfile: swapon failed: Invalid argument

No aparece nada!!!: [root@159 ~]# swapon -s

Reinciamos el servidor: reeboot

ssh root@XXX.XX.XXX.XXX PASS

[root@centos-s-2vcpu-4gb-nyc1-01 ~]# free -m total used free shared buff/cache available Mem: 3790 1198 1629 9 963 2330 Swap: 0 0 0

[root@centos-s-2vcpu-4gb-nyc1-01 ~]# fallocate -l 4G /swapfile

[root@centos-s-2vcpu-4gb-nyc1-01 ~]# ls -lh /swapfile -rw-------. 1 root root 4.0G Mar 18 03:54 /swapfile

[root@centos-s-2vcpu-4gb-nyc1-01 ~]# chmod 600 /swapfile

[root@centos-s-2vcpu-4gb-nyc1-01 ~]# ls -lh /swapfile -rw-------. 1 root root 4.0G Mar 18 03:54 /swapfile

[root@centos-s-2vcpu-4gb-nyc1-01 ~]# mkswap /swapfile Setting up swapspace version 1, size = 4194300 KiB no label, UUID=810db92a-a575-4652-9ed0-a731f7fad584

[root@centos-s-2vcpu-4gb-nyc1-01 ~]# swapon /swapfile Filename Type Size Used Priority /swapfile file 4194300 0 -1

[root@centos-s-2vcpu-4gb-nyc1-01 ~]# swapon -s Filename Type Size Used Priority /swapfile file 4194300 0 -1

[root@centos-s-2vcpu-4gb-nyc1-01 ~]# free -m total used free shared buff/cache available Mem: 3790 1221 1574 9 994 2307 Swap: 4095 0 4095