我想知道使用以下方法创建交换文件有什么区别
fallocate -l 1G /swapfile
和
dd if=/dev/zero of=/swapfile bs=1024 count=1024
两者似乎都运行良好,但是哪一个比另一个有优势呢?
我在网上找到的唯一答案是它fallocate
并不适用于所有文件系统。
答案1
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
页:
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
可能比 更快dd
,但它不适合创建交换文件,并且不受交换相关工具的支持。
答案2
Fallocate 速度更快,fallocate 手册页:
谬误用于操作文件分配的磁盘空间,可以解除分配或预分配。 对于支持 fallocate 系统调用,预分配通过分配块并将其标记为未初始化来快速完成,不需要对数据块进行 IO。这比通过用零填充来创建文件要快得多。