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ā
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.
$ 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'
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.
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.
Unless you frequently watch 4k movies, GIMP (Glimpse now ), open 2 browsers with 200 tabs and use Blender simultaneously - youāre good to go without swap i think
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! <ā This a joke obviously
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ā¦
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 )