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.

user8292439
  • 3,808
  • 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: https://antipaucity.com/2017/08/31/fallocate-vs-dd-for-swap-file-creation/ – Jonas Czech Mar 19 '18 at 13:41
  • 2
    @JonasCz: Yes… but no! See muru’s answer. – David Foerster Mar 19 '18 at 22:28

2 Answers2

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.

muru
  • 197,895
  • 55
  • 485
  • 740
  • 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 '18 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 '18 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 '18 at 21:26
  • From the fallocate(1) man page: For filesystems which support the fallocate system call, pre‐allocation is done quickly by allocating blocks and marking them as uninitialized, … – Will Crawford Mar 19 '18 at 23:43
  • 1
    So if it isn't doing that, someone needs to file a bug :) – Will Crawford Mar 19 '18 at 23:43
  • 1
    @ilkkachu somebody has reproduced the problem on xfs, at least: https://bugzilla.redhat.com/show_bug.cgi?id=1129205 – muru Mar 20 '18 at 00:01
  • Interesting. I've used fallocate on ext4 and xfs systems with no problems. (Not that often, of course, since it's not a regular occurrence...) – RonJohn Mar 20 '18 at 03:58
  • Man pages have changed meanwhile. I'm not sure how to read that, but I think with Linux >=4.18 it might be ok to use fallocate for xfs http://manpages.ubuntu.com/manpages/eoan/en/man8/swapon.8.html – lumbric Jan 16 '20 at 16:10
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.

Pablo Bianchi
  • 15,657
stumblebee
  • 3,547