fallocate failed

最近在看Linux性能相关的东西,如果有兴趣,可以和我一起学习课程
Linux性能主要取决于CPU、内存和磁盘,在学习内存性能部分开启SWAP时遇到问题:

$ fallocate -l 8G ./swap
fallocate failed: 不支持的操作

man了一下:

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 datavblocks. This is much faster than creating a file by filling it with zeroes. SEE ALSO truncate(1), fallocate(2), posix_fallocate(3)

查了下我的阿里云服务器上有 truncate,没有 posix_fallocate, 于是

truncate -s 8G ./swap

没有错误,继续执行:

chmod 600 ./swap
mkswap ./swap
swapon ./swap //此时出错 swapon: /mnt/linux/20/swap:将跳过 - 它似乎有空洞。

可见truncate生成的文件还是有问题,truncate的替代命令中还有dd,于是再尝试:

dd if=/dev/zero of=./swap bs=16384 count=512

然后再继续执行 chmod等相关操作,即能正确开启 swap--可以通过free查看。