Swap file creation error

What went wrong here? I think I followed these instructions properly

[kde@kde-80ru ~]$ sudo fallocate -l 2G /swapfile
[sudo] password for kde:
[kde@kde-80ru ~]$ sudo chmod 600 /swapfile
[kde@kde-80ru ~]$ sudo mkswap /swapfile
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=5a1da881-168e-4c5c-aa5b-e4eec2193078
[kde@kde-80ru ~]$ sudo swapon /swapfile
swapon: /swapfile: swapon failed: Invalid argument

What filesystem do you use?

Perhaps BTRFS?

The btrfs filesystem cannot support swap files because it moves file data around and the Linux swap code only takes the swap file’s mapping once. Catastrophe would occur if this is allowed – random file data that used blocks that the swap file once used would get corrupted.

P.S. They should really change “Invalid argument” to “Swap unsupported for this filesystem”

1 Like

ext4

From swapon(8) manpage:

Files with holes

The swap file implementation in the kernel expects to be able to write to the file directly, without the assistance of the filesystem. This is a problem on files with holes or on copy-on-write files on filesystems like Btrfs.

Commands like cp(1) or truncate(1) create files with holes. These files will be rejected by swapon.

Preallocated files created by fallocate(1) may be interpreted as files with holes too depending of the filesystem. Preallocated swap files are supported on XFS since Linux 4.18.

The most portable solution to create a swap file is to use dd(1) and /dev/zero.

https://bugs.archlinux.org/task/66979
https://bugs.archlinux.org/task/66921

2 Likes

Try using dd to make a swapfile, it’s much more reliable than fallocate.

For example, to make a 2 GiB swapfile:

sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 status=progress

And then, as normal: first chmod, then mkswap, and finally, swapon.

3 Likes

Here’s a link to the procedure:

https://forum.endeavouros.com/t/trouble-with-swapon/5327/11

2 Likes

Thanks guys, that worked

3 Likes

truncate is another option,

sudo truncate -s2G /swapfile
$ truncate --help
Usage: truncate OPTION... FILE...
Shrink or extend the size of each FILE to the specified size

A FILE argument that does not exist is created.

If a FILE is larger than the specified size, the extra data is lost.
If a FILE is shorter, it is extended and the sparse extended part (hole)
reads as zero bytes.

Mandatory arguments to long options are mandatory for short options too.
  -c, --no-create        do not create any files
  -o, --io-blocks        treat SIZE as number of IO blocks instead of bytes
  -r, --reference=RFILE  base size on RFILE
  -s, --size=SIZE        set or adjust the file size by SIZE bytes
      --help     display this help and exit
      --version  output version information and exit

The SIZE argument is an integer and optional unit (example: 10K is 10*1024).
Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,... (powers of 1000).
Binary prefixes can be used, too: KiB=K, MiB=M, and so on.

SIZE may also be prefixed by one of the following modifying characters:
'+' extend by, '-' reduce by, '<' at most, '>' at least,
'/' round down to multiple of, '%' round up to multiple of.

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation <https://www.gnu.org/software/coreutils/truncate>
or available locally via: info '(coreutils) truncate invocation'
5 Likes

I’m pretty sure that you can’t use fallocate anymore. The Arch Wiki says use dd.

3 Likes

@joekamprad
Seems it’s time to update our wiki with dd as well then :wink:

1 Like

okay i will do so now.

Note: Using dd to allocate a swap file is the most portable solution, see swapon(8) § Files with holes for details.

4 Likes

“it’s not recommended making a swap partition on a SSD, this can damage the medium.” :flushed:

Is this really true? then i will delete my swap partition! :thinking:

Generally yes, well unless you don’t care about SSD lifespan or data on it :wink:

SSD: Less write operations = more life

1 Like

swapping naturally generates numerous write actions when the operating system needs to swap out. One does not want to have such write operations on an SSD, which is limited in the amount of write operations that can be recorded over the lifetime of the SSD. Sufficient RAM in the computer would be the best way to avoid swapping. If you also have a conventional hard disk in your system, you do nothing wrong to create the swap partition on the hard disk.

1 Like

Should I create a swap partition on an SSD? This question comes up from time to time. When we only have one SSD drive in our computer and we have relatively little memory, we don’t have many choices.

1 Like

OK, thanks for help. I have 8 Gigs RAM and a seperate HDD.:smirk::+1:

2 Likes

Unless you frequently watch 4k movies, GIMP (Glimpse now :laughing:), open 2 browsers with 200 tabs and use Blender simultaneously - you’re good to go without swap i think :slight_smile:

2 Likes

That sounds like me sometimes. I created a 1GB swapfile “just in case”. Although it pains me to think that I’m causing more wear on my SSD. I honestly don’t think I’ve seen that swap file more than halfway though, even with multiple VMs, browsers, and streaming video running. In other words, if you use and SSD, don’t bother with swap, just download more RAM! :rofl: <— This a joke obviously

3 Likes

Just another thought on swap being located on an SSD. I would say it depends more on the reason you have a swap. IF it is going to be used, the an SSD is contra-indicated for most. However, if the presence of swap is just for ‘having’ it, and it doesn’t get used… then why not? I don’t think I have seen it used for several years now…

3 Likes

That is reasonable, but i get paranoid and don’t trust my system and myself sometimes…
What if something memory-leaks…? (My Deepin past speaks, sorry :rofl:)

Although, technically you’re correct! :+1:

1 Like