40

I'm wondering what the difference is between creating a swapfile with

fallocate -l 1G /swapfile

and

dd if=/dev/zero of=/swapfile bs=1024 count=1024

both seem to work fine, but does one have an advantage over the other?

The only thing I could find online was that fallocate does not work on all file systems.

2
  • 1
    fallocate is usually faster (since it doesn't fill the created file with zeros) - otherwise no differences though, the end result is the same. See: antipaucity.com/2017/08/31/… Mar 19, 2018 at 13:41
  • 2
    @JonasCz: Yes… but no! See muru’s answer. Mar 19, 2018 at 22:28

2 Answers 2

44

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.

8
  • 2
    The mkswap manpage also says: To set up a swap file, it is necessary to create that file before initializing it with mkswap, e.g. using a command like fallocate --length 8GiB swapfile I'm confused.
    – stumblebee
    Mar 19, 2018 at 14:00
  • 8
    @stumblebee and that will work fine on filesystems which don't support pre-allocated files where fallocate will essentially work like dd, but not on ext4, which is the default and by far the most commonly used Linux filesystem.
    – muru
    Mar 19, 2018 at 14:05
  • 2
    I'm a bit confused as to why fallocate would be a problem. It seems to, well, allocate the space. (As it says on the label.) And doing fallocate -l 1g /swaptest && mkswap /swaptest && swapon /swaptest on ext4 doesn't complain about anything. truncate -l 1g would be different, since it just sets the file size but doesn't allocate any blocks.
    – ilkkachu
    Mar 19, 2018 at 21:26
  • 1
    So if it isn't doing that, someone needs to file a bug :) Mar 19, 2018 at 23:43
  • 1
    @ilkkachu somebody has reproduced the problem on xfs, at least: bugzilla.redhat.com/show_bug.cgi?id=1129205
    – muru
    Mar 20, 2018 at 0:01
5

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .