我想知道使用哪种方法创建交换文件的区别是什么。
fallocate -l 1G /swapfile
和
dd if=/dev/zero of=/swapfile bs=1024 count=1024
两者似乎都能正常工作,但是有没有一个比另一个更有优势呢?
我在网上找到的唯一信息是,
fallocate
在所有文件系统上都不起作用。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.
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.
fallocate --length 8GiB swapfile
的命令。我感到困惑。 - stumblebeefallocate
会成为一个问题。它似乎在分配空间。(就像标签上说的那样。)而且,在ext4
上执行fallocate -l 1g /swaptest && mkswap /swaptest && swapon /swaptest
没有任何错误提示。而truncate -l 1g
则不同,因为它只是设置文件的大小,而没有分配任何块。 - ilkkachufallocate(1)
的手册中:对于支持fallocate系统调用的文件系统,预分配是通过分配块并将其标记为未初始化来快速完成的... - Will CrawfordFallocate更快,来自the fallocate manpage:
fallocate用于操作文件的分配磁盘空间,可以释放或预分配。对于支持fallocate系统调用的文件系统,预分配通过分配块并将其标记为未初始化来快速完成,不需要IO到数据块。这比用零填充文件创建文件要快得多。
fallocate
通常更快(因为它不会用零填充创建的文件)-否则没有区别,最终结果是相同的。请参见:https://antipaucity.com/2017/08/31/fallocate-vs-dd-for-swap-file-creation/ - Jonas Czech