So I'm following a tutorial to install OTRS which is Open source Ticket Request System. So in order to install, it requires: 4GB of Swap space. Here's the command I used:

[root@ip-10-0-7-41 ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda1       50G   14G   37G  27% /
devtmpfs        478M     0  478M   0% /dev
tmpfs           496M     0  496M   0% /dev/shm
tmpfs           496M   13M  484M   3% /run
tmpfs           496M     0  496M   0% /sys/fs/cgroup
tmpfs           100M     0  100M   0% /run/user/1000
[root@ip-10-0-7-41 ~]# fallocate -l 4G /myswap
[root@ip-10-0-7-41 ~]# ls -lh /myswap
-rw-r--r--. 1 root root 4.0G Jul  8 08:44 /myswap
[root@ip-10-0-7-41 ~]# chmod 600 /myswap
[root@ip-10-0-7-41 ~]# mkswap /myswap
Setting up swapspace version 1, size = 4194300 KiB
no label, UUID=3656082a-148d-4604-96fb-5b4604fa5b2e
[root@ip-10-0-7-41 ~]# swapon /myswap
swapon: /myswap: swapon failed: Invalid argument

You can see : Invalid argument error here. I tried many time in vain to enable it.Someone please tell me how to fix this error. (I'm running this CentOS 7 on AWS Instance EC2)

[root@ip-10-0-7-41 ~]# df -T | awk '{print $1,$2,$NF}' | grep "^/dev"
/dev/xvda1 xfs /
  • What filesystem is this happening on? btrfs by any chance? – schaiba Jul 8 '16 at 8:54
  • @schaiba Hello, i edited my question. I think filesystem is xfs. – The One Jul 8 '16 at 8:56
  • 2
    On XFS indeed it's better to use dd. Please bear in mind that not all filesystems support swap or at least not in the same way. – schaiba Jul 8 '16 at 9:01
  • @schaiba I didn't know very well about type of filesystem. Thanks so much. – The One Jul 8 '16 at 9:04
  • 2
    The invalid argument is misleading. If you check the system logs, you'll probably see a corresponding message, kernel: swapon: swapfile has holes, which tells you what the actual problem is. – Anthony Geoghegan Mar 10 '20 at 13:17

The problem with fallocate(1) 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. Reference : https://bugzilla.redhat.com/show_bug.cgi?id=1129205

I'd faced this issue earlier with my box too. So instead of using fallocate, I used dd as the link suggests

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

and moving ahead with chmod, mkswap & swapon commands. Bingo ! It worked.

  • 1
    Odd, an fallocate'd swap file worked fine for me on Ubuntu 16.04.3 LTS but it fails with "swapon failed: Invalid argument" on Red Hat Enterprise Linux Server release 7.4 (Maipo). The dd command provided above indeed works on RedHat. There must be something different between the two OSes that allows fallocate to work on Ubuntu. – Davidian1024 May 30 '18 at 0:28
  • Solution works on CentOS 7.5 – NerdOfCode Aug 31 '18 at 20:32
  • dd appears to be doing a lot more work time dd if=/dev/zero of=/myswap count=4096 bs=1MiB takes 14.003 seconds where time fallocate -l 4G /swap3 takes 0.018 seconds, but they produce the same size file. I am guessing that fallocate does not copy in zeros the way dd does and swapon needs those zeros. – Daniel Wisehart Feb 2 '19 at 9:32
  • 1
    @DanielWisehart swapon doesn't need the zeros. swapon needs the physical space to actually be there already, and not just reserved for future use. You could even fill your swap file with /dev/random and it would still work (but slow to create the swap file). – Amedee Van Gasse Mar 27 '19 at 13:17
  • 1
    Neither of these methods worked for me on the latest Arch Linux on 27 August 2019. – Theodore R. Smith Aug 27 '19 at 20:50

Follow these steps, it works on DigitalOcean's droplets. I tested. Change the amount 4096 according to your need

yum install nano -y

sudo dd if=/dev/zero of=/swapfile count=4096 bs=1MiB
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

sudo nano /etc/fstab

add this line:

/swapfile   swap    swap    sw  0   0

run this command

sudo sysctl vm.swappiness=10

sudo nano /etc/sysctl.conf

add this line

vm.swappiness = 10
vm.vfs_cache_pressure = 50

To verify swap's size

swapon --summary
free -h
  • 3
    My dd command would be dd if=/dev/zero of=/swapfile count=4 bs=1GiB because I can't be bothered with calculating mebibytes in gibibytes if dd can do that for me. Did you know that in the days of yore people expressed it in kibibytes and hand-calculated that to gibibytes? Incredible! – Amedee Van Gasse Mar 27 '19 at 13:19
  • @AmedeeVanGasse you version of the dd command actually requires 1GiB of memory and the command will fail if you don't have that much free memory. – Fabiano Jul 12 '19 at 22:04
  • @Fabiano yes and? Your point is... ? – Amedee Van Gasse Jul 15 '19 at 7:24
  • 4
    @AmedeeVanGasse my point is that if you just don't wanna bother with calculating MiBs and GiBs, bs= is not the proper way to do that, as it has unintended consequences, like hurt efficiency and likely not work on most low cost hostings. If you are just lazy, use count_bytes and let dd do for you both the math and choosing of an efficient block size: dd if=/dev/zero of=/swapfile count=4GiB iflag=count_bytes =) – Fabiano Aug 8 '19 at 8:19
  • iflag=count_bytes is an excellent improvement to your comment. And I'm not lazy, I care about readability. In 6 months time when I read my script again (or when the next maintainer comes along), the reader must understand what is going on. It's not just computers who read code, humans read code too! – Amedee Van Gasse Aug 9 '19 at 8:51

In my case there was an error in the kernel log:

BTRFS warning (device sdd1): swapfile must not be copy-on-write

Disabling CoW (chattr +c /swapfile) before allocating space fixed the problem.

Not the answer you're looking for? Browse other questions tagged or ask your own question.