fallocate vs dd for swapfile?
Solution 1:
From the mkswap
manpage:
Note that a swap file must not contain any holes. Using cp(1) to
create the file is not acceptable. Neither is use of fallocate(1) on
file systems that support preallocated files, such as XFS or ext4, or
on copy-on-write filesystems like btrfs. It is recommended to use
dd(1) and /dev/zero in these cases. Please read notes from swapon(8)
before adding a swap file to copy-on-write filesystems.
And from the swapon
manpage:
You should not use swapon on a file with holes. This can be seen in
the system log as
swapon: swapfile has 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 preallocated files (e.g. fallocate(1)) on filesystems
like XFS or ext4, and on copy-on-write filesystems like btrfs.
It follows that, while fallocate
may be faster than dd
, it's not suitable for creating swap files and not supported by swap-related tools.
Solution 2:
Fallocate is faster, From the fallocate manpage:
fallocate is used to manipulate the allocated disk space for a file, either to deallocate or preallocate it. For filesystems which support the fallocate system call, preallocation is done quickly by allocating blocks and marking them as uninitialized, requiring no IO to the data blocks. This is much faster than creating a file by filling it with zeros.